✨ 添加主机身份类型.
This commit is contained in:
@@ -31,6 +31,10 @@ public class HostIdentityDO extends BaseDO {
|
||||
@TableField("name")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "类型")
|
||||
@TableField("type")
|
||||
private String type;
|
||||
|
||||
@Schema(description = "用户名")
|
||||
@TableField("username")
|
||||
private String username;
|
||||
|
||||
@@ -30,6 +30,9 @@ public class HostIdentityCacheDTO implements LongCacheIdModel, Serializable {
|
||||
@Schema(description = "名称")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "类型")
|
||||
private String type;
|
||||
|
||||
@Schema(description = "用户名")
|
||||
private String username;
|
||||
|
||||
|
||||
@@ -29,6 +29,11 @@ public class HostIdentityCreateRequest implements Serializable {
|
||||
@Schema(description = "名称")
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 12)
|
||||
@Schema(description = "类型")
|
||||
private String type;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 128)
|
||||
@Schema(description = "用户名")
|
||||
|
||||
@@ -31,6 +31,10 @@ public class HostIdentityQueryRequest extends PageRequest {
|
||||
@Schema(description = "名称")
|
||||
private String name;
|
||||
|
||||
@Size(max = 12)
|
||||
@Schema(description = "类型")
|
||||
private String type;
|
||||
|
||||
@Size(max = 128)
|
||||
@Schema(description = "用户名")
|
||||
private String username;
|
||||
|
||||
@@ -34,6 +34,11 @@ public class HostIdentityUpdateRequest implements UpdatePasswordAction {
|
||||
@Schema(description = "名称")
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 12)
|
||||
@Schema(description = "类型")
|
||||
private String type;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 128)
|
||||
@Schema(description = "用户名")
|
||||
|
||||
@@ -31,6 +31,9 @@ public class HostIdentityVO implements Serializable {
|
||||
@Schema(description = "名称")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "类型")
|
||||
private String type;
|
||||
|
||||
@Schema(description = "用户名")
|
||||
private String username;
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.orion.ops.module.asset.enums;
|
||||
|
||||
/**
|
||||
* 主机身份类型
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/9/21 19:01
|
||||
*/
|
||||
public enum HostIdentityTypeEnum {
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
PASSWORD,
|
||||
|
||||
/**
|
||||
* 秘钥
|
||||
*/
|
||||
KEY,
|
||||
|
||||
;
|
||||
|
||||
public static HostIdentityTypeEnum of(String type) {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
for (HostIdentityTypeEnum value : values()) {
|
||||
if (value.name().equals(type)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import com.orion.ops.module.asset.entity.request.host.HostIdentityCreateRequest;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostIdentityQueryRequest;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostIdentityUpdateRequest;
|
||||
import com.orion.ops.module.asset.entity.vo.HostIdentityVO;
|
||||
import com.orion.ops.module.asset.enums.HostIdentityTypeEnum;
|
||||
import com.orion.ops.module.asset.service.HostIdentityService;
|
||||
import com.orion.ops.module.infra.api.DataExtraApi;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -64,7 +65,7 @@ public class HostIdentityServiceImpl implements HostIdentityService {
|
||||
public Long createHostIdentity(HostIdentityCreateRequest request) {
|
||||
log.info("HostIdentityService-createHostIdentity request: {}", JSON.toJSONString(request));
|
||||
// 检查秘钥是否存在
|
||||
this.checkKeyIdPresent(request.getKeyId());
|
||||
this.checkCreateParams(request);
|
||||
// 转换
|
||||
HostIdentityDO record = HostIdentityConvert.MAPPER.to(request);
|
||||
// 查询数据是否冲突
|
||||
@@ -85,19 +86,25 @@ public class HostIdentityServiceImpl implements HostIdentityService {
|
||||
@Override
|
||||
public Integer updateHostIdentityById(HostIdentityUpdateRequest request) {
|
||||
log.info("HostIdentityService-updateHostIdentityById request: {}", JSON.toJSONString(request));
|
||||
// 查询
|
||||
// 验证参数
|
||||
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
|
||||
HostIdentityTypeEnum type = Valid.valid(HostIdentityTypeEnum::of, request.getType());
|
||||
if (HostIdentityTypeEnum.KEY.equals(type)) {
|
||||
// 秘钥认证
|
||||
this.checkKeyId(request.getKeyId());
|
||||
}
|
||||
// 查询主机身份
|
||||
HostIdentityDO record = hostIdentityDAO.selectById(id);
|
||||
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
|
||||
// 检查秘钥是否存在
|
||||
this.checkKeyIdPresent(request.getKeyId());
|
||||
// 转换
|
||||
HostIdentityDO updateRecord = HostIdentityConvert.MAPPER.to(request);
|
||||
// 查询数据是否冲突
|
||||
this.checkHostIdentityPresent(updateRecord);
|
||||
// 设置密码
|
||||
String newPassword = PasswordModifier.getEncryptNewPassword(request);
|
||||
updateRecord.setPassword(newPassword);
|
||||
if (HostIdentityTypeEnum.PASSWORD.equals(type)) {
|
||||
// 设置密码
|
||||
String newPassword = PasswordModifier.getEncryptNewPassword(request);
|
||||
updateRecord.setPassword(newPassword);
|
||||
}
|
||||
// 更新
|
||||
LambdaUpdateWrapper<HostIdentityDO> wrapper = Wrappers.<HostIdentityDO>lambdaUpdate()
|
||||
.set(HostIdentityDO::getKeyId, request.getKeyId())
|
||||
@@ -105,10 +112,7 @@ public class HostIdentityServiceImpl implements HostIdentityService {
|
||||
int effect = hostIdentityDAO.update(updateRecord, wrapper);
|
||||
log.info("HostIdentityService-updateHostIdentityById effect: {}", effect);
|
||||
// 删除缓存
|
||||
if (!record.getName().equals(updateRecord.getName()) ||
|
||||
!record.getUsername().equals(updateRecord.getUsername())) {
|
||||
RedisMaps.delete(HostCacheKeyDefine.HOST_IDENTITY);
|
||||
}
|
||||
RedisMaps.delete(HostCacheKeyDefine.HOST_IDENTITY);
|
||||
return effect;
|
||||
}
|
||||
|
||||
@@ -155,6 +159,7 @@ public class HostIdentityServiceImpl implements HostIdentityService {
|
||||
}
|
||||
// 设置秘钥名称
|
||||
List<Long> keyIdList = dataGrid.stream()
|
||||
.filter(s -> HostIdentityTypeEnum.KEY.name().equals(s.getType()))
|
||||
.map(HostIdentityVO::getKeyId)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
@@ -212,14 +217,28 @@ public class HostIdentityServiceImpl implements HostIdentityService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查秘钥是否存在
|
||||
* 检查创建参数
|
||||
*
|
||||
* @param request request
|
||||
*/
|
||||
private void checkCreateParams(HostIdentityCreateRequest request) {
|
||||
HostIdentityTypeEnum type = Valid.valid(HostIdentityTypeEnum::of, request.getType());
|
||||
if (HostIdentityTypeEnum.PASSWORD.equals(type)) {
|
||||
// 密码认证
|
||||
Valid.notBlank(request.getPassword(), ErrorMessage.PARAM_MISSING);
|
||||
} else if (HostIdentityTypeEnum.KEY.equals(type)) {
|
||||
// 秘钥认证
|
||||
this.checkKeyId(request.getKeyId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 keyId 是否存在
|
||||
*
|
||||
* @param keyId keyId
|
||||
*/
|
||||
private void checkKeyIdPresent(Long keyId) {
|
||||
if (keyId == null) {
|
||||
return;
|
||||
}
|
||||
private void checkKeyId(Long keyId) {
|
||||
Valid.notNull(keyId, ErrorMessage.PARAM_MISSING);
|
||||
Valid.notNull(hostKeyDAO.selectById(keyId), ErrorMessage.KEY_ABSENT);
|
||||
}
|
||||
|
||||
@@ -234,6 +253,7 @@ public class HostIdentityServiceImpl implements HostIdentityService {
|
||||
return hostIdentityDAO.wrapper()
|
||||
.eq(HostIdentityDO::getId, request.getId())
|
||||
.like(HostIdentityDO::getName, request.getName())
|
||||
.eq(HostIdentityDO::getType, request.getType())
|
||||
.like(HostIdentityDO::getUsername, request.getUsername())
|
||||
.eq(HostIdentityDO::getKeyId, request.getKeyId())
|
||||
.and(Strings.isNotEmpty(searchValue), c -> c
|
||||
|
||||
@@ -243,52 +243,62 @@ public class HostTerminalServiceImpl implements HostTerminalService {
|
||||
private HostTerminalConnectDTO getHostConnectInfo(HostDO host,
|
||||
HostSshConfigModel config,
|
||||
HostSshExtraModel extra) {
|
||||
// 获取认证方式
|
||||
HostSshAuthTypeEnum authType = HostSshAuthTypeEnum.of(config.getAuthType());
|
||||
HostExtraSshAuthTypeEnum extraAuthType = Optional.ofNullable(extra)
|
||||
.map(HostSshExtraModel::getAuthType)
|
||||
.map(HostExtraSshAuthTypeEnum::of)
|
||||
.orElse(HostExtraSshAuthTypeEnum.DEFAULT);
|
||||
if (HostExtraSshAuthTypeEnum.CUSTOM_KEY.equals(extraAuthType)) {
|
||||
// 自定义秘钥
|
||||
authType = HostSshAuthTypeEnum.KEY;
|
||||
config.setKeyId(extra.getKeyId());
|
||||
if (extra.getUsername() != null) {
|
||||
config.setUsername(extra.getUsername());
|
||||
}
|
||||
} else if (HostExtraSshAuthTypeEnum.CUSTOM_IDENTITY.equals(extraAuthType)) {
|
||||
// 自定义身份
|
||||
authType = HostSshAuthTypeEnum.IDENTITY;
|
||||
config.setIdentityId(extra.getIdentityId());
|
||||
}
|
||||
Long keyId = null;
|
||||
// 填充认证信息
|
||||
HostTerminalConnectDTO conn = new HostTerminalConnectDTO();
|
||||
conn.setHostId(host.getId());
|
||||
conn.setHostName(host.getName());
|
||||
conn.setHostAddress(host.getAddress());
|
||||
conn.setPort(config.getPort());
|
||||
conn.setTimeout(config.getConnectTimeout());
|
||||
conn.setCharset(config.getCharset());
|
||||
conn.setFileNameCharset(config.getFileNameCharset());
|
||||
conn.setFileContentCharset(config.getFileContentCharset());
|
||||
conn.setTimeout(config.getConnectTimeout());
|
||||
conn.setUsername(config.getUsername());
|
||||
// 填充身份信息
|
||||
if (HostSshAuthTypeEnum.PASSWORD.equals(authType)) {
|
||||
conn.setPassword(config.getPassword());
|
||||
} else if (HostSshAuthTypeEnum.KEY.equals(authType)) {
|
||||
// 秘钥认证
|
||||
keyId = config.getKeyId();
|
||||
} else if (HostSshAuthTypeEnum.IDENTITY.equals(authType)) {
|
||||
|
||||
// 获取自定义认证方式
|
||||
HostExtraSshAuthTypeEnum extraAuthType = Optional.ofNullable(extra)
|
||||
.map(HostSshExtraModel::getAuthType)
|
||||
.map(HostExtraSshAuthTypeEnum::of)
|
||||
.orElse(null);
|
||||
if (HostExtraSshAuthTypeEnum.CUSTOM_KEY.equals(extraAuthType)) {
|
||||
// 自定义秘钥
|
||||
config.setAuthType(HostSshAuthTypeEnum.KEY.name());
|
||||
config.setKeyId(extra.getKeyId());
|
||||
if (extra.getUsername() != null) {
|
||||
config.setUsername(extra.getUsername());
|
||||
}
|
||||
} else if (HostExtraSshAuthTypeEnum.CUSTOM_IDENTITY.equals(extraAuthType)) {
|
||||
// 自定义身份
|
||||
config.setAuthType(HostSshAuthTypeEnum.IDENTITY.name());
|
||||
config.setIdentityId(extra.getIdentityId());
|
||||
}
|
||||
|
||||
// 身份认证
|
||||
HostSshAuthTypeEnum authType = HostSshAuthTypeEnum.of(config.getAuthType());
|
||||
if (HostSshAuthTypeEnum.IDENTITY.equals(authType)) {
|
||||
// 身份认证
|
||||
HostIdentityDO identity = hostIdentityDAO.selectById(config.getIdentityId());
|
||||
Valid.notNull(identity, ErrorMessage.IDENTITY_ABSENT);
|
||||
keyId = identity.getKeyId();
|
||||
conn.setUsername(identity.getUsername());
|
||||
conn.setPassword(identity.getPassword());
|
||||
config.setUsername(identity.getUsername());
|
||||
HostIdentityTypeEnum identityType = HostIdentityTypeEnum.of(identity.getType());
|
||||
if (HostIdentityTypeEnum.PASSWORD.equals(identityType)) {
|
||||
// 密码类型
|
||||
authType = HostSshAuthTypeEnum.PASSWORD;
|
||||
config.setPassword(identity.getPassword());
|
||||
} else if (HostIdentityTypeEnum.KEY.equals(identityType)) {
|
||||
// 秘钥类型
|
||||
authType = HostSshAuthTypeEnum.KEY;
|
||||
config.setKeyId(identity.getKeyId());
|
||||
}
|
||||
}
|
||||
// 设置秘钥信息
|
||||
if (keyId != null) {
|
||||
|
||||
// 填充认证信息
|
||||
conn.setUsername(config.getUsername());
|
||||
if (HostSshAuthTypeEnum.PASSWORD.equals(authType)) {
|
||||
// 密码认证
|
||||
conn.setPassword(config.getPassword());
|
||||
} else if (HostSshAuthTypeEnum.KEY.equals(authType)) {
|
||||
// 秘钥认证
|
||||
Long keyId = config.getKeyId();
|
||||
HostKeyDO key = hostKeyDAO.selectById(keyId);
|
||||
Valid.notNull(key, ErrorMessage.KEY_ABSENT);
|
||||
conn.setKeyId(keyId);
|
||||
@@ -296,7 +306,6 @@ public class HostTerminalServiceImpl implements HostTerminalService {
|
||||
conn.setPrivateKey(key.getPrivateKey());
|
||||
conn.setPrivateKeyPassword(key.getPassword());
|
||||
}
|
||||
// 连接
|
||||
return conn;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user