Skip to content
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

resource/aws_cloudfront_distribution: Validate *_cache_behavior forwarded_values cookies configuration block forward argument #8563

Merged
merged 1 commit into from
May 9, 2019

Conversation

bflad
Copy link
Contributor

@bflad bflad commented May 8, 2019

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" comments, they generate extra noise for pull request followers and do not help prioritize the request

Closes #8493

Release note for CHANGELOG:

resource/aws_cloudfront_distribution: Validate `*_cache_behavior` `forwarded_values` `cookies` configuration block `forward` argument

This also prevents panics with an additional nil check, which bubbles up the API error should the attribute validation be removed for some reason. Verified the panic by changing an existing test configuration to forward = "" without code updates:

=== CONT  TestAccAWSCloudFrontDistribution_S3Origin
panic: interface conversion: interface {} is nil, not map[string]interface {}

goroutine 668 [running]:
github.com/terraform-providers/terraform-provider-aws/aws.expandForwardedValues(0xc000cd72f0, 0xc000cd7170)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/cloudfront_distribution_configuration_structure.go:418 +0x515
github.com/terraform-providers/terraform-provider-aws/aws.expandCloudFrontDefaultCacheBehavior(0xc000cd7170, 0x5e9a943)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/cloudfront_distribution_configuration_structure.go:196 +0x21b
github.com/terraform-providers/terraform-provider-aws/aws.expandDistributionConfig(0xc00039fe30, 0xc000a89f20)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/cloudfront_distribution_configuration_structure.go:39 +0x23a
github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsCloudFrontDistributionCreate(0xc00039fe30, 0x4fd92c0, 0xc0004b7000, 0x2, 0xa49a740)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_cloudfront_distribution.go:740 +0x6c

With the nil check in place, the API error is returned:

--- FAIL: TestAccAWSCloudFrontDistribution_S3Origin (18.14s)
    testing.go:568: Step 0 error: errors during apply:

        Error: error creating CloudFront Distribution: InvalidParameter: 1 validation error(s) found.
        - missing required field, CreateDistributionWithTagsInput.DistributionConfigWithTags.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Cookies.

By adding ValidateFunc we can prevent this issue during planning:

--- FAIL: TestAccAWSCloudFrontDistribution_S3Origin (1.73s)
    testing.go:568: Step 0 error: config is invalid: expected default_cache_behavior.0.forwarded_values.0.cookies.0.forward to be one of [all none whitelist], got

Output from acceptance testing:

--- PASS: TestAccAWSCloudFrontDistribution_Origin_EmptyOriginID (1.90s)
--- PASS: TestAccAWSCloudFrontDistribution_Origin_EmptyDomainName (2.03s)
--- PASS: TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_ForwardedValues_Headers (604.75s)
--- PASS: TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_ForwardedValues_Headers (605.93s)
--- PASS: TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_ForwardedValues_Cookies_WhitelistedNames (604.23s)
--- PASS: TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_ForwardedValues_Cookies_WhitelistedNames (606.72s)
--- PASS: TestAccAWSCloudFrontDistribution_disappears (697.81s)
--- PASS: TestAccAWSCloudFrontDistribution_IsIPV6EnabledConfig (2383.17s)
--- PASS: TestAccAWSCloudFrontDistribution_noCustomErrorResponseConfig (2384.35s)
--- PASS: TestAccAWSCloudFrontDistribution_RetainOnDelete (2384.95s)
--- PASS: TestAccAWSCloudFrontDistribution_OriginGroups (2385.23s)
--- PASS: TestAccAWSCloudFrontDistribution_orderedCacheBehavior (2386.96s)
--- PASS: TestAccAWSCloudFrontDistribution_multiOrigin (2387.28s)
--- PASS: TestAccAWSCloudFrontDistribution_WaitForDeployment (2387.45s)
--- PASS: TestAccAWSCloudFrontDistribution_S3Origin (2387.61s)
--- PASS: TestAccAWSCloudFrontDistribution_HTTP11Config (2390.63s)
--- PASS: TestAccAWSCloudFrontDistribution_customOrigin (2392.71s)
--- PASS: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn_ConflictsWithCloudFrontDefaultCertificate (2391.49s)
--- PASS: TestAccAWSCloudFrontDistribution_noOptionalItemsConfig (2393.55s)
--- PASS: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn (2397.30s)
--- PASS: TestAccAWSCloudFrontDistribution_S3OriginWithTags (2956.22s)
--- PASS: TestAccAWSCloudFrontDistribution_Enabled (2959.58s)

…rwarded_values` `cookies` configuration block `forward` argument

Reference: #8493

This also prevents panics with an additional `nil` check, which bubbles up the API error should the attribute validation be removed for some reason. Verified the panic by changing an existing test configuration to `forward = ""` without code updates:

```
=== CONT  TestAccAWSCloudFrontDistribution_S3Origin
panic: interface conversion: interface {} is nil, not map[string]interface {}

