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_s3_bucket: Prevent empty replication_configuration rules filter crash #7887

Merged
merged 3 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ func resourceAwsS3BucketReplicationConfigurationUpdate(s3conn *s3.S3, d *schema.
rcRule.SourceSelectionCriteria = ruleSsc
}

if f, ok := rr["filter"].([]interface{}); ok && len(f) > 0 {
if f, ok := rr["filter"].([]interface{}); ok && len(f) > 0 && f[0] != nil {
// XML schema V2.
rcRule.Priority = aws.Int64(int64(rr["priority"].(int)))
rcRule.Filter = &s3.ReplicationRuleFilter{}
Expand Down
12 changes: 11 additions & 1 deletion aws/resource_aws_s3_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ func testSweepS3BucketObjects(region string) error {
for _, bucket := range output.Buckets {
bucketName := aws.StringValue(bucket.Name)

if !strings.HasPrefix(bucketName, "tf-acc") && !strings.HasPrefix(bucketName, "tf-object-test") && !strings.HasPrefix(bucketName, "tf-test") {
hasPrefix := false
prefixes := []string{"mybucket.", "mylogs.", "tf-acc", "tf-object-test", "tf-test"}

for _, prefix := range prefixes {
if strings.HasPrefix(bucketName, prefix) {
hasPrefix = true
break
}
}

if !hasPrefix {
log.Printf("[INFO] Skipping S3 Bucket: %s", bucketName)
continue
}
Expand Down
38 changes: 34 additions & 4 deletions aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ func testSweepS3Buckets(region string) error {
for _, bucket := range output.Buckets {
name := aws.StringValue(bucket.Name)

if !strings.HasPrefix(name, "tf-acc") && !strings.HasPrefix(name, "tf-object-test") && !strings.HasPrefix(name, "tf-test") {
hasPrefix := false
prefixes := []string{"mybucket.", "mylogs.", "tf-acc", "tf-object-test", "tf-test"}

for _, prefix := range prefixes {
if strings.HasPrefix(name, prefix) {
hasPrefix = true
break
}
}

if !hasPrefix {
log.Printf("[INFO] Skipping S3 Bucket: %s", name)
continue
}
Expand Down Expand Up @@ -1598,11 +1608,31 @@ func testAccCheckAWSS3BucketDestroyWithProvider(s *terraform.State, provider *sc
continue
}

_, err := conn.HeadBucket(&s3.HeadBucketInput{
input := &s3.HeadBucketInput{
Bucket: aws.String(rs.Primary.ID),
}

// Retry for S3 eventual consistency
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.HeadBucket(input)

if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NotFound", "") {
return nil
}

if err != nil {
return resource.NonRetryableError(err)
}

return resource.RetryableError(fmt.Errorf("AWS S3 Bucket still exists: %s", rs.Primary.ID))
})
if err == nil {
return fmt.Errorf("AWS S3 Bucket still exists: %s", rs.Primary.ID)

if isResourceTimeoutError(err) {
_, err = conn.HeadBucket(input)
}

if err != nil {
return err
}
}
return nil
Expand Down