SFTP 操作日志.

This commit is contained in:
lijiahang
2024-03-05 18:07:26 +08:00
parent a75ead9a58
commit 554c62abf7
22 changed files with 650 additions and 108 deletions

View File

@@ -1,5 +1,5 @@
### 分页查询 SFTP 操作日志
POST {{baseUrl}}/asset/sftp-log/query
POST {{baseUrl}}/asset/host-sftp-log/query
Content-Type: application/json
Authorization: {{token}}
@@ -9,4 +9,9 @@ Authorization: {{token}}
}
### 删除 SFTP 操作日志
DELETE {{baseUrl}}/asset/host-sftp-log/delete?idList=1,2,3
Authorization: {{token}}
###

View File

@@ -1,24 +1,25 @@
package com.orion.ops.module.asset.controller;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.framework.biz.operator.log.core.annotation.OperatorLog;
import com.orion.ops.framework.common.validator.group.Page;
import com.orion.ops.framework.log.core.annotation.IgnoreLog;
import com.orion.ops.framework.log.core.enums.IgnoreLogMode;
import com.orion.ops.framework.web.core.annotation.RestWrapper;
import com.orion.ops.module.asset.define.operator.HostTerminalOperatorType;
import com.orion.ops.module.asset.entity.request.host.HostSftpLogQueryRequest;
import com.orion.ops.module.asset.entity.vo.HostSftpLogVO;
import com.orion.ops.module.asset.service.HostSftpLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* SFTP 操作日志服务 api
@@ -32,7 +33,7 @@ import javax.annotation.Resource;
@Validated
@RestWrapper
@RestController
@RequestMapping("/asset/sftp-log")
@RequestMapping("/asset/host-sftp-log")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class HostSftpLogController {
@@ -42,9 +43,18 @@ public class HostSftpLogController {
@IgnoreLog(IgnoreLogMode.RET)
@PostMapping("/query")
@Operation(summary = "分页查询 SFTP 操作日志")
@PreAuthorize("@ss.hasPermission('infra:operator-log:query')")
public DataGrid<HostSftpLogVO> querySftpLogPage(@Validated(Page.class) @RequestBody HostSftpLogQueryRequest request) {
return hostSftpLogService.querySftpLogPage(request);
@PreAuthorize("@ss.hasAnyPermission('infra:operator-log:query', 'asset:host-sftp-log:management:query')")
public DataGrid<HostSftpLogVO> getHostSftpLogPage(@Validated(Page.class) @RequestBody HostSftpLogQueryRequest request) {
return hostSftpLogService.getHostSftpLogPage(request);
}
@OperatorLog(HostTerminalOperatorType.DELETE_SFTP_LOG)
@DeleteMapping("/delete")
@Operation(summary = "删除 SFTP 操作日志")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasAnyPermission('infra:operator-log:delete', 'asset:host-sftp-log:management:delete')")
public Integer deleteHostSftpLog(@RequestParam("idList") List<Long> idList) {
return hostSftpLogService.deleteHostSftpLog(idList);
}
}

View File

@@ -21,6 +21,8 @@ public class HostTerminalOperatorType extends InitializingOperatorTypes {
public static final String CONNECT = "host-terminal:connect";
public static final String DELETE_SFTP_LOG = "host-terminal:delete-sftp-log";
public static final String SFTP_MKDIR = "host-terminal:sftp-mkdir";
public static final String SFTP_TOUCH = "host-terminal:sftp-touch";
@@ -55,6 +57,7 @@ public class HostTerminalOperatorType extends InitializingOperatorTypes {
public OperatorType[] types() {
return new OperatorType[]{
new OperatorType(L, CONNECT, "连接主机 ${connectType} <sb>${hostName}</sb>"),
new OperatorType(H, DELETE_SFTP_LOG, "删除 SFTP 操作日志 <sb>${count}</sb> 条"),
new OperatorType(L, SFTP_MKDIR, "创建文件夹 ${hostName} <sb>${path}</sb>"),
new OperatorType(L, SFTP_TOUCH, "创建文件 ${hostName} <sb>${path}</sb>"),
new OperatorType(M, SFTP_MOVE, "移动文件 ${hostName} <sb>${path}</sb> 至 <sb>${target}</sb>"),

View File

@@ -32,14 +32,20 @@ public class HostSftpLogVO implements Serializable {
@Schema(description = "用户id")
private Long userId;
@Schema(description = "主机id")
private Long hostId;
@Schema(description = "用户名")
private String username;
@Schema(description = "traceId")
private String traceId;
@Schema(description = "主机id")
private Long hostId;
@Schema(description = "主机名称")
private String hostName;
@Schema(description = "主机地址")
private String hostAddress;
@Schema(description = "操作文件")
private String[] paths;
@Schema(description = "请求ip")
private String address;
@@ -50,40 +56,16 @@ public class HostSftpLogVO implements Serializable {
@Schema(description = "userAgent")
private String userAgent;
@Schema(description = "风险等级")
private String riskLevel;
@Schema(description = "模块")
private String module;
@Schema(description = "操作类型")
private String type;
@Schema(description = "日志")
private String logInfo;
@Schema(description = "参数")
private Map<String, Object> extra;
@Schema(description = "操作结果 0失败 1成功")
private Integer result;
@Schema(description = "错误信息")
private String errorMessage;
@Schema(description = "返回值")
private String returnValue;
@Schema(description = "操作时间")
private Integer duration;
@Schema(description = "开始时间")
private Date startTime;
@Schema(description = "结束时间")
private Date endTime;
@Schema(description = "创建时间")
private Date createTime;
}

View File

@@ -2,7 +2,6 @@ package com.orion.ops.module.asset.handler.host.terminal.handler;
import com.orion.lang.utils.collect.Maps;
import com.orion.ops.framework.biz.operator.log.core.utils.OperatorLogs;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.define.operator.HostTerminalOperatorType;
import com.orion.ops.module.asset.handler.host.terminal.enums.OutputTypeEnum;
@@ -54,7 +53,7 @@ public class SftpRemoveHandler extends AbstractTerminalHandler<SftpBaseRequest>
.build());
// 保存操作日志
Map<String, Object> extra = Maps.newMap();
extra.put(OperatorLogs.PATH, String.join(Const.COMMA, paths));
extra.put(OperatorLogs.PATH, payload.getPath());
this.saveOperatorLog(payload, channel,
extra, HostTerminalOperatorType.SFTP_REMOVE,
startTime, ex);

View File

@@ -4,6 +4,8 @@ import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.module.asset.entity.request.host.HostSftpLogQueryRequest;
import com.orion.ops.module.asset.entity.vo.HostSftpLogVO;
import java.util.List;
/**
* SFTP 操作日志 服务类
*
@@ -19,6 +21,14 @@ public interface HostSftpLogService {
* @param request request
* @return rows
*/
DataGrid<HostSftpLogVO> querySftpLogPage(HostSftpLogQueryRequest request);
DataGrid<HostSftpLogVO> getHostSftpLogPage(HostSftpLogQueryRequest request);
/**
* 删除 SFTP 操作日志
*
* @param idList idList
* @return effect
*/
Integer deleteHostSftpLog(List<Long> idList);
}

View File

@@ -1,8 +1,12 @@
package com.orion.ops.module.asset.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.Arrays1;
import com.orion.lang.utils.Strings;
import com.orion.ops.framework.biz.operator.log.core.utils.OperatorLogs;
import com.orion.ops.framework.common.constant.ExtraFieldConst;
import com.orion.ops.module.asset.convert.HostSftpLogConvert;
import com.orion.ops.module.asset.define.operator.HostTerminalOperatorType;
import com.orion.ops.module.asset.entity.request.host.HostSftpLogQueryRequest;
@@ -33,7 +37,49 @@ public class HostSftpLogServiceImpl implements HostSftpLogService {
private OperatorLogApi operatorLogApi;
@Override
public DataGrid<HostSftpLogVO> querySftpLogPage(HostSftpLogQueryRequest request) {
public DataGrid<HostSftpLogVO> getHostSftpLogPage(HostSftpLogQueryRequest request) {
// 查询
OperatorLogQueryDTO query = this.buildQueryInfo(request);
DataGrid<OperatorLogDTO> dataGrid = operatorLogApi.getOperatorLogPage(query);
// 转换
List<HostSftpLogVO> rows = dataGrid.stream()
.map(s -> {
JSONObject extra = JSON.parseObject(s.getExtra());
HostSftpLogVO vo = HostSftpLogConvert.MAPPER.to(s);
vo.setHostId(extra.getLong(ExtraFieldConst.HOST_ID));
vo.setHostName(extra.getString(ExtraFieldConst.HOST_NAME));
vo.setHostAddress(extra.getString(ExtraFieldConst.ADDRESS));
vo.setPaths(extra.getString(ExtraFieldConst.PATH).split("\\|"));
vo.setExtra(extra);
return vo;
}).collect(Collectors.toList());
// 返回
DataGrid<HostSftpLogVO> result = new DataGrid<>();
result.setRows(rows);
result.setPage(dataGrid.getPage());
result.setLimit(dataGrid.getLimit());
result.setSize(dataGrid.getSize());
result.setTotal(dataGrid.getTotal());
return result;
}
@Override
public Integer deleteHostSftpLog(List<Long> idList) {
log.info("HostSftpLogService.deleteSftpLog start {}", JSON.toJSONString(idList));
Integer effect = operatorLogApi.deleteOperatorLog(idList);
log.info("HostSftpLogService.deleteSftpLog finish {}", effect);
// 设置日志参数
OperatorLogs.add(OperatorLogs.COUNT, effect);
return effect;
}
/**
* 构建查询对象
*
* @param request request
* @return query
*/
private OperatorLogQueryDTO buildQueryInfo(HostSftpLogQueryRequest request) {
Long hostId = request.getHostId();
String type = request.getType();
// 构建参数
@@ -55,19 +101,7 @@ public class HostSftpLogServiceImpl implements HostSftpLogService {
if (hostId != null) {
query.setExtra("\"hostId\": " + hostId + ",");
}
// 查询
DataGrid<OperatorLogDTO> dataGrid = operatorLogApi.getOperatorLogList(query);
// 返回
DataGrid<HostSftpLogVO> result = new DataGrid<>();
List<HostSftpLogVO> rows = dataGrid.stream()
.map(HostSftpLogConvert.MAPPER::to)
.collect(Collectors.toList());
result.setRows(rows);
result.setPage(dataGrid.getPage());
result.setLimit(dataGrid.getLimit());
result.setSize(dataGrid.getSize());
result.setTotal(dataGrid.getTotal());
return result;
return query;
}
}