Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/common/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,13 @@ const (
// BKExtendResourceNameField the audit extend resource name field
BKExtendResourceNameField = "extend_resource_name"

// BKSceneField the audit scene field
BKSceneField = "scene"
// BKSceneDescField the audit scene description field
BKSceneDescField = "scene_desc"
// BKSceneCodeField the audit scene code field
BKSceneCodeField = "scene_code"

// BKLabelField the audit resource name field
BKLabelField = "label"

Expand Down
30 changes: 30 additions & 0 deletions src/common/http/header/accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,33 @@ func IsInnerReq(header http.Header) bool {
func SetIsInnerReqHeader(header http.Header) {
header.Set(IsInnerReqHeader, "true")
}

// GetScene get audit scene from http header
func GetScene(header http.Header) string {
return header.Get(CCSceneHeader)
}

// GetSceneDesc get audit scene description from http header
func GetSceneDesc(header http.Header) string {
return header.Get(CCSceneDescHeader)
}

// GetSceneCode get audit scene code from http header
func GetSceneCode(header http.Header) string {
return header.Get(CCSceneCodeHeader)
}

// SetScene set audit scene to http header
func SetScene(header http.Header, value string) {
header.Set(CCSceneHeader, value)
}

// SetSceneDesc set audit scene description to http header
func SetSceneDesc(header http.Header, value string) {
header.Set(CCSceneDescHeader, value)
}

// SetSceneCode set audit scene code to http header
func SetSceneCode(header http.Header, value string) {
header.Set(CCSceneCodeHeader, value)
}
9 changes: 9 additions & 0 deletions src/common/http/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ const (

// IsInnerReqHeader is the http header key that represents if request is an inner request
IsInnerReqHeader = "X-Bkcmdb-Is-Inner-Request"

// CCSceneHeader is the audit scene http header key
CCSceneHeader = "X-CC-Scene"

// CCSceneDescHeader is the audit scene description http header key
CCSceneDescHeader = "X-CC-Scene-Desc"

// CCSceneCodeHeader is the audit scene code http header key
CCSceneCodeHeader = "X-CC-Scene-Code"
)
8 changes: 8 additions & 0 deletions src/common/http/header/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func CCHeader(header http.Header) http.Header {
httpheader.SetReqFromWeb(newHeader)
}
newHeader.Add(common.ReadReferenceKey, header.Get(common.ReadReferenceKey))
httpheader.SetScene(newHeader, httpheader.GetScene(header))
httpheader.SetSceneDesc(newHeader, httpheader.GetSceneDesc(header))
httpheader.SetSceneCode(newHeader, httpheader.GetSceneCode(header))
Comment on lines +44 to +46
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不要放在header里去实现

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原本定的方案是放到header里面,是否放到body中更合适?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前的技术实现方案没有对过,这个场景不适合放在header里
可以修改一下技术方案,评审通过后再编码提交~


return newHeader
}
Expand Down Expand Up @@ -96,6 +99,11 @@ func NewHeader(header http.Header) http.Header {
httpheader.SetReqFromWeb(newHeader)
}

// Copy audit scene headers
httpheader.SetScene(newHeader, httpheader.GetScene(header))
httpheader.SetSceneDesc(newHeader, httpheader.GetSceneDesc(header))
httpheader.SetSceneCode(newHeader, httpheader.GetSceneCode(header))

return newHeader
}

Expand Down
29 changes: 29 additions & 0 deletions src/common/metadata/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ type AuditQueryCondition struct {
FuzzyQuery bool `json:"fuzzy_query"`
// Condition is used for new way to search audit log by user or resource_name
Condition []querybuilder.AtomRule `json:"condition"`
// Scene filters audit logs by operation scene
Scene string `json:"scene"`
// SceneCode filters audit logs by operation scene code
SceneCode string `json:"scene_code"`
}

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

