From 8c74a543155ceff5828bbc5c3c72366729ea23c5 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Thu, 20 Jun 2019 19:42:21 +0200 Subject: [PATCH] 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 --- .../internal/transport/sshd/JGitSshConfig.java | 16 ++++++++++------ .../transport/sshd/SshdSessionFactory.java | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/transport/sshd/JGitSshConfig.java b/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/transport/sshd/JGitSshConfig.java index 6468b3e27..54a2a052a 100644 --- a/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/transport/sshd/JGitSshConfig.java +++ b/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/internal/transport/sshd/JGitSshConfig.java @@ -83,7 +83,9 @@ */ 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 @@ -92,20 +94,22 @@ public class JGitSshConfig implements HostConfigEntryResolver { * @param home * user's home directory for the purpose of ~ replacement * @param config - * file to load. + * file to load; may be {@code null} if no ssh config file + * handling is desired * @param localUserName * 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) { - configFile = new OpenSshConfigFile(home, config, localUserName); + this.localUserName = localUserName; + configFile = config == null ? null : new OpenSshConfigFile(home, config, localUserName); } @Override public HostConfigEntry resolveEffectiveHost(String host, int port, SocketAddress localAddress, String username, 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(); // Apache MINA conflates all keys, even multi-valued ones, in one map // and puts multiple values separated by commas in one string. See @@ -131,7 +135,7 @@ public HostConfigEntry resolveEffectiveHost(String host, int port, String user = username != null && !username.isEmpty() ? username : entry.getValue(SshConstants.USER); if (user == null || user.isEmpty()) { - user = configFile.getLocalUserName(); + user = localUserName; } config.setUsername(user); config.setProperty(SshConstants.USER, user); diff --git a/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/transport/sshd/SshdSessionFactory.java b/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/transport/sshd/SshdSessionFactory.java index 90dc8ca50..ea5d81155 100644 --- a/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/transport/sshd/SshdSessionFactory.java +++ b/org.eclipse.jgit.ssh.apache/src/org/eclipse/jgit/transport/sshd/SshdSessionFactory.java @@ -360,11 +360,25 @@ private HostConfigEntryResolver getHostConfigEntryResolver( @NonNull File homeDir, @NonNull File sshDir) { return defaultHostConfigEntryResolver.computeIfAbsent( new Tuple(new Object[] { homeDir, sshDir }), - t -> new JGitSshConfig(homeDir, - new File(sshDir, SshConstants.CONFIG), + t -> new JGitSshConfig(homeDir, getSshConfig(sshDir), 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 * verify server host keys. The default implementation returns a