Skip to content

Commit 56a3d29

Browse files
author
Ben Lucas
authored
feat(policies): data source for custom policies (#342)
* implementation and tests for new data source sysdig_secure_custom_policy * add documentation for data source sysdig_secure_custom_policy
1 parent 714e968 commit 56a3d29

7 files changed

+202
-57
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceSysdigSecureCustomPolicy() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigSecureCustomPolicyRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createPolicyDataSourceSchema(),
23+
}
24+
}
25+
26+
func dataSourceSysdigSecureCustomPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
27+
return commonDataSourceSecurePolicyRead(ctx, d, meta, "custom policy", isCustomPolicy)
28+
}
29+
30+
func isCustomPolicy(policy v2.Policy) bool {
31+
return !policy.IsDefault && policy.TemplateId == 0
32+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//go:build tf_acc_sysdig || tf_acc_sysdig_secure || tf_acc_policies
2+
3+
package sysdig_test
4+
5+
import (
6+
"os"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
13+
"github.com/draios/terraform-provider-sysdig/sysdig"
14+
)
15+
16+
func TestAccCustomPolicyDataSource(t *testing.T) {
17+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() {
21+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
22+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
23+
}
24+
},
25+
ProviderFactories: map[string]func() (*schema.Provider, error){
26+
"sysdig": func() (*schema.Provider, error) {
27+
return sysdig.Provider(), nil
28+
},
29+
},
30+
Steps: []resource.TestStep{
31+
{
32+
Config: customPolicyDataSource(rText),
33+
},
34+
},
35+
})
36+
}
37+
38+
func customPolicyDataSource(name string) string {
39+
return `
40+
resource "sysdig_secure_custom_policy" "sample" {
41+
name = "%s"
42+
description = "Test Description"
43+
enabled = true
44+
}
45+
46+
data "sysdig_secure_custom_policy" "example" {
47+
name = "%s"
48+
depends_on=[ sysdig_secure_custom_policy.sample ]
49+
}
50+
`
51+
}

sysdig/data_source_sysdig_secure_managed_policy.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"time"
66

7+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
)
@@ -12,7 +13,7 @@ func dataSourceSysdigSecureManagedPolicy() *schema.Resource {
1213
timeout := 5 * time.Minute
1314

1415
return &schema.Resource{
15-
ReadContext: dataSourceSysdigManagedPolicyRead,
16+
ReadContext: dataSourceSysdigSecureManagedPolicyRead,
1617

1718
Timeouts: &schema.ResourceTimeout{
1819
Read: schema.DefaultTimeout(timeout),
@@ -22,26 +23,10 @@ func dataSourceSysdigSecureManagedPolicy() *schema.Resource {
2223
}
2324
}
2425

25-
func dataSourceSysdigManagedPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
26-
client, err := getSecurePolicyClient(meta.(SysdigClients))
27-
if err != nil {
28-
return diag.FromErr(err)
29-
}
30-
31-
policyName := d.Get("name").(string)
32-
policyType := d.Get("type").(string)
33-
34-
policy, err := getManagedPolicy(ctx, client, policyName, policyType)
35-
if err != nil {
36-
return diag.FromErr(err)
37-
}
38-
39-
loadedPolicy, _, err := client.GetPolicyByID(ctx, policy.ID)
40-
if err != nil {
41-
return diag.FromErr(err)
42-
}
43-
44-
policyDataSourceToResourceData(loadedPolicy, d)
26+
func dataSourceSysdigSecureManagedPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
27+
return commonDataSourceSecurePolicyRead(ctx, d, meta, "managed policy", isManagedPolicy)
28+
}
4529

46-
return nil
30+
func isManagedPolicy(policy v2.Policy) bool {
31+
return policy.IsDefault
4732
}

sysdig/data_source_sysdig_secure_managed_ruleset.go

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,9 @@ func dataSourceSysdigSecureManagedRuleset() *schema.Resource {
2424
}
2525

2626
func dataSourceSysdigManagedRulesetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
27-
client, err := getSecurePolicyClient(meta.(SysdigClients))
28-
if err != nil {
29-
return diag.FromErr(err)
30-
}
31-
32-
policyName := d.Get("name").(string)
33-
policyType := d.Get("type").(string)
34-
35-
policies, _, err := client.GetPolicies(ctx)
36-
if err != nil {
37-
return diag.FromErr(err)
38-
}
39-
40-
var policy v2.Policy
41-
for _, existingPolicy := range policies {
42-
if existingPolicy.Name == policyName && existingPolicy.Type == policyType {
43-
if existingPolicy.IsDefault || existingPolicy.TemplateId == 0 {
44-
return diag.Errorf("policy is not a managed ruleset")
45-
}
46-
policy = existingPolicy
47-
break
48-
}
49-
}
50-
51-
if policy.ID == 0 {
52-
return diag.Errorf("unable to find managed ruleset")
53-
}
54-
55-
loadedPolicy, _, err := client.GetPolicyByID(ctx, policy.ID)
56-
if err != nil {
57-
return diag.FromErr(err)
58-
}
59-
60-
policyDataSourceToResourceData(loadedPolicy, d)
27+
return commonDataSourceSecurePolicyRead(ctx, d, meta, "managed ruleset", isManagedRuleset)
28+
}
6129

