-
Notifications
You must be signed in to change notification settings - Fork 79
refactor(albwaf): address open issues #1667
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,39 @@ | ||
| resource "stackit_alb_waf_managed_rule_set" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example-managed-rule-set" | ||
| type = "TYPE_OWASP_CRS" | ||
| } | ||
|
|
||
| resource "stackit_alb_waf_custom_rule_group" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example-custom-rule-group" | ||
| rules = [ | ||
| { | ||
| behavior = { | ||
| action = "ACTION_DENY" | ||
| } | ||
| conditions = [ | ||
| { | ||
| operator = { | ||
| type = "OPERATOR_VALIDATE_UTF8_ENCODING" | ||
| } | ||
| variable = { | ||
| type = "VARIABLE_REQUEST_HEADERS" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| resource "stackit_alb_waf_configuration" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example-waf-configuration" | ||
| } | ||
|
|
||
| managed_rule_set_name = stackit_alb_waf_managed_rule_set.example.name | ||
| custom_rule_group_name = stackit_alb_waf_custom_rule_group.example.name | ||
|
|
||
| labels = { | ||
| "key" = "value" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -664,6 +664,7 @@ func testAccCheckDestroy(s *terraform.State) error { | |
| checkFunctions := []func(s *terraform.State) error{ | ||
| testAlbWafCustomRuleGroupDestroy, | ||
| testAlbWafManagedRuleSetDestroy, | ||
| testAlbWafWafConfigurationDestroy, | ||
| } | ||
| var errs []error | ||
|
|
||
|
|
@@ -743,3 +744,36 @@ func testAlbWafManagedRuleSetDestroy(s *terraform.State) error { | |
| } | ||
| return nil | ||
| } | ||
|
|
||
| func testAlbWafWafConfigurationDestroy(s *terraform.State) error { | ||
| ctx := context.Background() | ||
| client, err := createClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| wafConfigurationToDestroy := []string{} | ||
| for _, rs := range s.RootModule().Resources { | ||
| if rs.Type != "stackit_alb_waf_configuration" { | ||
| continue | ||
| } | ||
| // waf configuration transform id: "[projectId],[region],[name]" | ||
| name := strings.Split(rs.Primary.ID, core.Separator)[2] | ||
| wafConfigurationToDestroy = append(wafConfigurationToDestroy, name) | ||
| } | ||
|
|
||
| resp, err := client.DefaultAPI.ListWAF(ctx, testutil.ProjectId, testutil.Region).Execute() | ||
| if err != nil { | ||
| return fmt.Errorf("getting resp: %w", err) | ||
| } | ||
|
|
||
| for _, item := range resp.Items { | ||
| if utils.Contains(wafConfigurationToDestroy, item.GetName()) { | ||
|
Comment on lines
+765
to
+771
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://docs.api.stackit.cloud/documentation/alb-waf/version/v1#tag/WAF/operation/WAFService_ListWAF Looks like |
||
| _, err := client.DefaultAPI.DeleteWAF(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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -237,24 +237,11 @@ func (r *managedRuleSetResource) ModifyPlan(ctx context.Context, req resource.Mo | |
| return | ||
| } | ||
|
|
||
| // Warn the user if the name is changing, as this triggers a replacement. | ||
| // Deletion of the old resource will fail if another resource (e.g. stackit_alb_waf_configuration) | ||
| // still references this managed rule set. | ||
| if !req.State.Raw.IsNull() { | ||
| var stateModel Model | ||
| resp.Diagnostics.Append(req.State.Get(ctx, &stateModel)...) | ||
| if !resp.Diagnostics.HasError() && !stateModel.Name.IsNull() && !stateModel.Name.IsUnknown() { | ||
| if !planModel.Name.Equal(stateModel.Name) { | ||
| resp.Diagnostics.AddWarning( | ||
| "Managed Rule Set name change requires resource replacement", | ||
| fmt.Sprintf( | ||
| "Changing the \"name\" attribute from %q to %q will destroy and recreate this resource. "+ | ||
| "If another resource (e.g. \"stackit_alb_waf_configuration\") references this managed rule set "+ | ||
| "by name, the replacement will fail. Remove or update that dependency before applying this change.", | ||
| stateModel.Name.ValueString(), planModel.Name.ValueString(), | ||
| ), | ||
| ) | ||
| } | ||
| if !resp.Diagnostics.HasError() { | ||
| utils.WarnIfNameChanges(stateModel.Name, planModel.Name, "Managed Rule Set", &resp.Diagnostics) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same conditional logic as one file above |
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,14 @@ func (r *wafResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq | |
| return | ||
| } | ||
|
|
||
| if !req.State.Raw.IsNull() { | ||
| var stateModel Model | ||
| resp.Diagnostics.Append(req.State.Get(ctx, &stateModel)...) | ||
| if !resp.Diagnostics.HasError() { | ||
| albwafUtils.WarnIfNameChanges(stateModel.Name, planModel.Name, "WAF Configuration", &resp.Diagnostics) | ||
| } | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
| resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
|
|
@@ -407,7 +415,7 @@ func toUpdatePayload(ctx context.Context, model *Model) (*albWaf.UpdateWAFPayloa | |
| } | ||
|
|
||
| var labels *map[string]string | ||
| if !(model.Labels.IsNull() || model.Labels.IsUnknown()) { | ||
| if !tfutils.IsUndefined(model.Labels) { | ||
| diags := model.Labels.ElementsAs(ctx, &labels, false) | ||
| if diags.HasError() { | ||
| return nil, core.DiagsToError(diags) | ||
|
|
@@ -426,7 +434,7 @@ func toCreatePayload(ctx context.Context, model *Model) (*albWaf.CreateWAFPayloa | |
| } | ||
|
|
||
| var labels *map[string]string | ||
| if !(model.Labels.IsNull() || model.Labels.IsUnknown()) { | ||
| if !tfutils.IsUndefined(model.Labels) { | ||
| diags := model.Labels.ElementsAs(ctx, &labels, false) | ||
| if diags.HasError() { | ||
| return nil, core.DiagsToError(diags) | ||
|
|
||
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.
Sometimes I get an index out of bound panic on lines like these when running acceptance tests because
rs.Primary.ID == "".Id add an if here to prevent dangling resources.