Skip to content

Commit

Permalink
Update provider's S3 bucket lookup to use GetBucketRegion utility (#1…
Browse files Browse the repository at this point in the history
…4221)

* Update provider's S3 bucket lookup to use GetBucketRegion utility

Replaces the usage of S3's GetBucketLocation with the aws-sdk-go's
GetBucketRegion utility. This utility can discover the bucket's region
without authentication, and can be configured to be compatible with
FIPS endpoints.

Fixes #14217
Related to aws/aws-sdk-go#3115

* Add AWS SDK for Go s3manager dependency

Adds a dependency on the AWS SDK for Go's `s3manager`, and `s3iface`
packages. These packages make the s3manager packages's GetBucketRegion
utility available for discovering a S3 bucket's locations.

These packages are used by PR #14221.
  • Loading branch information
jasdel authored Jul 27, 2020
1 parent e0dd196 commit 02477a9
Show file tree
Hide file tree
Showing 19 changed files with 3,120 additions and 33 deletions.
16 changes: 6 additions & 10 deletions aws/data_source_aws_s3_bucket.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package aws

import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -91,19 +94,12 @@ func dataSourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
}

func bucketLocation(client *AWSClient, d *schema.ResourceData, bucket string) error {
location, err := client.s3conn.GetBucketLocation(
&s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
},
)
region, err := s3manager.GetBucketRegionWithClient(context.Background(), client.s3conn, bucket, func(r *request.Request) {
r.Config.S3ForcePathStyle = aws.Bool(false)
})
if err != nil {
return err
}
var region string
if location.LocationConstraint != nil {
region = *location.LocationConstraint
}
region = normalizeRegion(region)
if err := d.Set("region", region); err != nil {
return err
}
Expand Down
20 changes: 8 additions & 12 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -14,7 +15,9 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -1271,23 +1274,16 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
}

// Add the region as an attribute

locationResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return s3conn.GetBucketLocation(
&s3.GetBucketLocationInput{
Bucket: aws.String(d.Id()),
},
)
discoveredRegion, err := retryOnAwsCode("NotFound", func() (interface{}, error) {
return s3manager.GetBucketRegionWithClient(context.Background(), s3conn, d.Id(), func(r *request.Request) {
r.Config.S3ForcePathStyle = aws.Bool(false)
})
})
if err != nil {
return fmt.Errorf("error getting S3 Bucket location: %s", err)
}

var region string
if location, ok := locationResponse.(*s3.GetBucketLocationOutput); ok && location.LocationConstraint != nil {
region = aws.StringValue(location.LocationConstraint)
}
region = normalizeRegion(region)
region := discoveredRegion.(string)
if err := d.Set("region", region); err != nil {
return err
}
Expand Down
18 changes: 7 additions & 11 deletions aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -14,8 +15,10 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -126,21 +129,14 @@ func testSweepS3Buckets(region string) error {
}

func testS3BucketRegion(conn *s3.S3, bucket string) (string, error) {
input := &s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
}

output, err := conn.GetBucketLocation(input)

region, err := s3manager.GetBucketRegionWithClient(context.Background(), conn, bucket, func(r *request.Request) {
r.Config.S3ForcePathStyle = aws.Bool(false)
})
if err != nil {
return "", err
}

if output == nil || output.LocationConstraint == nil {
return "us-east-1", nil
}

return aws.StringValue(output.LocationConstraint), nil
return region, nil
}

func testS3BucketObjectLockEnabled(conn *s3.S3, bucket string) (bool, error) {
Expand Down
443 changes: 443 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/service/s3/s3iface/interface.go

Large diffs are not rendered by default.

Loading

0 comments on commit 02477a9

Please sign in to comment.