62-
return nil
30+
func isManagedRuleset(policy v2.Policy) bool {
31+
return !policy.IsDefault && policy.TemplateId != 0
6332
}

sysdig/data_source_sysdig_secure_policy.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package sysdig
22

33
import (
4+
"context"
45
"strconv"
56
"strings"
67

78
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
911
)
1012

@@ -149,3 +151,42 @@ func policyDataSourceToResourceData(policy v2.Policy, d *schema.ResourceData) {
149151

150152
_ = d.Set("rules", rules)
151153
}
154+
155+
func commonDataSourceSecurePolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}, resourceName string, isPolicyCorrectType func(v2.Policy) bool) diag.Diagnostics {
156+
client, err := getSecurePolicyClient(meta.(SysdigClients))
157+
if err != nil {
158+
return diag.FromErr(err)
159+
}
160+
161+
policyName := d.Get("name").(string)
162+
policyType := d.Get("type").(string)
163+
164+
policies, _, err := client.GetPolicies(ctx)
165+
if err != nil {
166+
return diag.FromErr(err)
167+
}
168+
169+
var policy v2.Policy
170+
for _, existingPolicy := range policies {
171+
if existingPolicy.Name == policyName && existingPolicy.Type == policyType {
172+
if !isPolicyCorrectType(existingPolicy) {
173+
return diag.Errorf("policy is not a %s", resourceName)
174+
}
175+
policy = existingPolicy
176+
break
177+
}
178+
}
179+
180+
if policy.ID == 0 {
181+
return diag.Errorf("unable to find %s", resourceName)
182+
}
183+
184+
loadedPolicy, _, err := client.GetPolicyByID(ctx, policy.ID)
185+
if err != nil {
186+
return diag.FromErr(err)
187+
}
188+
189+
policyDataSourceToResourceData(loadedPolicy, d)
190+
191+
return nil
192+
}

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func Provider() *schema.Provider {
127127
DataSourcesMap: map[string]*schema.Resource{
128128
"sysdig_secure_trusted_cloud_identity": dataSourceSysdigSecureTrustedCloudIdentity(),
129129
"sysdig_secure_notification_channel": dataSourceSysdigSecureNotificationChannel(),
130+
"sysdig_secure_custom_policy": dataSourceSysdigSecureCustomPolicy(),
130131
"sysdig_secure_managed_policy": dataSourceSysdigSecureManagedPolicy(),
131132
"sysdig_secure_managed_ruleset": dataSourceSysdigSecureManagedRuleset(),
132133
"sysdig_secure_rule_container": dataSourceSysdigSecureRuleContainer(),
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
subcategory: "Sysdig Secure"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_secure_custom_policy"
5+
description: |-
6+
Retrieves a Sysdig Secure Custom Policy.
7+
---
8+
9+
# Data Source: sysdig_secure_custom_policy
10+
11+
Retrieves the information of an existing Sysdig Secure Custom Policy.
12+
13+
-> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.
14+
15+
## Example Usage
16+
17+
```terraform
18+
data "sysdig_secure_custom_policy" "example" {
19+
name = "Write apt database"
20+
type = "falco"
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
* `name` - (Required) The name of the Secure custom policy.
27+
28+
* `type` - (Optional) Specifies the type of the runtime policy. Must be one of: `falco`, `list_matching`, `k8s_audit`,
29+
`aws_cloudtrail`, `gcp_auditlog`, `azure_platformlogs`. By default it is `falco`.
30+
31+
## Attributes Reference
32+
33+
In addition to all arguments above, the following attributes are exported:
34+
35+
* `id` - The id for the custom policy.
36+
37+
* `description` - The description for the custom policy.
38+
39+
* `severity` - The severity of Secure policy. The accepted values
40+
are: 0, 1, 2, 3 (High), 4, 5 (Medium), 6 (Low) and 7 (Info).
41+
42+
* `enabled` - Whether the policy is enabled or not.
43+
44+
* `runbook` - Customer provided url that provides a runbook for a given policy.
45+
46+
* `scope` - The application scope for the policy.
47+
48+
* `rules` - An array of rules with the properties `name` and `enabled` to identify the rule name and whether it is enabled.
49+
50+
* `notification_channels` - IDs of the notification channels to send alerts to
51+
when the policy is fired.
52+
53+
### Actions block
54+
55+
The actions block is optional and supports:
56+
57+
* `container` - (Optional) The action applied to container when this Policy is
58+
triggered. Can be *stop*, *pause* or *kill*. If this is not specified,
59+
no action will be applied at the container level.
60+
61+
* `capture` - (Optional) Captures with Sysdig the stream of system calls:
62+
* `seconds_before_event` - (Required) Captures the system calls during the
63+
amount of seconds before the policy was triggered.
64+
* `seconds_after_event` - (Required) Captures the system calls for the amount
65+
of seconds after the policy was triggered.
66+
* `name` - (Optional) The name of the capture file

0 commit comments

Comments
 (0)