-
Notifications
You must be signed in to change notification settings - Fork 55
stateful rules: fix docs and ci #611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9fa368c
trivial change
kmvachhani 1e020fe
Update ci-provider-docs.yaml
kmvachhani 2f81a14
add stateful data source
kmvachhani 6fd2ef3
Update data_source_sysdig_secure_rule_stateful_test.go
kmvachhani 29b0166
fix test
kmvachhani 9e6d6bf
Update data_source_sysdig_secure_rule_stateful.go
kmvachhani d9332ce
Update data_source_sysdig_secure_rule_stateful_test.go
kmvachhani 6102da5
add support for stateful rule count
kmvachhani 74ae360
add source to tests
kmvachhani e12069f
set missing fields
kmvachhani 14d1747
Update data_source_sysdig_secure_rule_stateful_count_test.go
kmvachhani 72056d8
Update data_source_sysdig_secure_rule_stateful_count_test.go
kmvachhani d2bf917
address review comments
kmvachhani 73ca4be
Update data_source_sysdig_secure_rule_stateful.go
kmvachhani 8a404d2
Update data_source_sysdig_secure_rule_stateful.go
kmvachhani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package sysdig | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func dataSourceSysdigSecureRuleStateful() *schema.Resource { | ||
| timeout := 1 * time.Minute | ||
|
|
||
| return &schema.Resource{ | ||
| ReadContext: dataSourceSysdigRuleStatefulRead, | ||
|
|
||
| Timeouts: &schema.ResourceTimeout{ | ||
| Read: schema.DefaultTimeout(timeout), | ||
| }, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "name": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| }, | ||
| "id": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| }, | ||
| "version": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| }, | ||
| "source": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ValidateDiagFunc: validateDiagFunc(validateStatefulRuleSource), | ||
| }, | ||
| "ruletype": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "append": { | ||
| Type: schema.TypeBool, | ||
| Computed: true, | ||
| }, | ||
| "exceptions": { | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "name": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "values": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceSysdigRuleStatefulRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| client, err := getSecureRuleClient(meta.(SysdigClients)) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| nameObj, ok := d.GetOk("name") | ||
| if !ok { | ||
| return diag.FromErr(errors.New("name is required")) | ||
| } | ||
|
|
||
| name := nameObj.(string) | ||
|
|
||
| sourceObj, ok := d.GetOk("source") | ||
| if !ok { | ||
| return diag.FromErr(errors.New("source is required")) | ||
| } | ||
|
|
||
| source := sourceObj.(string) | ||
|
|
||
| rules, err := client.GetStatefulRuleGroup(ctx, name, source) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| ruleIndexObj, ok := d.GetOk("index") | ||
| ruleIndex := 0 | ||
| if ok { | ||
| ruleIndex = ruleIndexObj.(int) | ||
| } | ||
|
|
||
| rule := rules[ruleIndex] | ||
|
|
||
| if len(rules) == 0 { | ||
| d.SetId("") | ||
| } else { | ||
| d.SetId(strconv.Itoa(rule.ID)) | ||
| } | ||
|
|
||
| _ = d.Set("name", rule.Name) | ||
| _ = d.Set("source", source) | ||
|
|
||
| if rule.Details.Append != nil { | ||
| _ = d.Set("append", *rule.Details.Append) | ||
| } | ||
|
|
||
| exceptions := make([]any, 0, len(rule.Details.Exceptions)) | ||
| for _, exception := range rule.Details.Exceptions { | ||
| if exception == nil { | ||
| return diag.Errorf("exception is nil") | ||
| } | ||
| valuesData, err := json.Marshal(exception.Values) | ||
| if err != nil { | ||
| return diag.Errorf("error marshalling exception values '%+v': %s", exception.Values, err) | ||
| } | ||
|
|
||
| exceptions = append(exceptions, map[string]any{ | ||
| "name": exception.Name, | ||
| "values": string(valuesData), | ||
| }) | ||
| } | ||
|
|
||
| if err := d.Set("exceptions", exceptions); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package sysdig | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func dataSourceSysdigSecureRuleStatefulCount() *schema.Resource { | ||
| timeout := 1 * time.Minute | ||
|
|
||
| return &schema.Resource{ | ||
| ReadContext: dataSourceSysdigRuleStatefulCountRead, | ||
|
|
||
| Timeouts: &schema.ResourceTimeout{ | ||
| Read: schema.DefaultTimeout(timeout), | ||
| }, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "name": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "source": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ValidateDiagFunc: validateDiagFunc(validateStatefulRuleSource), | ||
| }, | ||
| "rule_count": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceSysdigRuleStatefulCountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| client, err := getSecureRuleClient(meta.(SysdigClients)) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| ruleName := d.Get("name").(string) | ||
| ruleType := d.Get("source").(string) | ||
| rules, err := client.GetStatefulRuleGroup(ctx, ruleName, ruleType) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| d.SetId(fmt.Sprintf("count_%s", ruleName)) | ||
|
||
| _ = d.Set("name", ruleName) | ||
| _ = d.Set("rule_count", len(rules)) | ||
|
|
||
| return nil | ||
| } | ||
56 changes: 56 additions & 0 deletions
56
sysdig/data_source_sysdig_secure_rule_stateful_count_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| //go:build tf_acc_sysdig || tf_acc_sysdig_secure || tf_acc_policies || tf_acc_onprem_secure | ||
|
|
||
| package sysdig_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
|
||
| "github.com/draios/terraform-provider-sysdig/sysdig" | ||
| ) | ||
|
|
||
| func TestAccRuleStatefulCountDataSource(t *testing.T) { | ||
|
|
||
| if strings.HasSuffix(os.Getenv("SYSDIG_SECURE_URL"), "ibm.com") { | ||
| t.Skip("Skipping stateful tests for IBM Cloud") | ||
| return | ||
| } | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { | ||
| if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" { | ||
| t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests") | ||
| } | ||
| }, | ||
| ProviderFactories: map[string]func() (*schema.Provider, error){ | ||
| "sysdig": func() (*schema.Provider, error) { | ||
| return sysdig.Provider(), nil | ||
| }, | ||
| }, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: ruleStatefulCountDataSource(), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("data.sysdig_secure_rule_stateful_count.data_stateful_rule_append", "rule_count", "2"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func ruleStatefulCountDataSource() string { | ||
| return fmt.Sprintf(` | ||
| %s | ||
|
|
||
| data "sysdig_secure_rule_stateful_count" "data_stateful_rule_append" { | ||
| name = "API Gateway Enumeration Detected" | ||
| source = "awscloudtrail_stateful" | ||
| depends_on = [ sysdig_secure_rule_stateful.stateful_rule_append ] | ||
| } | ||
| `, ruleStatefulAppend()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //go:build tf_acc_sysdig || tf_acc_sysdig_secure || tf_acc_policies || tf_acc_onprem_secure | ||
|
|
||
| package sysdig_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
|
||
| "github.com/draios/terraform-provider-sysdig/sysdig" | ||
| ) | ||
|
|
||
| func TestAccRuleStatefulDataSource(t *testing.T) { | ||
|
|
||
| if strings.HasSuffix(os.Getenv("SYSDIG_SECURE_URL"), "ibm.com") { | ||
| t.Skip("Skipping stateful tests for IBM Cloud") | ||
| return | ||
| } | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { | ||
| if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" { | ||
| t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests") | ||
| } | ||
| }, | ||
| ProviderFactories: map[string]func() (*schema.Provider, error){ | ||
| "sysdig": func() (*schema.Provider, error) { | ||
| return sysdig.Provider(), nil | ||
| }, | ||
| }, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: ruleStatefulDataSource(), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func ruleStatefulDataSource() string { | ||
| return fmt.Sprintf(` | ||
| %s | ||
|
|
||
| data "sysdig_secure_rule_stateful" "data_stateful_rule_append" { | ||
| name = "API Gateway Enumeration Detected" | ||
| source = "awscloudtrail_stateful" | ||
| depends_on = [ sysdig_secure_rule_stateful.stateful_rule_append ] | ||
| } | ||
| `, ruleStatefulAppend()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Could we use the
ruleIndex, okform here to avoid panic since we are not enforcing a schema on this field?