-
Notifications
You must be signed in to change notification settings - Fork 162
/
operation.go
544 lines (504 loc) · 12.9 KB
/
operation.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package middleware
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/labulaka521/crocodile/common/log"
"github.com/labulaka521/crocodile/core/config"
"github.com/labulaka521/crocodile/core/model"
"github.com/labulaka521/crocodile/core/utils/define"
"github.com/labulaka521/crocodile/core/utils/resp"
"go.uber.org/zap"
)
const (
task = "task"
hostgroup = "hostgroup"
host = "host"
user = "user"
)
var moduleMap = map[string]string{
task: "任务",
hostgroup: "主机组",
host: "主机",
user: "用户",
}
// Oprtation save all user operate log
func Oprtation() func(c *gin.Context) {
return func(c *gin.Context) {
// operate id
// 操作人id,姓名,用户类型
// Method 创建,修改,删除
// 模块: 用户,主机组,主机,任务,用户
// 操作对象名称
// 修改时间
// 修改字段
// 旧值
// 新值
for _, url := range excludepath {
if strings.Contains(c.Request.RequestURI, url) {
c.Next()
return
}
}
// save operate on request method [POST,PUT,DELETE]
if !(c.Request.Method == http.MethodPost ||
c.Request.Method == http.MethodPut ||
c.Request.Method == http.MethodDelete) {
c.Next()
return
}
var (
module string
oldData interface{}
newData interface{}
name string
id string
exist bool
modulename string
operatetimne int64
)
operatetimne = time.Now().Unix()
if len(strings.Split(c.Request.URL.Path, "/")) > 3 {
module = strings.Split(c.Request.URL.Path, "/")[3]
}
if _, exist = moduleMap[module]; !exist {
c.Next()
return
}
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
log.Error("ioutil.ReadAll failed", zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
c.Request.Body = ioutil.NopCloser(bytes.NewReader(body))
ctx, cancel := context.WithTimeout(context.Background(),
config.CoreConf.Server.DB.MaxQueryTime.Duration)
defer cancel()
uid := c.GetString("uid")
username := c.GetString("username")
// 获取用户的类型
var role define.Role
if v, ok := c.Get("role"); ok {
role = v.(define.Role)
}
columns := make([]define.Column, 0, 50)
// 一些不能使用修改前后两次数据对比的审计
// 复制任务
// /api/v1/task/clone post
if c.Request.Method == http.MethodPost && c.Request.URL.Path == "/api/v1/task/clone" {
idname := define.IDName{}
err = json.Unmarshal(body, &idname)
if err != nil {
log.Error("json.Unmarshal failed", zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
clonetask, err := model.GetTaskByID(ctx, idname.ID)
if err != nil {
log.Error("model.GetTaskByID failed", zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
model.SaveOperateLog(ctx,
uid,
username,
role,
c.Request.Method,
module,
idname.Name,
operatetimne,
fmt.Sprintf("通过任务 %s 克隆新的任务 %s", clonetask.Name, idname.Name), columns)
if err != nil {
log.Error("model.SaveOperateLog failed", zap.Error(err))
}
c.Next()
return
}
// 删除日志
// /api/v1/task/log delete
if c.Request.Method == http.MethodDelete && c.Request.URL.Path == "/api/v1/task/log" {
cleanlog := define.Cleanlog{}
err = json.Unmarshal(body, &cleanlog)
if err != nil {
log.Error("json.Unmarshal failed", zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
var desc string
if cleanlog.PreDay > 0 {
desc = fmt.Sprintf("清除任务%s %d天前的任务日志", cleanlog.Name, cleanlog.PreDay)
} else {
desc = "清除全部日志"
}
model.SaveOperateLog(ctx,
uid,
username,
role,
c.Request.Method,
module,
cleanlog.Name,
operatetimne,
desc, columns)
if err != nil {
log.Error("model.SaveOperateLog failed", zap.Error(err))
}
c.Next()
return
}
// 运行任务
// /api/v1/task/run PUT
if c.Request.Method == http.MethodPut && c.Request.URL.Path == "/api/v1/task/run" {
getid := define.GetID{}
err = json.Unmarshal(body, &getid)
if err != nil {
log.Error("json.Unmarshal failed", zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
task, err := model.GetTaskByID(ctx, getid.ID)
if err != nil {
log.Error("model.GetTaskByID failed", zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
model.SaveOperateLog(ctx,
uid,
username,
role,
c.Request.Method,
module,
task.Name,
operatetimne,
fmt.Sprintf("触发运行任务%s", task.Name), columns)
if err != nil {
log.Error("model.SaveOperateLog failed", zap.Error(err))
}
c.Next()
return
}
// 杀死任务
// /api/v1/task/kill PUT
if c.Request.Method == http.MethodPut && c.Request.URL.Path == "/api/v1/task/kill" {
getid := define.GetID{}
err = json.Unmarshal(body, &getid)
if err != nil {
log.Error("json.Unmarshal failed", zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
task, err := model.GetTaskByID(ctx, getid.ID)
if err != nil {
log.Error("model.GetTaskByID failed", zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
model.SaveOperateLog(ctx,
uid,
username,
role,
c.Request.Method,
module,
task.Name,
operatetimne,
fmt.Sprintf("终止任务%s", task.Name), columns)
if err != nil {
log.Error("model.SaveOperateLog failed", zap.Error(err))
}
c.Next()
return
}
// get old data
switch c.Request.Method {
case http.MethodPost:
getname := define.GetName{}
err := json.Unmarshal(body, &getname)
if err != nil {
log.Error("json.Unmarshal failed", zap.Error(err))
c.Next()
return
}
name = getname.Name
case http.MethodPut, http.MethodDelete:
getid := define.GetID{}
err := json.Unmarshal(body, &getid)
if err != nil {
log.Error("json.Unmarshal failed", zap.Error(err))
c.Next()
return
}
id = getid.ID
switch module {
case user:
userData, err := model.GetUserByID(ctx, id)
if err != nil {
log.Error("model.GetUserByID", zap.String("uid", id), zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
modulename = userData.Name
oldData = *userData
case hostgroup:
hostgroupData, err := model.GetHostGroupByID(ctx, id)
if err != nil {
log.Error("model.GetHostGroupByID", zap.String("id", id), zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
modulename = hostgroupData.Name
oldData = *hostgroupData
case host:
hostData, err := model.GetHostByID(ctx, id)
if err != nil {
log.Error("model.GetHostByID", zap.String("id", id), zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
modulename = hostData.Addr
oldData = *hostData
case task:
taskData, err := model.GetTaskByID(ctx, id)
if err != nil {
log.Error("model.GetHostByID", zap.String("id", id), zap.Error(err))
resp.JSON(c, resp.ErrInternalServer, nil)
return
}
modulename = taskData.Name
oldData = *taskData
default:
log.Debug("can get module name from url", zap.String("url", c.Request.URL.Path))
c.Next()
return
}
default:
c.Next()
return
}
c.Next()
if c.GetInt("statuscode") != 0 {
log.Error("req status code is not 0", zap.Int("statuscode", c.GetInt("statuscode")))
return
}
// get new data
switch c.Request.Method {
case http.MethodPost:
switch module {
case user:
userData, err := model.GetUserByName(ctx, name)
if err != nil {
log.Error("model.GetUserByID", zap.String("name", name), zap.Error(err))
// resp.JSON(c, resp.ErrInternalServer, nil)
return
}
modulename = userData.Name
newData = *userData
case hostgroup:
hostgroupData, err := model.GetHostGroupByName(ctx, name)
if err != nil {
log.Error("model.GetHostGroupByName", zap.String("name", name), zap.Error(err))
// resp.JSON(c, resp.ErrInternalServer, nil)
return
}
modulename = hostgroupData.Name
newData = *hostgroupData
case task:
taskData, err := model.GetTaskByName(ctx, name)
if err != nil {
log.Error("model.GetTaskByName", zap.String("name", name), zap.Error(err))
// resp.JSON(c, resp.ErrInternalServer, nil)
return
}
modulename = taskData.Name
newData = *taskData
default:
log.Debug("can get module name from url", zap.String("url", c.Request.URL.Path))
c.Next()
return
}
case http.MethodPut:
switch module {
case user:
userData, err := model.GetUserByID(ctx, id)
if err != nil {
log.Error("model.GetUserByID", zap.String("uid", id), zap.Error(err))
// resp.JSON(c, resp.ErrInternalServer, nil)
return
}
newData = *userData
case hostgroup:
hostgroupData, err := model.GetHostGroupByID(ctx, id)
if err != nil {
log.Error("model.GetHostGroupByID", zap.String("id", id), zap.Error(err))
// resp.JSON(c, resp.ErrInternalServer, nil)
return
}
newData = *hostgroupData
case host:
hostData, err := model.GetHostByID(ctx, id)
if err != nil {
log.Error("model.GetHostByID", zap.String("id", id), zap.Error(err))
// resp.JSON(c, resp.ErrInternalServer, nil)
return
}
newData = *hostData
case task:
taskData, err := model.GetTaskByID(ctx, id)
if err != nil {
log.Error("model.GetHostByID", zap.String("id", id), zap.Error(err))
// resp.JSON(c, resp.ErrInternalServer, nil)
return
}
newData = *taskData
default:
log.Debug("can get module name from url", zap.String("url", c.Request.URL.Path))
c.Next()
return
}
}
parseColumn(oldData, newData, &columns, "")
if len(columns) == 0 {
return
}
err = model.SaveOperateLog(ctx,
uid,
username,
role,
c.Request.Method,
module,
modulename,
operatetimne,
"",
columns)
if err != nil {
log.Error("model.SaveOperateLog failed", zap.Error(err))
}
}
}
func parseColumn(oldData, newData interface{}, columns *[]define.Column, precm string) {
oldt := reflect.TypeOf(oldData)
newt := reflect.TypeOf(newData)
oldv := reflect.ValueOf(oldData)
newv := reflect.ValueOf(newData)
var total int
// 特殊情况 结构体不相同 任务中TaskData
if oldv.Kind() != reflect.Invalid &&
newv.Kind() != reflect.Invalid &&
oldt.Name() != newt.Name() {
parsestruct(oldData, true, columns, precm)
parsestruct(newData, false, columns, precm)
return
}
// 默认为结构体相同
if oldv.Kind() != reflect.Invalid {
total = oldv.NumField() // 字段数量
} else if newv.Kind() != reflect.Invalid {
total = newv.NumField()
}
for i := 0; i < total; i++ {
var (
comment string
oldvalue interface{}
newvalue interface{}
anonStruct bool
)
if oldv.Kind() != reflect.Invalid {
ft := oldt.Field(i)
if ft.Anonymous {
anonStruct = true
} else {
cm := ft.Tag.Get("comment") // get tag comment
if cm == "" {
continue
}
comment = cm
}
oldvalue = oldv.Field(i).Interface() // get value
}
if newv.Kind() != reflect.Invalid {
ft := newt.Field(i)
if ft.Anonymous {
anonStruct = true
} else {
cm := ft.Tag.Get("comment")
if cm == "" {
continue
}
comment = cm
}
newvalue = newv.Field(i).Interface()
}
// 当字段是匿名结构体
if anonStruct {
parseColumn(oldvalue, newvalue, columns, "")
continue
}
if reflect.DeepEqual(oldvalue, newvalue) { // check is equal
continue
}
// 任务数据是结构体数据类型
if comment == "任务数据" {
parseColumn(oldvalue, newvalue, columns, comment)
continue
}
// 取消密码回显
if comment == "密码" {
oldvalue = "-"
newvalue = "-"
}
if precm != "" {
var name string
if oldv.Kind() != reflect.Invalid {
name = oldt.Name()
} else if newv.Kind() != reflect.Invalid {
name = newt.Name()
}
comment = fmt.Sprintf("%s-%s-%s", precm, name, comment)
}
c := define.Column{
Name: comment,
OldValue: oldvalue,
NewValue: newvalue,
}
*columns = append(*columns, c)
}
}
// parsestruct get struct tag comment and struct's value
func parsestruct(structdata interface{},
isold bool, /*judge value is old or new */
columns *[]define.Column,
precm string) {
t := reflect.TypeOf(structdata)
v := reflect.ValueOf(structdata)
for i := 0; i < v.NumField(); i++ {
var (
oldvalue interface{}
newvalue interface{}
comment string
column define.Column
)
ft := t.Field(i)
cm := ft.Tag.Get("comment")
if cm == "" {
continue
}
comment = fmt.Sprintf("%s-%s-%s", precm, t.Name(), cm)
if isold {
oldvalue = v.Field(i).Interface()
} else {
newvalue = v.Field(i).Interface()
}
column = define.Column{
Name: comment,
OldValue: oldvalue,
NewValue: newvalue,
}
*columns = append(*columns, column)
}
}