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>
This commit is contained in:
Thomas Wolf 2019-06-20 19:42:21 +02:00 committed by Matthias Sohn
parent 4e8d5d4c63
commit 8c74a54315
2 changed files with 26 additions and 8 deletions

View File

@ -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);

View File

@ -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