goroutine 668 [running]:
github.com/terraform-providers/terraform-provider-aws/aws.expandForwardedValues(0xc000cd72f0, 0xc000cd7170)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/cloudfront_distribution_configuration_structure.go:418 +0x515
github.com/terraform-providers/terraform-provider-aws/aws.expandCloudFrontDefaultCacheBehavior(0xc000cd7170, 0x5e9a943)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/cloudfront_distribution_configuration_structure.go:196 +0x21b
github.com/terraform-providers/terraform-provider-aws/aws.expandDistributionConfig(0xc00039fe30, 0xc000a89f20)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/cloudfront_distribution_configuration_structure.go:39 +0x23a
github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsCloudFrontDistributionCreate(0xc00039fe30, 0x4fd92c0, 0xc0004b7000, 0x2, 0xa49a740)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_cloudfront_distribution.go:740 +0x6c
```

With the `nil` check in place, the API error is returned:

```
--- FAIL: TestAccAWSCloudFrontDistribution_S3Origin (18.14s)
    testing.go:568: Step 0 error: errors during apply:

        Error: error creating CloudFront Distribution: InvalidParameter: 1 validation error(s) found.
        - missing required field, CreateDistributionWithTagsInput.DistributionConfigWithTags.DistributionConfig.DefaultCacheBehavior.ForwardedValues.Cookies.
```

By adding `ValidateFunc` we can prevent this issue during planning:

```
--- FAIL: TestAccAWSCloudFrontDistribution_S3Origin (1.73s)
    testing.go:568: Step 0 error: config is invalid: expected default_cache_behavior.0.forwarded_values.0.cookies.0.forward to be one of [all none whitelist], got
```

Output from acceptance testing:

```
--- PASS: TestAccAWSCloudFrontDistribution_Origin_EmptyOriginID (1.90s)
--- PASS: TestAccAWSCloudFrontDistribution_Origin_EmptyDomainName (2.03s)
--- PASS: TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_ForwardedValues_Headers (604.75s)
--- PASS: TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_ForwardedValues_Headers (605.93s)
--- PASS: TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_ForwardedValues_Cookies_WhitelistedNames (604.23s)
--- PASS: TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_ForwardedValues_Cookies_WhitelistedNames (606.72s)
--- PASS: TestAccAWSCloudFrontDistribution_disappears (697.81s)
--- PASS: TestAccAWSCloudFrontDistribution_IsIPV6EnabledConfig (2383.17s)
--- PASS: TestAccAWSCloudFrontDistribution_noCustomErrorResponseConfig (2384.35s)
--- PASS: TestAccAWSCloudFrontDistribution_RetainOnDelete (2384.95s)
--- PASS: TestAccAWSCloudFrontDistribution_OriginGroups (2385.23s)
--- PASS: TestAccAWSCloudFrontDistribution_orderedCacheBehavior (2386.96s)
--- PASS: TestAccAWSCloudFrontDistribution_multiOrigin (2387.28s)
--- PASS: TestAccAWSCloudFrontDistribution_WaitForDeployment (2387.45s)
--- PASS: TestAccAWSCloudFrontDistribution_S3Origin (2387.61s)
--- PASS: TestAccAWSCloudFrontDistribution_HTTP11Config (2390.63s)
--- PASS: TestAccAWSCloudFrontDistribution_customOrigin (2392.71s)
--- PASS: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn_ConflictsWithCloudFrontDefaultCertificate (2391.49s)
--- PASS: TestAccAWSCloudFrontDistribution_noOptionalItemsConfig (2393.55s)
--- PASS: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn (2397.30s)
--- PASS: TestAccAWSCloudFrontDistribution_S3OriginWithTags (2956.22s)
--- PASS: TestAccAWSCloudFrontDistribution_Enabled (2959.58s)
```
@bflad bflad added enhancement Requests to existing resources that expand the functionality or scope. service/cloudfront Issues and PRs that pertain to the cloudfront service. labels May 8, 2019
@bflad bflad requested a review from a team May 8, 2019 11:57
@ghost ghost added the size/XS Managed by automation to categorize the size of a PR. label May 8, 2019
@bflad bflad added this to the v2.10.0 milestone May 9, 2019
@bflad bflad merged commit aa0214d into master May 9, 2019
@bflad bflad deleted the f-aws_cloudfront_distribution-forward-ValidateFunc branch May 9, 2019 23:45
bflad added a commit that referenced this pull request May 9, 2019
@bflad
Copy link
Contributor Author

bflad commented May 10, 2019

This has been released in version 2.10.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Mar 30, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/cloudfront Issues and PRs that pertain to the cloudfront service. size/XS Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

crash on terraform apply
2 participants