Skip to content

Commit

Permalink
Rules order in policy showing drift during apply (#494)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
jacklongsd and kmvachhani authored Apr 1, 2024
1 parent 0a79a54 commit e33c861
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
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

0 comments on commit e33c861

Please sign in to comment.