Skip to content

Commit e33c861

Browse files
Rules order in policy showing drift during apply (#494)
* Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * ignore changes in rule orders (#495) * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Rules Order In Policy Showing Drift During Apply * Remove logs * Test * Test * Test * Test * Test * Logs * Logs * Logs * Logs * Logs * PR Comments --------- Co-authored-by: kmvachhani <63314936+kmvachhani@users.noreply.github.com>
1 parent 0a79a54 commit e33c861

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

sysdig/resource_sysdig_secure_custom_policy.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,63 @@ func customPolicyToResourceData(policy *v2.Policy, d *schema.ResourceData) {
123123
_ = d.Set("type", "falco")
124124
}
125125

126-
rules := []map[string]interface{}{}
126+
rules := getPolicyRulesFromResourceData(d)
127+
newRules := []map[string]interface{}{}
127128
for _, rule := range policy.Rules {
128-
rules = append(rules, map[string]interface{}{
129+
newRules = append(newRules, map[string]interface{}{
129130
"name": rule.Name,
130131
"enabled": rule.Enabled,
131132
})
132133
}
133-
_ = d.Set("rules", rules)
134+
currentRules := []map[string]interface{}{}
135+
for _, rule := range rules {
136+
currentRules = append(currentRules, map[string]interface{}{
137+
"name": rule.Name,
138+
"enabled": rule.Enabled,
139+
})
140+
}
141+
142+
if !arePolicyRulesEquivalent(currentRules, newRules) {
143+
_ = d.Set("rules", newRules)
144+
} else {
145+
_ = d.Set("rules", currentRules)
146+
}
147+
}
148+
149+
func getPolicyRulesFromResourceData(d *schema.ResourceData) []*v2.PolicyRule {
150+
rules := d.Get("rules").([]interface{})
151+
policyRules := make([]*v2.PolicyRule, len(rules))
152+
153+
for i, rule := range rules {
154+
policyRules[i] = &v2.PolicyRule{
155+
Name: rule.(map[string]interface{})["name"].(string),
156+
Enabled: rule.(map[string]interface{})["enabled"].(bool),
157+
}
158+
}
159+
160+
return policyRules
161+
}
162+
163+
func arePolicyRulesEquivalent(newRules []map[string]interface{}, currentRules []map[string]interface{}) bool {
164+
if len(newRules) != len(currentRules) {
165+
return false
166+
}
167+
currentRulesMap := make(map[string]bool, 0)
168+
for _, rule := range currentRules {
169+
ruleName := rule["name"].(string)
170+
enabled := rule["enabled"].(bool)
171+
currentRulesMap[ruleName] = enabled
172+
}
173+
for _, rule := range newRules {
174+
newRuleEnabled := rule["enabled"].(bool)
175+
newRulesName := rule["name"].(string)
176+
if enabled, ok := currentRulesMap[newRulesName]; !ok {
177+
return false
178+
} else if enabled != newRuleEnabled {
179+
return false
180+
}
181+
}
182+
return true
134183
}
135184

136185
func resourceSysdigCustomPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

sysdig/resource_sysdig_secure_custom_policy_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
func TestAccCustomPolicy(t *testing.T) {
1717
rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) }
18-
18+
policy1 := rText()
1919
resource.ParallelTest(t, resource.TestCase{
2020
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv),
2121
ProviderFactories: map[string]func() (*schema.Provider, error){
@@ -25,13 +25,16 @@ func TestAccCustomPolicy(t *testing.T) {
2525
},
2626
Steps: []resource.TestStep{
2727
{
28-
Config: customPolicyWithName(rText()),
28+
Config: customPolicyWithName(policy1),
2929
},
3030
{
3131
ResourceName: "sysdig_secure_custom_policy.sample",
3232
ImportState: true,
3333
ImportStateVerify: true,
3434
},
35+
{
36+
Config: customPolicyWithRulesOrderChange(policy1),
37+
},
3538
{
3639
Config: customPolicyWithoutActions(rText()),
3740
},
@@ -75,6 +78,10 @@ resource "sysdig_secure_custom_policy" "sample" {
7578
scope = "container.id != \"\""
7679
runbook = "https://sysdig.com"
7780
81+
rules {
82+
name = "Write below etc"
83+
enabled = true
84+
}
7885
rules {
7986
name = sysdig_secure_rule_falco.terminal_shell.name
8087
enabled = true
@@ -94,6 +101,41 @@ resource "sysdig_secure_custom_policy" "sample" {
94101
`, secureNotificationChannelEmailWithName(name), ruleFalcoTerminalShell(name), name, name)
95102
}
96103

104+
func customPolicyWithRulesOrderChange(name string) string {
105+
return fmt.Sprintf(`
106+
%s
107+
%s
108+
resource "sysdig_secure_custom_policy" "sample" {
109+
name = "TERRAFORM TEST 1 %s"
110+
description = "TERRAFORM TEST %s"
111+
enabled = true
112+
severity = 4
113+
scope = "container.id != \"\""
114+
runbook = "https://sysdig.com"
115+
116+
rules {
117+
name = sysdig_secure_rule_falco.terminal_shell.name
118+
enabled = true
119+
}
120+
rules {
121+
name = "Write below etc"
122+
enabled = true
123+
}
124+
125+
actions {
126+
container = "stop"
127+
capture {
128+
seconds_before_event = 5
129+
seconds_after_event = 10
130+
name = "testcapture"
131+
}
132+
}
133+
134+
notification_channels = [sysdig_secure_notification_channel_email.sample_email.id]
135+
}
136+
`, secureNotificationChannelEmailWithName(name), ruleFalcoTerminalShell(name), name, name)
137+
}
138+
97139
func customPolicyWithoutActions(name string) string {
98140
return fmt.Sprintf(`
99141
%s

0 commit comments

Comments
 (0)