Skip to content

Commit 7816645

Browse files
author
Tamir
committed
squash all
1 parent e8b284b commit 7816645

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

.changelog/30340.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_wafv2_web_acl: Add optional `token_domains` argument.
3+
```

internal/service/wafv2/flex.go

+4
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,10 @@ func expandWebACLRules(l []interface{}) []*wafv2.Rule {
842842
return rules
843843
}
844844

845+
func expandTokenDomains(s *schema.Set) []*string {
846+
return flex.ExpandStringSet(s)
847+
}
848+
845849
func expandWebACLRule(m map[string]interface{}) *wafv2.Rule {
846850
if m == nil {
847851
return nil

internal/service/wafv2/web_acl.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,19 @@ func ResourceWebACL() *schema.Resource {
143143
ForceNew: true,
144144
ValidateFunc: validation.StringInSlice(wafv2.Scope_Values(), false),
145145
},
146-
names.AttrTags: tftags.TagsSchema(),
147-
names.AttrTagsAll: tftags.TagsSchemaComputed(),
146+
names.AttrTags: tftags.TagsSchema(),
147+
names.AttrTagsAll: tftags.TagsSchemaComputed(),
148+
"token_domains": {
149+
Type: schema.TypeSet,
150+
Optional: true,
151+
Elem: &schema.Schema{
152+
Type: schema.TypeString,
153+
ValidateFunc: validation.All(
154+
validation.StringLenBetween(1, 253),
155+
validation.StringMatch(regexp.MustCompile(`^[\w\.\-/]+$`), "must contain only alphanumeric, hyphen, dot, underscore and forward-slash characters"),
156+
),
157+
},
158+
},
148159
"visibility_config": visibilityConfigSchema(),
149160
},
150161

@@ -173,6 +184,10 @@ func resourceWebACLCreate(ctx context.Context, d *schema.ResourceData, meta inte
173184
input.Description = aws.String(v.(string))
174185
}
175186

187+
if v, ok := d.GetOk("token_domains"); ok {
188+
input.TokenDomains = expandTokenDomains(v.(*schema.Set))
189+
}
190+
176191
outputRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, webACLCreateTimeout, func() (interface{}, error) {
177192
return conn.CreateWebACLWithContext(ctx, input)
178193
}, wafv2.ErrCodeWAFUnavailableEntityException)
@@ -223,6 +238,9 @@ func resourceWebACLRead(ctx context.Context, d *schema.ResourceData, meta interf
223238
if err := d.Set("visibility_config", flattenVisibilityConfig(webACL.VisibilityConfig)); err != nil {
224239
return diag.Errorf("setting visibility_config: %s", err)
225240
}
241+
if err := d.Set("token_domains", aws.StringValueSlice(webACL.TokenDomains)); err != nil {
242+
return diag.Errorf("setting token_domains: %s", err)
243+
}
226244

227245
return nil
228246
}
@@ -249,6 +267,10 @@ func resourceWebACLUpdate(ctx context.Context, d *schema.ResourceData, meta inte
249267
input.Description = aws.String(v.(string))
250268
}
251269

270+
if v, ok := d.GetOk("token_domains"); ok {
271+
input.TokenDomains = expandTokenDomains(v.(*schema.Set))
272+
}
273+
252274
_, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, webACLUpdateTimeout, func() (interface{}, error) {
253275
return conn.UpdateWebACLWithContext(ctx, input)
254276
}, wafv2.ErrCodeWAFUnavailableEntityException)

internal/service/wafv2/web_acl_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,52 @@ func TestAccWAFV2WebACL_Operators_maxNested(t *testing.T) {
22402240
})
22412241
}
22422242

2243+
func TestAccWAFV2WebACL_tokenDomains(t *testing.T) {
2244+
ctx := acctest.Context(t)
2245+
var v wafv2.WebACL
2246+
webACLName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
2247+
domain1 := "mywebsite.com"
2248+
domain2 := "myotherwebsite.com"
2249+
resourceName := "aws_wafv2_web_acl.test"
2250+
2251+
resource.ParallelTest(t, resource.TestCase{
2252+
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckScopeRegional(ctx, t) },
2253+
ErrorCheck: acctest.ErrorCheck(t, wafv2.EndpointsID),
2254+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
2255+
CheckDestroy: testAccCheckWebACLDestroy(ctx),
2256+
Steps: []resource.TestStep{
2257+
{
2258+
Config: testAccWebACLConfig_tokenDomains(webACLName, domain1, domain2),
2259+
Check: resource.ComposeTestCheckFunc(
2260+
testAccCheckWebACLExists(ctx, resourceName, &v),
2261+
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "wafv2", regexp.MustCompile(`regional/webacl/.+$`)),
2262+
resource.TestCheckResourceAttr(resourceName, "name", webACLName),
2263+
resource.TestCheckResourceAttr(resourceName, "description", webACLName),
2264+
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
2265+
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
2266+
resource.TestCheckResourceAttr(resourceName, "default_action.#", "1"),
2267+
resource.TestCheckResourceAttr(resourceName, "default_action.0.allow.#", "1"),
2268+
resource.TestCheckResourceAttr(resourceName, "default_action.0.block.#", "0"),
2269+
resource.TestCheckResourceAttr(resourceName, "token_domains.#", "2"),
2270+
resource.TestCheckTypeSetElemAttr(resourceName, "token_domains.*", domain1),
2271+
resource.TestCheckTypeSetElemAttr(resourceName, "token_domains.*", domain2),
2272+
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
2273+
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
2274+
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.metric_name", "friendly-metric-name"),
2275+
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.sampled_requests_enabled", "false"),
2276+
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
2277+
),
2278+
},
2279+
{
2280+
ResourceName: resourceName,
2281+
ImportState: true,
2282+
ImportStateVerify: true,
2283+
ImportStateIdFunc: testAccWebACLImportStateIdFunc(resourceName),
2284+
},
2285+
},
2286+
})
2287+
}
2288+
22432289
func testAccCheckWebACLDestroy(ctx context.Context) resource.TestCheckFunc {
22442290
return func(s *terraform.State) error {
22452291
for _, rs := range s.RootModule().Resources {
@@ -4332,3 +4378,24 @@ resource "aws_wafv2_web_acl" "test" {
43324378
}
43334379
`, name)
43344380
}
4381+
4382+
func testAccWebACLConfig_tokenDomains(name, domain1, domain2 string) string {
4383+
return fmt.Sprintf(`
4384+
resource "aws_wafv2_web_acl" "test" {
4385+
name = %[1]q
4386+
description = %[1]q
4387+
scope = "REGIONAL"
4388+
4389+
default_action {
4390+
allow {}
4391+
}
4392+
4393+
token_domains = [%[2]q, %[3]q]
4394+
visibility_config {
4395+
cloudwatch_metrics_enabled = false
4396+
metric_name = "friendly-metric-name"
4397+
sampled_requests_enabled = false
4398+
}
4399+
}
4400+
`, name, domain1, domain2)
4401+
}

