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

d/aws_s3_bucket_object: Don't crash when S3 HeadObject returns empty response #14154

10 changes: 7 additions & 3 deletions aws/data_source_aws_s3_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func dataSourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) e
if err != nil {
return fmt.Errorf("Failed getting S3 object: %s Bucket: %q Object: %q", err, bucket, key)
}
if out.DeleteMarker != nil && *out.DeleteMarker {
if aws.BoolValue(out.DeleteMarker) {
return fmt.Errorf("Requested S3 object %q%s has been deleted",
bucket+key, versionText)
}
Expand All @@ -165,10 +165,14 @@ func dataSourceAwsS3BucketObjectRead(d *schema.ResourceData, meta interface{}) e
d.Set("content_length", out.ContentLength)
d.Set("content_type", out.ContentType)
// See https://forums.aws.amazon.com/thread.jspa?threadID=44003
d.Set("etag", strings.Trim(*out.ETag, `"`))
d.Set("etag", strings.Trim(aws.StringValue(out.ETag), `"`))
d.Set("expiration", out.Expiration)
d.Set("expires", out.Expires)
d.Set("last_modified", out.LastModified.Format(time.RFC1123))
if out.LastModified != nil {
d.Set("last_modified", out.LastModified.Format(time.RFC1123))
} else {
d.Set("last_modified", "")
}
d.Set("metadata", pointersMapToStringList(out.Metadata))
d.Set("object_lock_legal_hold_status", out.ObjectLockLegalHoldStatus)
d.Set("object_lock_mode", out.ObjectLockMode)
Expand Down
33 changes: 33 additions & 0 deletions aws/data_source_aws_s3_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,26 @@ func TestAccDataSourceAWSS3BucketObject_MultipleSlashes(t *testing.T) {
})
}

func TestAccDataSourceAWSS3BucketObject_SingleSlashAsKey(t *testing.T) {
var dsObj s3.GetObjectOutput
dataSourceName := "data.aws_s3_bucket_object.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: testAccAWSDataSourceS3ObjectConfigSingleSlashAsKey(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsS3ObjectDataSourceExists(dataSourceName, &dsObj),
),
},
},
})
}

func testAccCheckAwsS3ObjectDataSourceExists(n string, obj *s3.GetObjectOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -651,3 +671,16 @@ data "aws_s3_bucket_object" "obj3" {

return resources, both
}

func testAccAWSDataSourceS3ObjectConfigSingleSlashAsKey(rName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "test" {
bucket = %[1]q
}

data "aws_s3_bucket_object" "test" {
bucket = aws_s3_bucket.test.bucket
key = "/"
}
`, rName)
}
4 changes: 4 additions & 0 deletions aws/internal/keyvaluetags/s3_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func S3ObjectListTags(conn *s3.S3, bucket, key string) (KeyValueTags, error) {

output, err := conn.GetObjectTagging(input)

if tfawserr.ErrCodeEquals(err, tfs3.NoSuchTagSet) {
return New(nil), nil
}

if err != nil {
return New(nil), err
}
Expand Down