密钥存储修改

This commit is contained in:
唐潇凯 2025-07-03 09:30:38 +08:00
parent 38ba61ed3a
commit d4a3b9b1b9
3 changed files with 20 additions and 71 deletions

View File

@ -25,12 +25,6 @@ public class SecretKeyVO extends PageParam {
@Schema(description = "密钥描述", example = "用于加密敏感数据的密钥")
private String description;
@Schema(description = "存储路径(本地上传文件路径)", example = "/data/secret_keys/")
private String path;
@Schema(description = "文件名", example = "key.pem")
private String fileName;
@Schema(description = "密钥密码", example = "******")
private String password;
@ -42,4 +36,11 @@ public class SecretKeyVO extends PageParam {
@Schema(description = "关联的机器ID列表", requiredMode = Schema.RequiredMode.REQUIRED, example = "[1024, 2048]")
private List<Long> machineInfoIds;
@Schema(description = "私钥", requiredMode = Schema.RequiredMode.REQUIRED, example = "******")
private String private_key;
@Schema(description = "公钥", requiredMode = Schema.RequiredMode.REQUIRED, example = "******")
private String public_key;
}

View File

@ -28,14 +28,17 @@ public class SecretKeyDO extends BaseDO {
@TableField(value = "description")
private String description;
//oss存储路径
@TableField(value = "path")
private String path;
@TableField
private String fileName;
//密钥密码
@TableField(value = "password")
private String password;
@TableField(value = "public_key")
private String public_key;
@TableField(value = "private_key")
private String private_key;
}

View File

@ -29,8 +29,6 @@ public class SecretKeyServiceImpl implements SecretKeyService {
@Resource
private MachineInfoService machineInfoService;
@Resource
private AliYunOssClient aliYunOssClient;
@Resource
private SecretKeyMapper secretKeyMapper;
@ -50,31 +48,16 @@ public class SecretKeyServiceImpl implements SecretKeyService {
@Override
public Long createSecretKey(SecretKeyVO secretKeyVO) {
validateSecretKeyAdd(secretKeyVO);
String ossPath = upLoadSecretKey(secretKeyVO.getPath());
//检查得到的oss路径是否为空
validateSecretKeyPath(ossPath);
secretKeyVO.setPath(ossPath);
SecretKeyDO secretKeyDO = BeanUtils.toBean(secretKeyVO, SecretKeyDO.class);
//todo检查密钥合法
secretKeyMapper.insert(secretKeyDO);
return secretKeyDO.getId();
}
@Override
public void updateSecretKey(SecretKeyVO secretKeyVO) {
SecretKeyDO secretKeyDO = validateSecretKeyExists(secretKeyVO.getId());
//如果路径改变==改变密钥
if (!secretKeyDO.getPath().equals(secretKeyVO.getPath())) {
//todo检查密钥合法
String ossPath = upLoadSecretKey(secretKeyVO.getPath());
BeanUtils.copyProperties(secretKeyVO, secretKeyDO);
secretKeyDO.setPath(ossPath);
} else {
BeanUtils.copyProperties(secretKeyVO, secretKeyDO);
}
secretKeyMapper.updateById(secretKeyDO);
}
@ -87,25 +70,8 @@ public class SecretKeyServiceImpl implements SecretKeyService {
@Override
@Transactional
public void deleteSecretKeyList(List<Long> ids) {
ids.forEach(
secretKeyId -> {
SecretKeyDO secretKeyDO = validateSecretKeyExists(secretKeyId);
if (secretKeyDO.getPath() != null && !secretKeyDO.getPath().isEmpty()) {
try {
//文件名
//删除子目录文件需要在前面加上根目录文件路径
String fileName = secretKeyDO.getPath().substring(secretKeyDO.getPath().lastIndexOf("/") + 1);
aliYunOssClient.delete(fileName);
} catch (Exception e) {
throw exception(DELETE_FILE_FAIL);
}
}
}
);
//绑定的机器全部设置为空
machineInfoService.bindingSecretKey(ids,null);
secretKeyMapper.deleteBatchIds(ids);
}
@ -114,38 +80,17 @@ public class SecretKeyServiceImpl implements SecretKeyService {
return secretKeyMapper.selectPage(secretKeyVO);
}
public String upLoadSecretKey(String localPath) {
//使用S3FileClient上传文件
aliYunOssClient.init();
//传输到指定文件需要在path前面加上文件路径
String path = IdUtil.fastSimpleUUID() + ".txt";
//上传文件是从本地上传这里传的是本地文件地址
byte[] content = ResourceUtil.readBytes(localPath);
String ossPath;
try {
ossPath = aliYunOssClient.upload(content, path, "txt");
} catch (Exception e) {
throw exception(UPLOADING_FILE_FAIL);
}
return ossPath;
}
@VisibleForTesting
void validateSecretKeyAdd(SecretKeyVO secretKeyVO) {
if (secretKeyVO == null) {
throw exception(SECRET_KEY_NULL);
}
if (secretKeyVO.getPath().isEmpty()) {
throw exception(SECRET_KEY_PATH_NULL);
}
}
@VisibleForTesting
void validateSecretKeyPath(String path) {
if (path.isEmpty()) {
throw exception(SECRET_KEY_PATH_NULL);
}
}
@VisibleForTesting