-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…50) * Support public key auth for SSH #SCM-832 Adds a TransportConfigCallback to all remote commands, which adds a public/private key based identity for repositories with ssh URLs if configured. * Updated documentation for #SCM-832 * [SCM-832] Updated documentation * [SCM-832] Added debug logging maven-scm-provider-jgit now outputs the private key used when run as mvn -X
- Loading branch information
Showing
6 changed files
with
153 additions
and
12 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
...main/java/org/apache/maven/scm/provider/git/jgit/command/JGitTransportConfigCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.apache.maven.scm.provider.git.jgit.command; | ||
|
||
/* | ||
* 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 com.jcraft.jsch.JSch; | ||
import com.jcraft.jsch.JSchException; | ||
import com.jcraft.jsch.Session; | ||
import org.apache.maven.scm.log.ScmLogger; | ||
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; | ||
import org.eclipse.jgit.api.TransportConfigCallback; | ||
import org.eclipse.jgit.transport.*; | ||
import org.eclipse.jgit.util.FS; | ||
import org.eclipse.jgit.util.StringUtils; | ||
|
||
/** | ||
* Implementation of {@link TransportConfigCallback} which adds | ||
* a public/private key identity to ssh URLs if configured. | ||
*/ | ||
public class JGitTransportConfigCallback implements TransportConfigCallback { | ||
private SshSessionFactory sshSessionFactory = null; | ||
|
||
public JGitTransportConfigCallback(GitScmProviderRepository repo, ScmLogger logger) { | ||
if (repo.getFetchInfo().getProtocol().equals("ssh")) { | ||
if (!StringUtils.isEmptyOrNull(repo.getPrivateKey()) && repo.getPassphrase() == null) { | ||
logger.debug("using private key with passphrase: " + repo.getPrivateKey()); | ||
sshSessionFactory = new UnprotectedPrivateKeySessionFactory(repo); | ||
} else if (!StringUtils.isEmptyOrNull(repo.getPrivateKey()) && repo.getPassphrase() != null) { | ||
logger.debug("using private key: " + repo.getPrivateKey()); | ||
sshSessionFactory = new ProtectedPrivateKeyFileSessionFactory(repo); | ||
} else { | ||
sshSessionFactory = new SimpleSessionFactory(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void configure(Transport transport) { | ||
if (transport instanceof SshTransport) { | ||
SshTransport sshTransport = (SshTransport) transport; | ||
sshTransport.setSshSessionFactory(sshSessionFactory); | ||
} | ||
} | ||
|
||
static private class SimpleSessionFactory extends JschConfigSessionFactory { | ||
@Override | ||
protected void configure(OpenSshConfig.Host host, Session session) { | ||
} | ||
} | ||
|
||
static private abstract class PrivateKeySessionFactory extends SimpleSessionFactory { | ||
private final GitScmProviderRepository repo; | ||
|
||
public GitScmProviderRepository getRepo() { | ||
return repo; | ||
} | ||
|
||
public PrivateKeySessionFactory(GitScmProviderRepository repo) { | ||
this.repo = repo; | ||
} | ||
} | ||
|
||
static private class UnprotectedPrivateKeySessionFactory extends PrivateKeySessionFactory { | ||
|
||
public UnprotectedPrivateKeySessionFactory(GitScmProviderRepository repo) { | ||
super(repo); | ||
} | ||
|
||
@Override | ||
protected JSch createDefaultJSch(FS fs) throws JSchException { | ||
JSch defaultJSch = super.createDefaultJSch(fs); | ||
defaultJSch.addIdentity(getRepo().getPrivateKey()); | ||
return defaultJSch; | ||
} | ||
} | ||
|
||
static private class ProtectedPrivateKeyFileSessionFactory extends PrivateKeySessionFactory { | ||
|
||
public ProtectedPrivateKeyFileSessionFactory(GitScmProviderRepository repo) { | ||
super(repo); | ||
} | ||
|
||
@Override | ||
protected JSch createDefaultJSch(FS fs) throws JSchException { | ||
JSch defaultJSch = super.createDefaultJSch(fs); | ||
defaultJSch.addIdentity(getRepo().getPrivateKey(), getRepo().getPassphrase()); | ||
return defaultJSch; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters