Browse Source

sshd: allow setting a null ssh config

The same effect could already be obtained if the ~/.ssh/config file
did not exist. But that is more difficult to control by clients,
since JGit would pick up the config if it was then created. Therefore
allow specifying a null config explicitly to permanently switch off
config file handling.

Change-Id: Iedf8a7f4d5c1ca08e0a513ed28301d8e5261b22a
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
stable-5.5
Thomas Wolf 6 years ago committed by Matthias Sohn
parent
commit
8c74a54315
  1. 16
      org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/transport/sshd/JGitSshConfig.java
  2. 18
      org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/transport/sshd/SshdSessionFactory.java

16
org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/transport/sshd/JGitSshConfig.java

@ -83,7 +83,9 @@ import org.eclipse.jgit.transport.SshConstants;
*/ */
public class JGitSshConfig implements HostConfigEntryResolver { public class JGitSshConfig implements HostConfigEntryResolver {
private OpenSshConfigFile configFile; private final OpenSshConfigFile configFile;
private final String localUserName;
/** /**
* Creates a new {@link OpenSshConfigFile} that will read the config from * Creates a new {@link OpenSshConfigFile} that will read the config from
@ -92,20 +94,22 @@ public class JGitSshConfig implements HostConfigEntryResolver {
* @param home * @param home
* user's home directory for the purpose of ~ replacement * user's home directory for the purpose of ~ replacement
* @param config * @param config
* file to load. * file to load; may be {@code null} if no ssh config file
* handling is desired
* @param localUserName * @param localUserName
* user name of the current user on the local host OS * user name of the current user on the local host OS
*/ */
public JGitSshConfig(@NonNull File home, @NonNull File config, public JGitSshConfig(@NonNull File home, File config,
@NonNull String localUserName) { @NonNull String localUserName) {
configFile = new OpenSshConfigFile(home, config, localUserName); this.localUserName = localUserName;
configFile = config == null ? null : new OpenSshConfigFile(home, config, localUserName);
} }
@Override @Override
public HostConfigEntry resolveEffectiveHost(String host, int port, public HostConfigEntry resolveEffectiveHost(String host, int port,
SocketAddress localAddress, String username, SocketAddress localAddress, String username,
AttributeRepository attributes) throws IOException { AttributeRepository attributes) throws IOException {
HostEntry entry = configFile.lookup(host, port, username); HostEntry entry = configFile == null ? new HostEntry() : configFile.lookup(host, port, username);
JGitHostConfigEntry config = new JGitHostConfigEntry(); JGitHostConfigEntry config = new JGitHostConfigEntry();
// Apache MINA conflates all keys, even multi-valued ones, in one map // Apache MINA conflates all keys, even multi-valued ones, in one map
// and puts multiple values separated by commas in one string. See // and puts multiple values separated by commas in one string. See
@ -131,7 +135,7 @@ public class JGitSshConfig implements HostConfigEntryResolver {
String user = username != null && !username.isEmpty() ? username String user = username != null && !username.isEmpty() ? username
: entry.getValue(SshConstants.USER); : entry.getValue(SshConstants.USER);
if (user == null || user.isEmpty()) { if (user == null || user.isEmpty()) {
user = configFile.getLocalUserName(); user = localUserName;
} }
config.setUsername(user); config.setUsername(user);
config.setProperty(SshConstants.USER, user); config.setProperty(SshConstants.USER, user);

18
org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/transport/sshd/SshdSessionFactory.java

@ -360,11 +360,25 @@ public class SshdSessionFactory extends SshSessionFactory implements Closeable {
@NonNull File homeDir, @NonNull File sshDir) { @NonNull File homeDir, @NonNull File sshDir) {
return defaultHostConfigEntryResolver.computeIfAbsent( return defaultHostConfigEntryResolver.computeIfAbsent(
new Tuple(new Object[] { homeDir, sshDir }), new Tuple(new Object[] { homeDir, sshDir }),
t -> new JGitSshConfig(homeDir, t -> new JGitSshConfig(homeDir, getSshConfig(sshDir),
new File(sshDir, SshConstants.CONFIG),
getLocalUserName())); getLocalUserName()));
} }
/**
* Determines the ssh config file. The default implementation returns
* ~/.ssh/config. If the file does not exist and is created later it will be
* picked up. To not use a config file at all, return {@code null}.
*
* @param sshDir
* representing ~/.ssh/
* @return the file (need not exist), or {@code null} if no config file
* shall be used
* @since 5.5
*/
protected File getSshConfig(@NonNull File sshDir) {
return new File(sshDir, SshConstants.CONFIG);
}
/** /**
* Obtain a {@link ServerKeyVerifier} to read known_hosts files and to * Obtain a {@link ServerKeyVerifier} to read known_hosts files and to
* verify server host keys. The default implementation returns a * verify server host keys. The default implementation returns a

Loading…
Cancel
Save