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