From 12783981c376678cc77824f0da9eb973fc87f967 Mon Sep 17 00:00:00 2001 From: wangyu096 Date: Thu, 4 Jan 2024 10:26:14 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E5=AE=A1=E8=AE=A1=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=94=AF=E5=87=BA=E8=BE=93=E5=87=BA=20scope=5Ftype/scope=5Fid?= =?UTF-8?q?=20#2685?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/build.gradle | 2 +- .../AddResourceScopeAuditPostFilter.java | 54 +++++++++++++++++++ .../config/JobAuditAutoConfiguration.java | 6 +++ .../bk/job/common/context/JobContext.java | 2 +- .../common/context/JobContextThreadLocal.java | 2 +- .../bk/job/common/util/JobContextUtil.java | 2 +- 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/backend/commons/common-audit/src/main/java/com/tencent/bk/job/common/audit/AddResourceScopeAuditPostFilter.java diff --git a/src/backend/build.gradle b/src/backend/build.gradle index a981b17167..7bff036985 100644 --- a/src/backend/build.gradle +++ b/src/backend/build.gradle @@ -144,7 +144,7 @@ ext { set('bkjobVersion', "1.0.0") println "bkjobVersion:" + bkjobVersion } - set('bkAuditJavaSdkVersion', "1.0.7-SNAPSHOT") + set('bkAuditJavaSdkVersion', "1.0.8") set('mockitoVersion', "4.0.0") set('embeddedRedisVersion', "0.6") } diff --git a/src/backend/commons/common-audit/src/main/java/com/tencent/bk/job/common/audit/AddResourceScopeAuditPostFilter.java b/src/backend/commons/common-audit/src/main/java/com/tencent/bk/job/common/audit/AddResourceScopeAuditPostFilter.java new file mode 100644 index 0000000000..989ee7f2e6 --- /dev/null +++ b/src/backend/commons/common-audit/src/main/java/com/tencent/bk/job/common/audit/AddResourceScopeAuditPostFilter.java @@ -0,0 +1,54 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.audit; + +import com.tencent.bk.audit.filter.AuditPostFilter; +import com.tencent.bk.audit.model.AuditEvent; +import com.tencent.bk.job.common.model.dto.AppResourceScope; +import com.tencent.bk.job.common.util.JobContextUtil; +import lombok.extern.slf4j.Slf4j; + +/** + * 审计事件增加 scopeType, scopeId + */ +@Slf4j +public class AddResourceScopeAuditPostFilter implements AuditPostFilter { + + @Override + public AuditEvent map(AuditEvent auditEvent) { + if (auditEvent == null) { + return null; + } + AppResourceScope appResourceScope = JobContextUtil.getAppResourceScope(); + if (appResourceScope != null) { + if (log.isDebugEnabled()) { + log.debug("Add resource scope for audit event, resourceScope: {}", appResourceScope); + } + auditEvent.setScopeType(appResourceScope.getType().getValue()); + auditEvent.setScopeId(appResourceScope.getId()); + } + return auditEvent; + } +} diff --git a/src/backend/commons/common-audit/src/main/java/com/tencent/bk/job/common/audit/config/JobAuditAutoConfiguration.java b/src/backend/commons/common-audit/src/main/java/com/tencent/bk/job/common/audit/config/JobAuditAutoConfiguration.java index 0836edbb31..6b08188fea 100644 --- a/src/backend/commons/common-audit/src/main/java/com/tencent/bk/job/common/audit/config/JobAuditAutoConfiguration.java +++ b/src/backend/commons/common-audit/src/main/java/com/tencent/bk/job/common/audit/config/JobAuditAutoConfiguration.java @@ -27,6 +27,7 @@ import com.tencent.bk.audit.AuditRequestProvider; import com.tencent.bk.audit.config.AuditAutoConfiguration; import com.tencent.bk.audit.config.AuditProperties; +import com.tencent.bk.job.common.audit.AddResourceScopeAuditPostFilter; import com.tencent.bk.job.common.audit.JobAuditExceptionResolver; import com.tencent.bk.job.common.audit.JobAuditRequestProvider; import lombok.extern.slf4j.Slf4j; @@ -56,4 +57,9 @@ public JobAuditExceptionResolver auditExceptionResolver() { log.info("Init JobAuditExceptionResolver"); return new JobAuditExceptionResolver(); } + + @Bean + public AddResourceScopeAuditPostFilter addResourceScopeAuditPostFilter() { + return new AddResourceScopeAuditPostFilter(); + } } diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/context/JobContext.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/context/JobContext.java index 0050cc9cfb..4229b3918b 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/context/JobContext.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/context/JobContext.java @@ -37,7 +37,7 @@ import java.util.Map; /** - * @since 6/11/2019 10:26 + * Job http 请求上下文 */ @Data public class JobContext { diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/context/JobContextThreadLocal.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/context/JobContextThreadLocal.java index 81e1851377..6f2667d765 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/context/JobContextThreadLocal.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/context/JobContextThreadLocal.java @@ -25,7 +25,7 @@ package com.tencent.bk.job.common.context; /** - * @since 6/11/2019 10:26 + * Job http 请求上下文托管(ThreadLocal方式) */ public class JobContextThreadLocal { diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/JobContextUtil.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/JobContextUtil.java index 9ff5bf8b23..c168f3adce 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/JobContextUtil.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/JobContextUtil.java @@ -42,7 +42,7 @@ import java.util.Map; /** - * @since 6/11/2019 10:27 + * Job http 请求上下文工具类 */ @Slf4j public class JobContextUtil {