mirror of
https://github.com/LamGC/Oracle-Sentry.git
synced 2025-07-01 04:47:27 +00:00
fact: 添加 SSH 端口配置项.
添加 SSH 端口配置项以允许自定义 SSH 连接端口. 本次提交也补充了相应的测试项, 覆盖率 100%.
This commit is contained in:
@ -65,7 +65,8 @@ public class InstanceSsh implements AutoCloseable {
|
||||
if (instancePublicIps.stream().findFirst().isEmpty()) {
|
||||
throw new IllegalStateException("Instance has no public IP available.");
|
||||
}
|
||||
String connectUri = "ssh://" + authInfo.getUsername() + "@" + instancePublicIps.stream().findFirst().get() + ":22";
|
||||
String connectUri = "ssh://" + authInfo.getUsername() + "@" +
|
||||
instancePublicIps.stream().findFirst().get() + ":" + authInfo.getPort();
|
||||
log.info("SSH 正在连接: {}", connectUri);
|
||||
ConnectFuture connect = sshClient.connect(connectUri);
|
||||
connect.verify();
|
||||
|
@ -15,11 +15,8 @@ public abstract class SshAuthInfo {
|
||||
private final static Logger log = LoggerFactory.getLogger(SshAuthInfo.class);
|
||||
|
||||
private String username;
|
||||
/**
|
||||
* 使用 Sha256 计算的密钥指纹.
|
||||
*/
|
||||
private PublicKey serverKey;
|
||||
|
||||
private int port;
|
||||
private SshAuthIdentityProvider provider;
|
||||
|
||||
/**
|
||||
@ -65,6 +62,22 @@ public abstract class SshAuthInfo {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 SSH 连接端口.
|
||||
* @param port SSH 端口号.
|
||||
*/
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 SSH 端口号.
|
||||
* @return 返回 SSH 端口号.
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 SSH 认证配置提供器.
|
||||
* <p> 设置后, 可在首次连接认证通过后, 保存服务器公钥到文件中.
|
||||
|
@ -56,6 +56,24 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
|
||||
throw new JsonParseException("Unsupported authentication type: " + authType);
|
||||
}
|
||||
info.setUsername(getFieldToStringOrFail(infoObject, "username"));
|
||||
String portStr = getFieldToString(infoObject, "port");
|
||||
if (portStr != null) {
|
||||
try {
|
||||
int port = Integer.parseInt(portStr);
|
||||
if (checkPortNumber(port)) {
|
||||
info.setPort(port);
|
||||
} else {
|
||||
log.warn("端口号非法, 将使用默认端口号.(Input: {})", port);
|
||||
info.setPort(22);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("端口号无法转换成数字, 端口号将使用默认端口号.(Input: {})", portStr);
|
||||
info.setPort(22);
|
||||
}
|
||||
} else {
|
||||
info.setPort(22);
|
||||
}
|
||||
|
||||
String serverKeyStr = getFieldToString(infoObject, "serverKey");
|
||||
if (!Strings.isNullOrEmpty(serverKeyStr)) {
|
||||
try {
|
||||
@ -88,6 +106,7 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
|
||||
|
||||
json.addProperty("authType", src.getType().toString());
|
||||
json.addProperty("username", src.getUsername());
|
||||
json.addProperty("port", src.getPort());
|
||||
if (src.getServerKey() != null) {
|
||||
json.addProperty("serverKey", encodeSshPublicKey(src.getServerKey()));
|
||||
} else {
|
||||
@ -96,6 +115,10 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
|
||||
return json;
|
||||
}
|
||||
|
||||
private boolean checkPortNumber(int port) {
|
||||
return port >= 0 && port <= 65535;
|
||||
}
|
||||
|
||||
private String getFieldToStringOrFail(JsonObject object, String field) {
|
||||
if (!object.has(field) || !object.get(field).isJsonPrimitive()) {
|
||||
throw new JsonParseException("Missing field: " + field);
|
||||
|
Reference in New Issue
Block a user