-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathcloud_node.go
649 lines (566 loc) · 22.6 KB
/
cloud_node.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
package model
import (
"context"
"encoding/json"
"fmt"
"time"
"k8s.io/utils/strings/slices"
"github.com/deepfence/ThreatMapper/deepfence_server/pkg/constants"
ctl "github.com/deepfence/ThreatMapper/deepfence_utils/controls"
"github.com/deepfence/ThreatMapper/deepfence_utils/directory"
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
"github.com/deepfence/ThreatMapper/deepfence_utils/telemetry"
"github.com/deepfence/ThreatMapper/deepfence_utils/utils"
"github.com/deepfence/ThreatMapper/deepfence_utils/utils/ingesters"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
const (
PostureProviderAWS = "aws"
PostureProviderAWSOrg = "aws_org"
PostureProviderGCP = "gcp"
PostureProviderGCPOrg = "gcp_org"
PostureProviderAzure = "azure"
PostureProviderAzureOrg = "azure_org"
PostureProviderLinux = "linux"
PostureProviderKubernetes = "kubernetes"
)
var (
PostureProviderOrgMap = map[string]string{
PostureProviderAWS: PostureProviderAWSOrg,
PostureProviderGCP: PostureProviderGCPOrg,
PostureProviderAzure: PostureProviderAzureOrg,
}
)
var SupportedPostureProviders = []string{PostureProviderAWS, PostureProviderGCP,
PostureProviderAzure, PostureProviderLinux, PostureProviderKubernetes}
type CloudNodeMonitoredAccount struct {
NodeID string `json:"node_id" validate:"required" required:"true"`
AccountName string `json:"account_name"`
AccountID string `json:"account_id" validate:"required" required:"true"`
}
type CloudNodeAccountRegisterReq struct {
NodeID string `json:"node_id" validate:"required" required:"true"`
AccountName string `json:"account_name"`
HostNodeID string `json:"host_node_id" validate:"required" required:"true"`
AccountID string `json:"account_id" validate:"required" required:"true"`
CloudProvider string `json:"cloud_provider" validate:"required,oneof=aws gcp azure" enum:"aws,gcp,azure" required:"true"`
IsOrganizationDeployment bool `json:"is_organization_deployment"`
MonitoredAccounts []CloudNodeMonitoredAccount `json:"monitored_accounts"`
OrganizationAccountID string `json:"organization_account_id"`
Version string `json:"version" validate:"required" required:"true"`
PersistentVolumeSupported bool `json:"persistent_volume_supported"`
InstallationID string `json:"installation_id" validate:"required" required:"true"`
InitialRequest bool `json:"initial_request"`
}
type CloudNodeAccountsListReq struct {
CloudProvider string `json:"cloud_provider" enum:"aws,gcp,azure,linux,kubernetes,aws_org,gcp_org,azure_org" required:"true"`
Window FetchWindow `json:"window" required:"true"`
}
type CloudNodeProvidersListReq struct{}
type CloudNodeProvidersListResp struct {
Providers []PostureProvider `json:"providers" required:"true"`
}
type CloudNodeAccountsListResp struct {
CloudNodeAccountInfo []CloudNodeAccountInfo `json:"cloud_node_accounts_info" required:"true"`
Total int `json:"total" required:"true"`
}
type CloudNodeAccountInfo struct {
NodeID string `json:"node_id"`
NodeName string `json:"node_name"`
AccountName string `json:"account_name"`
CloudProvider string `json:"cloud_provider" enum:"aws,gcp,azure,aws_org,gcp_org,azure_org"`
CompliancePercentage float64 `json:"compliance_percentage"`
Active bool `json:"active"`
LastScanID string `json:"last_scan_id"`
LastScanStatus string `json:"last_scan_status"`
RefreshMessage string `json:"refresh_message"`
RefreshMetadata string `json:"refresh_metadata"`
RefreshStatus string `json:"refresh_status"`
RefreshStatusMap map[string]int64 `json:"refresh_status_map"`
ScanStatusMap map[string]int64 `json:"scan_status_map"`
Version string `json:"version"`
HostNodeID string `json:"host_node_id"`
}
func (v CloudNodeAccountInfo) NodeType() string {
switch v.CloudProvider {
case PostureProviderKubernetes:
return utils.NodeTypeKubernetesCluster
case PostureProviderLinux:
return utils.NodeTypeHost
}
return utils.NodeTypeCloudNode
}
func (CloudNodeAccountInfo) ExtendedField() string {
return ""
}
func (v CloudNodeAccountInfo) id() string {
return v.NodeID
}
func (v CloudNodeAccountInfo) ScanType() utils.Neo4jScanType {
switch v.CloudProvider {
case PostureProviderAWS, PostureProviderGCP, PostureProviderAzure, PostureProviderAWSOrg, PostureProviderGCPOrg, PostureProviderAzureOrg:
return utils.NEO4JCloudComplianceScan
case PostureProviderKubernetes, PostureProviderLinux:
return utils.NEO4JComplianceScan
default:
return utils.NEO4JCloudComplianceScan
}
}
func (v CloudNodeAccountInfo) LatestScanIDField() string {
return ingesters.LatestScanIDField[v.ScanType()]
}
func (v CloudNodeAccountInfo) ScanResultType() string {
switch v.CloudProvider {
case PostureProviderAWS, PostureProviderGCP, PostureProviderAzure, PostureProviderAWSOrg, PostureProviderGCPOrg, PostureProviderAzureOrg:
return "CloudCompliance"
case PostureProviderKubernetes, PostureProviderLinux:
return "Compliance"
default:
return "CloudCompliance"
}
}
func (v CloudNodeAccountInfo) GetPassStatus() []string {
switch v.CloudProvider {
case PostureProviderAWS, PostureProviderGCP, PostureProviderAzure, PostureProviderAWSOrg, PostureProviderGCPOrg, PostureProviderAzureOrg, PostureProviderKubernetes:
return []string{"ok", "info", "skip"}
case PostureProviderLinux:
return []string{"warn", "pass"}
default:
return []string{"skip", "ok", "info", "pass", "warn"}
}
}
func (v CloudNodeAccountInfo) GetCategory() string {
return v.CloudProvider
}
func (CloudNodeAccountInfo) GetJSONCategory() string {
return "cloud_provider"
}
type CloudComplianceScanDetails struct {
ScanID string `json:"scan_id"`
ScanTypes []string `json:"scan_types"`
AccountID string `json:"account_id"`
Benchmarks []ctl.CloudComplianceScanBenchmark `json:"benchmarks"`
StopRequested bool `json:"stop_requested"`
}
type PendingCloudComplianceScan struct {
ScanID string `json:"scan_id"`
ScanType string `json:"scan_type"`
Controls []string `json:"controls"`
AccountID string `json:"account_id"`
}
type CloudNodeControlReq struct {
NodeID string `json:"node_id"`
CloudProvider string `json:"cloud_provider" required:"true" enum:"aws,gcp,azure,linux,kubernetes"`
ComplianceType string `json:"compliance_type" enum:"hipaa,gdpr,pci,nist,cis,soc_2,nsa-cisa,aws_foundational_security" required:"true"`
}
type CloudNodeEnableDisableReq struct {
NodeID string `json:"node_id"`
ControlsIDs []string `json:"control_ids"`
}
type CloudNodeControlResp struct {
Controls []CloudNodeComplianceControl `json:"controls"`
}
type CloudNodeComplianceControl struct {
NodeID string `json:"node_id"`
Title string `json:"title"`
Description string `json:"description"`
Service string `json:"service"`
CategoryHierarchy []string `json:"category_hierarchy"`
CategoryHierarchyShort string `json:"category_hierarchy_short"`
ControlID string `json:"control_id"`
Enabled bool `json:"enabled"`
ComplianceType string `json:"compliance_type"`
ProblemTitle string `json:"problem_title"`
}
type PostureProvider struct {
Name string `json:"name"`
NodeCount int64 `json:"node_count"`
NodeCountInactive int64 `json:"node_count_inactive"`
NodeLabel string `json:"node_label"`
ScanCount int64 `json:"scan_count"`
CompliancePercentage float64 `json:"compliance_percentage"`
ResourceCount int64 `json:"resource_count"`
}
func UpsertCloudAccount(ctx context.Context, nodeDetails map[string]interface{}, isOrganizationDeployment bool, hostNodeID string, initialRequest bool) error {
ctx, span := telemetry.NewSpan(ctx, "model", "upsert-cloud-compliance-node")
defer span.End()
driver, err := directory.Neo4jClient(ctx)
if err != nil {
return err
}
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
defer session.Close(ctx)
tx, err := session.BeginTransaction(ctx, neo4j.WithTxTimeout(30*time.Second))
if err != nil {
return err
}
defer tx.Close(ctx)
var setRefreshStatusQuery string
var scheduleRefreshStatusQuery string
// Organization account node does not have refresh status. It is rather an aggregation of all child account statuses.
if !isOrganizationDeployment {
setRefreshStatusQuery = `ON CREATE SET n.refresh_status = '` + utils.ScanStatusStarting + `', n.refresh_message = '', n.refresh_metadata = ''`
if initialRequest {
session2 := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
defer session2.Close(ctx)
tx2, err := session2.BeginTransaction(ctx, neo4j.WithTxTimeout(30*time.Second))
if err != nil {
return err
}
defer tx2.Close(ctx)
res, err := tx2.Run(ctx,
`MATCH (n:CloudNode{node_id:$node_id}) RETURN coalesce(n.installation_id,''), coalesce(n.refresh_status,'')`,
map[string]interface{}{"node_id": nodeDetails["node_id"]})
if err != nil {
return err
}
rec, err := res.Single(ctx)
// ON CREATE will handle if Node does not exist
if err == nil {
if rec.Values[0].(string) != nodeDetails["installation_id"] {
scheduleRefreshStatusQuery = `, n.refresh_status = '` + utils.ScanStatusStarting + `', n.refresh_message = '', n.refresh_metadata = ''`
} else if rec.Values[1].(string) == utils.ScanStatusInProgress {
// Make it STARTING so that cloud scanner control request will pick it and continue
scheduleRefreshStatusQuery = `, n.refresh_status = '` + utils.ScanStatusStarting + `'`
}
}
}
}
if _, err = tx.Run(ctx, `
MERGE (r:Node{node_id:$host_node_id, node_type: "cloud_agent"})
WITH $param as row, r
MERGE (n:CloudNode{node_id:row.node_id})
`+setRefreshStatusQuery+`
MERGE (r) -[:HOSTS]-> (n)
SET n+= row, n.active = true, n.updated_at = TIMESTAMP(), n.version = row.version, r.node_name = $host_node_id,
r.active = true, r.agent_running = true, r.updated_at = TIMESTAMP()`+scheduleRefreshStatusQuery,
map[string]interface{}{
"param": nodeDetails,
"host_node_id": hostNodeID,
}); err != nil {
return err
}
return tx.Commit(ctx)
}
func UpsertChildCloudAccounts(ctx context.Context, nodeDetails []map[string]interface{}, parentNodeID string, installationID string, hostNodeID string, initialRequest bool) error {
ctx, span := telemetry.NewSpan(ctx, "model", "upsert-cloud-compliance-node")
defer span.End()
driver, err := directory.Neo4jClient(ctx)
if err != nil {
return err
}
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
defer session.Close(ctx)
tx, err := session.BeginTransaction(ctx, neo4j.WithTxTimeout(30*time.Second))
if err != nil {
return err
}
defer tx.Close(ctx)
accountIDRefreshStatusMap := make(map[string]map[string]string)
if initialRequest {
session2 := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
defer session2.Close(ctx)
tx2, err := session2.BeginTransaction(ctx, neo4j.WithTxTimeout(30*time.Second))
if err != nil {
return err
}
defer tx2.Close(ctx)
res, err := tx2.Run(ctx,
`MATCH (m:CloudNode{node_id:$node_id}) -[:IS_CHILD]-> (n:CloudNode)
RETURN coalesce(n.node_name,''), coalesce(n.installation_id,''), coalesce(n.refresh_status,'')`,
map[string]interface{}{"node_id": parentNodeID})
if err != nil {
return err
}
recs, err := res.Collect(ctx)
if err != nil {
return err
}
for _, rec := range recs {
accountID := rec.Values[0].(string)
if rec.Values[1].(string) != installationID {
accountIDRefreshStatusMap[accountID] = map[string]string{
"refresh_status": utils.ScanStatusStarting, "refresh_message": "", "refresh_metadata": ""}
} else if rec.Values[2].(string) == utils.ScanStatusInProgress {
// Make it STARTING so that cloud scanner control request will pick it and continue
accountIDRefreshStatusMap[accountID] = map[string]string{"refresh_status": utils.ScanStatusStarting}
}
}
}
for i, nodeDetail := range nodeDetails {
if refreshStatus, ok := accountIDRefreshStatusMap[nodeDetail["node_name"].(string)]; ok {
for k, v := range refreshStatus {
nodeDetails[i][k] = v
}
}
}
if _, err = tx.Run(ctx, `
MERGE (r:Node{node_id:$host_node_id, node_type: "cloud_agent"})
MERGE (m:CloudNode{node_id: $parent_node_id})
WITH r, m
UNWIND $param as row
MERGE (n:CloudNode{node_id:row.node_id})
ON CREATE SET n.refresh_status = '`+utils.ScanStatusStarting+`', n.refresh_message = '', n.refresh_metadata = ''
MERGE (m) -[:IS_CHILD]-> (n)
MERGE (r) -[:HOSTS]-> (n)
SET n+= row, n.active = true, n.updated_at = TIMESTAMP(), n.version = row.version, r.active = true,
r.agent_running=true, r.updated_at = TIMESTAMP()`,
map[string]interface{}{
"param": nodeDetails,
"parent_node_id": parentNodeID,
"host_node_id": hostNodeID,
}); err != nil {
return err
}
return tx.Commit(ctx)
}
func getPostureProviderCache(ctx context.Context) []PostureProvider {
var postureProvidersCache []PostureProvider
rdb, err := directory.RedisClient(ctx)
if err != nil {
log.Error().Msgf("GetCloudProvidersList redis : %v", err)
return postureProvidersCache
}
postureProvidersStr, err := rdb.Get(ctx, constants.RedisKeyPostureProviders).Result()
if err != nil {
log.Error().Msgf("GetCloudProvidersList redis : %v", err)
return postureProvidersCache
}
err = json.Unmarshal([]byte(postureProvidersStr), &postureProvidersCache)
if err != nil {
log.Error().Msgf("GetCloudProvidersList redis : %v", err)
}
return postureProvidersCache
}
func GetCloudProvidersList(ctx context.Context) ([]PostureProvider, error) {
ctx, span := telemetry.NewSpan(ctx, "model", "get-cloud-providers-list")
defer span.End()
driver, err := directory.Neo4jClient(ctx)
if err != nil {
return nil, err
}
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
defer session.Close(ctx)
tx, err := session.BeginTransaction(ctx, neo4j.WithTxTimeout(30*time.Second))
if err != nil {
return nil, err
}
defer tx.Close(ctx)
postureProvidersCache := getPostureProviderCache(ctx)
postureProviders := []PostureProvider{
{Name: PostureProviderAWS, NodeLabel: "Accounts"},
// {Name: PostureProviderAWSOrg, NodeLabel: "Organizations"},
{Name: PostureProviderGCP, NodeLabel: "Accounts"},
// {Name: PostureProviderGCPOrg, NodeLabel: "Organizations"},
{Name: PostureProviderAzure, NodeLabel: "Accounts"},
// {Name: PostureProviderAzureOrg, NodeLabel: "Organizations"},
{Name: PostureProviderLinux, NodeLabel: "Hosts"},
{Name: PostureProviderKubernetes, NodeLabel: "Clusters"},
}
providersIndex := make(map[string]int)
for i, provider := range postureProviders {
providersIndex[provider.Name] = i
}
for _, postureProvider := range postureProvidersCache {
postureProviders[providersIndex[postureProvider.Name]] = postureProvider
}
// Hosts
query := `MATCH (m:Node)
WHERE m.pseudo=false and m.agent_running=true
RETURN m.active, count(m)`
r, err := tx.Run(ctx, query, map[string]interface{}{})
if err == nil {
records, err := r.Collect(ctx)
if err == nil {
for _, record := range records {
if record.Values[0].(bool) {
postureProviders[providersIndex[PostureProviderLinux]].NodeCount = record.Values[1].(int64)
} else {
postureProviders[providersIndex[PostureProviderLinux]].NodeCountInactive = record.Values[1].(int64)
}
}
}
} else {
log.Warn().Msgf("GetCloudProvidersList Linux : %v", err)
}
// Kubernetes
query = `MATCH (m:KubernetesCluster)
WHERE m.pseudo=false and m.agent_running=true
RETURN m.active, count(m)`
r, err = tx.Run(ctx, query, map[string]interface{}{})
if err == nil {
records, err := r.Collect(ctx)
if err == nil {
for _, record := range records {
if record.Values[0].(bool) {
postureProviders[providersIndex[PostureProviderKubernetes]].NodeCount = record.Values[1].(int64)
} else {
postureProviders[providersIndex[PostureProviderKubernetes]].NodeCountInactive = record.Values[1].(int64)
}
}
}
} else {
log.Warn().Msgf("GetCloudProvidersList Kubernetes : %v", err)
}
// CloudNodes
query = `MATCH (m:CloudNode)
RETURN m.cloud_provider, m.active, count(m)`
r, err = tx.Run(ctx, query, map[string]interface{}{})
if err == nil {
records, err := r.Collect(ctx)
if err == nil {
for _, record := range records {
provider := record.Values[0].(string)
if slices.Contains([]string{PostureProviderAWSOrg, PostureProviderGCPOrg, PostureProviderAzureOrg}, provider) {
continue
}
if record.Values[1].(bool) {
postureProviders[providersIndex[provider]].NodeCount = record.Values[2].(int64)
} else {
postureProviders[providersIndex[provider]].NodeCountInactive = record.Values[2].(int64)
}
}
}
} else {
log.Warn().Msgf("GetCloudProvidersList CloudNode : %v", err)
}
return postureProviders, nil
}
func GetCloudComplianceNodesList(ctx context.Context, cloudProvider string, fw FetchWindow) (CloudNodeAccountsListResp, error) {
ctx, span := telemetry.NewSpan(ctx, "model", "get-cloud-compliance-nodes-list")
defer span.End()
driver, err := directory.Neo4jClient(ctx)
if err != nil {
return CloudNodeAccountsListResp{Total: 0}, err
}
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
defer session.Close(ctx)
tx, err := session.BeginTransaction(ctx, neo4j.WithTxTimeout(30*time.Second))
if err != nil {
return CloudNodeAccountsListResp{Total: 0}, err
}
defer tx.Close(ctx)
isOrgListing := false
neo4jNodeType := "CloudNode"
passStatus := []string{"ok", "info", "skip"}
switch cloudProvider {
case PostureProviderAWSOrg:
cloudProvider = PostureProviderAWS
isOrgListing = true
case PostureProviderGCPOrg:
cloudProvider = PostureProviderGCP
isOrgListing = true
case PostureProviderAzureOrg:
cloudProvider = PostureProviderAzure
isOrgListing = true
case PostureProviderKubernetes:
neo4jNodeType = "KubernetesCluster"
case PostureProviderLinux:
neo4jNodeType = "Node"
passStatus = []string{"warn", "pass"}
}
var res neo4j.ResultWithContext
var query string
switch {
case cloudProvider == PostureProviderKubernetes || cloudProvider == PostureProviderLinux:
nonKubeFilter := ""
if cloudProvider == PostureProviderLinux {
nonKubeFilter = "{kubernetes_cluster_id:''}"
}
query = `
MATCH (n:` + string(neo4jNodeType) + nonKubeFilter + `)
WHERE n.pseudo=false
RETURN n.node_id, n.node_name, $cloud_provider, n.active, n.updated_at, COALESCE(n.compliance_latest_scan_id, ''), COALESCE(n.compliance_latest_scan_status, '')
ORDER BY n.updated_at` + fw.FetchWindow2CypherQuery()
case isOrgListing:
query = `
MATCH (m:` + string(neo4jNodeType) + `{cloud_provider:$cloud_provider+'_org'}) -[:IS_CHILD]-> (n:` + string(neo4jNodeType) + `)
RETURN n.node_id, n.node_name, $cloud_provider, n.active, n.updated_at, COALESCE(n.cloud_compliance_latest_scan_id, ''), COALESCE(n.cloud_compliance_latest_scan_status, '')
ORDER BY n.updated_at` + fw.FetchWindow2CypherQuery()
default:
query = `
MATCH (n:` + string(neo4jNodeType) + `{cloud_provider: $cloud_provider})
RETURN n.node_id, n.node_name, $cloud_provider, n.active, n.updated_at, COALESCE(n.cloud_compliance_latest_scan_id, ''), COALESCE(n.cloud_compliance_latest_scan_status, '')
ORDER BY n.updated_at` + fw.FetchWindow2CypherQuery()
}
log.Debug().Msgf("posture query: %v", query)
res, err = tx.Run(ctx,
query,
map[string]interface{}{
"cloud_provider": cloudProvider,
"pass_status": passStatus},
)
if err != nil {
return CloudNodeAccountsListResp{Total: 0}, err
}
recs, err := res.Collect(ctx)
if err != nil {
return CloudNodeAccountsListResp{Total: 0}, err
}
cloudNodeAccountsInfo := []CloudNodeAccountInfo{}
for _, rec := range recs {
tmp := CloudNodeAccountInfo{
NodeID: rec.Values[0].(string),
NodeName: rec.Values[1].(string),
CloudProvider: rec.Values[2].(string),
CompliancePercentage: 0,
Active: rec.Values[3].(bool),
LastScanID: rec.Values[5].(string),
LastScanStatus: rec.Values[6].(string),
}
cloudNodeAccountsInfo = append(cloudNodeAccountsInfo, tmp)
}
total := fw.Offset + len(cloudNodeAccountsInfo)
var countRes neo4j.ResultWithContext
if isOrgListing {
countRes, err = tx.Run(ctx, `
MATCH (m:CloudNode) -[:IS_CHILD]-> (n:CloudNode{cloud_provider: $cloud_provider})
RETURN COUNT(m)`,
map[string]interface{}{"cloud_provider": cloudProvider})
} else {
countRes, err = tx.Run(ctx, fmt.Sprintf(`
MATCH (n:%s {cloud_provider: $cloud_provider})
RETURN COUNT(*)`, neo4jNodeType),
map[string]interface{}{"cloud_provider": cloudProvider})
}
if err != nil {
return CloudNodeAccountsListResp{Total: 0}, err
}
countRec, err := countRes.Single(ctx)
if err != nil {
return CloudNodeAccountsListResp{CloudNodeAccountInfo: cloudNodeAccountsInfo, Total: total}, nil
}
total = int(countRec.Values[0].(int64))
return CloudNodeAccountsListResp{CloudNodeAccountInfo: cloudNodeAccountsInfo, Total: total}, nil
}
type CloudAccountRefreshReq struct {
NodeIDs []string `json:"node_ids" validate:"required,gt=0" required:"true"`
}
func (c *CloudAccountRefreshReq) SetCloudAccountRefresh(ctx context.Context) error {
ctx, span := telemetry.NewSpan(ctx, "model", "set-cloud-account-refresh")
defer span.End()
driver, err := directory.Neo4jClient(ctx)
if err != nil {
return err
}
session := driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
defer session.Close(ctx)
tx, err := session.BeginTransaction(ctx, neo4j.WithTxTimeout(30*time.Second))
if err != nil {
return err
}
defer tx.Close(ctx)
if _, err = tx.Run(ctx, `
UNWIND $batch as cloudNode
MATCH (m:CloudNode{node_id: cloudNode})
SET m.refresh_status = '`+utils.ScanStatusStarting+`', m.refresh_message = '', m.refresh_metadata = ''`,
map[string]interface{}{
"batch": c.NodeIDs,
}); err != nil {
return err
}
return tx.Commit(ctx)
}
type CloudAccountDeleteReq struct {
NodeIDs []string `json:"node_ids" validate:"required,gt=0" required:"true"`
}