Skip to content

Commit d5675c3

Browse files
author
Ben Lucas
authored
feat(policies): data source for managed ruleset (#334)
* implement data source for managed ruleset * add documentation for managed rulesets. ensure tests run with policy tests. fix documentation for managed policy.
1 parent 730cbba commit d5675c3

7 files changed

+189
-3
lines changed

sysdig/data_source_sysdig_secure_managed_policy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build tf_acc_sysdig || tf_acc_sysdig_secure
1+
//go:build tf_acc_sysdig || tf_acc_sysdig_secure || tf_acc_policies
22

33
package sysdig_test
44

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 dataSourceSysdigSecureManagedRuleset() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigManagedRulesetRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createPolicyDataSourceSchema(),
23+
}
24+
}
25+
26+
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)
61+
62+
return nil
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//go:build tf_acc_sysdig || tf_acc_sysdig_secure || tf_acc_policies
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
14+
"github.com/draios/terraform-provider-sysdig/sysdig"
15+
)
16+
17+
func TestAccManagedRulesetDataSource(t *testing.T) {
18+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
19+
20+
resource.ParallelTest(t, resource.TestCase{
21+
PreCheck: func() {
22+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
23+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
24+
}
25+
},
26+
ProviderFactories: map[string]func() (*schema.Provider, error){
27+
"sysdig": func() (*schema.Provider, error) {
28+
return sysdig.Provider(), nil
29+
},
30+
},
31+
Steps: []resource.TestStep{
32+
{
33+
Config: managedRulesetDataSource(rText),
34+
},
35+
},
36+
})
37+
}
38+
39+
func managedRulesetDataSource(name string) string {
40+
return fmt.Sprintf(`
41+
resource "sysdig_secure_managed_ruleset" "sample" {
42+
name = "%s"
43+
description = "Test Description"
44+
inherited_from {
45+
name = "Sysdig Runtime Threat Detection"
46+
type = "falco"
47+
}
48+
enabled = true
49+
}
50+
51+
data "sysdig_secure_managed_ruleset" "example" {
52+
depends_on = [sysdig_secure_managed_ruleset.sample]
53+
name = "%s"
54+
type = "falco"
55+
}
56+
`, name, name)
57+
}

sysdig/data_source_sysdig_secure_policy.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ func policyDataSourceToResourceData(policy v2.Policy, d *schema.ResourceData) {
127127
if action.Type != "POLICY_ACTION_CAPTURE" {
128128
action := strings.Replace(action.Type, "POLICY_ACTION_", "", 1)
129129
actions[0]["container"] = strings.ToLower(action)
130-
//d.Set("actions.0.container", strings.ToLower(action))
131130
} else {
132131
actions[0]["capture"] = []map[string]interface{}{{
133132
"seconds_after_event": action.AfterEventNs / 1000000000,

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
"sysdig_secure_trusted_cloud_identity": dataSourceSysdigSecureTrustedCloudIdentity(),
128128
"sysdig_secure_notification_channel": dataSourceSysdigSecureNotificationChannel(),
129129
"sysdig_secure_managed_policy": dataSourceSysdigSecureManagedPolicy(),
130+
"sysdig_secure_managed_ruleset": dataSourceSysdigSecureManagedRuleset(),
130131

131132
"sysdig_current_user": dataSourceSysdigCurrentUser(),
132133
"sysdig_user": dataSourceSysdigUser(),

website/docs/d/secure_managed_policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Retrieves the information of an existing Sysdig Secure Managed Policy.
1515
## Example Usage
1616

1717
```terraform
18-
data "sysdig_secure_notification_channel" "sample-email" {
18+
data "sysdig_secure_managed_policy" "example" {
1919
name = "Sysdig Runtime Threat Detection"
2020
type = "falco"
2121
}
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_managed_ruleset"
5+
description: |-
6+
Retrieves a Sysdig Secure Managed Ruleset.
7+
---
8+
9+
# sysdig_secure_managed_ruleset
10+
11+
Retrieves the information of an existing Sysdig Secure Managed Ruleset.
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_managed_ruleset" "example" {
19+
name = "Sysdig Runtime Threat Detection - Managed Ruleset"
20+
type = "falco"
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
* `name` - (Required) The name of the Secure managed ruleset.
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 managed policy.
36+
37+
* `description` - The description for the managed 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)