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();
}
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;
}
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;
}
}
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;
}
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);
}
}
}
Aggregations