@@ -22,6 +22,8 @@ const (
22
22
defaultMalwareTag = "malware"
23
23
defaultDriftTag = "drift"
24
24
defaultMLTag = "machine_learning"
25
+
26
+ driftElementType = "DRIFT"
25
27
)
26
28
27
29
type Target interface {
@@ -143,38 +145,97 @@ func setTFResourcePolicyRulesDrift(d *schema.ResourceData, policy v2.PolicyRules
143
145
return errors .New ("The policy must have at least one rule attached to it" )
144
146
}
145
147
146
- rules := []map [string ]interface {} {}
148
+ var rules []map [string ]interface {}
147
149
for _ , rule := range policy .Rules {
148
- // Only a single block of exceptions and prohibited binaries is allowed
149
- exceptions := []map [string ]interface {}{{
150
- "items" : rule .Details .(* v2.DriftRuleDetails ).Exceptions .Items ,
151
- "match_items" : rule .Details .(* v2.DriftRuleDetails ).Exceptions .MatchItems ,
152
- }}
150
+ driftDetails , ok := rule .Details .(* v2.DriftRuleDetails )
151
+ if ! ok {
152
+ return errors .New ("unexpected rule details type, expected DriftRuleDetails" )
153
+ }
154
+
155
+ // Directly use fields assuming backend returns zero values (not nil)
156
+ exceptionsItems := driftDetails .Exceptions .Items
157
+ exceptionsMatchItems := driftDetails .Exceptions .MatchItems
158
+
159
+ var exceptionsBlock []map [string ]interface {}
160
+ if len (exceptionsItems ) > 0 || exceptionsMatchItems {
161
+ exceptionsBlock = []map [string ]interface {}{
162
+ {
163
+ "items" : exceptionsItems ,
164
+ "match_items" : exceptionsMatchItems ,
165
+ },
166
+ }
167
+ }
153
168
154
- prohibitedBinaries := []map [string ]interface {}{{
155
- "items" : rule .Details .(* v2.DriftRuleDetails ).ProhibitedBinaries .Items ,
156
- "match_items" : rule .Details .(* v2.DriftRuleDetails ).ProhibitedBinaries .MatchItems ,
157
- }}
169
+ prohibitedItems := driftDetails .ProhibitedBinaries .Items
170
+ prohibitedMatchItems := driftDetails .ProhibitedBinaries .MatchItems
158
171
159
- mode := rule .Details .(* v2.DriftRuleDetails ).Mode
160
- enabled := true
161
- if mode == "disabled" {
162
- enabled = false
172
+ var prohibitedBinariesBlock []map [string ]interface {}
173
+ if len (prohibitedItems ) > 0 || prohibitedMatchItems {
174
+ prohibitedBinariesBlock = []map [string ]interface {}{
175
+ {
176
+ "items" : prohibitedItems ,
177
+ "match_items" : prohibitedMatchItems ,
178
+ },
179
+ }
163
180
}
164
181
165
- rules = append (rules , map [string ]interface {}{
166
- "id" : rule .Id ,
167
- "name" : rule .Name ,
168
- "description" : rule .Description ,
169
- "version" : rule .Version ,
170
- "tags" : rule .Tags ,
171
- "enabled" : enabled ,
172
- "exceptions" : exceptions ,
173
- "prohibited_binaries" : prohibitedBinaries ,
174
- })
182
+ processBasedExceptionsItems := driftDetails .ProcessBasedExceptions .Items
183
+ processBasedExceptionMatchItems := driftDetails .ProcessBasedExceptions .MatchItems
184
+
185
+ var processBasedExceptionsBlock []map [string ]interface {}
186
+ if len (processBasedExceptionsItems ) > 0 || processBasedExceptionMatchItems {
187
+ processBasedExceptionsBlock = []map [string ]interface {}{
188
+ {
189
+ "items" : processBasedExceptionsItems ,
190
+ "match_items" : processBasedExceptionMatchItems ,
191
+ },
192
+ }
193
+ }
194
+
195
+ processBasedProhibitedBinariesItems := driftDetails .ProcessBasedDenylist .Items
196
+ processBasedProhibitedBinariesMatchItems := driftDetails .ProcessBasedDenylist .MatchItems
197
+
198
+ var processBasedProhibitedBinariesBlock []map [string ]interface {}
199
+ if len (processBasedProhibitedBinariesItems ) > 0 || processBasedProhibitedBinariesMatchItems {
200
+ processBasedProhibitedBinariesBlock = []map [string ]interface {}{
201
+ {
202
+ "items" : processBasedProhibitedBinariesItems ,
203
+ "match_items" : processBasedProhibitedBinariesMatchItems ,
204
+ },
205
+ }
206
+ }
207
+
208
+ mode := driftDetails .Mode
209
+ enabled := (mode != "disabled" )
210
+
211
+ ruleMap := map [string ]interface {}{
212
+ "id" : rule .Id ,
213
+ "name" : rule .Name ,
214
+ "description" : rule .Description ,
215
+ "version" : rule .Version ,
216
+ "tags" : rule .Tags ,
217
+ "enabled" : enabled ,
218
+ }
219
+
220
+ if exceptionsBlock != nil {
221
+ ruleMap ["exceptions" ] = exceptionsBlock
222
+ }
223
+ if prohibitedBinariesBlock != nil {
224
+ ruleMap ["prohibited_binaries" ] = prohibitedBinariesBlock
225
+ }
226
+ if processBasedExceptionsBlock != nil {
227
+ ruleMap ["process_based_exceptions" ] = processBasedExceptionsBlock
228
+ }
229
+ if processBasedProhibitedBinariesBlock != nil {
230
+ ruleMap ["process_based_prohibited_binaries" ] = processBasedProhibitedBinariesBlock
231
+ }
232
+
233
+ rules = append (rules , ruleMap )
175
234
}
176
235
177
- _ = d .Set ("rule" , rules )
236
+ if err := d .Set ("rule" , rules ); err != nil {
237
+ return err
238
+ }
178
239
179
240
return nil
180
241
}
@@ -414,18 +475,13 @@ func setPolicyRulesDrift(policy *v2.PolicyRulesComposite, d *schema.ResourceData
414
475
// TODO: Iterate over a list of rules instead of hard-coding the index values
415
476
// TODO: Should we assume that only a single Malware rule can be attached to a policy?
416
477
417
- exceptions := & v2.RuntimePolicyRuleList {}
418
- if _ , ok := d .GetOk ("rule.0.exceptions" ); ok { // TODO: Do not hardcode the indexes
419
- exceptions .Items = schemaSetToList (d .Get ("rule.0.exceptions.0.items" ))
420
- exceptions .MatchItems = d .Get ("rule.0.exceptions.0.match_items" ).(bool )
421
- }
478
+ exceptions := extractIntoRuntimePolicyRuleList ("rule.0.exceptions" , d )
422
479
423
- // TODO: Extract into a function
424
- prohibitedBinaries := & v2.RuntimePolicyRuleList {}
425
- if _ , ok := d .GetOk ("rule.0.prohibited_binaries" ); ok { // TODO: Do not hardcode the indexes
426
- prohibitedBinaries .Items = schemaSetToList (d .Get ("rule.0.prohibited_binaries.0.items" ))
427
- prohibitedBinaries .MatchItems = d .Get ("rule.0.prohibited_binaries.0.match_items" ).(bool )
428
- }
480
+ prohibitedBinaries := extractIntoRuntimePolicyRuleList ("rule.0.prohibited_binaries" , d )
481
+
482
+ processBasedExceptions := extractIntoRuntimePolicyRuleList ("rule.0.process_based_exceptions" , d )
483
+
484
+ processBasedProhibitedBinaries := extractIntoRuntimePolicyRuleList ("rule.0.process_based_prohibited_binaries" , d )
429
485
430
486
tags := schemaSetToList (d .Get ("rule.0.tags" ))
431
487
// Set default tags as field tags must not be null
@@ -445,10 +501,12 @@ func setPolicyRulesDrift(policy *v2.PolicyRulesComposite, d *schema.ResourceData
445
501
Description : d .Get ("rule.0.description" ).(string ),
446
502
Tags : tags ,
447
503
Details : v2.DriftRuleDetails {
448
- RuleType : v2 .ElementType ("DRIFT" ), // TODO: Use const
449
- Mode : mode ,
450
- Exceptions : exceptions ,
451
- ProhibitedBinaries : prohibitedBinaries ,
504
+ RuleType : v2 .ElementType (driftElementType ), // TODO: Use const
505
+ Mode : mode ,
506
+ Exceptions : & exceptions ,
507
+ ProhibitedBinaries : & prohibitedBinaries ,
508
+ ProcessBasedExceptions : & processBasedExceptions ,
509
+ ProcessBasedDenylist : & processBasedProhibitedBinaries ,
452
510
},
453
511
}
454
512
@@ -468,6 +526,22 @@ func setPolicyRulesDrift(policy *v2.PolicyRulesComposite, d *schema.ResourceData
468
526
return nil
469
527
}
470
528
529
+ func extractIntoRuntimePolicyRuleList (key string , d * schema.ResourceData ) v2.RuntimePolicyRuleList {
530
+ if _ , ok := d .GetOk (key ); ok {
531
+ items := schemaSetToList (d .Get (key + ".0.items" ))
532
+ matchItems := d .Get (key + ".0.match_items" ).(bool )
533
+
534
+ return v2.RuntimePolicyRuleList {
535
+ Items : items ,
536
+ MatchItems : matchItems ,
537
+ }
538
+ }
539
+ return v2.RuntimePolicyRuleList {
540
+ Items : []string {},
541
+ MatchItems : false ,
542
+ }
543
+ }
544
+
471
545
func setPolicyRulesML (policy * v2.PolicyRulesComposite , d * schema.ResourceData ) error {
472
546
policy .Policy .Rules = []* v2.PolicyRule {}
473
547
policy .Rules = []* v2.RuntimePolicyRule {}
0 commit comments