feat(cdn): Add support for configuring WAF#1372
feat(cdn): Add support for configuring WAF#1372matheuspolitano wants to merge 81 commits intostackitcloud:mainfrom
Conversation
…b.com/matheuspolitano/terraform-provider-stackit into mp/cdn/feat/implement-configuring-waf
|
|
||
| // getWafSet extracts strings from HCL set, sorts them and returns the slice | ||
| func getWafSet(ctx context.Context, tfSet basetypes.SetValue) []string { | ||
| if utils.IsUndefined(tfSet) { | ||
| return nil | ||
| } | ||
| var elements []string | ||
| diags := tfSet.ElementsAs(ctx, &elements, true) | ||
| if diags.HasError() { | ||
| return []string{} | ||
| } | ||
| sort.Strings(elements) | ||
| return elements | ||
| } |
There was a problem hiding this comment.
Why is it needed to sort the slice? Takes the API care of the order? If not, you can use conversion.StringSetToSlice() instead this function, there you shouldn't ignore the error like it was done here
There was a problem hiding this comment.
i've removed the sort
There was a problem hiding this comment.
same here, remove this function and use instead everywhere conversion.StringSetToSlice()
| // getWafSet extracts strings from HCL set, sorts them and returns the slice | |
| func getWafSet(ctx context.Context, tfSet basetypes.SetValue) []string { | |
| if utils.IsUndefined(tfSet) { | |
| return nil | |
| } | |
| var elements []string | |
| diags := tfSet.ElementsAs(ctx, &elements, true) | |
| if diags.HasError() { | |
| return []string{} | |
| } | |
| sort.Strings(elements) | |
| return elements | |
| } |
| mapWafString := func(apiVal *string) types.String { | ||
| if apiVal != nil { | ||
| return types.StringValue(*apiVal) | ||
| } | ||
| return types.StringNull() | ||
| } |
There was a problem hiding this comment.
you can use the function types.StringPointerValue() from the terraform framework itself instead of this function
There was a problem hiding this comment.
I meant here to remove the whole mapWafString function and use everywhere types.StringPointerValue() instead. It does the same thing but keeps the code clean because this is a function of the framework
| mapWafString := func(apiVal *string) types.String { | |
| if apiVal != nil { | |
| return types.StringValue(*apiVal) | |
| } | |
| return types.StringNull() | |
| } |
Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com>
…b.com/matheuspolitano/terraform-provider-stackit into mp/cdn/feat/implement-configuring-waf
Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com>
…b.com/matheuspolitano/terraform-provider-stackit into mp/cdn/feat/implement-configuring-waf
| mapWafString := func(apiVal *string) types.String { | ||
| if apiVal != nil { | ||
| return types.StringValue(*apiVal) | ||
| } | ||
| return types.StringNull() | ||
| } |
There was a problem hiding this comment.
I meant here to remove the whole mapWafString function and use everywhere types.StringPointerValue() instead. It does the same thing but keeps the code clean because this is a function of the framework
| mapWafString := func(apiVal *string) types.String { | |
| if apiVal != nil { | |
| return types.StringValue(*apiVal) | |
| } | |
| return types.StringNull() | |
| } |
|
|
||
| // getWafSet extracts strings from HCL set, sorts them and returns the slice | ||
| func getWafSet(ctx context.Context, tfSet basetypes.SetValue) []string { | ||
| if utils.IsUndefined(tfSet) { | ||
| return nil | ||
| } | ||
| var elements []string | ||
| diags := tfSet.ElementsAs(ctx, &elements, true) | ||
| if diags.HasError() { | ||
| return []string{} | ||
| } | ||
| sort.Strings(elements) | ||
| return elements | ||
| } |
There was a problem hiding this comment.
same here, remove this function and use instead everywhere conversion.StringSetToSlice()
| // getWafSet extracts strings from HCL set, sorts them and returns the slice | |
| func getWafSet(ctx context.Context, tfSet basetypes.SetValue) []string { | |
| if utils.IsUndefined(tfSet) { | |
| return nil | |
| } | |
| var elements []string | |
| diags := tfSet.ElementsAs(ctx, &elements, true) | |
| if diags.HasError() { | |
| return []string{} | |
| } | |
| sort.Strings(elements) | |
| return elements | |
| } |
| // Helper to unconditionally map set fields | ||
| func mustMapStringSet(ctx context.Context, apiList []string) types.Set { | ||
| if apiList != nil { | ||
| setVal, diags := types.SetValueFrom(ctx, types.StringType, apiList) | ||
| if !diags.HasError() { | ||
| return setVal | ||
| } | ||
| } | ||
| return types.SetNull(types.StringType) | ||
| } |
There was a problem hiding this comment.
Move this to the conversion package and name it to something like SliceToStringSet and avoid context if possible and don't ignore the error.
Could be something like this:
func SliceToStringSet(list []string) (types.Set, error) {
if list == nil {
return types.SetNull(types.StringType), nil
}
set := make([]attr.Value, len(list))
for idx, v := range list {
stringValue := types.StringValue(v)
set[idx] = stringValue
}
result, diags := types.SetValue(types.StringType, set)
if diags.HasError() {
return types.SetNull(types.StringType), fmt.Errorf("converting to SetValue: %v", diags.Errors())
}
return result, nil
}| isWafDisabled := (distribution.Config.Waf.Mode == cdnSdk.WAFMODE_DISABLED) && | ||
| (distribution.Config.Waf.Type == cdnSdk.WAFTYPE_FREE) | ||
|
|
||
| var wafVal attr.Value | ||
| if isWafDisabled && (isImport || utils.IsUndefined(oldConfig.Waf)) { | ||
| wafVal = types.ObjectNull(wafTypes) | ||
| } else { |
There was a problem hiding this comment.
I'm against this workaround here. The datasource shows always waf with the attributes and the resource should behave the same, even if it's not configured by the customer
| resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.waf.allowed_http_methods.#", "1"), | ||
| resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.waf.allowed_http_methods.0", "GET"), |
There was a problem hiding this comment.
Don't set the values here by hand. Use them from you config.
| resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.waf.allowed_http_methods.#", "1"), | |
| resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.waf.allowed_http_methods.0", "GET"), | |
| resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.waf.allowed_http_methods.#", "1"), | |
| resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.waf.allowed_http_methods.0", testConfigVarsHttp["waf_allowed_http_methods"]), |
Change it also for all other checks, even in the Read via datasource and update step
Description
https://jira.schwarz/browse/STACKITCDN-723
I want to add support for the waf configuration block in the CDN distribution resource,so that users can programmatically manage security tiers, paranoia levels, and granular rule overrides (Enabled/Disabled/Log-only) via Terraform.
Checklist
make fmtexamples/directory)make generate-docs(will be checked by CI)make test(will be checked by CI)make lint(will be checked by CI)