Skip to content

Commit b85c3a9

Browse files
authored
fix: Import correcly exceptions in Falco rules (#217)
1 parent 61fe222 commit b85c3a9

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

sysdig/resource_sysdig_secure_rule_falco.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"strconv"
89
"strings"
910
"time"
@@ -146,10 +147,55 @@ func resourceSysdigRuleFalcoRead(ctx context.Context, d *schema.ResourceData, me
146147
if rule.Details.Append != nil {
147148
_ = d.Set("append", *rule.Details.Append)
148149
}
150+
if err := updateResourceDataExceptions(d, rule.Details.Exceptions); err != nil {
151+
return diag.FromErr(err)
152+
}
153+
154+
return nil
155+
}
156+
157+
func updateResourceDataExceptions(d *schema.ResourceData, ruleExceptions []*secure.Exception) error {
158+
exceptions := make([]any, 0, len(ruleExceptions))
159+
for _, exception := range ruleExceptions {
160+
valuesData, err := json.Marshal(exception.Values)
161+
if err != nil {
162+
return fmt.Errorf("error marshalling exception values '%+v': %s", exception.Values, err)
163+
}
164+
fields, err := fieldOrCompsToStringSlice(exception.Fields)
165+
if err != nil {
166+
return fmt.Errorf("error converting exception fields '%+v': %s", exception.Fields, err)
167+
}
168+
comps, err := fieldOrCompsToStringSlice(exception.Comps)
169+
if err != nil {
170+
return fmt.Errorf("error converting exception comps '%+v': %s", exception.Comps, err)
171+
}
149172

173+
exceptions = append(exceptions, map[string]any{
174+
"name": exception.Name,
175+
"comps": comps,
176+
"values": string(valuesData),
177+
"fields": fields,
178+
})
179+
}
180+
_ = d.Set("exceptions", exceptions)
150181
return nil
151182
}
152183

184+
func fieldOrCompsToStringSlice(fields any) ([]string, error) {
185+
elements := []string{}
186+
switch t := fields.(type) {
187+
case []interface{}:
188+
for _, field := range t {
189+
elements = append(elements, field.(string))
190+
}
191+
case string:
192+
elements = append(elements, t)
193+
default:
194+
return nil, fmt.Errorf("unexpected type: %T", t)
195+
}
196+
return elements, nil
197+
}
198+
153199
func resourceSysdigRuleFalcoUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
154200
client, err := meta.(SysdigClients).sysdigSecureClient()
155201
if err != nil {

sysdig/resource_sysdig_secure_rule_falco_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,19 @@ func TestAccRuleFalco(t *testing.T) {
7070
{
7171
Config: ruleFalcoWithExceptions(randomText),
7272
},
73+
{
74+
ResourceName: "sysdig_secure_rule_falco.falco_rule_with_exceptions",
75+
ImportState: true,
76+
ImportStateVerify: true,
77+
},
7378
{
7479
Config: existingFalcoRuleWithExceptions(randomText),
7580
},
81+
{
82+
ResourceName: "sysdig_secure_rule_falco.attach_to_cluster_admin_role_exceptions",
83+
ImportState: true,
84+
ImportStateVerify: true,
85+
},
7686
},
7787
})
7888
}
@@ -156,7 +166,7 @@ resource "sysdig_secure_rule_falco" "terminal_shell_append" {
156166

157167
func ruleFalcoWithExceptions(name string) string {
158168
return fmt.Sprintf(`
159-
resource "sysdig_secure_rule_falco" "attach_to_cluster_admin_role" {
169+
resource "sysdig_secure_rule_falco" "falco_rule_with_exceptions" {
160170
name = "TERRAFORM TEST %s - Attach to cluster-admin Role"
161171
condition = "kevt and clusterrolebinding and kcreate and ka.req.binding.role=cluster-admin"
162172
description = "Detect any attempt to create a ClusterRoleBinding to the cluster-admin user"

0 commit comments

Comments
 (0)