@@ -143,38 +143,97 @@ func setTFResourcePolicyRulesDrift(d *schema.ResourceData, policy v2.PolicyRules
143
143
return errors .New ("The policy must have at least one rule attached to it" )
144
144
}
145
145
146
- rules := []map [string ]interface {} {}
146
+ var rules []map [string ]interface {}
147
147
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
- }}
148
+ driftDetails , ok := rule .Details .(* v2.DriftRuleDetails )
149
+ if ! ok {
150
+ return errors .New ("unexpected rule details type, expected DriftRuleDetails" )
151
+ }
152
+
153
+ // Directly use fields assuming backend returns zero values (not nil)
154
+ exceptionsItems := driftDetails .Exceptions .Items
155
+ exceptionsMatchItems := driftDetails .Exceptions .MatchItems
156
+
157
+ var exceptionsBlock []map [string ]interface {}
158
+ if len (exceptionsItems ) > 0 || exceptionsMatchItems {
159
+ exceptionsBlock = []map [string ]interface {}{
160
+ {
161
+ "items" : exceptionsItems ,
162
+ "match_items" : exceptionsMatchItems ,
163
+ },
164
+ }
165
+ }
153
166
154
- prohibitedBinaries := []map [string ]interface {}{{
155
- "items" : rule .Details .(* v2.DriftRuleDetails ).ProhibitedBinaries .Items ,
156
- "match_items" : rule .Details .(* v2.DriftRuleDetails ).ProhibitedBinaries .MatchItems ,
157
- }}
167
+ prohibitedItems := driftDetails .ProhibitedBinaries .Items
168
+ prohibitedMatchItems := driftDetails .ProhibitedBinaries .MatchItems
158
169
159
- mode := rule .Details .(* v2.DriftRuleDetails ).Mode
160
- enabled := true
161
- if mode == "disabled" {
162
- enabled = false
170
+ var prohibitedBinariesBlock []map [string ]interface {}
171
+ if len (prohibitedItems ) > 0 || prohibitedMatchItems {
172
+ prohibitedBinariesBlock = []map [string ]interface {}{
173
+ {
174
+ "items" : prohibitedItems ,
175
+ "match_items" : prohibitedMatchItems ,
176
+ },
177
+ }
163
178
}
164
179
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
- })
180
+ processBasedExceptionsItems := driftDetails .ProcessBasedExceptions .Items
181
+ processBasedExceptionMatchItems := driftDetails .ProcessBasedExceptions .MatchItems
182
+
183
+ var processBasedExceptionsBlock []map [string ]interface {}
184
+ if len (processBasedExceptionsItems ) > 0 || processBasedExceptionMatchItems {
185
+ processBasedExceptionsBlock = []map [string ]interface {}{
186
+ {
187
+ "items" : processBasedExceptionsItems ,
188
+ "match_items" : processBasedExceptionMatchItems ,
189
+ },
190
+ }
191
+ }
192
+
193
+ processBasedProhibitedBinariesItems := driftDetails .ProcessBasedDenylist .Items
194
+ processBasedProhibitedBinariesMatchItems := driftDetails .ProcessBasedDenylist .MatchItems
195
+
196
+ var processBasedProhibitedBinariesBlock []map [string ]interface {}
197
+ if len (processBasedProhibitedBinariesItems ) > 0 || processBasedProhibitedBinariesMatchItems {
198
+ processBasedProhibitedBinariesBlock = []map [string ]interface {}{
199
+ {
200
+ "items" : processBasedProhibitedBinariesItems ,
201
+ "match_items" : processBasedProhibitedBinariesMatchItems ,
202
+ },
203
+ }
204
+ }
205
+
206
+ mode := driftDetails .Mode
207
+ enabled := (mode != "disabled" )
208
+
209
+ ruleMap := map [string ]interface {}{
210
+ "id" : rule .Id ,
211
+ "name" : rule .Name ,
212
+ "description" : rule .Description ,
213
+ "version" : rule .Version ,
214
+ "tags" : rule .Tags ,
215
+ "enabled" : enabled ,
216
+ }
217
+
218
+ if exceptionsBlock != nil {
219
+ ruleMap ["exceptions" ] = exceptionsBlock
220
+ }
221
+ if prohibitedBinariesBlock != nil {
222
+ ruleMap ["prohibited_binaries" ] = prohibitedBinariesBlock
223
+ }
224
+ if processBasedExceptionsBlock != nil {
225
+ ruleMap ["process_based_exceptions" ] = processBasedExceptionsBlock
226
+ }
227
+ if processBasedProhibitedBinariesBlock != nil {
228
+ ruleMap ["process_based_prohibited_binaries" ] = processBasedProhibitedBinariesBlock
229
+ }
230
+
231
+ rules = append (rules , ruleMap )
175
232
}
176
233
177
- _ = d .Set ("rule" , rules )
234
+ if err := d .Set ("rule" , rules ); err != nil {
235
+ return err
236
+ }
178
237
179
238
return nil
180
239
}
@@ -418,13 +477,43 @@ func setPolicyRulesDrift(policy *v2.PolicyRulesComposite, d *schema.ResourceData
418
477
if _ , ok := d .GetOk ("rule.0.exceptions" ); ok { // TODO: Do not hardcode the indexes
419
478
exceptions .Items = schemaSetToList (d .Get ("rule.0.exceptions.0.items" ))
420
479
exceptions .MatchItems = d .Get ("rule.0.exceptions.0.match_items" ).(bool )
480
+ } else {
481
+ // initialize Items and MatchItems so we comply with structure and not generate drift
482
+ exceptions .Items = []string {}
483
+ exceptions .MatchItems = false
421
484
}
422
485
423
486
// TODO: Extract into a function
424
487
prohibitedBinaries := & v2.RuntimePolicyRuleList {}
425
488
if _ , ok := d .GetOk ("rule.0.prohibited_binaries" ); ok { // TODO: Do not hardcode the indexes
426
489
prohibitedBinaries .Items = schemaSetToList (d .Get ("rule.0.prohibited_binaries.0.items" ))
427
490
prohibitedBinaries .MatchItems = d .Get ("rule.0.prohibited_binaries.0.match_items" ).(bool )
491
+ } else {
492
+ // initialize Items and MatchItems so we comply with structure and not generate drift
493
+ prohibitedBinaries .Items = []string {}
494
+ prohibitedBinaries .MatchItems = false
495
+ }
496
+
497
+ // TODO: Extract into a function
498
+ processBasedExceptions := & v2.RuntimePolicyRuleList {}
499
+ if _ , ok := d .GetOk ("rule.0.process_based_exceptions" ); ok { // TODO: Do not hardcode the indexes
500
+ processBasedExceptions .Items = schemaSetToList (d .Get ("rule.0.process_based_exceptions.0.items" ))
501
+ processBasedExceptions .MatchItems = d .Get ("rule.0.process_based_exceptions.0.match_items" ).(bool )
502
+ } else {
503
+ // initialize Items and MatchItems so we comply with structure and not generate drift
504
+ processBasedExceptions .Items = []string {}
505
+ processBasedExceptions .MatchItems = false
506
+ }
507
+
508
+ // TODO: Extract into a function
509
+ processBasedProhibitedBinaries := & v2.RuntimePolicyRuleList {}
510
+ if _ , ok := d .GetOk ("rule.0.process_based_prohibited_binaries" ); ok { // TODO: Do not hardcode the indexes
511
+ processBasedProhibitedBinaries .Items = schemaSetToList (d .Get ("rule.0.process_based_prohibited_binaries.0.items" ))
512
+ processBasedProhibitedBinaries .MatchItems = d .Get ("rule.0.process_based_prohibited_binaries.0.match_items" ).(bool )
513
+ } else {
514
+ // initialize Items and MatchItems so we comply with structure and not generate drift
515
+ processBasedProhibitedBinaries .Items = []string {}
516
+ processBasedProhibitedBinaries .MatchItems = false
428
517
}
429
518
430
519
tags := schemaSetToList (d .Get ("rule.0.tags" ))
@@ -445,10 +534,12 @@ func setPolicyRulesDrift(policy *v2.PolicyRulesComposite, d *schema.ResourceData
445
534
Description : d .Get ("rule.0.description" ).(string ),
446
535
Tags : tags ,
447
536
Details : v2.DriftRuleDetails {
448
- RuleType : v2 .ElementType ("DRIFT" ), // TODO: Use const
449
- Mode : mode ,
450
- Exceptions : exceptions ,
451
- ProhibitedBinaries : prohibitedBinaries ,
537
+ RuleType : v2 .ElementType ("DRIFT" ), // TODO: Use const
538
+ Mode : mode ,
539
+ Exceptions : exceptions ,
540
+ ProhibitedBinaries : prohibitedBinaries ,
541
+ ProcessBasedExceptions : processBasedExceptions ,
542
+ ProcessBasedDenylist : processBasedProhibitedBinaries ,
452
543
},
453
544
}
454
545
0 commit comments