1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.git.jgit.command;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
29 import org.eclipse.jgit.transport.CredentialsProvider;
30 import org.eclipse.jgit.transport.URIish;
31 import org.eclipse.jgit.transport.sshd.IdentityPasswordProvider;
32 import org.eclipse.jgit.transport.sshd.KeyPasswordProvider;
33 import org.eclipse.jgit.transport.sshd.SshdSessionFactory;
34 import org.eclipse.jgit.util.StringUtils;
35 import org.slf4j.Logger;
36
37
38
39
40
41 public class ScmProviderAwareSshdSessionFactory extends SshdSessionFactory {
42 private final GitScmProviderRepository repo;
43 private final Logger logger;
44
45 public ScmProviderAwareSshdSessionFactory(GitScmProviderRepository repo, Logger logger) {
46 this.repo = repo;
47 this.logger = logger;
48 }
49
50 @Override
51 protected List<Path> getDefaultIdentities(File sshDir) {
52 if (!StringUtils.isEmptyOrNull(repo.getPrivateKey())) {
53 logger.debug("Using private key at {}", repo.getPrivateKey());
54 return Collections.singletonList(Paths.get(repo.getPrivateKey()));
55 } else {
56 return super.getDefaultIdentities(sshDir);
57 }
58 }
59
60 @Override
61 protected KeyPasswordProvider createKeyPasswordProvider(CredentialsProvider provider) {
62 if (repo.getPassphrase() != null) {
63 return new IdentityPasswordProvider(provider) {
64 @Override
65 public char[] getPassphrase(URIish uri, int attempt) throws IOException {
66 if (attempt > 0) {
67 throw new IOException(
68 "Passphrase was not correct in first attempt, " + "canceling further attempts!");
69 }
70 logger.debug("Using stored passphrase");
71 return repo.getPassphrase().toCharArray();
72 }
73 };
74 } else {
75 return super.createKeyPasswordProvider(provider);
76 }
77 }
78 }