-
Notifications
You must be signed in to change notification settings - Fork 416
/
task.go
379 lines (340 loc) · 12.6 KB
/
task.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
package baseline
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/bytedance/Elkeid/server/manager/biz/common"
"github.com/bytedance/Elkeid/server/manager/infra"
"github.com/bytedance/Elkeid/server/manager/infra/def"
"github.com/bytedance/Elkeid/server/manager/infra/ylog"
"github.com/bytedance/Elkeid/server/manager/internal/asset_center"
"github.com/bytedance/Elkeid/server/manager/internal/atask"
"github.com/bytedance/Elkeid/server/manager/internal/dbtask"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// 基线任务下发
type taskResp struct {
ID string `json:"id"`
Code int `json:"code"`
Msg string `json:"msg"`
}
// 心跳
type HB struct {
AgentId string `json:"agent_id" bson:"agent_id"`
Plugin []map[string]interface{} `json:"plugins" bson:"plugins"`
}
// 判断插件是否在线
func isPluginOnline(pluginName string, list []map[string]interface{}) bool {
for _, v := range list {
if name, ok := v["name"].(string); ok && name == pluginName {
return true
}
}
return false
}
// 下发基线任务
func SendBaselineTask(agentIdList []string, user string, taskMsg def.AgentTaskMsg) (taskResList []taskResp, err error) {
c := context.Background()
runList := make([]string, 0, len(agentIdList))
hbCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.AgentHeartBeatCollection)
searchFilter := bson.M{}
searchFilter["last_heartbeat_time"] = bson.M{"$gte": time.Now().Unix() - asset_center.DEFAULT_OFFLINE_DURATION}
if len(agentIdList) != 0 {
searchFilter["agent_id"] = bson.M{"$in": agentIdList}
} else {
return
}
findOpt := options.FindOptions{}
findOpt.Projection = bson.M{"agent_id": 1, "plugins": 1}
cursor, err := hbCol.Find(c, searchFilter, &findOpt)
for cursor.Next(context.Background()) {
var hb HB
err := cursor.Decode(&hb)
if err != nil {
continue
}
taskRes := taskResp{
ID: hb.AgentId,
Code: 0,
Msg: "",
}
// 判断基线插件是否存在
if !isPluginOnline(taskMsg.Name, hb.Plugin) {
taskRes.Code = -1
taskRes.Msg = fmt.Sprintf("The dependent plugin %s is not online.", taskMsg.Name)
}
taskResList = append(taskResList, taskRes)
runList = append(runList, hb.AgentId)
}
// 下发任务
if len(runList) == 0 {
return
}
taskParam := atask.AgentTask{
IDList: runList,
TaskName: taskMsg.Name,
TaskUser: user,
Data: def.ConfigRequest{
Task: taskMsg,
},
}
_, _, err = atask.CreateTaskAndRun(&taskParam, atask.TypeAgentTask, 5)
if err != nil {
ylog.Errorf("SendBaselineTask", err.Error())
return
}
time.Sleep(5 * time.Second)
return
}
// 开始基线检查
type CheckRequest struct {
GroupId int `json:"group_id"`
BaselineId int `json:"baseline_id"`
HostList []string `json:"host_list"`
CheckList []int `json:"check_list"`
User string `json:"user"`
}
func StartCheck(checkRequest CheckRequest) (err error) {
c := context.Background()
startTime := time.Now().UnixNano()
type AgentStruct struct {
AgentId string `json:"agent_id" bson:"agent_id"`
}
// 初始化一个弱口令的agentBaseline 数据
weakPassAgentBaseline := AgentBaselineInfo{
BaselineId: WeakPassBaseline,
CheckId: 1,
BaselineVersion: "1.0",
CheckLevel: BaselineCheckHigh,
Type: "WeakPassword",
CheckName: "System login weak password detection",
Description: "Check if the system login is a weak password.",
Solution: "Change the password used for system login, it is recommended to use uppercase and lowercase + special character passwords.",
TypeCn: "弱口令",
CheckNameCn: "系统登录弱口令检测",
DescriptionCn: "检查系统登录是否为弱口令。",
SolutionCn: "更改系统登录所使用的的口令,建议使用大小写+特殊字符的密码",
CreateTime: 0,
UpdateTime: time.Now().Unix(),
Status: "passed",
}
agentBaseCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.AgentBaselineColl)
baselineCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.BaseLineInfoColl)
checkInfoCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.BaselineCheckInfoColl)
writeOption := &options.BulkWriteOptions{}
writeOption.SetOrdered(false)
// 获取待检查基线列表
groupCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.BaselineGroupInfo)
baselineIdList := make([]int, 0)
if checkRequest.BaselineId != 0 {
baselineIdList = append(baselineIdList, checkRequest.BaselineId)
} else if checkRequest.GroupId != 0 {
var groupInfo GroupInfo
err := groupCol.FindOne(c, bson.M{"group_id": checkRequest.GroupId}).Decode(&groupInfo)
if err != nil {
ylog.Infof("Find error", err.Error())
}
for _, baselineInfo := range groupInfo.BaselineList {
baselineIdList = append(baselineIdList, baselineInfo.BaselineId)
}
} else {
return
}
// 依次下发基线任务
taskStatusWrite := make([]mongo.WriteModel, 0, len(checkRequest.HostList)*len(baselineIdList))
checkTaskWrite := make([]mongo.WriteModel, 0, len(checkRequest.HostList)*len(baselineIdList)*20)
baselineCheckList := make([]int, 0)
for _, baselineId := range baselineIdList {
// 检查项筛选
var baselineInfo BaselineInfo
err := baselineCol.FindOne(c, bson.M{"baseline_id": baselineId}).Decode(&baselineInfo)
if err != nil {
ylog.Infof("Find error", err.Error())
}
if len(checkRequest.CheckList) == 0 {
baselineCheckList = baselineInfo.CheckIdList
} else {
baselineCheckList = checkRequest.CheckList
}
// 获取符合条件的agent列表
agentList := getBaselineAgentList(checkRequest.HostList, baselineInfo)
// 基线任务下发
taskResList := make([]taskResp, 0)
if baselineId == WeakPassBaseline {
// 弱口令基线任务下发
taskMsg := def.AgentTaskMsg{
Name: "collector",
DataType: WeakPassDataType,
}
taskResList, err = SendBaselineTask(agentList, checkRequest.User, taskMsg)
// 将弱口令数据置为false
agentIdList := make([]string, 0)
for _, taskRes := range taskResList {
agentIdList = append(agentIdList, taskRes.ID)
}
weakPassWrite := make([]mongo.WriteModel, len(agentIdList))
for _, agentId := range agentIdList {
agentInfo := dbtask.AgentInfoSearch(agentId)
weakPassAgentBaseline.AgentId = agentId
weakPassAgentBaseline.Hostname = agentInfo.Hostname
weakPassAgentBaseline.Tags = agentInfo.Tags
weakPassAgentBaseline.IntranetIpv4 = agentInfo.IntranetIpv4
weakPassAgentBaseline.ExtranetIpv4 = agentInfo.ExtranetIpv4
model := mongo.NewUpdateOneModel().
SetFilter(bson.M{"agent_id": agentId, "baseline_id": WeakPassBaseline}).
SetUpdate(bson.M{"$set": weakPassAgentBaseline}).SetUpsert(true)
weakPassWrite = append(checkTaskWrite, model)
}
_, err := agentBaseCol.BulkWrite(c, weakPassWrite, writeOption)
if err != nil {
ylog.Errorf("BulkWrite error", err.Error())
}
} else {
taskData, _ := json.Marshal(struct {
BaselineID int `json:"baseline_id"`
BaseLineVersion string `json:"baseline_version"`
CheckIdList []int `json:"check_id_list"`
}{
BaselineID: baselineId,
BaseLineVersion: DefaultBaseLineVersion,
CheckIdList: baselineCheckList,
})
taskMsg := def.AgentTaskMsg{
Name: "baseline",
Data: string(taskData),
DataType: BaselineDataType,
}
taskResList, err = SendBaselineTask(agentList, checkRequest.User, taskMsg)
}
// 更新任务状态
for _, taskRes := range taskResList {
agentId := taskRes.ID
var taskStatusUpdate bson.M
var checkStatusUpdate bson.M
if taskRes.Code != 0 {
taskStatusUpdate = bson.M{"status": "error", "last_check_time": time.Now().Unix(), "msg": taskRes.Msg}
checkStatusUpdate = bson.M{"task_status": "error", "update_time": time.Now().Unix(), "err_reason": taskRes.Msg}
} else {
taskStatusUpdate = bson.M{"status": "running", "last_check_time": time.Now().Unix(), "msg": ""}
checkStatusUpdate = bson.M{"task_status": "running", "update_time": time.Now().Unix()}
}
if baselineId == WeakPassBaseline {
taskStatusUpdate["high_risk_num"] = 0
taskStatusUpdate["pass_num"] = 1
agentInfo := dbtask.AgentInfoSearch(agentId)
weakPassAgentBaseline.AgentId = agentId
taskStatusUpdate["hostname"] = agentInfo.Hostname
taskStatusUpdate["tags"] = agentInfo.Tags
taskStatusUpdate["intranet_ipv4"] = agentInfo.IntranetIpv4
taskStatusUpdate["extranet_ipv4"] = agentInfo.ExtranetIpv4
}
// 更新基线任务状态
model := mongo.NewUpdateOneModel().
SetFilter(bson.M{"agent_id": agentId, "baseline_id": baselineId}).
SetUpdate(bson.M{"$set": taskStatusUpdate}).SetUpsert(true)
taskStatusWrite = append(taskStatusWrite, model)
// 更新检查项任务状态
for _, checkId := range baselineCheckList {
model := mongo.NewUpdateOneModel().
SetFilter(bson.M{"agent_id": agentId, "baseline_id": baselineId, "check_id": checkId}).
SetUpdate(bson.M{"$set": checkStatusUpdate}).SetUpsert(true)
checkTaskWrite = append(checkTaskWrite, model)
}
}
// 更新基线检测状态
if len(checkRequest.HostList) == 0 {
baselineStatusCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.BaselineStatus)
_, err := baselineStatusCol.UpdateOne(c, bson.M{"baseline_id": baselineId},
bson.M{"$set": bson.M{"status": "running", "last_check_time": time.Now().Unix()}})
if err != nil {
ylog.Errorf("Update error", err.Error())
}
}
// 更新检测状态
if len(checkRequest.HostList) == 0 && checkRequest.BaselineId == 0 && checkRequest.GroupId != 0 {
// 整个策略组检查
groupStatusCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.BaselineGroupStatus)
_, err := groupStatusCol.UpdateOne(c, bson.M{"group_id": checkRequest.GroupId},
bson.M{"$set": bson.M{"status": "running", "last_check_time": time.Now().Unix()}})
if err != nil {
ylog.Errorf("Update error", err.Error())
}
}
// 更新基线主机任务状态
taskStatusCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.BaselineTaskStatus)
if len(taskStatusWrite) > 0 {
_, err = taskStatusCol.BulkWrite(c, taskStatusWrite, writeOption)
if err != nil {
ylog.Errorf("StartCheckBaseline", err.Error())
}
}
// 更新检查项任务状态
if len(checkTaskWrite) > 0 {
_, err = agentBaseCol.BulkWrite(c, checkTaskWrite, writeOption)
if err != nil {
ylog.Errorf("StartCheckBaseline", err.Error())
}
}
// 更新检查项表任务状态
if len(baselineCheckList) > 0 {
_, err = checkInfoCol.UpdateMany(c,
bson.M{"baseline_id": baselineId, "check_id": bson.M{"$in": baselineCheckList}},
bson.M{"$set": bson.M{"status": "running", "update_time": time.Now().Unix()}})
if err != nil {
ylog.Errorf("StartCheckBaseline", err.Error())
}
}
}
// 开启协程跟踪任务结束状态
go judgeTaskTimeout("crontab")
// 让mongo主从数据库同步,确保任务运行状态写入mongo从库
endTime := time.Now().UnixNano()
if (endTime - startTime) < int64(2*time.Second) {
time.Sleep(1 * time.Second)
}
return
}
// 获取符合该baseline下发条件的agent列表
func getBaselineAgentList(beforeAgentList []string, baselineInfo BaselineInfo) (agentList []string) {
c := context.Background()
hbCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.AgentHeartBeatCollection)
appCol := infra.MongoClient.Database(infra.MongoDatabase).Collection(infra.FingerprintAppCollection)
type AgentStruct struct {
AgentId string `json:"agent_id" bson:"agent_id"`
}
if baselineInfo.BaselineId != 6000 {
// 获取符合条件的agent列表
searchFilter := make(map[string]interface{})
if len(beforeAgentList) != 0 {
searchFilter["agent_id"] = common.MongoInside{Inside: beforeAgentList}
}
searchFilter["last_heartbeat_time"] = bson.M{"$gte": time.Now().Unix() - asset_center.DEFAULT_OFFLINE_DURATION}
searchFilter["platform"] = common.MongoInside{Inside: baselineInfo.SystemList}
findOption := options.Find().SetProjection(bson.M{"agent_id": 1})
cursor, _ := hbCol.Find(c, searchFilter, findOption)
var agentStructList []AgentStruct
err := cursor.All(c, &agentStructList)
if err != nil {
return
}
for _, agentInfo := range agentStructList {
agentList = append(agentList, agentInfo.AgentId)
}
return
} else {
// 容器基线下发
cur, _ := appCol.Find(c, bson.M{"name": "kubelet"})
for cur.Next(c) {
var agentStruct AgentStruct
err := cur.Decode(&agentStruct)
if err != nil {
return nil
}
agentList = append(agentList, agentStruct.AgentId)
}
return
}
}