-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.go
754 lines (696 loc) · 28.8 KB
/
core.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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 monitor
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"cloud.google.com/go/firestore"
"cloud.google.com/go/functions/metadata"
pubsub "cloud.google.com/go/pubsub/apiv1"
"github.com/BrunoReboul/ram/utilities/cai"
"github.com/BrunoReboul/ram/utilities/ffo"
"github.com/BrunoReboul/ram/utilities/glo"
"github.com/BrunoReboul/ram/utilities/gps"
"github.com/BrunoReboul/ram/utilities/solution"
"github.com/google/uuid"
"github.com/open-policy-agent/opa/rego"
"google.golang.org/api/cloudresourcemanager/v1"
cloudresourcemanagerv2 "google.golang.org/api/cloudresourcemanager/v2"
pubsubpb "google.golang.org/genproto/googleapis/pubsub/v1"
)
// Global structure for global variables to optimize the cloud function performances
type Global struct {
assetsCollectionID string
assetsFilePath string
assetsFolderPath string
cloudresourcemanagerService *cloudresourcemanager.Service
cloudresourcemanagerServiceV2 *cloudresourcemanagerv2.Service // v2 is needed for folders
ctx context.Context
deploymentTime time.Time
environment string
firestoreClient *firestore.Client
functionName string
instanceName string
microserviceName string
opaFolderPath string
ownerLabelKeyName string
projectID string
PubSubID string
pubsubPublisherClient *pubsub.PublisherClient
ramComplianceStatusTopicName string
ramViolationTopicName string
regoModulesFolderPath string
retryTimeOutSeconds int64
step glo.Step
stepStack glo.Steps
violationResolverLabelKeyName string
writabelOPAFolderPath string
}
// feedMessage Cloud Asset Inventory feed message
type feedMessage struct {
Asset asset `json:"asset"`
Window cai.Window `json:"window"`
Deleted bool `json:"deleted"`
Origin string `json:"origin"`
StepStack glo.Steps `json:"step_stack,omitempty"`
}
// asset Cloud Asset Metadata
// Duplicate "iamPolicy" and "assetType en ensure compatibility beetween format in CAI feed, aka real time, and CAI Export aka batch
type asset struct {
Name string `json:"name"`
Owner string `json:"owner"`
ViolationResolver string `json:"violationResolver"`
AncestryPathDisplayName string `json:"ancestryPathDisplayName"`
AncestryPath string `json:"ancestryPath"`
AncestryPathLegacy string `json:"ancestry_path"`
AncestorsDisplayName []string `json:"ancestorsDisplayName"`
Ancestors []string `json:"ancestors"`
AssetType string `json:"assetType"`
AssetTypeLegacy string `json:"asset_type"`
IamPolicy json.RawMessage `json:"iamPolicy"`
IamPolicyLegacy json.RawMessage `json:"iam_policy"`
Resource json.RawMessage `json:"resource"`
ProjectID string `json:"projectID"`
}
// assets slice of asset
type assets []asset
// violations array of violation
type violations []violation
// violation from the "audit" rego policy in "audit.rego" module
type violation struct {
NonCompliance nonCompliance `json:"nonCompliance"`
FunctionConfig functionConfig `json:"functionConfig"`
ConstraintConfig constraintConfig `json:"constraintConfig"`
FeedMessage feedMessage `json:"feedMessage"`
RegoModules map[string]string `json:"regoModules"`
StepStack glo.Steps `json:"step_stack,omitempty"`
}
// nonCompliance form the "deny" rego policy in a <templateName>.rego module
type nonCompliance struct {
Message string `json:"message"`
Metadata map[string]interface{} `json:"metadata"`
}
// functionConfig function deployment settings
type functionConfig struct {
FunctionName string `json:"functionName"`
DeploymentTime time.Time `json:"deploymentTime"`
ProjectID string `json:"projectID"`
Environment string `json:"environment"`
}
// constraintConfig expose content of the constraint yaml file
type constraintConfig struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata constraintMetadata `json:"metadata"`
Spec spec `json:"spec"`
}
// constraintMetadata Constraint's metadata
type constraintMetadata struct {
Name string `json:"name"`
Annotations map[string]interface{} `json:"annotation"`
}
// spec Constraint's specifications
type spec struct {
Severity string `json:"severity"`
Match map[string]interface{} `json:"match"`
Parameters map[string]interface{} `json:"parameters"`
}
// compliantLog log entry when compliant
type compliantLog struct {
ComplianceStatus ComplianceStatus `json:"complianceStatus"`
AssetsJSONDocument json.RawMessage `json:"assetsJSONDocument"`
}
// ComplianceStatus by asset, by rule, true/false compliance status
type ComplianceStatus struct {
AssetName string `json:"assetName"`
AssetInventoryTimeStamp time.Time `json:"assetInventoryTimeStamp"`
AssetInventoryOrigin string `json:"assetInventoryOrigin"`
RuleName string `json:"ruleName"`
RuleDeploymentTimeStamp time.Time `json:"ruleDeploymentTimeStamp"`
Compliant bool `json:"compliant"`
Deleted bool `json:"deleted"`
StepStack glo.Steps `json:"step_stack,omitempty"`
}
// Initialize is to be executed in the init() function of the cloud function to optimize the cold start
func Initialize(ctx context.Context, global *Global) (err error) {
log.SetFlags(0)
global.ctx = ctx
var instanceDeployment InstanceDeployment
initID := fmt.Sprintf("%v", uuid.New())
err = ffo.ReadUnmarshalYAML(solution.PathToFunctionCode+solution.SettingsFileName, &instanceDeployment)
if err != nil {
log.Println(glo.Entry{
Severity: "CRITICAL",
Message: "init_failed",
Description: fmt.Sprintf("ReadUnmarshalYAML %s %v", solution.SettingsFileName, err),
InitID: initID,
})
return err
}
global.environment = instanceDeployment.Core.EnvironmentName
global.instanceName = instanceDeployment.Core.InstanceName
global.microserviceName = instanceDeployment.Core.ServiceName
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "NOTICE",
Message: "coldstart",
InitID: initID,
})
assetsFileName := instanceDeployment.Settings.Service.AssetsFileName
assetsFolderName := instanceDeployment.Settings.Service.AssetsFolderName
global.assetsCollectionID = instanceDeployment.Core.SolutionSettings.Hosting.FireStore.CollectionIDs.Assets
global.deploymentTime = instanceDeployment.Settings.Instance.DeploymentTime
global.functionName = instanceDeployment.Core.InstanceName
global.opaFolderPath = instanceDeployment.Settings.Service.OPAFolderPath
global.ownerLabelKeyName = instanceDeployment.Core.SolutionSettings.Monitoring.LabelKeyNames.Owner
global.projectID = instanceDeployment.Core.SolutionSettings.Hosting.ProjectID
global.ramComplianceStatusTopicName = instanceDeployment.Core.SolutionSettings.Hosting.Pubsub.TopicNames.RAMComplianceStatus
global.ramViolationTopicName = instanceDeployment.Core.SolutionSettings.Hosting.Pubsub.TopicNames.RAMViolation
global.retryTimeOutSeconds = instanceDeployment.Settings.Service.GCF.RetryTimeOutSeconds
global.violationResolverLabelKeyName = instanceDeployment.Core.SolutionSettings.Monitoring.LabelKeyNames.ViolationResolver
global.writabelOPAFolderPath = instanceDeployment.Settings.Service.WritabelOPAFolderPath
regoModulesFolderName := instanceDeployment.Settings.Service.RegoModulesFolderName
global.assetsFolderPath = global.writabelOPAFolderPath + "/" + assetsFolderName
global.assetsFilePath = global.assetsFolderPath + "/" + assetsFileName
global.regoModulesFolderPath = global.opaFolderPath + "/" + regoModulesFolderName
// services are initialized with context.Background() because it should
// persist between function invocations.
global.cloudresourcemanagerService, err = cloudresourcemanager.NewService(ctx)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "init_failed",
Description: fmt.Sprintf("cloudresourcemanager.NewService %v", err),
InitID: initID,
})
return err
}
global.cloudresourcemanagerServiceV2, err = cloudresourcemanagerv2.NewService(ctx)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "init_failed",
Description: fmt.Sprintf("cloudresourcemanagerv2.NewService %v", err),
InitID: initID,
})
return err
}
global.pubsubPublisherClient, err = pubsub.NewPublisherClient(global.ctx)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "init_failed",
Description: fmt.Sprintf("pubsub.NewPublisherClient %v", err),
InitID: initID,
})
return err
}
global.firestoreClient, err = firestore.NewClient(global.ctx, global.projectID)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "init_failed",
Description: fmt.Sprintf("firestore.NewClient %v", err),
InitID: initID,
})
return err
}
return nil
}
// EntryPoint is the function to be executed for each cloud function occurence
func EntryPoint(ctxEvent context.Context, PubSubMessage gps.PubSubMessage, global *Global) error {
// log.Println(string(PubSubMessage.Data))
metadata, err := metadata.FromContext(ctxEvent)
if err != nil {
// Assume an error on the function invoker and try again.
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "redo_on_transient",
Description: fmt.Sprintf("pubsub_id no available metadata.FromContext: %v", err),
TriggeringPubsubID: global.PubSubID,
})
return err
}
global.stepStack = nil
global.PubSubID = metadata.EventID
parts := strings.Split(metadata.Resource.Name, "/")
global.step = glo.Step{
StepID: fmt.Sprintf("%s/%s", parts[len(parts)-1], global.PubSubID),
StepTimestamp: metadata.Timestamp,
}
now := time.Now()
d := now.Sub(metadata.Timestamp)
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "NOTICE",
Message: "start",
TriggeringPubsubID: global.PubSubID,
TriggeringPubsubAgeSeconds: d.Seconds(),
TriggeringPubsubTimestamp: &metadata.Timestamp,
Now: &now,
})
if d.Seconds() > float64(global.retryTimeOutSeconds) {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "noretry",
Description: "Pubsub message too old",
TriggeringPubsubID: global.PubSubID,
TriggeringPubsubAgeSeconds: d.Seconds(),
TriggeringPubsubTimestamp: &metadata.Timestamp,
Now: &now,
})
return nil
}
if strings.Contains(string(PubSubMessage.Data), "You have successfully configured real time feed") {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "NOTICE",
Message: "cancel",
Description: fmt.Sprintf("ignored pubsub message: %s", string(PubSubMessage.Data)),
TriggeringPubsubID: global.PubSubID,
})
return nil
}
var complianceStatus ComplianceStatus
var compliantLog compliantLog
assetsJSONDocument, feedMessage, err := buildAssetsDocument(PubSubMessage, global)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "noretry",
Description: fmt.Sprintf("buildAssetsDocument(PubSubMessage, global) %v", err),
TriggeringPubsubID: global.PubSubID,
})
return nil
}
compliantLog.AssetsJSONDocument = assetsJSONDocument
complianceStatus.AssetName = feedMessage.Asset.Name
complianceStatus.AssetInventoryTimeStamp = feedMessage.Window.StartTime
complianceStatus.AssetInventoryOrigin = feedMessage.Origin
complianceStatus.RuleName = global.functionName
complianceStatus.RuleDeploymentTimeStamp = global.deploymentTime
complianceStatus.StepStack = global.stepStack
countViolations := 0
if feedMessage.Deleted == true {
complianceStatus.Deleted = feedMessage.Deleted
// bool cannot be nil and have a zero value to false
complianceStatus.Compliant = true
} else {
complianceStatus.Deleted = false
resultSet, feedMessage, err := evalutateConstraints(assetsJSONDocument, feedMessage, global)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "noretry",
Description: fmt.Sprintf("evalutateConstraints(assetsJSONDocument, feedMessage, global) %v", err),
TriggeringPubsubID: global.PubSubID,
})
return nil
}
violations, err := inspectResultSet(resultSet, feedMessage, global)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "noretry",
Description: fmt.Sprintf("inspectResultSet(resultSet, feedMessage, global) %v", err),
TriggeringPubsubID: global.PubSubID,
})
return nil
}
if len(violations) == 0 {
complianceStatus.Compliant = true
} else {
complianceStatus.Compliant = false
for i, violation := range violations {
countViolations = i
violation.StepStack = global.stepStack
violationJSON, err := json.Marshal(violation)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "noretry",
Description: fmt.Sprintf("json.Marshal(violation) %v", err),
TriggeringPubsubID: global.PubSubID,
})
return nil
}
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "INFO",
Message: fmt.Sprintf("not_compliant %s violationNum %d", complianceStatus.AssetName, i),
Description: fmt.Sprintf("origin %s timestamp %v violationJSON %s", complianceStatus.AssetInventoryOrigin, complianceStatus.AssetInventoryTimeStamp, string(violationJSON)),
TriggeringPubsubID: global.PubSubID,
})
err = publishPubSubMessage(violationJSON, global.ramViolationTopicName, global)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "redo_on_transient",
Description: err.Error(),
TriggeringPubsubID: global.PubSubID,
})
return err
}
}
}
}
complianceStatusJSON, err := json.Marshal(complianceStatus)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "noretry",
Description: fmt.Sprintf("json.Marshal(complianceStatus) %v", err),
TriggeringPubsubID: global.PubSubID,
})
return nil
}
err = publishPubSubMessage(complianceStatusJSON, global.ramComplianceStatusTopicName, global)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "redo_on_transient",
Description: err.Error(),
TriggeringPubsubID: global.PubSubID,
})
return err
}
compliantLog.ComplianceStatus = complianceStatus
if complianceStatus.Compliant == true {
CompliantLogJSON, err := json.Marshal(compliantLog)
if err != nil {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "CRITICAL",
Message: "noretry",
Description: fmt.Sprintf("json.Marshal(compliantLog) %v", err),
TriggeringPubsubID: global.PubSubID,
})
return nil
}
if complianceStatus.Deleted == true {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "INFO",
Message: fmt.Sprintf("deleted %s", complianceStatus.AssetName),
Description: fmt.Sprintf("origin %s timestamp %v CompliantLogJSON %s", complianceStatus.AssetInventoryOrigin, complianceStatus.AssetInventoryTimeStamp, string(CompliantLogJSON)),
TriggeringPubsubID: global.PubSubID,
})
} else {
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "INFO",
Message: fmt.Sprintf("compliant %s", complianceStatus.AssetName),
Description: fmt.Sprintf("origin %s timestamp %v CompliantLogJSON %s", complianceStatus.AssetInventoryOrigin, complianceStatus.AssetInventoryTimeStamp, string(CompliantLogJSON)),
TriggeringPubsubID: global.PubSubID,
})
}
}
var status string
if complianceStatus.Compliant {
status = "compliant"
} else {
status = "not_compliant"
}
now = time.Now()
latency := now.Sub(metadata.Timestamp)
latencyE2E := now.Sub(global.stepStack[0].StepTimestamp)
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "NOTICE",
Message: fmt.Sprintf("finish %s %s", status, complianceStatus.AssetName),
Description: fmt.Sprintf("number of violations %d", countViolations),
Now: &now,
TriggeringPubsubID: global.PubSubID,
OriginEventTimestamp: &metadata.Timestamp,
LatencySeconds: latency.Seconds(),
LatencyE2ESeconds: latencyE2E.Seconds(),
StepStack: global.stepStack,
Compliant: complianceStatus.Compliant,
AssetInventoryOrigin: feedMessage.Origin,
})
return nil
}
func publishPubSubMessage(docJSON []byte, topicName string, global *Global) error {
var pubSubMessage pubsubpb.PubsubMessage
pubSubMessage.Data = docJSON
var pubsubMessages []*pubsubpb.PubsubMessage
pubsubMessages = append(pubsubMessages, &pubSubMessage)
var publishRequest pubsubpb.PublishRequest
publishRequest.Topic = fmt.Sprintf("projects/%s/topics/%s", global.projectID, topicName)
publishRequest.Messages = pubsubMessages
pubsubResponse, err := global.pubsubPublisherClient.Publish(global.ctx, &publishRequest)
if err != nil {
return fmt.Errorf("global.pubsubPublisherClient.Publish: %v", err)
}
log.Println(glo.Entry{
MicroserviceName: global.microserviceName,
InstanceName: global.instanceName,
Environment: global.environment,
Severity: "INFO",
Message: fmt.Sprintf("published to topic %s", topicName),
Description: fmt.Sprintf("msg ids %v", pubsubResponse.MessageIds),
TriggeringPubsubID: global.PubSubID,
})
_ = pubsubResponse
return nil
}
// inspectResultSet explore rego query output and craft violation document
func inspectResultSet(resultSet rego.ResultSet, feedMessage feedMessage, global *Global) (violations, error) {
var violations violations
var violation violation
regoModules, err := importRegoModulesCode(global)
if err != nil {
return nil, err
}
Expressions := resultSet[0].Expressions
if len(Expressions) != 0 {
expressionValue := *Expressions[0]
// log.Println("Rego Query: ", expressionValue.Text)
// location := *expressionValue.Location
// log.Printf("Position in query: Row %d Col %d\n", location.Row, location.Col)
var valuesInterface interface{} = expressionValue.Value
if values, ok := valuesInterface.([]interface{}); ok {
for i := 0; i < len(values); i++ {
var valueInterface interface{} = values[i]
if value, ok := valueInterface.(map[string]interface{}); ok {
violation.FunctionConfig.FunctionName = global.functionName
violation.FunctionConfig.ProjectID = global.projectID
violation.FunctionConfig.Environment = global.environment
violation.FunctionConfig.DeploymentTime = global.deploymentTime
violation.FeedMessage = feedMessage
var violationInterface interface{} = value["violation"]
if ruleViolation, ok := violationInterface.(map[string]interface{}); ok {
var msgInterface interface{} = ruleViolation["msg"]
if msg, ok := msgInterface.(string); ok {
violation.NonCompliance.Message = msg
}
var detailsInterface interface{} = ruleViolation["details"]
if details, ok := detailsInterface.(map[string]interface{}); ok {
violation.NonCompliance.Metadata = details
}
}
var constraintConfigInterface interface{} = value["constraint_config"]
if constraintConfig, ok := constraintConfigInterface.(map[string]interface{}); ok {
var apiVersionInterface interface{} = constraintConfig["apiVersion"]
if apiVersion, ok := apiVersionInterface.(string); ok {
violation.ConstraintConfig.APIVersion = apiVersion
}
var kindInterface interface{} = constraintConfig["kind"]
if kind, ok := kindInterface.(string); ok {
violation.ConstraintConfig.Kind = kind
}
var metadataInterface interface{} = constraintConfig["metadata"]
if metadata, ok := metadataInterface.(map[string]interface{}); ok {
var nameInterface interface{} = metadata["name"]
if name, ok := nameInterface.(string); ok {
violation.ConstraintConfig.Metadata.Name = name
}
var annotationsInterface interface{} = metadata["annotations"]
if annotations, ok := annotationsInterface.(map[string]interface{}); ok {
violation.ConstraintConfig.Metadata.Annotations = annotations
}
}
var specInterface interface{} = constraintConfig["spec"]
if spec, ok := specInterface.(map[string]interface{}); ok {
var severityInterface interface{} = spec["severity"]
if severity, ok := severityInterface.(string); ok {
violation.ConstraintConfig.Spec.Severity = severity
}
var matchInterface interface{} = spec["match"]
if match, ok := matchInterface.(map[string]interface{}); ok {
violation.ConstraintConfig.Spec.Match = match
}
var parametersInterface interface{} = spec["parameters"]
if parameters, ok := parametersInterface.(map[string]interface{}); ok {
violation.ConstraintConfig.Spec.Parameters = parameters
}
}
}
violation.RegoModules = regoModules
}
violations = append(violations, violation)
}
}
}
return violations, nil
}
// importRegoModulesCode read regoModule code to be added in violation for logging / troubleshooting purposes
func importRegoModulesCode(global *Global) (regoModules map[string]string, err error) {
regoModules = make(map[string]string)
files, err := ioutil.ReadDir(global.regoModulesFolderPath)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadDir(global.regoModulesFolderPath) %v", err)
}
for _, file := range files {
regoCode, err := ioutil.ReadFile(global.regoModulesFolderPath + "/" + file.Name())
if err != nil {
return nil, fmt.Errorf("ioutil.ReadFile(global.regoModulesFolderPath %v", err)
}
regoModules[file.Name()] = string(regoCode)
}
return regoModules, nil
}
// evalutateConstraints audit assets data to rego rules
func evalutateConstraints(assetsJSONDocument []byte, feedMessage feedMessage, global *Global) (rego.ResultSet, feedMessage, error) {
var resultSet rego.ResultSet
if _, err := os.Stat(global.assetsFilePath); os.IsExist(err) {
// log.Println("Found ", assetsFilePath)
err := os.Remove(global.assetsFilePath)
if err != nil {
return resultSet, feedMessage, fmt.Errorf("os.Remove(assetsFilePath) %v", err)
}
}
if _, err := os.Stat(global.assetsFolderPath); os.IsNotExist(err) {
err = os.MkdirAll(global.assetsFolderPath, 0755)
if err != nil {
return resultSet, feedMessage, fmt.Errorf("os.MkdirAll(assetsFolderPath, 0755) %v", err)
}
}
err := ioutil.WriteFile(global.assetsFilePath, assetsJSONDocument, 0644)
if err != nil {
return resultSet, feedMessage, fmt.Errorf("ioutil.WriteFile(assetsFilePath, assetsJSONDocument, 0644) %v", err)
}
ctx := context.Background()
rego := rego.New(rego.Query("audit"),
rego.Load([]string{global.opaFolderPath, global.writabelOPAFolderPath}, nil),
rego.Package("validator.gcp.lib"))
resultSet, err = rego.Eval(ctx)
if err != nil {
return resultSet, feedMessage, fmt.Errorf("rego.Eval %v", err)
}
return resultSet, feedMessage, nil
}
// buildAssetsDocument
func buildAssetsDocument(pubSubMessage gps.PubSubMessage, global *Global) ([]byte, feedMessage, error) {
var feedMessage feedMessage
var assetsJSONDocument []byte
var assets assets
err := json.Unmarshal(pubSubMessage.Data, &feedMessage)
if err != nil {
return assetsJSONDocument, feedMessage, fmt.Errorf("json.Unmarshal(pubSubMessage.Data, &feedMessage) %v", err)
}
if feedMessage.StepStack != nil {
global.stepStack = append(feedMessage.StepStack, global.step)
} else {
var caiStep glo.Step
caiStep.StepTimestamp = feedMessage.Window.StartTime
caiStep.StepID = fmt.Sprintf("%s/%s", feedMessage.Asset.Name, caiStep.StepTimestamp.Format(time.RFC3339))
global.stepStack = append(global.stepStack, caiStep)
global.stepStack = append(global.stepStack, global.step)
}
if feedMessage.Origin == "" {
feedMessage.Origin = "real-time"
}
feedMessage.Asset.AncestryPath = cai.BuildAncestryPath(feedMessage.Asset.Ancestors)
feedMessage.Asset.AncestorsDisplayName, feedMessage.Asset.ProjectID = cai.BuildAncestorsDisplayName(global.ctx,
feedMessage.Asset.Ancestors,
global.assetsCollectionID,
global.firestoreClient,
global.cloudresourcemanagerService,
global.cloudresourcemanagerServiceV2)
feedMessage.Asset.AncestryPathDisplayName = cai.BuildAncestryPath(feedMessage.Asset.AncestorsDisplayName)
feedMessage.Asset.Owner, _ = cai.GetAssetLabelValue(global.ownerLabelKeyName, feedMessage.Asset.Resource)
feedMessage.Asset.ViolationResolver, _ = cai.GetAssetLabelValue(global.violationResolverLabelKeyName, feedMessage.Asset.Resource)
// Duplicate fileds into fieldLegacy for compatibility with existing policy library templates
feedMessage.Asset.IamPolicyLegacy = feedMessage.Asset.IamPolicy
feedMessage.Asset.AssetTypeLegacy = feedMessage.Asset.AssetType
feedMessage.Asset.AncestryPathLegacy = feedMessage.Asset.AncestryPath
assets = append(assets, feedMessage.Asset)
assetsJSONDocument, err = json.Marshal(assets)
if err != nil {
return assetsJSONDocument, feedMessage, fmt.Errorf("json.Marshal(assets) %v", err)
}
return assetsJSONDocument, feedMessage, nil
}