Skip to content

Rules order in policy showing drift during apply #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions sysdig/resource_sysdig_secure_custom_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,63 @@ func customPolicyToResourceData(policy *v2.Policy, d *schema.ResourceData) {
_ = d.Set("type", "falco")
}

rules := []map[string]interface{}{}
rules := getPolicyRulesFromResourceData(d)
newRules := []map[string]interface{}{}
for _, rule := range policy.Rules {
rules = append(rules, map[string]interface{}{
newRules = append(newRules, map[string]interface{}{
"name": rule.Name,
"enabled": rule.Enabled,
})
}
_ = d.Set("rules", rules)
currentRules := []map[string]interface{}{}
for _, rule := range rules {
currentRules = append(currentRules, map[string]interface{}{
"name": rule.Name,
"enabled": rule.Enabled,
})
}

if !arePolicyRulesEquivalent(currentRules, newRules) {
_ = d.Set("rules", newRules)
} else {
_ = d.Set("rules", currentRules)
}
}

func getPolicyRulesFromResourceData(d *schema.ResourceData) []*v2.PolicyRule {
rules := d.Get("rules").([]interface{})
policyRules := make([]*v2.PolicyRule, len(rules))

for i, rule := range rules {
policyRules[i] = &v2.PolicyRule{
Name: rule.(map[string]interface{})["name"].(string),
Enabled: rule.(map[string]interface{})["enabled"].(bool),
}
}

return policyRules
}

func arePolicyRulesEquivalent(newRules []map[string]interface{}, currentRules []map[string]interface{}) bool {
if len(newRules) != len(currentRules) {
return false
}
currentRulesMap := make(map[string]bool, 0)
for _, rule := range currentRules {
ruleName := rule["name"].(string)
enabled := rule["enabled"].(bool)
currentRulesMap[ruleName] = enabled
}
for _, rule := range newRules {
newRuleEnabled := rule["enabled"].(bool)
newRulesName := rule["name"].(string)
if enabled, ok := currentRulesMap[newRulesName]; !ok {
return false
} else if enabled != newRuleEnabled {
return false
}
}
return true
}

func resourceSysdigCustomPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
46 changes: 44 additions & 2 deletions sysdig/resource_sysdig_secure_custom_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestAccCustomPolicy(t *testing.T) {
rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) }

policy1 := rText()
resource.ParallelTest(t, resource.TestCase{
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv),
ProviderFactories: map[string]func() (*schema.Provider, error){
Expand All @@ -25,13 +25,16 @@ func TestAccCustomPolicy(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: customPolicyWithName(rText()),
Config: customPolicyWithName(policy1),
},
{
ResourceName: "sysdig_secure_custom_policy.sample",
ImportState: true,
ImportStateVerify: true,
},
{
Config: customPolicyWithRulesOrderChange(policy1),
},
{
Config: customPolicyWithoutActions(rText()),
},
Expand Down Expand Up @@ -75,6 +78,10 @@ resource "sysdig_secure_custom_policy" "sample" {
scope = "container.id != \"\""
runbook = "https://sysdig.com"

rules {
name = "Write below etc"
enabled = true
}
rules {
name = sysdig_secure_rule_falco.terminal_shell.name
enabled = true
Expand All @@ -94,6 +101,41 @@ resource "sysdig_secure_custom_policy" "sample" {
`, secureNotificationChannelEmailWithName(name), ruleFalcoTerminalShell(name), name, name)
}

func customPolicyWithRulesOrderChange(name string) string {
return fmt.Sprintf(`
%s
%s
resource "sysdig_secure_custom_policy" "sample" {
name = "TERRAFORM TEST 1 %s"
description = "TERRAFORM TEST %s"
enabled = true
severity = 4
scope = "container.id != \"\""
runbook = "https://sysdig.com"

rules {
name = sysdig_secure_rule_falco.terminal_shell.name
enabled = true
}
rules {
name = "Write below etc"
enabled = true
}

actions {
container = "stop"
capture {
seconds_before_event = 5
seconds_after_event = 10
name = "testcapture"
}
}

notification_channels = [sysdig_secure_notification_channel_email.sample_email.id]
}
`, secureNotificationChannelEmailWithName(name), ruleFalcoTerminalShell(name), name, name)
}

func customPolicyWithoutActions(name string) string {
return fmt.Sprintf(`
%s
Expand Down
Loading