@Override
public void allow(final Host host, final PublicKey key, final boolean persist) {
if(null == database) {
log.warn(String.format("Missing database %s", database));
super.allow(host, key, persist);
}
else {
try {
// Add the host key to the in-memory database
final OpenSSHKnownHosts.HostEntry entry
= new OpenSSHKnownHosts.HostEntry(null, format(host), KeyType.fromKey(key), key);
database.entries().add(entry);
if(persist) {
if(file.attributes().getPermission().isWritable()) {
// Also try to add the key to a known_host file
database.write(entry);
}
}
}
catch(IOException e) {
log.error(String.format("Failure adding host key to database: %s", e.getMessage()));
super.allow(host, key, persist);
}
}
}
SSHChannel(TS3Config config) throws IOException {
if (!config.hasLoginCredentials()) {
throw new TS3ConnectionFailedException("Anonymous queries are not supported when using SSH.\n" +
"\t\tYou must specify a query username and password using TS3Config#setLoginCredentials.");
}
try {
client = new SSHClient();
File knownHostsFile = new File(OpenSSHKnownHosts.detectSSHDir(), KNOWN_HOSTS_FILE_NAME);
client.addHostKeyVerifier(new AutoAddKnownHosts(knownHostsFile));
client.setConnectTimeout(config.getCommandTimeout());
client.setTimeout(config.getCommandTimeout());
client.setRemoteCharset(StandardCharsets.UTF_8);
client.connect(config.getHost(), config.getQueryPort());
client.authPassword(config.getUsername(), config.getPassword());
session = client.startSession();
session.startShell();
} catch (UserAuthException uae) {
close();
throw new TS3ConnectionFailedException("Invalid query username or password");
} catch (IOException ioe) {
close();
throw ioe;
}
}