diff --git a/examples/terraform-aws-s3-example/main.tf b/examples/terraform-aws-s3-example/main.tf index 735a3894e..8c0c4a17f 100644 --- a/examples/terraform-aws-s3-example/main.tf +++ b/examples/terraform-aws-s3-example/main.tf @@ -75,7 +75,7 @@ resource "aws_s3_bucket" "test_bucket" { logging { target_bucket = aws_s3_bucket.test_bucket_logs.id - target_prefix = "/" + target_prefix = "TFStateLogs/" } tags = { diff --git a/examples/terraform-aws-s3-example/outputs.tf b/examples/terraform-aws-s3-example/outputs.tf index 7e3790d75..21001977a 100644 --- a/examples/terraform-aws-s3-example/outputs.tf +++ b/examples/terraform-aws-s3-example/outputs.tf @@ -8,4 +8,8 @@ output "bucket_arn" { output "logging_target_bucket" { value = tolist(aws_s3_bucket.test_bucket.logging)[0].target_bucket +} + +output "logging_target_prefix" { + value = tolist(aws_s3_bucket.test_bucket.logging)[0].target_prefix } \ No newline at end of file diff --git a/modules/aws/s3.go b/modules/aws/s3.go index 90cd580fd..e3c05eb43 100644 --- a/modules/aws/s3.go +++ b/modules/aws/s3.go @@ -261,7 +261,7 @@ func EmptyS3BucketE(t testing.TestingT, region string, name string) error { return err } -// GetS3BucketLoggingTarget fetches the given bucket's logging configuration status and returns it as a string +// GetS3BucketLoggingTarget fetches the given bucket's logging target bucket and returns it as a string func GetS3BucketLoggingTarget(t testing.TestingT, awsRegion string, bucket string) string { loggingTarget, err := GetS3BucketLoggingTargetE(t, awsRegion, bucket) require.NoError(t, err) @@ -269,7 +269,8 @@ func GetS3BucketLoggingTarget(t testing.TestingT, awsRegion string, bucket strin return loggingTarget } -// GetS3BucketLoggingE fetches the given bucket's versioning configuration status and returns it as a string +// GetS3BucketLoggingTargetE fetches the given bucket's logging target bucket and returns it as the following string: +// `TargetBucket` of the `LoggingEnabled` property for an S3 bucket func GetS3BucketLoggingTargetE(t testing.TestingT, awsRegion string, bucket string) (string, error) { s3Client, err := NewS3ClientE(t, awsRegion) if err != nil { @@ -291,6 +292,37 @@ func GetS3BucketLoggingTargetE(t testing.TestingT, awsRegion string, bucket stri return aws.StringValue(res.LoggingEnabled.TargetBucket), nil } +// GetS3BucketLoggingTargetPrefix fetches the given bucket's logging object prefix and returns it as a string +func GetS3BucketLoggingTargetPrefix(t testing.TestingT, awsRegion string, bucket string) string { + loggingObjectTargetPrefix, err := GetS3BucketLoggingTargetPrefixE(t, awsRegion, bucket) + require.NoError(t, err) + + return loggingObjectTargetPrefix +} + +// GetS3BucketLoggingTargetPrefixE fetches the given bucket's logging object prefix and returns it as the following string: +// `TargetPrefix` of the `LoggingEnabled` property for an S3 bucket +func GetS3BucketLoggingTargetPrefixE(t testing.TestingT, awsRegion string, bucket string) (string, error) { + s3Client, err := NewS3ClientE(t, awsRegion) + if err != nil { + return "", err + } + + res, err := s3Client.GetBucketLogging(&s3.GetBucketLoggingInput{ + Bucket: &bucket, + }) + + if err != nil { + return "", err + } + + if res.LoggingEnabled == nil { + return "", S3AccessLoggingNotEnabledErr{bucket, awsRegion} + } + + return aws.StringValue(res.LoggingEnabled.TargetPrefix), nil +} + // GetS3BucketVersioning fetches the given bucket's versioning configuration status and returns it as a string func GetS3BucketVersioning(t testing.TestingT, awsRegion string, bucket string) string { versioningStatus, err := GetS3BucketVersioningE(t, awsRegion, bucket) diff --git a/test/terraform_aws_s3_example_test.go b/test/terraform_aws_s3_example_test.go index 133894d81..0cb8f8361 100644 --- a/test/terraform_aws_s3_example_test.go +++ b/test/terraform_aws_s3_example_test.go @@ -64,5 +64,9 @@ func TestTerraformAwsS3Example(t *testing.T) { // Verify that our bucket has server access logging TargetBucket set to what's expected loggingTargetBucket := aws.GetS3BucketLoggingTarget(t, awsRegion, bucketID) expectedLogsTargetBucket := fmt.Sprintf("%s-logs", bucketID) + loggingObjectTargetPrefix := aws.GetS3BucketLoggingTargetPrefix(t, awsRegion, bucketID) + expectedLogsTargetPrefix := "TFStateLogs/" + assert.Equal(t, expectedLogsTargetBucket, loggingTargetBucket) + assert.Equal(t, expectedLogsTargetPrefix, loggingObjectTargetPrefix) }