refactor: 调整代码以更好的进行测试.

部分代码对测试不友好, 故在不影响原设计的情况下进行了代码调整, 以便更好的编写测试项.
This commit is contained in:
LamGC 2021-08-20 02:19:11 +08:00
parent 7ac17c6ed7
commit 35c45a858c
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D

View File

@ -1,10 +1,11 @@
package net.lamgc.oracle.sentry.oci.compute.ssh; package net.lamgc.oracle.sentry.oci.compute.ssh;
import com.google.common.base.Strings;
import com.google.gson.*; import com.google.gson.*;
import org.apache.sshd.common.config.keys.KeyUtils; import org.apache.sshd.common.config.keys.KeyUtils;
import org.apache.sshd.common.config.keys.PublicKeyEntry; import org.apache.sshd.common.config.keys.PublicKeyEntry;
import org.apache.sshd.common.config.keys.PublicKeyEntryDecoder; import org.apache.sshd.common.config.keys.PublicKeyEntryDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -23,6 +24,8 @@ import java.util.Collections;
*/ */
public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>, JsonDeserializer<SshAuthInfo> { public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>, JsonDeserializer<SshAuthInfo> {
private final static Logger log = LoggerFactory.getLogger(SshAuthInfoSerializer.class);
/** /**
* 本类唯一实例. * 本类唯一实例.
* <p> 序列化器支持多用. * <p> 序列化器支持多用.
@ -33,12 +36,9 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
@Override @Override
public SshAuthInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public SshAuthInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!json.isJsonObject()) {
throw new JsonParseException("It should be a JsonObject");
}
JsonObject infoObject = json.getAsJsonObject(); JsonObject infoObject = json.getAsJsonObject();
String type = getFieldToStringOrFail(infoObject, "authType"); String type = getFieldToStringOrFail(infoObject, "authType");
SshAuthInfo.AuthType authType = SshAuthInfo.AuthType.valueOf(type.toUpperCase()); SshAuthInfo.AuthType authType = getAuthType(type);
SshAuthInfo info; SshAuthInfo info;
if (authType == SshAuthInfo.AuthType.PASSWORD) { if (authType == SshAuthInfo.AuthType.PASSWORD) {
PasswordAuthInfo pswAuthInfo = new PasswordAuthInfo(); PasswordAuthInfo pswAuthInfo = new PasswordAuthInfo();
@ -56,12 +56,12 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
} }
info.setUsername(getFieldToStringOrFail(infoObject, "username")); info.setUsername(getFieldToStringOrFail(infoObject, "username"));
try { try {
info.setServerKey(decodeSshPublicKey( if (infoObject.has("serverKey") && infoObject.get("serverKey").isJsonPrimitive()) {
infoObject.has("serverKey") && infoObject.get("serverKey").isJsonPrimitive() ? info.setServerKey(decodeSshPublicKey(infoObject.get("serverKey").getAsString()));
infoObject.get("serverKey").getAsString() : }
null));
} catch (GeneralSecurityException | IOException e) { } catch (GeneralSecurityException | IOException e) {
throw new JsonParseException(e); info.setServerKey(null);
log.error("解析 ServerKey 时发生错误, 该 ServerKey 将为空.(后续连接需进行首次连接认证.)", e);
} }
return info; return info;
} }
@ -69,13 +69,6 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
@Override @Override
public JsonElement serialize(SshAuthInfo src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(SshAuthInfo src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject(); JsonObject json = new JsonObject();
json.addProperty("authType", src.getType().toString());
json.addProperty("username", src.getUsername());
try {
json.addProperty("serverKey", encodeSshPublicKey(src.getServerKey()));
} catch (IOException e) {
throw new JsonParseException(e);
}
if (src instanceof PasswordAuthInfo info) { if (src instanceof PasswordAuthInfo info) {
json.addProperty("password", info.getPassword()); json.addProperty("password", info.getPassword());
} else if (src instanceof PublicKeyAuthInfo info) { } else if (src instanceof PublicKeyAuthInfo info) {
@ -88,6 +81,14 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
} else { } else {
throw new JsonParseException("Unsupported type"); throw new JsonParseException("Unsupported type");
} }
json.addProperty("authType", src.getType().toString());
json.addProperty("username", src.getUsername());
if (src.getServerKey() != null) {
json.addProperty("serverKey", encodeSshPublicKey(src.getServerKey()));
} else {
json.add("serverKey", JsonNull.INSTANCE);
}
return json; return json;
} }
@ -99,10 +100,6 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
} }
private PublicKey decodeSshPublicKey(String publicKeyString) throws GeneralSecurityException, IOException { private PublicKey decodeSshPublicKey(String publicKeyString) throws GeneralSecurityException, IOException {
if (Strings.isNullOrEmpty(publicKeyString)) {
return null;
}
String[] strings = publicKeyString.split(" ", 3); String[] strings = publicKeyString.split(" ", 3);
@SuppressWarnings("unchecked") PublicKeyEntryDecoder<PublicKey, ?> decoder = @SuppressWarnings("unchecked") PublicKeyEntryDecoder<PublicKey, ?> decoder =
@ -110,14 +107,22 @@ public final class SshAuthInfoSerializer implements JsonSerializer<SshAuthInfo>,
return decoder.decodePublicKey(null, strings[0], Base64.getDecoder().decode(strings[1]), Collections.emptyMap()); return decoder.decodePublicKey(null, strings[0], Base64.getDecoder().decode(strings[1]), Collections.emptyMap());
} }
private String encodeSshPublicKey(PublicKey key) throws IOException { private String encodeSshPublicKey(PublicKey key) {
if (key == null) { try {
return null;
}
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
PublicKeyEntry.appendPublicKeyEntry(builder, key); PublicKeyEntry.appendPublicKeyEntry(builder, key);
return builder.toString(); return builder.toString();
} catch (IOException ignored) {
}
return null;
}
private SshAuthInfo.AuthType getAuthType(String type) {
try {
return SshAuthInfo.AuthType.valueOf(type.toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
} }
} }