Skip to content

Commit 4a18a3e

Browse files
authored
feat: Allow minimal configuration while appending to Falco rule (#55)
* feat: Allow minimal configuration while appending to Falco rule * docs: Update documentation for rules
1 parent 5949ebe commit 4a18a3e

10 files changed

+96
-39
lines changed

sysdig/resource_sysdig_secure_rule.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ func createRuleSchema(original map[string]*schema.Schema) map[string]*schema.Sch
1616
},
1717
"description": {
1818
Type: schema.TypeString,
19-
Required: true,
19+
Optional: true,
20+
Default: "",
2021
},
2122
"tags": {
2223
Type: schema.TypeList,

sysdig/resource_sysdig_secure_rule_falco.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package sysdig
22

33
import (
44
"context"
5-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
6-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
5+
"errors"
76
"strconv"
87
"strings"
98
"time"
109

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213

1314
"github.com/draios/terraform-provider-sysdig/sysdig/secure"
1415
)
@@ -33,17 +34,20 @@ func resourceSysdigSecureRuleFalco() *schema.Resource {
3334
},
3435
"output": {
3536
Type: schema.TypeString,
36-
Required: true,
37+
Optional: true,
38+
Default: "",
3739
},
3840
"priority": {
39-
Type: schema.TypeString,
40-
Required: true,
41-
ValidateFunc: validation.StringInSlice([]string{"emergency", "alert", "critical", "error", "warning", "notice", "informational", "informational", "debug"}, false),
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Default: "warning",
44+
ValidateDiagFunc: validateDiagFunc(validation.StringInSlice([]string{"emergency", "alert", "critical", "error", "warning", "notice", "informational", "debug"}, false)),
4245
},
4346
"source": {
44-
Type: schema.TypeString,
45-
Required: true,
46-
ValidateFunc: validation.StringInSlice([]string{"syscall", "k8s_audit"}, false),
47+
Type: schema.TypeString,
48+
Optional: true,
49+
Default: "",
50+
ValidateDiagFunc: validateDiagFunc(validation.StringInSlice([]string{"syscall", "k8s_audit"}, false)),
4751
},
4852
"append": {
4953
Type: schema.TypeBool,
@@ -60,7 +64,10 @@ func resourceSysdigRuleFalcoCreate(ctx context.Context, d *schema.ResourceData,
6064
return diag.FromErr(err)
6165
}
6266

63-
rule := resourceSysdigRuleFalcoFromResourceData(d)
67+
rule, err := resourceSysdigRuleFalcoFromResourceData(d)
68+
if err != nil {
69+
return diag.FromErr(err)
70+
}
6471

6572
rule, err = client.CreateRule(ctx, rule)
6673
if err != nil {
@@ -113,7 +120,10 @@ func resourceSysdigRuleFalcoUpdate(ctx context.Context, d *schema.ResourceData,
113120
return diag.FromErr(err)
114121
}
115122

116-
rule := resourceSysdigRuleFalcoFromResourceData(d)
123+
rule, err := resourceSysdigRuleFalcoFromResourceData(d)
124+
if err != nil {
125+
return diag.FromErr(err)
126+
}
117127

118128
rule.Version = d.Get("version").(int)
119129
rule.ID, _ = strconv.Atoi(d.Id())
@@ -144,22 +154,38 @@ func resourceSysdigRuleFalcoDelete(ctx context.Context, d *schema.ResourceData,
144154
return nil
145155
}
146156

147-
func resourceSysdigRuleFalcoFromResourceData(d *schema.ResourceData) secure.Rule {
157+
func resourceSysdigRuleFalcoFromResourceData(d *schema.ResourceData) (secure.Rule, error) {
148158
rule := ruleFromResourceData(d)
149159
rule.Details.RuleType = "FALCO"
150160

151-
rule.Details.Source = d.Get("source").(string)
152-
rule.Details.Output = d.Get("output").(string)
153-
rule.Details.Priority = d.Get("priority").(string)
161+
appendMode, appendModeIsSet := d.GetOk("append")
162+
if appendModeIsSet {
163+
ptr := appendMode.(bool)
164+
rule.Details.Append = &ptr
165+
}
166+
167+
if source, ok := d.GetOk("source"); ok && source.(string) != "" {
168+
rule.Details.Source = source.(string)
169+
} else if !appendModeIsSet || !(appendMode.(bool)) {
170+
return secure.Rule{}, errors.New("source must be set when append = false")
171+
}
172+
173+
if output, ok := d.GetOk("output"); ok && output.(string) != "" {
174+
rule.Details.Output = output.(string)
175+
} else if !appendModeIsSet || !(appendMode.(bool)) {
176+
return secure.Rule{}, errors.New("output must be set when append = false")
177+
}
178+
179+
if priority, ok := d.GetOk("priority"); ok && priority.(string) != "" {
180+
rule.Details.Priority = priority.(string)
181+
} else if !appendModeIsSet || !(appendMode.(bool)) {
182+
return secure.Rule{}, errors.New("priority must be set when append = false")
183+
}
184+
154185
rule.Details.Condition = &secure.Condition{
155186
Condition: d.Get("condition").(string),
156187
Components: []interface{}{},
157188
}
158189

159-
if appendMode, ok := d.GetOk("append"); ok {
160-
ptr := appendMode.(bool)
161-
rule.Details.Append = &ptr
162-
}
163-
164-
return rule
190+
return rule, nil
165191
}

sysdig/resource_sysdig_secure_rule_falco_test.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sysdig_test
33
import (
44
"fmt"
55
"os"
6+
"regexp"
67
"testing"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -41,6 +42,15 @@ func TestAccRuleFalco(t *testing.T) {
4142
{
4243
Config: ruleFalcoKubeAudit(rText()),
4344
},
45+
// Incorrect configurations
46+
{
47+
Config: ruleFalcoTerminalShellWithMissingOuput(rText()),
48+
ExpectError: regexp.MustCompile("output must be set when append = false"),
49+
},
50+
{
51+
Config: ruleFalcoTerminalShellWithMissingSource(rText()),
52+
ExpectError: regexp.MustCompile("source must be set when append = false"),
53+
},
4454
},
4555
})
4656
}
@@ -49,13 +59,39 @@ func ruleFalcoTerminalShell(name string) string {
4959
return fmt.Sprintf(`
5060
resource "sysdig_secure_rule_falco" "terminal_shell" {
5161
name = "TERRAFORM TEST %s - Terminal Shell"
52-
description = "TERRAFORM TEST %s"
5362
tags = ["container", "shell", "mitre_execution"]
5463
5564
condition = "spawned_process and container and shell_procs and proc.tty != 0 and container_entrypoint"
5665
output = "A shell was spawned in a container with an attached terminal (user=%%user.name %%container.info shell=%%proc.name parent=%%proc.pname cmdline=%%proc.cmdline terminal=%%proc.tty container_id=%%container.id image=%%container.image.repository)"
5766
priority = "notice"
5867
source = "syscall" // syscall or k8s_audit
68+
}`, name)
69+
}
70+
71+
func ruleFalcoTerminalShellWithMissingOuput(name string) string {
72+
return fmt.Sprintf(`
73+
resource "sysdig_secure_rule_falco" "terminal_shell" {
74+
name = "TERRAFORM TEST %s - Terminal Shell"
75+
description = "TERRAFORM TEST %s"
76+
tags = ["container", "shell", "mitre_execution"]
77+
78+
condition = "spawned_process and container and shell_procs and proc.tty != 0 and container_entrypoint"
79+
priority = "notice"
80+
source = "syscall" // syscall or k8s_audit
81+
}`, name, name)
82+
}
83+
84+
func ruleFalcoTerminalShellWithMissingSource(name string) string {
85+
return fmt.Sprintf(`
86+
resource "sysdig_secure_rule_falco" "terminal_shell" {
87+
name = "TERRAFORM TEST %s - Terminal Shell"
88+
description = "TERRAFORM TEST %s"
89+
tags = ["container", "shell", "mitre_execution"]
90+
91+
condition = "spawned_process and container and shell_procs and proc.tty != 0 and container_entrypoint"
92+
output = "A shell was spawned in a container with an attached terminal (user=%%user.name %%container.info shell=%%proc.name parent=%%proc.pname cmdline=%%proc.cmdline terminal=%%proc.tty container_id=%%container.id image=%%container.image.repository)"
93+
priority = "notice"
94+
append = false
5995
}`, name, name)
6096
}
6197

@@ -91,13 +127,7 @@ func ruleFalcoTerminalShellWithAppend() string {
91127
return fmt.Sprintf(`
92128
resource "sysdig_secure_rule_falco" "terminal_shell_append" {
93129
name = "Terminal shell in container" # Sysdig-provided
94-
description = ""
95-
tags = ["shell", "mitre_execution"]
96-
97130
condition = "and spawned_process and shell_procs and proc.tty != 0 and container_entrypoint"
98-
output = "A shell was spawned in a container with an attached terminal (user=%%user.name %%container.info shell=%%proc.name parent=%%proc.pname cmdline=%%proc.cmdline terminal=%%proc.tty container_id=%%container.id image=%%container.image.repository)"
99-
priority = "notice"
100-
source = "syscall" // syscall or k8s_audit
101131
append = true
102132
}`)
103133
}

sysdig/secure/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ type Details struct {
150150
// Falco
151151
Append *bool `json:"append,omitempty"`
152152
Source string `json:"source,omitempty"`
153-
Output string `json:"output,omitempty"`
153+
Output string `json:"output"`
154154
Condition *Condition `json:"condition,omitempty"`
155155
Priority string `json:"priority,omitempty"`
156156

website/docs/r/sysdig_secure_rule_container.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "sysdig_secure_rule_container" "sample" {
2828
## Argument Reference
2929

3030
* `name` - (Required) The name of the Secure rule. It must be unique.
31-
* `description` - (Required) The description of Secure rule.
31+
* `description` - (Optional) The description of Secure rule. By default is empty.
3232
* `tags` - (Optional) A list of tags for this rule.
3333

3434
### Matching

website/docs/r/sysdig_secure_rule_falco.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ resource "sysdig_secure_rule_falco" "example" {
3333
The following arguments are supported:
3434

3535
* `name` - (Required) The name of the Secure rule. It must be unique.
36-
* `description` - (Required) The description of Secure rule.
36+
* `description` - (Optional) The description of Secure rule. By default is empty.
3737
* `tags` - (Optional) A list of tags for this rule.
3838

3939
- - -
4040

4141
### Conditions
4242

4343
* `condition` - (Required) A [Falco condition](https://falco.org/docs/rules/) is simply a Boolean predicate on Sysdig events expressed using the Sysdig [filter syntax](http://www.sysdig.org/wiki/sysdig-user-guide/#filtering) and macro terms.
44-
* `output` - (Required) Add additional information to each Falco notification's output.
45-
* `priority` - (Required) The priority of the Falco rule. It can be: "emergency", "alert", "critical", "error", "warning", "notice", "informational", "informational" or "debug".
46-
* `source` - (Required) The source of the event. It can be either "syscall" or "k8s_audit".
44+
* `output` - (Optional) Add additional information to each Falco notification's output. Required if append is false.
45+
* `priority` - (Optional) The priority of the Falco rule. It can be: "emergency", "alert", "critical", "error", "warning", "notice", "informational", "informational" or "debug". By default is "warning".
46+
* `source` - (Optional) The source of the event. It can be either "syscall" or "k8s_audit". Required if append is false.
4747
* `append` - (Optional) This indicates that the rule being created appends the condition to an existing Sysdig-provided rule. By default this is false. Appending to user-created rules is not supported by the API.
4848

4949
## Attributes Reference

website/docs/r/sysdig_secure_rule_filesystem.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ resource "sysdig_secure_rule_filesystem" "example" {
3838
The following arguments are supported:
3939

4040
* `name` - (Required) The name of the Secure rule. It must be unique.
41-
* `description` - (Required) The description of Secure rule.
41+
* `description` - (Optional) The description of Secure rule. By default is empty.
4242
* `tags` - (Optional) A list of tags for this rule.
4343

4444
### Read Only

website/docs/r/sysdig_secure_rule_network.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ resource "sysdig_secure_rule_network" "example" {
4141
The following arguments are supported:
4242

4343
* `name` - (Required) The name of the Secure rule. It must be unique.
44-
* `description` - (Required) The description of Secure rule.
44+
* `description` - (Optional) The description of Secure rule. By default is empty.
4545
* `tags` - (Optional) A list of tags for this rule.
4646

4747
### Disallow incoming or outgoing connections

website/docs/r/sysdig_secure_rule_process.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "sysdig_secure_rule_process" "sample" {
2828
## Argument Reference
2929

3030
* `name` - (Required) The name of the Secure rule. It must be unique.
31-
* `description` - (Required) The description of Secure rule.
31+
* `description` - (Optional) The description of Secure rule. By default is empty.
3232
* `tags` - (Optional) A list of tags for this rule.
3333

3434
### Matching

website/docs/r/sysdig_secure_rule_syscall.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ resource "sysdig_secure_rule_syscall" "foo" {
2727
## Argument Reference
2828

2929
* `name` - (Required) The name of the Secure rule. It must be unique.
30-
* `description` - (Required) The description of Secure rule.
30+
* `description` - (Optional) The description of Secure rule. By default is empty.
3131
* `tags` - (Optional) A list of tags for this rule.
3232

3333
### Matching

0 commit comments

Comments
 (0)