Skip to content

Commit

Permalink
add credentials provider for JGit
Browse files Browse the repository at this point in the history
add test for SSH authentication
  • Loading branch information
kwin committed Jul 12, 2022
1 parent ee650d9 commit 7064322
Show file tree
Hide file tree
Showing 21 changed files with 861 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ protected void setScmProviders( Map<String, ScmProvider> providers )
{
requireNonNull( providers );
this.scmProviders.clear();
providers.forEach( this::setScmProvider );
// first provider must not be overwritten by subsequent ones if they are registered for the same key
providers.forEach( this.scmProviders::putIfAbsent );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import javax.inject.Singleton;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
Expand Down Expand Up @@ -54,6 +56,13 @@
public class GitExeScmProvider
extends AbstractGitScmProvider
{
private final Map<String, String> environmentVariables;

public GitExeScmProvider()
{
environmentVariables = new HashMap<>();
}

/** {@inheritDoc} */
protected GitCommand getAddCommand()
{
Expand All @@ -75,13 +84,13 @@ protected GitCommand getChangeLogCommand()
/** {@inheritDoc} */
protected GitCommand getCheckInCommand()
{
return new GitCheckInCommand();
return new GitCheckInCommand( environmentVariables );
}

/** {@inheritDoc} */
protected GitCommand getCheckOutCommand()
{
return new GitCheckOutCommand();
return new GitCheckOutCommand( environmentVariables );
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -141,7 +150,7 @@ protected GitCommand getBlameCommand()
/** {@inheritDoc} */
protected GitCommand getRemoteInfoCommand()
{
return new GitRemoteInfoCommand();
return new GitRemoteInfoCommand( environmentVariables );
}

/** {@inheritDoc} */
Expand All @@ -162,4 +171,9 @@ protected String getRepositoryURL( File path )

return result.getInfoItems().get( 0 ).getURL();
}

public void setEnvironmentVariable( String key, String value )
{
environmentVariables.put( key, value );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Command line construction utility.
Expand Down Expand Up @@ -87,15 +89,30 @@ public static void addTarget( Commandline cl, List<File> files )
}
}

/**
*
* @param workingDirectory
* @param command
* @return TODO
*/
public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
{
return getBaseGitCommandLine( workingDirectory, command, Collections.emptyMap() );
}

/**
*
* @param workingDirectory
* @param command
* @param environment
* @return TODO
*/
public static Commandline getBaseGitCommandLine( File workingDirectory, String command )
public static Commandline getBaseGitCommandLine( File workingDirectory, String command,
Map<String, String> environment )
{
return getAnonymousBaseGitCommandLine( workingDirectory, command );
Commandline cl = getAnonymousBaseGitCommandLine( workingDirectory, command );
environment.forEach( cl::addEnvironment );
return cl;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
Expand All @@ -57,6 +58,14 @@ public class GitCheckInCommand
extends AbstractCheckInCommand
implements GitCommand
{
private final Map<String, String> environmentVariables;

public GitCheckInCommand( Map<String, String> environmentVariables )
{
super();
this.environmentVariables = environmentVariables;
}

/** {@inheritDoc} */
protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
ScmVersion version )
Expand Down Expand Up @@ -145,7 +154,7 @@ protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, Sc
return new CheckInScmResult( null, statusConsumer.getChangedFiles() );
}

Commandline clCommit = createCommitCommandLine( repository, fileSet, messageFile );
Commandline clCommit = createCommitCommandLine( repository, fileSet, messageFile, environmentVariables );

exitCode = GitCommandLineUtils.execute( clCommit, stdout, stderr );
if ( exitCode != 0 )
Expand Down Expand Up @@ -211,11 +220,12 @@ protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, Sc
//
// ----------------------------------------------------------------------

public static Commandline createPushCommandLine( GitScmProviderRepository repository,
public Commandline createPushCommandLine( GitScmProviderRepository repository,
ScmFileSet fileSet, ScmVersion version )
throws ScmException
{
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" );
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push",
environmentVariables );

String branch = GitBranchCommand.getCurrentBranch( repository, fileSet );

Expand All @@ -232,10 +242,18 @@ public static Commandline createPushCommandLine( GitScmProviderRepository reposi
}

public static Commandline createCommitCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet,
File messageFile )
File messageFile )
throws ScmException
{
return createCommitCommandLine( repository, fileSet, messageFile, Collections.emptyMap() );
}

public static Commandline createCommitCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet,
File messageFile, Map<String, String> environmentVariables )
throws ScmException
{
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "commit" );
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "commit",
environmentVariables );

