Search in sources :

Example 1 with MachineSecretKeyDO

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

the class MachineKeyServiceImpl method addSecretKey.

@Override
@Transactional(rollbackFor = Exception.class)
public Long addSecretKey(MachineKeyRequest request) {
    MachineSecretKeyDO key = new MachineSecretKeyDO();
    key.setKeyName(request.getName());
    key.setDescription(request.getDescription());
    String file = PathBuilders.getSecretKeyPath();
    String path = MachineKeyService.getKeyPath(file);
    key.setSecretKeyPath(file);
    Files1.touch(path);
    byte[] keyFileData = Base64s.decode(Strings.bytes(request.getFile()));
    FileWriters.writeFast(path, keyFileData);
    key.setPassword(ValueMix.encrypt(request.getPassword()));
    // 加载key
    SessionHolder.addIdentity(path, request.getPassword());
    machineSecretKeyDAO.insert(key);
    // 设置日志参数
    EventParamsHolder.addParams(key);
    return key.getId();
}
Also used : MachineSecretKeyDO(com.orion.ops.entity.domain.MachineSecretKeyDO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with MachineSecretKeyDO

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

the class MachineKeyServiceImpl method mountOrDumpKeys.

@Override
public Map<String, Integer> mountOrDumpKeys(List<Long> idList, boolean mount) {
    Map<String, Integer> map = Maps.newLinkedMap();
    for (Long id : idList) {
        MachineSecretKeyDO key = Valid.notNull(machineSecretKeyDAO.selectById(id), "秘钥未找到");
        Integer status = this.mountOrDump(key, mount);
        map.put(id + Strings.EMPTY, status);
    }
    // 设置日志参数
    EventParamsHolder.addParam(EventKeys.ID_LIST, idList);
    EventParamsHolder.addParam(EventKeys.COUNT, idList.size());
    return map;
}
Also used : MachineSecretKeyDO(com.orion.ops.entity.domain.MachineSecretKeyDO)

Example 3 with MachineSecretKeyDO

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

the class MachineKeyServiceImpl method listKeys.

@Override
public DataGrid<MachineSecretKeyVO> listKeys(MachineKeyRequest request) {
    final int page = request.getPage();
    final int limit = request.getLimit();
    final boolean checkStatus = request.getMountStatus() != null;
    if (checkStatus) {
        request.setPage(Const.N_1);
        request.setLimit(Const.N_100000);
    }
    LambdaQueryWrapper<MachineSecretKeyDO> wrapper = new LambdaQueryWrapper<MachineSecretKeyDO>().like(Strings.isNotBlank(request.getName()), MachineSecretKeyDO::getKeyName, request.getName()).like(Strings.isNotBlank(request.getDescription()), MachineSecretKeyDO::getDescription, request.getDescription()).orderByDesc(MachineSecretKeyDO::getCreateTime);
    DataGrid<MachineSecretKeyVO> dataGrid = DataQuery.of(machineSecretKeyDAO).page(request).wrapper(wrapper).dataGrid(MachineSecretKeyVO.class);
    if (!dataGrid.isEmpty()) {
        List<String> loadKeys = SessionHolder.getLoadKeys();
        for (MachineSecretKeyVO row : dataGrid.getRows()) {
            String path = row.getPath();
            boolean isFile = Files1.isFile(new File(MachineKeyService.getKeyPath(path)));
            if (isFile) {
                boolean match = loadKeys.stream().anyMatch(key -> key.endsWith(path));
                if (match) {
                    row.setMountStatus(MountKeyStatus.MOUNTED.getStatus());
                } else {
                    row.setMountStatus(MountKeyStatus.DUMPED.getStatus());
                }
            } else {
                row.setMountStatus(MountKeyStatus.NOT_FOUND.getStatus());
            }
        }
    }
    if (!checkStatus) {
        return dataGrid;
    } else {
        // 手动过滤
        List<MachineSecretKeyVO> totalRows = dataGrid.stream().filter(row -> request.getMountStatus().equals(row.getMountStatus())).collect(Collectors.toList());
        List<MachineSecretKeyVO> rows = new LimitList<>(totalRows, limit).page(page);
        // 封装返回
        DataGrid<MachineSecretKeyVO> newDataGrid = DataGrid.of(rows, totalRows.size());
        newDataGrid.setPage(page);
        newDataGrid.setLimit(limit);
        return newDataGrid;
    }
}
Also used : EventKeys(com.orion.ops.consts.event.EventKeys) ValueMix(com.orion.ops.utils.ValueMix) MachineSecretKeyVO(com.orion.ops.entity.vo.MachineSecretKeyVO) Date(java.util.Date) MessageConst(com.orion.ops.consts.MessageConst) EventParamsHolder(com.orion.ops.consts.event.EventParamsHolder) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) FileWriters(com.orion.utils.io.FileWriters) ObjectIds(com.orion.id.ObjectIds) MachineSecretKeyDO(com.orion.ops.entity.domain.MachineSecretKeyDO) LimitList(com.orion.lang.collect.LimitList) MachineSecretKeyDAO(com.orion.ops.dao.MachineSecretKeyDAO) PathBuilders(com.orion.ops.utils.PathBuilders) Service(org.springframework.stereotype.Service) MachineKeyRequest(com.orion.ops.entity.request.MachineKeyRequest) Map(java.util.Map) Maps(com.orion.utils.collect.Maps) Const(com.orion.ops.consts.Const) MountKeyStatus(com.orion.ops.consts.machine.MountKeyStatus) DataGrid(com.orion.lang.wrapper.DataGrid) MachineKeyService(com.orion.ops.service.api.MachineKeyService) Resource(javax.annotation.Resource) Valid(com.orion.ops.utils.Valid) Collectors(java.util.stream.Collectors) File(java.io.File) Files1(com.orion.utils.io.Files1) DataQuery(com.orion.ops.utils.DataQuery) Converts(com.orion.utils.convert.Converts) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Base64s(com.orion.utils.codec.Base64s) SessionHolder(com.orion.remote.channel.SessionHolder) Strings(com.orion.utils.Strings) Transactional(org.springframework.transaction.annotation.Transactional) MachineSecretKeyVO(com.orion.ops.entity.vo.MachineSecretKeyVO) MachineSecretKeyDO(com.orion.ops.entity.domain.MachineSecretKeyDO) File(java.io.File)

