View Javadoc
1   /*
2    * Copyright (C) 2018, 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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   * A bridge between a JGit {@link SshConfigStore} and the Apache MINA sshd
30   * {@link HostConfigEntryResolver}.
31   */
32  public class JGitSshConfig implements HostConfigEntryResolver {
33  
34  	private final SshConfigStore configFile;
35  
36  	/**
37  	 * Creates a new {@link JGitSshConfig} that will read the config from the
38  	 * given {@link SshConfigStore}.
39  	 *
40  	 * @param store
41  	 *            to use
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  		// Apache MINA conflates all keys, even multi-valued ones, in one map
56  		// and puts multiple values separated by commas in one string. See
57  		// the javadoc on HostConfigEntry.
58  		Map<String, String> allOptions = new TreeMap<>(
59  				String.CASE_INSENSITIVE_ORDER);
60  		allOptions.putAll(entry.getOptions());
61  		// And what if a value contains a comma??
62  		entry.getMultiValuedOptions().entrySet().stream()
63  				.forEach(e -> allOptions.put(e.getKey(),
64  						String.join(",", e.getValue()))); //$NON-NLS-1$
65  		config.setProperties(allOptions);
66  		// The following is an extension from JGitHostConfigEntry
67  		config.setMultiValuedOptions(entry.getMultiValuedOptions());
68  		// Also make sure the underlying properties are set
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); //$NON-NLS-1$
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  }