Skip to content

Commit aeee262

Browse files
authored
stateful rules: fix docs and ci (#611)
1 parent bbaf485 commit aeee262

8 files changed

+350
-8
lines changed

.github/workflows/ci-provider-docs.yaml

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v2
18-
- uses: actions/cache@v2
18+
- uses: actions/cache@v4
1919
continue-on-error: true
2020
id: cache-terraform-plugin-dir
2121
timeout-minutes: 2
@@ -34,12 +34,12 @@ jobs:
3434
run: |
3535
echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_ENV
3636
- if: steps.cache-terraform-plugin-dir.outputs.cache-hit != 'true' || steps.cache-terraform-plugin-dir.outcome == 'failure'
37-
uses: actions/cache@v2
37+
uses: actions/cache@v4
3838
with:
3939
path: ${{ env.GOCACHE }}
4040
key: ${{ runner.os }}-GOCACHE-${{ hashFiles('go.sum') }}-${{ hashFiles('sysdig/**') }}
4141
- if: steps.cache-terraform-plugin-dir.outputs.cache-hit != 'true' || steps.cache-terraform-plugin-dir.outcome == 'failure'
42-
uses: actions/cache@v2
42+
uses: actions/cache@v4
4343
with:
4444
path: ~/go/pkg/mod
4545
key: ${{ runner.os }}-go-pkg-mod-${{ hashFiles('go.sum') }}
@@ -53,15 +53,15 @@ jobs:
5353
runs-on: ubuntu-latest
5454
steps:
5555
- uses: actions/checkout@v2
56-
- uses: actions/cache@v2
56+
- uses: actions/cache@v4
5757
continue-on-error: true
5858
id: cache-terraform-providers-schema
5959
timeout-minutes: 2
6060
with:
6161
path: terraform-providers-schema
6262
key: ${{ runner.os }}-terraform-providers-schema-${{ hashFiles('go.sum') }}-${{ hashFiles('sysdig/**') }}
6363
- if: steps.cache-terraform-providers-schema.outputs.cache-hit != 'true' || steps.cache-terraform-providers-schema.outcome == 'failure'
64-
uses: actions/cache@v2
64+
uses: actions/cache@v4
6565
timeout-minutes: 2
6666
with:
6767
path: terraform-plugin-dir
@@ -97,14 +97,14 @@ jobs:
9797
go-version: ${{ env.GO_VERSION }}
9898
check-latest: true
9999
cache: true
100-
- uses: actions/cache@v2
100+
- uses: actions/cache@v4
101101
continue-on-error: true
102102
timeout-minutes: 2
103103
with:
104104
path: ~/go/pkg/mod
105105
key: ${{ runner.os }}-go-pkg-mod-${{ hashFiles('go.sum') }}
106106
- run: cd /tmp && go install github.com/bflad/tfproviderdocs@latest
107-
- uses: actions/cache@v2
107+
- uses: actions/cache@v4
108108
timeout-minutes: 2
109109
with:
110110
path: terraform-providers-schema
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"strconv"
8+
"time"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
func dataSourceSysdigSecureRuleStateful() *schema.Resource {
15+
timeout := 1 * time.Minute
16+
17+
return &schema.Resource{
18+
ReadContext: dataSourceSysdigRuleStatefulRead,
19+
20+
Timeouts: &schema.ResourceTimeout{
21+
Read: schema.DefaultTimeout(timeout),
22+
},
23+
24+
Schema: map[string]*schema.Schema{
25+
"name": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
ForceNew: true,
29+
},
30+
"id": {
31+
Type: schema.TypeInt,
32+
Computed: true,
33+
},
34+
"version": {
35+
Type: schema.TypeInt,
36+
Computed: true,
37+
},
38+
"source": {
39+
Type: schema.TypeString,
40+
Required: true,
41+
ValidateDiagFunc: validateDiagFunc(validateStatefulRuleSource),
42+
},
43+
"ruletype": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
47+
"append": {
48+
Type: schema.TypeBool,
49+
Computed: true,
50+
},
51+
"exceptions": {
52+
Type: schema.TypeList,
53+
Computed: true,
54+
Elem: &schema.Resource{
55+
Schema: map[string]*schema.Schema{
56+
"name": {
57+
Type: schema.TypeString,
58+
Required: true,
59+
},
60+
"values": {
61+
Type: schema.TypeString,
62+
Required: true,
63+
},
64+
},
65+
},
66+
},
67+
},
68+
}
69+
}
70+
71+
func dataSourceSysdigRuleStatefulRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
72+
client, err := getSecureRuleClient(meta.(SysdigClients))
73+
if err != nil {
74+
return diag.FromErr(err)
75+
}
76+
77+
nameObj, ok := d.GetOk("name")
78+
if !ok {
79+
return diag.FromErr(errors.New("name is required"))
80+
}
81+
82+
name := nameObj.(string)
83+
84+
sourceObj, ok := d.GetOk("source")
85+
if !ok {
86+
return diag.FromErr(errors.New("source is required"))
87+
}
88+
89+
source := sourceObj.(string)
90+
91+
rules, err := client.GetStatefulRuleGroup(ctx, name, source)
92+
if err != nil {
93+
return diag.FromErr(err)
94+
}
95+
96+
ruleIndexObj, ok := d.GetOk("index")
97+
ruleIndex := 0
98+
if ok {
99+
ruleIndex, ok = ruleIndexObj.(int)
100+
if !ok {
101+
return diag.FromErr(errors.New("index must be an integer"))
102+
}
103+
}
104+
105+
rule := rules[ruleIndex]
106+
107+
if len(rules) == 0 {
108+
d.SetId("")
109+
} else {
110+
d.SetId(strconv.Itoa(rule.ID))
111+
}
112+
113+
_ = d.Set("name", rule.Name)
114+
_ = d.Set("source", source)
115+
116+
if rule.Details.Append != nil {
117+
_ = d.Set("append", *rule.Details.Append)
118+
}
119+
120+
exceptions := make([]any, 0, len(rule.Details.Exceptions))
121+
for _, exception := range rule.Details.Exceptions {
122+
if exception == nil {
123+
return diag.Errorf("exception is nil")
124+
}
125+
valuesData, err := json.Marshal(exception.Values)
126+
if err != nil {
127+
return diag.Errorf("error marshalling exception values '%+v': %s", exception.Values, err)
128+
}
129+
130+
exceptions = append(exceptions, map[string]any{
131+
"name": exception.Name,
132+
"values": string(valuesData),
133+
})
134+
}
135+
136+
if err := d.Set("exceptions", exceptions); err != nil {
137+
return diag.FromErr(err)
138+
}
139+
140+
return nil
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceSysdigSecureRuleStatefulCount() *schema.Resource {
13+
timeout := 1 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigRuleStatefulCountRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: map[string]*schema.Schema{
23+
"name": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
"source": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
ValidateDiagFunc: validateDiagFunc(validateStatefulRuleSource),
31+
},
32+
"rule_count": {
33+
Type: schema.TypeInt,
34+
Computed: true,
35+
},
36+
},
37+
}
38+
}
39+
40+
func dataSourceSysdigRuleStatefulCountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
41+
client, err := getSecureRuleClient(meta.(SysdigClients))
42+
if err != nil {
43+
return diag.FromErr(err)
44+
}
45+
46+
ruleName := d.Get("name").(string)
47+
ruleType := d.Get("source").(string)
48+
rules, err := client.GetStatefulRuleGroup(ctx, ruleName, ruleType)
49+
if err != nil {
50+
return diag.FromErr(err)
51+
}
52+
53+
d.SetId(fmt.Sprintf("count__%s__%s", ruleName, ruleType))
54+
_ = d.Set("name", ruleName)
55+
_ = d.Set("rule_count", len(rules))
56+
57+
return nil
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//go:build tf_acc_sysdig || tf_acc_sysdig_secure || tf_acc_policies || tf_acc_onprem_secure
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"strings"
9+
"testing"
10+
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 TestAccRuleStatefulCountDataSource(t *testing.T) {
18+
19+
if strings.HasSuffix(os.Getenv("SYSDIG_SECURE_URL"), "ibm.com") {
20+
t.Skip("Skipping stateful tests for IBM Cloud")
21+
return
22+
}
23+
24+
resource.Test(t, resource.TestCase{
25+
PreCheck: func() {
26+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
27+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
28+
}
29+
},
30+
ProviderFactories: map[string]func() (*schema.Provider, error){
31+
"sysdig": func() (*schema.Provider, error) {
32+
return sysdig.Provider(), nil
33+
},
34+
},
35+
Steps: []resource.TestStep{
36+
{
37+
Config: ruleStatefulCountDataSource(),
38+
Check: resource.ComposeTestCheckFunc(
39+
resource.TestCheckResourceAttr("data.sysdig_secure_rule_stateful_count.data_stateful_rule_append", "rule_count", "2"),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func ruleStatefulCountDataSource() string {
47+
return fmt.Sprintf(`
48+
%s
49+
50+
data "sysdig_secure_rule_stateful_count" "data_stateful_rule_append" {
51+
name = "API Gateway Enumeration Detected"
52+
source = "awscloudtrail_stateful"
53+
depends_on = [ sysdig_secure_rule_stateful.stateful_rule_append ]
54+
}
55+
`, ruleStatefulAppend())
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//go:build tf_acc_sysdig || tf_acc_sysdig_secure || tf_acc_policies || tf_acc_onprem_secure
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"strings"
9+
"testing"
10+
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 TestAccRuleStatefulDataSource(t *testing.T) {
18+
19+
if strings.HasSuffix(os.Getenv("SYSDIG_SECURE_URL"), "ibm.com") {
20+
t.Skip("Skipping stateful tests for IBM Cloud")
21+
return
22+
}
23+
24+
resource.Test(t, resource.TestCase{
25+
PreCheck: func() {
26+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
27+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
28+
}
29+
},
30+
ProviderFactories: map[string]func() (*schema.Provider, error){
31+
"sysdig": func() (*schema.Provider, error) {
32+
return sysdig.Provider(), nil
33+
},
34+
},
35+
Steps: []resource.TestStep{
36+
{
37+
Config: ruleStatefulDataSource(),
38+
},
39+
},
40+
})
41+
}
42+
43+
func ruleStatefulDataSource() string {
44+
return fmt.Sprintf(`
45+
%s
46+
47+
data "sysdig_secure_rule_stateful" "data_stateful_rule_append" {
48+
name = "API Gateway Enumeration Detected"
49+
source = "awscloudtrail_stateful"
50+
depends_on = [ sysdig_secure_rule_stateful.stateful_rule_append ]
51+
}
52+
`, ruleStatefulAppend())
53+
}

sysdig/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ func (p *SysdigProvider) Provider() *schema.Provider {
234234
"sysdig_secure_rule_syscall": dataSourceSysdigSecureRuleSyscall(),
235235
"sysdig_secure_posture_policies": dataSourceSysdigSecurePosturePolicies(),
236236
"sysdig_secure_custom_role_permissions": dataSourceSysdigSecureCustomRolePermissions(),
237+
"sysdig_secure_rule_stateful": dataSourceSysdigSecureRuleStateful(),
238+
"sysdig_secure_rule_stateful_count": dataSourceSysdigSecureRuleStatefulCount(),
237239

238240
"sysdig_current_user": dataSourceSysdigCurrentUser(),
239241
"sysdig_user": dataSourceSysdigUser(),

website/docs/d/secure_rule_stateful.md

-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,3 @@ Supported fields for exceptions:
4444
* `name` - The name of the existing exception definition.
4545
* `values` - Contains tuples of values. Each item in the tuple should align 1-1 with the corresponding field
4646
and comparison operator.
47-

0 commit comments

Comments
 (0)