1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.internal.transport.sshd;
11
12 import static org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.flag;
13 import static org.eclipse.jgit.internal.transport.ssh.OpenSshConfigFile.positive;
14
15 import java.io.IOException;
16 import java.net.SocketAddress;
17 import java.util.Map;
18 import java.util.TreeMap;
19
20 import org.apache.sshd.client.config.hosts.HostConfigEntry;
21 import org.apache.sshd.client.config.hosts.HostConfigEntryResolver;
22 import org.apache.sshd.common.AttributeRepository;
23 import org.apache.sshd.common.util.net.SshdSocketAddress;
24 import org.eclipse.jgit.transport.SshConfigStore;
25 import org.eclipse.jgit.transport.SshConstants;
26 import org.eclipse.jgit.transport.SshSessionFactory;
27
28
29
30
31
32 public class JGitSshConfig implements HostConfigEntryResolver {
33
34 private final SshConfigStore configFile;
35
36
37
38
39
40
41
42
43 public JGitSshConfig(SshConfigStore store) {
44 configFile = store;
45 }
46
47 @Override
48 public HostConfigEntry resolveEffectiveHost(String host, int port,
49 SocketAddress localAddress, String username, String proxyJump,
50 AttributeRepository attributes) throws IOException {
51 SshConfigStore.HostConfig entry = configFile == null
52 ? SshConfigStore.EMPTY_CONFIG
53 : configFile.lookup(host, port, username);
54 JGitHostConfigEntry config = new JGitHostConfigEntry();
55
56
57
58 Map<String, String> allOptions = new TreeMap<>(
59 String.CASE_INSENSITIVE_ORDER);
60 allOptions.putAll(entry.getOptions());
61
62 entry.getMultiValuedOptions().entrySet().stream()
63 .forEach(e -> allOptions.put(e.getKey(),
64 String.join(",", e.getValue())));
65 config.setProperties(allOptions);
66
67 config.setMultiValuedOptions(entry.getMultiValuedOptions());
68
69 String hostName = entry.getValue(SshConstants.HOST_NAME);
70 if (hostName == null || hostName.isEmpty()) {
71 hostName = host;
72 }
73 config.setHostName(hostName);
74 config.setProperty(SshConstants.HOST_NAME, hostName);
75 config.setHost(SshdSocketAddress.isIPv6Address(hostName) ? "" : hostName);
76 String user = username != null && !username.isEmpty() ? username
77 : entry.getValue(SshConstants.USER);
78 if (user == null || user.isEmpty()) {
79 user = SshSessionFactory.getLocalUserName();
80 }
81 config.setUsername(user);
82 config.setProperty(SshConstants.USER, user);
83 int p = port >= 0 ? port : positive(entry.getValue(SshConstants.PORT));
84 config.setPort(p >= 0 ? p : SshConstants.SSH_DEFAULT_PORT);
85 config.setProperty(SshConstants.PORT,
86 Integer.toString(config.getPort()));
87 config.setIdentities(entry.getValues(SshConstants.IDENTITY_FILE));
88 config.setIdentitiesOnly(
89 flag(entry.getValue(SshConstants.IDENTITIES_ONLY)));
90 return config;
91 }
92
93 }