-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
supplementary_audit.go
232 lines (205 loc) · 7.14 KB
/
supplementary_audit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. 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.
*/
package operation
import (
"context"
"configcenter/src/apimachinery"
"configcenter/src/common"
"configcenter/src/common/auditoplog"
"configcenter/src/common/blog"
"configcenter/src/common/condition"
"configcenter/src/common/mapstr"
"configcenter/src/common/metadata"
"configcenter/src/scene_server/topo_server/core/inst"
"configcenter/src/scene_server/topo_server/core/model"
"configcenter/src/scene_server/topo_server/core/types"
)
// WrapperResult the data wrapper
type WrapperResult struct {
datas []inst.Inst
}
// AuditInterface audit log methods
type AuditInterface interface {
CreateSnapshot(instID int64, cond mapstr.MapStr) *WrapperResult
CommitCreateLog(preData, currData *WrapperResult, inst inst.Inst)
CommitDeleteLog(preData, currData *WrapperResult, inst inst.Inst)
CommitUpdateLog(preData, currData *WrapperResult, inst inst.Inst)
}
type auditLog struct {
client apimachinery.ClientSetInterface
inst InstOperationInterface
params types.ContextParams
obj model.Object
}
func (a *auditLog) commitSnapshot(preData, currData *WrapperResult, action auditoplog.AuditOpType) {
var targetData *WrapperResult
isPreItem := false
if nil != currData {
targetData = currData
} else if nil != preData {
isPreItem = true
targetData = preData
} else {
blog.Errorf("[audit] the curr data is empty")
return
}
for _, targetItem := range targetData.datas {
id, err := targetItem.GetInstID()
if nil != err {
blog.Errorf("[audit]failed to get the inst id, error info is %s", err.Error())
return
}
var preDataTmp, currDataTmp mapstr.MapStr
if !isPreItem {
currDataTmp = targetItem.GetValues()
} else {
preDataTmp = targetItem.GetValues()
}
if nil != preData && !isPreItem {
for _, preItem := range preData.datas {
preID, err := preItem.GetInstID()
if nil != err {
blog.Errorf("[audit]failed to get the inst id, error info is %s", err.Error())
continue
}
if id == preID {
preDataTmp = preItem.GetValues()
}
}
}
desc := ""
switch action {
case auditoplog.AuditOpTypeAdd:
desc = "create " + a.obj.GetObjectType()
case auditoplog.AuditOpTypeDel:
desc = "delete " + a.obj.GetObjectType()
case auditoplog.AuditOpTypeModify:
if currDataTmp[common.BKDataStatusField] != preDataTmp[common.BKDataStatusField] {
switch currDataTmp[common.BKDataStatusField] {
case common.DataStatusDisabled, string(common.DataStatusDisabled):
desc = "disabled " + a.obj.GetObjectType()
case common.DataStatusEnable, string(common.DataStatusEnable):
desc = "enable " + a.obj.GetObjectType()
default:
desc = "update " + a.obj.GetObjectType()
}
} else {
desc = "update " + a.obj.GetObjectType()
}
}
headers := []Header{}
attrs, err := a.obj.GetAttributesExceptInnerFields()
if nil != err {
blog.Errorf("[audit]failed to get the object(%s)' attribute, error info is %s", a.obj.GetID(), err.Error())
return
}
for _, attr := range attrs {
headers = append(headers, Header{
PropertyID: attr.GetID(),
PropertyName: attr.GetName(),
})
}
data := common.KvMap{
common.BKContentField: Content{
CurData: currDataTmp,
PreData: preDataTmp,
Headers: headers,
},
common.BKOpDescField: desc,
common.BKOpTypeField: action,
common.BKOpTargetField: a.obj.GetID(),
"inst_id": id,
}
bizID, err := targetItem.GetValues().String(common.BKAppIDField)
if nil != err {
blog.V(3).Infof("[audit] failed to get the bizid from the data(%#v), error info is %s", targetItem.GetValues(), err.Error())
}
if 0 == len(bizID) {
bizID = "0"
}
//fmt.Println("the data pre:", preDataTmp)
//fmt.Println("the data curr:", currDataTmp)
switch a.obj.GetObjectType() {
default:
rsp, err := a.client.AuditController().AddObjectLog(context.Background(), a.params.SupplierAccount, bizID, a.params.User, a.params.Header, data)
if nil != err {
blog.Errorf("[audit] failed to add audit log, error info is %s", err.Error())
return
}
if !rsp.Result {
blog.Errorf("[audit] failed to add audit log, error info is %s", rsp.ErrMsg)
return
}
case common.BKInnerObjIDApp, common.BKINnerObjIDObject:
rsp, err := a.client.AuditController().AddObjectLog(context.Background(), a.params.SupplierAccount, bizID, a.params.User, a.params.Header, data)
if nil != err {
blog.Errorf("[audit] failed to add audit log, error info is %s", err.Error())
return
}
if !rsp.Result {
blog.Errorf("[audit] failed to add audit log, error info is %s", rsp.ErrMsg)
return
}
case common.BKInnerObjIDModule:
rsp, err := a.client.AuditController().AddModuleLog(context.Background(), a.params.SupplierAccount, bizID, a.params.User, a.params.Header, data)
if nil != err {
blog.Errorf("[audit] failed to add audit log, error info is %s", err.Error())
return
}
if !rsp.Result {
blog.Errorf("[audit] failed to add audit log, error info is %s", rsp.ErrMsg)
return
}
case common.BKInnerObjIDSet:
rsp, err := a.client.AuditController().AddSetLog(context.Background(), a.params.SupplierAccount, bizID, a.params.User, a.params.Header, data)
if nil != err {
blog.Errorf("[audit] failed to add audit log, error info is %s", err.Error())
return
}
if !rsp.Result {
blog.Errorf("[audit] failed to add audit log, error info is %s", rsp.ErrMsg)
return
}
}
}
}
func (a *auditLog) CreateSnapshot(instID int64, cond mapstr.MapStr) *WrapperResult {
query := &metadata.QueryInput{}
if instID >= 0 {
innerCond := condition.CreateCondition()
innerCond.Field(a.obj.GetInstIDFieldName()).Eq(instID)
cond.Merge(innerCond.ToMapStr())
}
query.Condition = cond
_, insts, err := a.inst.FindInst(a.params, a.obj, query, false)
if nil != err {
blog.Errorf("[audit] failed to create the snapshot, error info is %s", err.Error())
}
result := &WrapperResult{}
for _, inst := range insts {
result.datas = append(result.datas, inst)
}
return result
}
func (a *auditLog) CommitCreateLog(preData, currData *WrapperResult, inst inst.Inst) {
if nil == currData {
currData = &WrapperResult{}
currData.datas = append(currData.datas, inst)
}
a.commitSnapshot(preData, currData, auditoplog.AuditOpTypeAdd)
}
func (a *auditLog) CommitDeleteLog(preData, currData *WrapperResult, inst inst.Inst) {
a.commitSnapshot(preData, currData, auditoplog.AuditOpTypeDel)
}
func (a *auditLog) CommitUpdateLog(preData, currData *WrapperResult, inst inst.Inst) {
a.commitSnapshot(preData, currData, auditoplog.AuditOpTypeModify)
}