// AuditLog struct for audit log
Expand Down Expand Up @@ -209,6 +217,12 @@ type AuditLog struct {
RequestID string `json:"rid,omitempty" bson:"rid,omitempty"`
// todo ExtendResourceName for the temporary solution of ipv6
ExtendResourceName string `json:"extend_resource_name" bson:"extend_resource_name"`
// Scene the operation scene of the audit log
Scene string `json:"scene" bson:"scene"`
// SceneDesc the operation scene description of the audit log
SceneDesc string `json:"scene_desc" bson:"scene_desc"`
// SceneCode the operation scene code of the audit log
SceneCode string `json:"scene_code" bson:"scene_code"`
}

type bsonAuditLog struct {
Expand All @@ -227,6 +241,9 @@ type bsonAuditLog struct {
AppCode string `json:"code" bson:"code"`
RequestID string `json:"rid" bson:"rid"`
ExtendResourceName string `json:"extend_resource_name" bson:"extend_resource_name"`
Scene string `json:"scene" bson:"scene"`
SceneDesc string `json:"scene_desc" bson:"scene_desc"`
SceneCode string `json:"scene_code" bson:"scene_code"`
}

type jsonAuditLog struct {
Expand All @@ -245,6 +262,9 @@ type jsonAuditLog struct {
AppCode string `json:"code" bson:"code"`
RequestID string `json:"rid" bson:"rid"`
ExtendResourceName string `json:"extend_resource_name" bson:"extend_resource_name"`
Scene string `json:"scene" bson:"scene"`
SceneDesc string `json:"scene_desc" bson:"scene_desc"`
SceneCode string `json:"scene_code" bson:"scene_code"`
}

// DetailFactory TODO
Expand Down Expand Up @@ -293,6 +313,9 @@ func (auditLog *AuditLog) UnmarshalJSON(data []byte) error {
auditLog.AppCode = audit.AppCode
auditLog.RequestID = audit.RequestID
auditLog.ExtendResourceName = audit.ExtendResourceName
auditLog.Scene = audit.Scene
auditLog.SceneDesc = audit.SceneDesc
auditLog.SceneCode = audit.SceneCode

if audit.OperationDetail == nil {
return nil
Expand Down Expand Up @@ -357,6 +380,9 @@ func (auditLog *AuditLog) UnmarshalBSON(data []byte) error {
auditLog.AppCode = audit.AppCode
auditLog.RequestID = audit.RequestID
auditLog.ExtendResourceName = audit.ExtendResourceName
auditLog.Scene = audit.Scene
auditLog.SceneDesc = audit.SceneDesc
auditLog.SceneCode = audit.SceneCode

if audit.OperationDetail == nil {
return nil
Expand Down Expand Up @@ -416,6 +442,9 @@ func (auditLog AuditLog) MarshalBSON() ([]byte, error) {
audit.AppCode = auditLog.AppCode
audit.RequestID = auditLog.RequestID
audit.ExtendResourceName = auditLog.ExtendResourceName
audit.Scene = auditLog.Scene
audit.SceneDesc = auditLog.SceneDesc
audit.SceneCode = auditLog.SceneCode
var err error
switch val := auditLog.OperationDetail.(type) {
default:
Expand Down
1 change: 1 addition & 0 deletions src/scene_server/admin_server/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,5 @@ import (
_ "configcenter/src/scene_server/admin_server/upgrader/y3.14.202405141035"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.14.202410100930"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.14.202502101200"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.14.202601121450"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Tencent is pleased to support the open source community by making
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
* Copyright (C) 2017 Tencent. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package y3_14_202601121450

import (
"context"

"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/scene_server/admin_server/upgrader"
"configcenter/src/storage/dal"
"configcenter/src/storage/dal/types"

"go.mongodb.org/mongo-driver/bson"
)

func addAuditLogSceneIndex(ctx context.Context, db dal.RDB, conf *upgrader.Config) error {
idxArr, err := db.Table(common.BKTableNameAuditLog).Indexes(ctx)
if err != nil {
blog.Errorf("get table %s index error. err:%s", common.BKTableNameAuditLog, err.Error())
return err
}

createIdxArr := []types.Index{
{Name: "index_scene", Keys: bson.D{{common.BKSceneField, 1}}, Background: true},
{Name: "index_scene_code", Keys: bson.D{{common.BKSceneCodeField, 1}}, Background: true},
}
for _, idx := range createIdxArr {
exist := false
for _, existIdx := range idxArr {
if existIdx.Name == idx.Name {
exist = true
break
}
}
if exist {
continue
}
if err := db.Table(common.BKTableNameAuditLog).CreateIndex(ctx, idx); err != nil && !db.IsDuplicatedError(err) {
blog.ErrorJSON("create index to BKTableNameAuditLog error, err:%s, current index:%s, all create index:%s", err.Error(), idx, createIdxArr)
return err
}
}

return nil
}
42 changes: 42 additions & 0 deletions src/scene_server/admin_server/upgrader/y3.14.202601121450/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Tencent is pleased to support the open source community by making
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
* Copyright (C) 2017 Tencent. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package y3_14_202601121450

import (
"context"

"configcenter/src/common/blog"
"configcenter/src/scene_server/admin_server/upgrader"
"configcenter/src/storage/dal"
)

func init() {
upgrader.RegistUpgrader("y3.14.202601121450", upgrade)
}

func upgrade(ctx context.Context, db dal.RDB, conf *upgrader.Config) (err error) {

blog.Infof("start execute y3.14.202601121450")
err = addAuditLogSceneIndex(ctx, db, conf)
if err != nil {
blog.Errorf("upgrade y3.14.202601121450 add audit log scene index failed, error: %v", err)
return err
}
blog.Infof("execute y3.14.202601121450, add audit log scene index success!")

return nil
}
18 changes: 17 additions & 1 deletion src/scene_server/topo_server/service/auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *Service) SearchAuditList(ctx *rest.Contexts) {
// the front-end table display fields
fields := []string{common.BKFieldID, common.BKUser, common.BKResourceTypeField, common.BKActionField,
common.BKOperationTimeField, common.BKAppIDField, common.BKResourceIDField, common.BKResourceNameField,
common.BKExtendResourceNameField}
common.BKExtendResourceNameField, common.BKSceneField, common.BKSceneDescField, common.BKSceneCodeField}

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

if condition.Scene != "" {
cond[common.BKSceneField] = condition.Scene
}

if condition.SceneCode != "" {
cond[common.BKSceneCodeField] = condition.SceneCode
}

if condition.ResourceID != nil {
cond[common.BKResourceIDField] = condition.ResourceID
}
Expand Down Expand Up @@ -367,6 +375,14 @@ func buildInstAuditCondition(ctx *rest.Contexts, query metadata.InstAuditConditi
cond[common.BKFieldID] = mapstr.MapStr{common.BKDBIN: query.ID}
}

if query.Scene != "" {
cond[common.BKSceneField] = query.Scene
}

if query.SceneCode != "" {
cond[common.BKSceneCodeField] = query.SceneCode
}

timeCond, err := parseOperationTimeCondition(ctx.Kit, query.OperationTime)
if err != nil {
blog.Errorf("parse operation time condition failed, err: %v, rid: %s", err, ctx.Kit.Rid)
Expand Down
4 changes: 4 additions & 0 deletions src/source_controller/coreservice/core/auditlog/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (m *auditManager) CreateAuditLog(kit *rest.Kit, logs ...metadata.AuditLog)
if rid := kit.Rid; len(rid) > 0 {
log.RequestID = kit.Rid
}

log.Scene = httpheader.GetScene(kit.Header)
log.SceneDesc = httpheader.GetSceneDesc(kit.Header)
log.SceneCode = httpheader.GetSceneCode(kit.Header)
log.OperationTime = metadata.Now()
log.ID = int64(ids[index])

Expand Down