Example 4 with MachineSecretKeyDO

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

the class MachineKeyServiceImpl method removeSecretKey.

@Override
@Transactional(rollbackFor = Exception.class)
public Integer removeSecretKey(List<Long> idList) {
    int effect = 0;
    for (Long id : idList) {
        MachineSecretKeyDO key = machineSecretKeyDAO.selectById(id);
        if (key == null) {
            continue;
        }
        String secretKeyPath = MachineKeyService.getKeyPath(key.getSecretKeyPath());
        // 移除key
        SessionHolder.removeIdentity(secretKeyPath);
        // 删除key
        Files1.delete(secretKeyPath);
        effect += machineSecretKeyDAO.deleteById(id);
    }
    // 设置日志参数
    EventParamsHolder.addParam(EventKeys.ID_LIST, idList);
    EventParamsHolder.addParam(EventKeys.COUNT, idList.size());
    return effect;
}
Also used : MachineSecretKeyDO(com.orion.ops.entity.domain.MachineSecretKeyDO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with MachineSecretKeyDO

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

the class MachineKeyServiceImpl method mountAllKey.

@Override
public void mountAllKey() {
    List<MachineSecretKeyDO> keys = machineSecretKeyDAO.selectList(null);
    for (MachineSecretKeyDO key : keys) {
        String secretKeyPath = MachineKeyService.getKeyPath(key.getSecretKeyPath());
        File secretKey = new File(secretKeyPath);
        if (!Files1.isFile(secretKey)) {
            log.warn("加载ssh秘钥失败 未找到文件 {} {}", key.getKeyName(), secretKeyPath);
            continue;
        }
        log.info("加载ssh秘钥 {} {}", key.getKeyName(), secretKeyPath);
        String password = ValueMix.decrypt(key.getPassword());
        if (password == null) {
            log.warn("加载ssh秘钥失败 密码错误 {} {}", key.getKeyName(), secretKeyPath);
            continue;
        }
        try {
            SessionHolder.addIdentity(secretKeyPath, password);
        } catch (Exception e) {
            log.error("加载ssh秘钥失败 发生异常 {} {} {}", key.getKeyName(), secretKeyPath, e);
        }
    }
}
Also used : 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