website/docs/r/wafv2_web_acl.html.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ resource "aws_wafv2_web_acl" "example" {
6565
}
6666
}
6767
68+
token_domains = ["mywebsite.com", "myotherwebsite.com"]
69+
6870
visibility_config {
6971
cloudwatch_metrics_enabled = false
7072
metric_name = "friendly-rule-metric-name"
@@ -278,6 +280,7 @@ The following arguments are supported:
278280
* `rule` - (Optional) Rule blocks used to identify the web requests that you want to `allow`, `block`, or `count`. See [`rule`](#rule) below for details.
279281
* `scope` - (Required) Specifies whether this is for an AWS CloudFront distribution or for a regional application. Valid values are `CLOUDFRONT` or `REGIONAL`. To work with CloudFront, you must also specify the region `us-east-1` (N. Virginia) on the AWS provider.
280282
* `tags` - (Optional) Map of key-value pairs to associate with the resource. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
283+
* `token_domains` - (Optional) Specifies the domains that AWS WAF should accept in a web request token. This enables the use of tokens across multiple protected websites. When AWS WAF provides a token, it uses the domain of the AWS resource that the web ACL is protecting. If you don't specify a list of token domains, AWS WAF accepts tokens only for the domain of the protected resource. With a token domain list, AWS WAF accepts the resource's host domain plus all domains in the token domain list, including their prefixed subdomains.
281284
* `visibility_config` - (Required) Defines and enables Amazon CloudWatch metrics and web request sample collection. See [`visibility_config`](#visibility_config) below for details.
282285

283286
### `custom_response_body`

0 commit comments

Comments
 (0)