Search in sources :

Example 6 with MachineSecretKeyDO

use of com.orion.ops.entity.domain.MachineSecretKeyDO in project orion-ops by lijiahangmax.

the class MachineKeyServiceImpl method updateSecretKey.

@Override
@Transactional(rollbackFor = Exception.class)
public Integer updateSecretKey(MachineKeyRequest request) {
    // 查询key
    Long id = request.getId();
    MachineSecretKeyDO beforeKey = machineSecretKeyDAO.selectById(id);
    // 设置修改信息
    MachineSecretKeyDO updateKey = new MachineSecretKeyDO();
    updateKey.setId(id);
    updateKey.setKeyName(request.getName());
    updateKey.setDescription(request.getDescription());
    updateKey.setUpdateTime(new Date());
    String password = request.getPassword();
    String fileBase64 = request.getFile();
    final boolean updateKeyFile = !Strings.isBlank(fileBase64) || !Strings.isBlank(password);
    if (updateKeyFile) {
        // 移除原先的key
        String keyPath = MachineKeyService.getKeyPath(beforeKey.getSecretKeyPath());
        SessionHolder.removeIdentity(keyPath);
        // 修改秘钥文件 将新秘钥保存到本地
        if (!Strings.isBlank(fileBase64)) {
            Files1.delete(keyPath);
            String afterKeyFile = PathBuilders.getSecretKeyPath();
            keyPath = MachineKeyService.getKeyPath(afterKeyFile);
            Files1.touch(keyPath);
            byte[] keyFileData = Base64s.decode(Strings.bytes(fileBase64));
            FileWriters.writeFast(keyPath, keyFileData);
            updateKey.setSecretKeyPath(afterKeyFile);
        }
        // 修改密码
        if (!Strings.isBlank(password)) {
            updateKey.setPassword(ValueMix.encrypt(password));
            SessionHolder.addIdentity(keyPath, password);
        }
    }
    // 更新
    int effect = machineSecretKeyDAO.updateById(updateKey);
    // 设置日志参数
    EventParamsHolder.addParam(EventKeys.NAME, beforeKey.getKeyName());
    EventParamsHolder.addParams(updateKey);
    return effect;
}
Also used : MachineSecretKeyDO(com.orion.ops.entity.domain.MachineSecretKeyDO) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with MachineSecretKeyDO

use of com.orion.ops.entity.domain.MachineSecretKeyDO in project orion-ops by lijiahangmax.

the class MachineKeyServiceImpl method getKeyDetail.

@Override
public MachineSecretKeyVO getKeyDetail(Long id) {
    MachineSecretKeyDO key = machineSecretKeyDAO.selectById(id);
    Valid.notNull(key, MessageConst.UNKNOWN_DATA);
    MachineSecretKeyVO vo = Converts.to(key, MachineSecretKeyVO.class);
    // 设置挂载状态
    String path = vo.getPath();
    boolean isFile = Files1.isFile(new File(MachineKeyService.getKeyPath(path)));
    if (isFile) {
        vo.setMountStatus(this.getMountStatus(path));
    } else {
        vo.setMountStatus(MountKeyStatus.NOT_FOUND.getStatus());
    }
    return vo;
}
Also used : MachineSecretKeyVO(com.orion.ops.entity.vo.MachineSecretKeyVO) MachineSecretKeyDO(com.orion.ops.entity.domain.MachineSecretKeyDO) File(java.io.File)

Aggregations

MachineSecretKeyDO (com.orion.ops.entity.domain.MachineSecretKeyDO)7 Transactional (org.springframework.transaction.annotation.Transactional)4 File (java.io.File)3 MachineSecretKeyVO (com.orion.ops.entity.vo.MachineSecretKeyVO)2 Date (java.util.Date)2 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 ObjectIds (com.orion.id.ObjectIds)1 LimitList (com.orion.lang.collect.LimitList)1 DataGrid (com.orion.lang.wrapper.DataGrid)1 Const (com.orion.ops.consts.Const)1 MessageConst (com.orion.ops.consts.MessageConst)1 EventKeys (com.orion.ops.consts.event.EventKeys)1 EventParamsHolder (com.orion.ops.consts.event.EventParamsHolder)1 MountKeyStatus (com.orion.ops.consts.machine.MountKeyStatus)1 MachineSecretKeyDAO (com.orion.ops.dao.MachineSecretKeyDAO)1 MachineKeyRequest (com.orion.ops.entity.request.MachineKeyRequest)1 MachineKeyService (com.orion.ops.service.api.MachineKeyService)1 DataQuery (com.orion.ops.utils.DataQuery)1 PathBuilders (com.orion.ops.utils.PathBuilders)1 Valid (com.orion.ops.utils.Valid)1