-
Notifications
You must be signed in to change notification settings - Fork 72
feat(waf): Onboard Managed Rule Set #1594
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
Open
Manuelvaas
wants to merge
6
commits into
main
Choose a base branch
from
feature/waf-onboard-managed-rule-set
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ed8db1
feat(albwaf): onboard managed rule set
Manuelvaas bc5d7d8
add datasource and acc tests
Manuelvaas f49671a
fmt, lint and docs
Manuelvaas 2cb3a8e
add examples
Manuelvaas 62e83a2
rename albwaf -> albWaf
Manuelvaas 11f618b
generate docs
Manuelvaas 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_alb_waf_managed_rule_set Data Source - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| ALB WAF Managed Rule Set resource schema. Uses the default_region specified in the provider configuration as a fallback in case no region is defined on resource level. | ||
| --- | ||
|
|
||
| # stackit_alb_waf_managed_rule_set (Data Source) | ||
|
|
||
| ALB WAF Managed Rule Set resource schema. Uses the `default_region` specified in the provider configuration as a fallback in case no `region` is defined on resource level. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| data "stackit_alb_waf_managed_rule_set" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example-managed-rule-set" | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `name` (String) Managed Rule Set configuration name. | ||
| - `project_id` (String) STACKIT project ID associated with the ALB WAF Managed Rule Set. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `region` (String) STACKIT region name the resource is located in. If not defined, the provider region is used. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `groups` (Attributes Map) Inventory of all available Managed Rule Set groups and their current configuration. (see [below for nested schema](#nestedatt--groups)) | ||
| - `id` (String) Terraform's internal resource identifier. Structured as "`project_id`,`region`,`name`". | ||
| - `type` (String) Set the Managed Rule Set type. | ||
| - `usage` (Attributes) Managed Rule Set usage (see [below for nested schema](#nestedatt--usage)) | ||
| - `version` (String) Managed Rule Set version. | ||
|
|
||
| <a id="nestedatt--groups"></a> | ||
| ### Nested Schema for `groups` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `description` (String) A description of what this group covers. | ||
| - `group_name` (String) The name for the rule group. | ||
| - `rules` (Attributes Map) Rules of the rule group. (see [below for nested schema](#nestedatt--groups--rules)) | ||
|
|
||
| <a id="nestedatt--groups--rules"></a> | ||
| ### Nested Schema for `groups.rules` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `description` (String) A description of what this rule does. | ||
| - `mode` (String) The current mode of the rule. | ||
| - `severity` (String) Impact level. | ||
|
|
||
|
|
||
|
|
||
| <a id="nestedatt--usage"></a> | ||
| ### Nested Schema for `usage` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `count` (Number) Number of WAFs using this Managed Rule Set. | ||
| - `items` (List of String) List of WAFs that use this Managed Rule Set. |
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,69 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_alb_waf_managed_rule_set Resource - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| ALB WAF Managed Rule Set resource schema. Uses the default_region specified in the provider configuration as a fallback in case no region is defined on resource level. | ||
| --- | ||
|
|
||
| # stackit_alb_waf_managed_rule_set (Resource) | ||
|
|
||
| ALB WAF Managed Rule Set resource schema. Uses the `default_region` specified in the provider configuration as a fallback in case no `region` is defined on resource level. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| resource "stackit_alb_waf_managed_rule_set" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example-managed-rule-set" | ||
| type = "TYPE_OWASP_CRS" | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `name` (String) Managed Rule Set configuration name. | ||
| - `project_id` (String) STACKIT project ID associated with the ALB WAF Managed Rule Set. | ||
| - `type` (String) Set the Managed Rule Set type. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `region` (String) STACKIT region name the resource is located in. If not defined, the provider region is used. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `groups` (Attributes Map) Inventory of all available Managed Rule Set groups and their current configuration. (see [below for nested schema](#nestedatt--groups)) | ||
| - `id` (String) Terraform's internal resource identifier. Structured as "`project_id`,`region`,`name`". | ||
| - `usage` (Attributes) Managed Rule Set usage (see [below for nested schema](#nestedatt--usage)) | ||
| - `version` (String) Managed Rule Set version. | ||
|
|
||
| <a id="nestedatt--groups"></a> | ||
| ### Nested Schema for `groups` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `description` (String) A description of what this group covers. | ||
| - `group_name` (String) The name for the rule group. | ||
| - `rules` (Attributes Map) Rules of the rule group. (see [below for nested schema](#nestedatt--groups--rules)) | ||
|
|
||
| <a id="nestedatt--groups--rules"></a> | ||
| ### Nested Schema for `groups.rules` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `description` (String) A description of what this rule does. | ||
| - `mode` (String) The current mode of the rule. | ||
| - `severity` (String) Impact level. | ||
|
|
||
|
|
||
|
|
||
| <a id="nestedatt--usage"></a> | ||
| ### Nested Schema for `usage` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `count` (Number) Number of WAFs using this Managed Rule Set. | ||
| - `items` (List of String) List of WAFs that use this Managed Rule Set. |
4 changes: 4 additions & 0 deletions
4
examples/data-sources/stackit_alb_waf_managed_rule_set/data-source.tf
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,4 @@ | ||
| data "stackit_alb_waf_managed_rule_set" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example-managed-rule-set" | ||
| } |
5 changes: 5 additions & 0 deletions
5
examples/resources/stackit_alb_waf_managed_rule_set/resource.tf
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,5 @@ | ||
| resource "stackit_alb_waf_managed_rule_set" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example-managed-rule-set" | ||
| type = "TYPE_OWASP_CRS" | ||
| } |
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
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,184 @@ | ||
| package albwaf_test | ||
|
|
||
| import ( | ||
| "context" | ||
| _ "embed" | ||
| "errors" | ||
| "fmt" | ||
| "maps" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/config" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/plancheck" | ||
| "github.com/hashicorp/terraform-plugin-testing/terraform" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| albwaf "github.com/stackitcloud/stackit-sdk-go/services/albwaf/v1betaapi" | ||
|
|
||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil" | ||
| ) | ||
|
|
||
| var ( | ||
| //go:embed testdata/managed-rule-set.tf | ||
| managedRuleSetConfig string | ||
| ) | ||
|
|
||
| var testManagedRuleSet = config.Variables{ | ||
| "project_id": config.StringVariable(testutil.ProjectId), | ||
| "name": config.StringVariable("tf-acc-" + acctest.RandStringFromCharSet(8, acctest.CharSetAlpha)), | ||
| "type": config.StringVariable("TYPE_OWASP_CRS"), | ||
| } | ||
|
|
||
| var testManagedRuleSetUpdated = func() config.Variables { | ||
| updatedConfig := config.Variables{} | ||
| maps.Copy(updatedConfig, testManagedRuleSet) | ||
| updatedConfig["name"] = config.StringVariable(fmt.Sprintf("%s-updated", testutil.ConvertConfigVariable(updatedConfig["name"]))) | ||
| return updatedConfig | ||
| } | ||
|
|
||
| func TestAccManagedRuleSet(t *testing.T) { | ||
| resource.Test(t, resource.TestCase{ | ||
| ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, | ||
| CheckDestroy: testAccCheckDestroy, | ||
| Steps: []resource.TestStep{ | ||
| // Creation | ||
| { | ||
| ConfigVariables: testManagedRuleSet, | ||
| Config: fmt.Sprintf("%s\n%s", testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig(), managedRuleSetConfig), | ||
| Check: resource.ComposeAggregateTestCheckFunc( | ||
| resource.TestCheckResourceAttr("stackit_alb_waf_managed_rule_set.managed_rule_set", "project_id", testutil.ProjectId), | ||
| resource.TestCheckResourceAttr("stackit_alb_waf_managed_rule_set.managed_rule_set", "region", testutil.Region), | ||
| resource.TestCheckResourceAttrSet("stackit_alb_waf_managed_rule_set.managed_rule_set", "id"), | ||
| resource.TestCheckResourceAttr("stackit_alb_waf_managed_rule_set.managed_rule_set", "name", testutil.ConvertConfigVariable(testManagedRuleSet["name"])), | ||
|
|
||
| resource.TestCheckResourceAttr("stackit_alb_waf_managed_rule_set.managed_rule_set", "usage.count", "0"), | ||
| ), | ||
| }, | ||
| // Data source | ||
| { | ||
| ConfigVariables: testManagedRuleSet, | ||
| Config: fmt.Sprintf(` | ||
| %s | ||
| %s | ||
|
|
||
| data "stackit_alb_waf_managed_rule_set" "managed_rule_set" { | ||
| project_id = stackit_alb_waf_managed_rule_set.managed_rule_set.project_id | ||
| name = stackit_alb_waf_managed_rule_set.managed_rule_set.name | ||
| } | ||
| `, | ||
| testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig(), managedRuleSetConfig, | ||
| ), | ||
| Check: resource.ComposeAggregateTestCheckFunc( | ||
| resource.TestCheckResourceAttr("data.stackit_alb_waf_managed_rule_set.managed_rule_set", "project_id", testutil.ProjectId), | ||
| resource.TestCheckResourceAttr("data.stackit_alb_waf_managed_rule_set.managed_rule_set", "region", testutil.Region), | ||
| resource.TestCheckResourceAttrPair( | ||
| "data.stackit_alb_waf_managed_rule_set.managed_rule_set", "id", | ||
| "stackit_alb_waf_managed_rule_set.managed_rule_set", "id", | ||
| ), | ||
| resource.TestCheckResourceAttr("data.stackit_alb_waf_managed_rule_set.managed_rule_set", "name", testutil.ConvertConfigVariable(testManagedRuleSet["name"])), | ||
|
|
||
| resource.TestCheckResourceAttr("data.stackit_alb_waf_managed_rule_set.managed_rule_set", "usage.count", "0"), | ||
| ), | ||
| }, | ||
| // Import | ||
| { | ||
| ConfigVariables: testManagedRuleSet, | ||
| ResourceName: "stackit_alb_waf_managed_rule_set.managed_rule_set", | ||
| ImportStateIdFunc: func(s *terraform.State) (string, error) { | ||
| r, ok := s.RootModule().Resources["stackit_alb_waf_managed_rule_set.managed_rule_set"] | ||
| if !ok { | ||
| return "", fmt.Errorf("couldn't find resource stackit_alb_waf_managed_rule_set.managed_rule_set") | ||
| } | ||
| policyId, ok := r.Primary.Attributes["name"] | ||
| if !ok { | ||
| return "", fmt.Errorf("couldn't find attribute name") | ||
| } | ||
| return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, testutil.Region, policyId), nil | ||
| }, | ||
| ImportState: true, | ||
| ImportStateVerify: true, | ||
| }, | ||
| // Update | ||
| { | ||
| ConfigVariables: testManagedRuleSetUpdated(), | ||
| Config: fmt.Sprintf("%s\n%s", testutil.NewConfigBuilder().EnableBetaResources(true).BuildProviderConfig(), managedRuleSetConfig), | ||
| ConfigPlanChecks: resource.ConfigPlanChecks{ | ||
| PreApply: []plancheck.PlanCheck{ | ||
| plancheck.ExpectResourceAction("stackit_alb_waf_managed_rule_set.managed_rule_set", plancheck.ResourceActionReplace), | ||
| }, | ||
| }, | ||
| Check: resource.ComposeAggregateTestCheckFunc( | ||
| resource.TestCheckResourceAttr("stackit_alb_waf_managed_rule_set.managed_rule_set", "project_id", testutil.ProjectId), | ||
| resource.TestCheckResourceAttr("stackit_alb_waf_managed_rule_set.managed_rule_set", "region", testutil.Region), | ||
| resource.TestCheckResourceAttrSet("stackit_alb_waf_managed_rule_set.managed_rule_set", "id"), | ||
| resource.TestCheckResourceAttr("stackit_alb_waf_managed_rule_set.managed_rule_set", "name", testutil.ConvertConfigVariable(testManagedRuleSetUpdated()["name"])), | ||
|
|
||
| resource.TestCheckResourceAttr("stackit_alb_waf_managed_rule_set.managed_rule_set", "usage.count", "0"), | ||
| ), | ||
| }, | ||
| // Deletion is done by the framework implicitly | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func createClient() (*albwaf.APIClient, error) { | ||
| client, err := albwaf.NewAPIClient(testutil.NewConfigBuilder().BuildClientOptions(testutil.AlbWafCustomEndpoint, false)...) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating client: %w", err) | ||
| } | ||
|
|
||
| return client, nil | ||
| } | ||
|
|
||
| func testAccCheckDestroy(s *terraform.State) error { | ||
| checkFunctions := []func(s *terraform.State) error{ | ||
| testAlbWafManagedRuleSetDestroy, | ||
| } | ||
| var errs []error | ||
|
|
||
| for _, f := range checkFunctions { | ||
| func() { | ||
| err := f(s) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| }() | ||
| } | ||
| return errors.Join(errs...) | ||
| } | ||
|
|
||
| func testAlbWafManagedRuleSetDestroy(s *terraform.State) error { | ||
| ctx := context.Background() | ||
| client, err := createClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| managedRuleSetsToDestroy := []string{} | ||
| for _, rs := range s.RootModule().Resources { | ||
| if rs.Type != "stackit_alb_waf_managed_rule_set" { | ||
| continue | ||
| } | ||
| // managed rule set transform id: "[projectId],[region],[name]" | ||
| name := strings.Split(rs.Primary.ID, core.Separator)[2] | ||
| managedRuleSetsToDestroy = append(managedRuleSetsToDestroy, name) | ||
| } | ||
|
|
||
| resp, err := client.DefaultAPI.ListManagedRuleSets(ctx, testutil.ProjectId, testutil.Region).Execute() | ||
| if err != nil { | ||
| return fmt.Errorf("getting resp: %w", err) | ||
| } | ||
|
|
||
| for _, item := range resp.Items { | ||
| if utils.Contains(managedRuleSetsToDestroy, item.GetName()) { | ||
| _, err := client.DefaultAPI.DeleteManagedRuleSet(ctx, testutil.ProjectId, testutil.Region, item.GetName()).Execute() | ||
| if err != nil { | ||
| return fmt.Errorf("deleting policy %s during CheckDestroy: %w", item.GetName(), err) | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.