Skip to content

Commit

Permalink
simplify user key management
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Lamy <[email protected]>
  • Loading branch information
olamy committed May 7, 2024
1 parent 67f43ed commit 5aa74b6
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -58,7 +58,8 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
import org.apache.sshd.common.util.security.SecurityUtils;
import org.apache.sshd.common.util.security.bouncycastle.BouncyCastleSecurityProviderRegistrar;
import org.eclipse.jgit.api.AddNoteCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
Expand Down Expand Up @@ -129,11 +130,9 @@
import org.eclipse.jgit.transport.TransportHttp;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.sshd.ServerKeyDatabase;
import org.eclipse.jgit.transport.sshd.SshdSession;
import org.eclipse.jgit.transport.sshd.SshdSessionFactory;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.FS;
import org.jenkinsci.plugins.gitclient.jgit.PreemptiveAuthHttpClientConnectionFactory;
import org.jenkinsci.plugins.gitclient.jgit.SmartCredentialsProvider;
import org.jenkinsci.plugins.gitclient.verifier.HostKeyVerifierFactory;
Expand All @@ -158,6 +157,16 @@ public class JGitAPIImpl extends LegacyCompatibleGitAPIImpl {
private final HostKeyVerifierFactory hostKeyVerifierFactory;
private transient CredentialsProvider provider;

static {
// TODO should we have this configurable?

Check warning on line 161 in src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: should we have this configurable?
// to avoid any registration of using "net.i2p.crypto.eddsa.EdDSASecurityProvider";
LOGGER.info("configuring SecurityUtils, isRegistrationCompleted:" + SecurityUtils.isRegistrationCompleted());
SecurityUtils.setDefaultProviderChoice(new BouncyCastleSecurityProviderRegistrar());
LOGGER.info("SecurityUtils.isEDDSACurveSupported():" + SecurityUtils.isEDDSACurveSupported());

LOGGER.info("SecurityUtils.getRegisteredProviders():" + SecurityUtils.getRegisteredProviders());
}

JGitAPIImpl(File workspace, TaskListener listener) {
/* If workspace is null, then default to current directory to match
* CliGitAPIImpl behavior */
Expand Down Expand Up @@ -196,9 +205,8 @@ public SshdSessionFactory buildSshdSessionFactory(@NonNull final HostKeyVerifier
LOGGER.log(Level.SEVERE, "cannot create knowhosts file", e);

Check warning on line 205 in src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 204-205 are not covered by tests
}
}
return new SshdSessionFactory() {
private Path tmpKey;

return new SshdSessionFactory() {
@Override
protected SshConfigStore createSshConfigStore(File homeDir, File configFile, String localUserName) {
return new OpenSshConfigFile(homeDir, configFile, localUserName) {
Expand Down Expand Up @@ -239,35 +247,16 @@ protected Iterable<KeyPair> getDefaultKeys(File sshDir) {
.orElse(null);
if (sshUserPrivateKey != null) {

Check warning on line 248 in src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 248 is only partially covered, one branch is missing
try {
// be sure parent directories are here
Files.createDirectories(sshDir.getAbsoluteFile().toPath());
tmpKey = Files.createTempFile(sshDir.getAbsoluteFile().toPath(), "key", ".priv");
tmpKey.toFile().deleteOnExit();
Files.write(tmpKey, sshUserPrivateKey.getPrivateKeys());
FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider(tmpKey);
return fileKeyPairProvider.loadKeys(null);
} catch (IOException e) {
String keys = String.join(System.lineSeparator(), sshUserPrivateKey.getPrivateKeys());
return SecurityUtils.loadKeyPairIdentities(
null, () -> "key", IOUtils.toInputStream(keys, StandardCharsets.UTF_8), null);
} catch (IOException | GeneralSecurityException e) {
throw new RuntimeException(e.getMessage(), e);

Check warning on line 254 in src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 250-254 are not covered by tests
}
}
return Collections.emptyList();
}

@Override
public SshdSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms)
throws TransportException {
SshdSession sshdSession = super.getSession(uri, credentialsProvider, fs, tms);
sshdSession.addCloseListener(sshdSession1 -> {
try {
Files.deleteIfExists(tmpKey);
} catch (IOException e) {
// ignore
LOGGER.log(Level.FINE, "fail to delete file " + tmpKey, e);
}
});
return sshdSession;
}

@Override
protected String getDefaultPreferredAuthentications() {
// TODO do we really need/want password?

Check warning on line 262 in src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: do we really need/want password?
Expand Down

0 comments on commit 5aa74b6

Please sign in to comment.