Skip to content

Commit c748066

Browse files
author
Ben Lucas
authored
feat(policies): Resource for Secure managed ruleset (#326)
* initial implementation of sysdig_secure_managed_ruleset * fix issue with saving disabled rules on create * refactor to improve code reuse * add tests for sysdig_secure_managed_ruleset * add documentation for sysdig_secure_managed_ruleset * fix import so that it correctly associates the inherited_from property on import
1 parent 453bff8 commit c748066

7 files changed

+645
-158
lines changed

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func Provider() *schema.Provider {
7878
"sysdig_group_mapping": resourceSysdigGroupMapping(),
7979

8080
"sysdig_secure_managed_policy": resourceSysdigSecureManagedPolicy(),
81+
"sysdig_secure_managed_ruleset": resourceSysdigSecureManagedRuleset(),
8182
"sysdig_secure_policy": resourceSysdigSecurePolicy(),
8283
"sysdig_secure_notification_channel_email": resourceSysdigSecureNotificationChannelEmail(),
8384
"sysdig_secure_notification_channel_sns": resourceSysdigSecureNotificationChannelSNS(),

sysdig/resource_sysdig_secure_managed_policy.go

Lines changed: 34 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package sysdig
22

33
import (
44
"context"
5+
"errors"
56
"net/http"
67
"strconv"
7-
"strings"
88
"time"
99

1010
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
@@ -27,51 +27,21 @@ func resourceSysdigSecureManagedPolicy() *schema.Resource {
2727
Read: schema.DefaultTimeout(timeout),
2828
},
2929

30-
Schema: map[string]*schema.Schema{
31-
"name": {
32-
Type: schema.TypeString,
33-
Required: true,
34-
},
30+
Schema: createPolicySchema(map[string]*schema.Schema{
3531
"type": {
3632
Type: schema.TypeString,
3733
Optional: true,
3834
Default: "falco",
3935
ValidateDiagFunc: validateDiagFunc(validatePolicyType),
4036
},
41-
"enabled": {
42-
Type: schema.TypeBool,
43-
Optional: true,
44-
Default: true,
45-
},
4637
"disabled_rules": {
4738
Type: schema.TypeSet,
4839
Optional: true,
4940
Elem: &schema.Schema{
5041
Type: schema.TypeString,
5142
},
5243
},
53-
"scope": {
54-
Type: schema.TypeString,
55-
Optional: true,
56-
Default: "",
57-
},
58-
"version": {
59-
Type: schema.TypeInt,
60-
Computed: true,
61-
},
62-
"notification_channels": {
63-
Type: schema.TypeSet,
64-
Optional: true,
65-
Elem: &schema.Schema{
66-
Type: schema.TypeInt,
67-
},
68-
},
69-
"runbook": {
70-
Type: schema.TypeString,
71-
Optional: true,
72-
},
73-
"actions": policyActionBlockSchema,
74-
},
44+
}),
7545
}
7646
}
7747

@@ -81,78 +51,34 @@ func resourceSysdigManagedPolicyCreate(ctx context.Context, d *schema.ResourceDa
8151
return diag.FromErr(err)
8252
}
8353

84-
policies, _, err := client.GetPolicies(ctx)
85-
if err != nil {
86-
return diag.FromErr(err)
87-
}
88-
8954
policyName := d.Get("name").(string)
9055
policyType := d.Get("type").(string)
91-
var policy v2.Policy
92-
for _, existingPolicy := range policies {
93-
if existingPolicy.Name == policyName && existingPolicy.Type == policyType {
94-
if !existingPolicy.IsDefault {
95-
return diag.Errorf("policy is not a managed policy - use `resource_sysdig_secure_policy`")
96-
}
97-
policy = existingPolicy
98-
}
99-
}
10056

101-
if policy.ID == 0 {
102-
return diag.Errorf("unable to find managed policy")
57+
policy, err := getManagedPolicy(ctx, client, policyName, policyType)
58+
if err != nil {
59+
return diag.FromErr(err)
10360
}
10461

105-
updateManagedPolicyFromResourceData(&policy, d)
62+
updateManagedPolicyFromResourceData(policy, d)
10663

107-
policy, err = client.UpdatePolicy(ctx, policy)
64+
updatedPolicy, err := client.UpdatePolicy(ctx, *policy)
10865
if err != nil {
10966
return diag.FromErr(err)
11067
}
11168

112-
managedPolicyToResourceData(&policy, d)
69+
managedPolicyToResourceData(&updatedPolicy, d)
11370

11471
return nil
11572
}
11673

11774
func managedPolicyToResourceData(policy *v2.Policy, d *schema.ResourceData) {
118-
if policy.ID != 0 {
119-
d.SetId(strconv.Itoa(policy.ID))
120-
}
75+
commonPolicyToResourceData(policy, d)
12176

122-
_ = d.Set("name", policy.Name)
12377
if policy.Type != "" {
12478
_ = d.Set("type", policy.Type)
12579
} else {
12680
_ = d.Set("type", "falco")
12781
}
128-
_ = d.Set("enabled", policy.Enabled)
129-
_ = d.Set("scope", policy.Scope)
130-
_ = d.Set("version", policy.Version)
131-
_ = d.Set("notification_channels", policy.NotificationChannelIds)
132-
_ = d.Set("runbook", policy.Runbook)
133-
134-
actions := []map[string]interface{}{{}}
135-
for _, action := range policy.Actions {
136-
if action.Type != "POLICY_ACTION_CAPTURE" {
137-
action := strings.Replace(action.Type, "POLICY_ACTION_", "", 1)
138-
actions[0]["container"] = strings.ToLower(action)
139-
//d.Set("actions.0.container", strings.ToLower(action))
140-
} else {
141-
actions[0]["capture"] = []map[string]interface{}{{
142-
"seconds_after_event": action.AfterEventNs / 1000000000,
143-
"seconds_before_event": action.BeforeEventNs / 1000000000,
144-
"name": action.Name,
145-
}}
146-
}
147-
}
148-
149-
currentContainerAction := d.Get("actions.0.container").(string)
150-
currentCaptureAction := d.Get("actions.0.capture").([]interface{})
151-
// If the policy retrieved from service has no actions and the current state is default values,
152-
// then do not set the "actions" key as it may cause terraform to think there has been a state change
153-
if len(policy.Actions) > 0 || currentContainerAction != "" || len(currentCaptureAction) > 0 {
154-
_ = d.Set("actions", actions)
155-
}
15682

15783
disabledRules := []string{}
15884
for _, rule := range policy.Rules {
@@ -164,11 +90,7 @@ func managedPolicyToResourceData(policy *v2.Policy, d *schema.ResourceData) {
16490
}
16591

16692
func updateManagedPolicyFromResourceData(policy *v2.Policy, d *schema.ResourceData) {
167-
policy.Enabled = d.Get("enabled").(bool)
168-
policy.Runbook = d.Get("runbook").(string)
169-
policy.Scope = d.Get("scope").(string)
170-
171-
addActionsToPolicy(d, policy)
93+
commonPolicyFromResourceData(policy, d)
17294

17395
disabledRules := d.Get("disabled_rules").(*schema.Set)
17496
for _, rule := range policy.Rules {
@@ -178,12 +100,6 @@ func updateManagedPolicyFromResourceData(policy *v2.Policy, d *schema.ResourceDa
178100
rule.Enabled = true
179101
}
180102
}
181-
182-
policy.NotificationChannelIds = []int{}
183-
notificationChannelIdSet := d.Get("notification_channels").(*schema.Set)
184-
for _, id := range notificationChannelIdSet.List() {
185-
policy.NotificationChannelIds = append(policy.NotificationChannelIds, id.(int))
186-
}
187103
}
188104

189105
func resourceSysdigManagedPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
@@ -267,3 +183,26 @@ func resourceSysdigManagedPolicyUpdate(ctx context.Context, d *schema.ResourceDa
267183
}
268184
return nil
269185
}
186+
187+
func getManagedPolicy(ctx context.Context, client v2.PolicyInterface, policyName string, policyType string) (*v2.Policy, error) {
188+
policies, _, err := client.GetPolicies(ctx)
189+
if err != nil {
190+
return nil, err
191+
}
192+
193+
var policy v2.Policy
194+
for _, existingPolicy := range policies {
195+
if existingPolicy.Name == policyName && existingPolicy.Type == policyType {
196+
if !existingPolicy.IsDefault {
197+
return nil, errors.New("policy is not a managed policy")
198+
}
199+
policy = existingPolicy
200+
}
201+
}
202+
203+
if policy.ID != 0 {
204+
return &policy, nil
205+
}
206+
207+
return nil, errors.New("unable to find managed policy")
208+
}

sysdig/resource_sysdig_secure_managed_policy_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ func TestAccManagedPolicy(t *testing.T) {
4242
Config: managedPolicyWithMinimumConfiguration(),
4343
},
4444
{
45-
Config: managedPolicyWithKillAction(),
45+
Config: managedPolicyWithKillAction(),
46+
Destroy: true,
4647
},
4748
},
4849
})

0 commit comments

Comments
 (0)