Skip to content

Commit bdb0e8f

Browse files
author
Ben Lucas
authored
feat(policies): Data source for sysdig_secure_rule_syscall (#351)
* add data source for sysdig_secure_rule_syscall * add documentation for data source sysdig_secure_rule_syscall
1 parent 6c9dacf commit bdb0e8f

File tree

6 files changed

+139
-2
lines changed

6 files changed

+139
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 dataSourceSysdigSecureRuleSyscall() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigRuleSyscallRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createRuleDataSourceSchema(map[string]*schema.Schema{
23+
"matching": {
24+
Type: schema.TypeBool,
25+
Computed: true,
26+
},
27+
"syscalls": {
28+
Type: schema.TypeList,
29+
Computed: true,
30+
Elem: &schema.Schema{
31+
Type: schema.TypeString,
32+
},
33+
},
34+
}),
35+
}
36+
}
37+
38+
func dataSourceSysdigRuleSyscallRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
39+
return commonDataSourceSysdigRuleRead(ctx, d, meta, v2.RuleTypeSyscall, syscallRuleDataSourceToResourceData)
40+
}
41+
42+
func syscallRuleDataSourceToResourceData(rule v2.Rule, d *schema.ResourceData) diag.Diagnostics {
43+
if rule.Details.Syscalls == nil {
44+
return diag.Errorf("no syscall data for a syscall rule")
45+
}
46+
47+
_ = d.Set("matching", rule.Details.Syscalls.MatchItems)
48+
_ = d.Set("syscalls", rule.Details.Syscalls.Items)
49+
50+
return nil
51+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 TestAccRuleSyscallDataSource(t *testing.T) {
18+
rText := func() string { return 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: ruleSyscallDataSource(rText()),
34+
},
35+
},
36+
})
37+
}
38+
39+
func ruleSyscallDataSource(name string) string {
40+
return fmt.Sprintf(`
41+
%s
42+
43+
data "sysdig_secure_rule_syscall" "data_sample" {
44+
name = "TERRAFORM TEST %s"
45+
depends_on = [ sysdig_secure_rule_syscall.foo ]
46+
}
47+
`, ruleSyscallWithName(name), name)
48+
}

sysdig/internal/client/v2/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ const (
264264
RuleTypeFilesystem = "FILESYSTEM"
265265
RuleTypeNetwork = "NETWORK"
266266
RuleTypeProcess = "PROCESS"
267+
RuleTypeSyscall = "SYSCALL"
267268
)
268269

269270
type Details struct {

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func Provider() *schema.Provider {
136136
"sysdig_secure_rule_filesystem": dataSourceSysdigSecureRuleFilesystem(),
137137
"sysdig_secure_rule_network": dataSourceSysdigSecureRuleNetwork(),
138138
"sysdig_secure_rule_process": dataSourceSysdigSecureRuleProcess(),
139+
"sysdig_secure_rule_syscall": dataSourceSysdigSecureRuleSyscall(),
139140

140141
"sysdig_current_user": dataSourceSysdigCurrentUser(),
141142
"sysdig_user": dataSourceSysdigUser(),

sysdig/resource_sysdig_secure_rule_syscall.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package sysdig
22

33
import (
44
"context"
5-
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
65
"strconv"
76
"time"
87

8+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
9+
910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
)
@@ -133,7 +134,7 @@ func resourceSysdigRuleSyscallDelete(ctx context.Context, d *schema.ResourceData
133134

134135
func resourceSysdigRuleSyscallFromResourceData(d *schema.ResourceData) v2.Rule {
135136
rule := ruleFromResourceData(d)
136-
rule.Details.RuleType = "SYSCALL"
137+
rule.Details.RuleType = v2.RuleTypeSyscall
137138

138139
rule.Details.Syscalls = &v2.Syscalls{}
139140
rule.Details.Syscalls.MatchItems = d.Get("matching").(bool)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
subcategory: "Sysdig Secure"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_secure_rule_syscall"
5+
description: |-
6+
Retrieves a Sysdig Secure Syscall Rule.
7+
---
8+
9+
# Data Source: sysdig_secure_rule_syscall
10+
11+
Retrieves the information of an existing Sysdig Secure Syscall Rule.
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_rule_syscall" "example" {
19+
name = "Unexpected mount syscall"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
* `name` - (Required) The name of the Secure rule to retrieve.
26+
27+
## Attributes Reference
28+
29+
In addition to the argument above, the following attributes are exported:
30+
31+
* `description` - The description of Secure rule.
32+
* `tags` - A list of tags for this rule.
33+
* `matching` - Defines if the syscall name matches or not with the provided list.
34+
* `processes` - List of syscalls to match.
35+
* `version` - Current version of the resource in Sysdig Secure.

0 commit comments

Comments
 (0)