Skip to content

Commit f96d96f

Browse files
committed
feat: 审计日志增加scen,scen_code,scen_desc等字段,支撑上层应用使用
1 parent cc45f93 commit f96d96f

File tree

10 files changed

+189
-1
lines changed

10 files changed

+189
-1
lines changed

src/common/definitions.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,13 @@ const (
553553
// BKExtendResourceNameField the audit extend resource name field
554554
BKExtendResourceNameField = "extend_resource_name"
555555

556+
// BKSceneField the audit scene field
557+
BKSceneField = "scene"
558+
// BKSceneDescField the audit scene description field
559+
BKSceneDescField = "scene_desc"
560+
// BKSceneCodeField the audit scene code field
561+
BKSceneCodeField = "scene_code"
562+
556563
// BKLabelField the audit resource name field
557564
BKLabelField = "label"
558565

src/common/http/header/accessor.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,18 @@ func IsInnerReq(header http.Header) bool {
216216
func SetIsInnerReqHeader(header http.Header) {
217217
header.Set(IsInnerReqHeader, "true")
218218
}
219+
220+
// GetScene get audit scene from http header
221+
func GetScene(header http.Header) string {
222+
return header.Get(CCSceneHeader)
223+
}
224+
225+
// GetSceneDesc get audit scene description from http header
226+
func GetSceneDesc(header http.Header) string {
227+
return header.Get(CCSceneDescHeader)
228+
}
229+
230+
// GetSceneCode get audit scene code from http header
231+
func GetSceneCode(header http.Header) string {
232+
return header.Get(CCSceneCodeHeader)
233+
}

src/common/http/header/header.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,13 @@ const (
7575

7676
// IsInnerReqHeader is the http header key that represents if request is an inner request
7777
IsInnerReqHeader = "X-Bkcmdb-Is-Inner-Request"
78+
79+
// CCSceneHeader is the audit scene http header key
80+
CCSceneHeader = "X-CC-Scene"
81+
82+
// CCSceneDescHeader is the audit scene description http header key
83+
CCSceneDescHeader = "X-CC-Scene-Desc"
84+
85+
// CCSceneCodeHeader is the audit scene code http header key
86+
CCSceneCodeHeader = "X-CC-Scene-Code"
7887
)

src/common/http/header/util/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ func NewHeader(header http.Header) http.Header {
9696
httpheader.SetReqFromWeb(newHeader)
9797
}
9898

99+
// Copy audit scene headers
100+
newHeader.Set(httpheader.CCSceneHeader, httpheader.GetScene(header))
101+
newHeader.Set(httpheader.CCSceneDescHeader, httpheader.GetSceneDesc(header))
102+
newHeader.Set(httpheader.CCSceneCodeHeader, httpheader.GetSceneCode(header))
103+
99104
return newHeader
100105
}
101106

src/common/metadata/audit.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ type AuditQueryCondition struct {
9696
FuzzyQuery bool `json:"fuzzy_query"`
9797
// Condition is used for new way to search audit log by user or resource_name
9898
Condition []querybuilder.AtomRule `json:"condition"`
99+
// Scene filters audit logs by operation scene
100+
Scene string `json:"scene"`
101+
// SceneCode filters audit logs by operation scene code
102+
SceneCode string `json:"scene_code"`
99103
}
100104

101105
// Validate is a AuditQueryCondition validator to validate user resource_name condition whether exist at the same time
@@ -172,6 +176,10 @@ type InstAuditCondition struct {
172176
OperationTime OperationTimeCondition `json:"operation_time"`
173177
// ID is an audit record's id
174178
ID []int64 `json:"id"`
179+
// Scene filters audit logs by operation scene
180+
Scene string `json:"scene"`
181+
// SceneCode filters audit logs by operation scene code
182+
SceneCode string `json:"scene_code"`
175183
}
176184

177185
// AuditLog struct for audit log
@@ -209,6 +217,12 @@ type AuditLog struct {
209217
RequestID string `json:"rid,omitempty" bson:"rid,omitempty"`
210218
// todo ExtendResourceName for the temporary solution of ipv6
211219
ExtendResourceName string `json:"extend_resource_name" bson:"extend_resource_name"`
220+
// Scene the operation scene of the audit log
221+
Scene string `json:"scene" bson:"scene"`
222+
// SceneDesc the operation scene description of the audit log
223+
SceneDesc string `json:"scene_desc" bson:"scene_desc"`
224+
// SceneCode the operation scene code of the audit log
225+
SceneCode string `json:"scene_code" bson:"scene_code"`
212226
}
213227

214228
type bsonAuditLog struct {
@@ -227,6 +241,9 @@ type bsonAuditLog struct {
227241
AppCode string `json:"code" bson:"code"`
228242
RequestID string `json:"rid" bson:"rid"`
229243
ExtendResourceName string `json:"extend_resource_name" bson:"extend_resource_name"`
244+
Scene string `json:"scene" bson:"scene"`
245+
SceneDesc string `json:"scene_desc" bson:"scene_desc"`
246+
SceneCode string `json:"scene_code" bson:"scene_code"`
230247
}
231248

232249
type jsonAuditLog struct {
@@ -245,6 +262,9 @@ type jsonAuditLog struct {
245262
AppCode string `json:"code" bson:"code"`
246263
RequestID string `json:"rid" bson:"rid"`
247264
ExtendResourceName string `json:"extend_resource_name" bson:"extend_resource_name"`
265+
Scene string `json:"scene" bson:"scene"`
266+
SceneDesc string `json:"scene_desc" bson:"scene_desc"`
267+
SceneCode string `json:"scene_code" bson:"scene_code"`
248268
}
249269

250270
// DetailFactory TODO
@@ -293,6 +313,9 @@ func (auditLog *AuditLog) UnmarshalJSON(data []byte) error {
293313
auditLog.AppCode = audit.AppCode
294314
auditLog.RequestID = audit.RequestID
295315
auditLog.ExtendResourceName = audit.ExtendResourceName
316+
auditLog.Scene = audit.Scene
317+
auditLog.SceneDesc = audit.SceneDesc
318+
auditLog.SceneCode = audit.SceneCode
296319

297320
if audit.OperationDetail == nil {
298321
return nil
@@ -357,6 +380,9 @@ func (auditLog *AuditLog) UnmarshalBSON(data []byte) error {
357380
auditLog.AppCode = audit.AppCode
358381
auditLog.RequestID = audit.RequestID
359382
auditLog.ExtendResourceName = audit.ExtendResourceName
383+
auditLog.Scene = audit.Scene
384+
auditLog.SceneDesc = audit.SceneDesc
385+
auditLog.SceneCode = audit.SceneCode
360386

361387
if audit.OperationDetail == nil {
362388
return nil
@@ -416,6 +442,9 @@ func (auditLog AuditLog) MarshalBSON() ([]byte, error) {
416442
audit.AppCode = auditLog.AppCode
417443
audit.RequestID = auditLog.RequestID
418444
audit.ExtendResourceName = auditLog.ExtendResourceName
445+
audit.Scene = auditLog.Scene
446+
audit.SceneDesc = auditLog.SceneDesc
447+
audit.SceneCode = auditLog.SceneCode
419448
var err error
420449
switch val := auditLog.OperationDetail.(type) {
421450
default:

src/scene_server/admin_server/imports.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,5 @@ import (
120120
_ "configcenter/src/scene_server/admin_server/upgrader/y3.14.202405141035"
121121
_ "configcenter/src/scene_server/admin_server/upgrader/y3.14.202410100930"
122122
_ "configcenter/src/scene_server/admin_server/upgrader/y3.14.202502101200"
123+
_ "configcenter/src/scene_server/admin_server/upgrader/y3.14.202601121450"
123124
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making
3+
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
4+
* Copyright (C) 2017 Tencent. All rights reserved.
5+
* Licensed under the MIT License (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* We undertake not to change the open source license (MIT license) applicable
14+
* to the current version of the project delivered to anyone in the future.
15+
*/
16+
17+
package y3_14_202601121450
18+
19+
import (
20+
"context"
21+
22+
"configcenter/src/common"
23+
"configcenter/src/common/blog"
24+
"configcenter/src/scene_server/admin_server/upgrader"
25+
"configcenter/src/storage/dal"
26+
"configcenter/src/storage/dal/types"
27+
28+
"go.mongodb.org/mongo-driver/bson"
29+
)
30+
31+
func addAuditLogSceneIndex(ctx context.Context, db dal.RDB, conf *upgrader.Config) error {
32+
idxArr, err := db.Table(common.BKTableNameAuditLog).Indexes(ctx)
33+
if err != nil {
34+
blog.Errorf("get table %s index error. err:%s", common.BKTableNameAuditLog, err.Error())
35+
return err
36+
}
37+
38+
createIdxArr := []types.Index{
39+
{Name: "index_scene", Keys: bson.D{{common.BKSceneField, 1}}, Background: true},
40+
{Name: "index_scene_code", Keys: bson.D{{common.BKSceneCodeField, 1}}, Background: true},
41+
}
42+
for _, idx := range createIdxArr {
43+
exist := false
44+
for _, existIdx := range idxArr {
45+
if existIdx.Name == idx.Name {
46+
exist = true
47+
break
48+
}
49+
}
50+
if exist {
51+
continue
52+
}
53+
if err := db.Table(common.BKTableNameAuditLog).CreateIndex(ctx, idx); err != nil && !db.IsDuplicatedError(err) {
54+
blog.ErrorJSON("create index to BKTableNameAuditLog error, err:%s, current index:%s, all create index:%s", err.Error(), idx, createIdxArr)
55+
return err
56+
}
57+
}
58+
59+
return nil
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making
3+
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
4+
* Copyright (C) 2017 Tencent. All rights reserved.
5+
* Licensed under the MIT License (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* We undertake not to change the open source license (MIT license) applicable
14+
* to the current version of the project delivered to anyone in the future.
15+
*/
16+
17+
package y3_14_202601121450
18+
19+
import (
20+
"context"
21+
22+
"configcenter/src/common/blog"
23+
"configcenter/src/scene_server/admin_server/upgrader"
24+
"configcenter/src/storage/dal"
25+
)
26+
27+
func init() {
28+
upgrader.RegistUpgrader("y3.14.202601121450", upgrade)
29+
}
30+
31+
func upgrade(ctx context.Context, db dal.RDB, conf *upgrader.Config) (err error) {
32+
33+
blog.Infof("start execute y3.14.202601121450")
34+
err = addAuditLogSceneIndex(ctx, db, conf)
35+
if err != nil {
36+
blog.Errorf("upgrade y3.14.202601121450 add audit log scene index failed, error: %v", err)
37+
return err
38+
}
39+
blog.Infof("execute y3.14.202601121450, add audit log scene index success!")
40+
41+
return nil
42+
}

src/scene_server/topo_server/service/auditlog.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (s *Service) SearchAuditList(ctx *rest.Contexts) {
6060
// the front-end table display fields
6161
fields := []string{common.BKFieldID, common.BKUser, common.BKResourceTypeField, common.BKActionField,
6262
common.BKOperationTimeField, common.BKAppIDField, common.BKResourceIDField, common.BKResourceNameField,
63-
common.BKExtendResourceNameField}
63+
common.BKExtendResourceNameField, common.BKSceneField, common.BKSceneDescField, common.BKSceneCodeField}
6464

6565
cond := mapstr.MapStr{}
6666
condition := query.Condition
@@ -171,6 +171,14 @@ func (s *Service) parseAuditCond(kit *rest.Kit, condition metadata.AuditQueryCon
171171
cond[common.BKAppIDField] = condition.BizID
172172
}
173173

174+
if condition.Scene != "" {
175+
cond[common.BKSceneField] = condition.Scene
176+
}
177+
178+
if condition.SceneCode != "" {
179+
cond[common.BKSceneCodeField] = condition.SceneCode
180+
}
181+
174182
if condition.ResourceID != nil {
175183
cond[common.BKResourceIDField] = condition.ResourceID
176184
}
@@ -367,6 +375,14 @@ func buildInstAuditCondition(ctx *rest.Contexts, query metadata.InstAuditConditi
367375
cond[common.BKFieldID] = mapstr.MapStr{common.BKDBIN: query.ID}
368376
}
369377

378+
if query.Scene != "" {
379+
cond[common.BKSceneField] = query.Scene
380+
}
381+
382+
if query.SceneCode != "" {
383+
cond[common.BKSceneCodeField] = query.SceneCode
384+
}
385+
370386
timeCond, err := parseOperationTimeCondition(ctx.Kit, query.OperationTime)
371387
if err != nil {
372388
blog.Errorf("parse operation time condition failed, err: %v, rid: %s", err, ctx.Kit.Rid)

src/source_controller/coreservice/core/auditlog/audit.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func (m *auditManager) CreateAuditLog(kit *rest.Kit, logs ...metadata.AuditLog)
7272
if rid := kit.Rid; len(rid) > 0 {
7373
log.RequestID = kit.Rid
7474
}
75+
76+
log.Scene = httpheader.GetScene(kit.Header)
77+
log.SceneDesc = httpheader.GetSceneDesc(kit.Header)
78+
log.SceneCode = httpheader.GetSceneCode(kit.Header)
7579
log.OperationTime = metadata.Now()
7680
log.ID = int64(ids[index])
7781

0 commit comments

Comments
 (0)