cl.createArg().setValue( "--verbose" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.io.File;
import java.util.Map;

import org.apache.maven.scm.CommandParameter;
import org.apache.maven.scm.CommandParameters;
Expand Down Expand Up @@ -52,6 +53,14 @@ public class GitCheckOutCommand
extends AbstractCheckOutCommand
implements GitCommand
{
private final Map<String, String> environmentVariables;

public GitCheckOutCommand( Map<String, String> environmentVariables )
{
super();
this.environmentVariables = environmentVariables;
}

/**
* For git, the given repository is a remote one.
* We have to clone it first if the working directory does not contain a git repo yet,
Expand Down Expand Up @@ -104,7 +113,7 @@ public ScmResult executeCommand( ScmProviderRepository repo, ScmFileSet fileSet,
lastCommandLine = clClone.toString();
}

GitRemoteInfoCommand gitRemoteInfoCommand = new GitRemoteInfoCommand();
GitRemoteInfoCommand gitRemoteInfoCommand = new GitRemoteInfoCommand( environmentVariables );

RemoteInfoScmResult result = gitRemoteInfoCommand.executeRemoteInfoCommand( repository, null, null );

Expand Down Expand Up @@ -173,7 +182,8 @@ public static Commandline createCommandLine( GitScmProviderRepository repository
private Commandline createCloneCommand( GitScmProviderRepository repository, File workingDirectory,
ScmVersion version, boolean binary, boolean shallow )
{
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory.getParentFile(), "clone" );
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory.getParentFile(), "clone",
environmentVariables );

forceBinary( cl, binary );

Expand Down Expand Up @@ -240,7 +250,7 @@ private Commandline createPullCommand( GitScmProviderRepository repository, File
}
else
{
cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "pull" );
cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "pull", environmentVariables );

cl.createArg().setValue( repository.getFetchUrl() );
cl.createArg().setValue( "master" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import java.util.Map;

import org.apache.maven.scm.CommandParameters;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
Expand All @@ -38,6 +40,13 @@ public class GitRemoteInfoCommand
extends AbstractRemoteInfoCommand
implements GitCommand
{
private final Map<String, String> environmentVariables;

public GitRemoteInfoCommand( Map<String, String> environmentVariables )
{
super();
this.environmentVariables = environmentVariables;
}

@Override
public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet,
Expand Down Expand Up @@ -65,9 +74,9 @@ public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repos
//
// ----------------------------------------------------------------------

public static Commandline createCommandLine( GitScmProviderRepository repository )
public Commandline createCommandLine( GitScmProviderRepository repository )
{
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( null, "ls-remote" );
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( null, "ls-remote", environmentVariables );

cl.setWorkingDirectory( System.getProperty( "java.io.tmpdir" ) );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.apache.maven.scm.provider.git.gitexe.command.checkout;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;

import org.apache.maven.scm.provider.git.command.checkout.GitSshCheckOutCommandTckTest;
import org.apache.maven.scm.provider.git.gitexe.GitExeScmProvider;
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;

/**
*
*/
public class GitExeSshCheckOutCommandTckTest
extends GitSshCheckOutCommandTckTest
{
private Path knownHostsFile;

public static final String VARIABLE_GIT_SSH_COMMAND = "GIT_SSH_COMMAND"; // https://git-scm.com/docs/git#Documentation/git.txt-codeGITSSHCOMMANDcode, requires git 2.3.0 or newer
public GitExeSshCheckOutCommandTckTest() throws GeneralSecurityException, IOException
{
super();
}

@Override
protected String getScmProvider()
{
return "git";
}

@Override
public void initRepo() throws Exception
{
super.initRepo();
GitScmProviderRepository providerRepository = (GitScmProviderRepository) getScmRepository().getProviderRepository();
GitExeScmProvider provider = (GitExeScmProvider) getScmManager().getProviderByRepository( getScmRepository() );
knownHostsFile = Files.createTempFile( "known-hosts", null );
provider.setEnvironmentVariable( VARIABLE_GIT_SSH_COMMAND, "ssh -o UserKnownHostsFile=" + knownHostsFile.toString() +
" -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i " + providerRepository.getPrivateKey() );
}

@Override
public void removeRepo() throws Exception
{
super.removeRepo();
Files.deleteIfExists( knownHostsFile );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<!-- for testing SSH authentication -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-git</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
<version>2.8.0</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.8.0</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Loading

0 comments on commit 7064322

Please sign in to comment.