bug5311: better SSH connection handling in case an unknown/null key was used for launching instance and no explicit key is provided

This commit is contained in:
Axel Uhl committed 2021-09-23 23:55:26 +02:00
1 parent f030ccab1d
commit 85baacb663
1 file changed
+25 -20
@@ -199,27 +199,32 @@ public class AwsInstanceImpl<ShardingKey> implements AwsInstance<ShardingKey> {
" with key "+optionalKeyName+
" to instance with ID "+getInstanceId());
Channel result;
try {
result = Wait.wait(()->{
Session session = null;
try {
session = createSshSession(sshUserName, optionalKeyName, privateKeyEncryptionPassphrase);
session.setUserInfo(new YesUserInfo());
session.connect(optionalTimeout.map(d->d.asMillis()).orElse(0l).intValue());
return session.openChannel(channelType);
} catch (JSchException | IllegalStateException e) {
if (session != null) {
session.disconnect();
}
throw e;
}
},
channel->channel != null,
/* retryOnException */ true, optionalTimeout,
Duration.ONE_SECOND.times(5), Level.INFO,
"Trying to connect to " + getInstanceId() + " with user " + sshUserName + " using SSH");
} catch (TimeoutException timeout) {
if (!optionalKeyName.isPresent() && !Util.hasLength(getInstance().keyName())) {
logger.severe("SSH connection to "+this+" cannot be made because no key name is provided, neither explicitly nor during start-up");
result = null;
} else {
try {
result = Wait.wait(()->{
Session session = null;
try {
session = createSshSession(sshUserName, optionalKeyName, privateKeyEncryptionPassphrase);
session.setUserInfo(new YesUserInfo());
session.connect(optionalTimeout.map(d->d.asMillis()).orElse(0l).intValue());
return session.openChannel(channelType);
} catch (JSchException | IllegalStateException e) {
if (session != null) {
session.disconnect();
}
throw e;
}
},
channel->channel != null,
/* retryOnException */ true, optionalTimeout,
Duration.ONE_SECOND.times(5), Level.INFO,
"Trying to connect to " + getInstanceId() + " with user " + sshUserName + " using SSH");
} catch (TimeoutException timeout) {
result = null;
}
}
return result;
}