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

r/s3_bucket: read-only logging argument #22599

Merged
merged 7 commits into from
Feb 7, 2022
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
3 changes: 3 additions & 0 deletions .changelog/22599.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
resource/aws_s3_bucket: The `logging` argument has been deprecated and is now read-only. Use the `aws_s3_bucket_logging` resource instead.
```
100 changes: 32 additions & 68 deletions internal/service/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,27 +243,23 @@ func ResourceBucket() *schema.Resource {
},

"logging": {
Type: schema.TypeSet,
Optional: true,
Type: schema.TypeSet,
Computed: true,
Deprecated: "Use the aws_s3_bucket_logging resource instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_bucket": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Computed: true,
Deprecated: "Use the aws_s3_bucket_logging resource instead",
},
"target_prefix": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Computed: true,
Deprecated: "Use the aws_s3_bucket_logging resource instead",
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["target_bucket"]))
buf.WriteString(fmt.Sprintf("%s-", m["target_prefix"]))
return create.StringHashcode(buf.String())
},
},

"lifecycle_rule": {
Expand Down Expand Up @@ -791,12 +787,6 @@ func resourceBucketUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("logging") {
if err := resourceBucketInternalLoggingUpdate(conn, d); err != nil {
return err
}
}

if d.HasChange("lifecycle_rule") {
if err := resourceBucketLifecycleUpdate(conn, d); err != nil {
return err
Expand Down Expand Up @@ -1024,7 +1014,7 @@ func resourceBucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("request_payer", payer.Payer)
}

// Read the logging configuration
// Read the logging configuration if configured outside this resource
loggingResponse, err := verify.RetryOnAWSCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return conn.GetBucketLogging(&s3.GetBucketLoggingInput{
Bucket: aws.String(d.Id()),
Expand All @@ -1035,20 +1025,12 @@ func resourceBucketRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error getting S3 Bucket logging: %s", err)
}

lcl := make([]map[string]interface{}, 0, 1)
if logging, ok := loggingResponse.(*s3.GetBucketLoggingOutput); ok && logging.LoggingEnabled != nil {
v := logging.LoggingEnabled
lc := make(map[string]interface{})
if aws.StringValue(v.TargetBucket) != "" {
lc["target_bucket"] = aws.StringValue(v.TargetBucket)
if logging, ok := loggingResponse.(*s3.GetBucketLoggingOutput); ok {
if err := d.Set("logging", flattenBucketLoggingEnabled(logging.LoggingEnabled)); err != nil {
return fmt.Errorf("error setting logging: %s", err)
}
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
if aws.StringValue(v.TargetPrefix) != "" {
lc["target_prefix"] = aws.StringValue(v.TargetPrefix)
}
lcl = append(lcl, lc)
}
if err := d.Set("logging", lcl); err != nil {
return fmt.Errorf("error setting logging: %s", err)
} else {
d.Set("logging", nil)
}

// Read the lifecycle configuration
Expand Down Expand Up @@ -1608,41 +1590,6 @@ func resourceBucketInternalVersioningUpdate(conn *s3.S3, bucket string, versioni
return nil
}

func resourceBucketInternalLoggingUpdate(conn *s3.S3, d *schema.ResourceData) error {
logging := d.Get("logging").(*schema.Set).List()
bucket := d.Get("bucket").(string)
loggingStatus := &s3.BucketLoggingStatus{}

if len(logging) > 0 {
c := logging[0].(map[string]interface{})

loggingEnabled := &s3.LoggingEnabled{}
if val, ok := c["target_bucket"]; ok {
loggingEnabled.TargetBucket = aws.String(val.(string))
}
if val, ok := c["target_prefix"]; ok {
loggingEnabled.TargetPrefix = aws.String(val.(string))
}

loggingStatus.LoggingEnabled = loggingEnabled
}

i := &s3.PutBucketLoggingInput{
Bucket: aws.String(bucket),
BucketLoggingStatus: loggingStatus,
}
log.Printf("[DEBUG] S3 put bucket logging: %#v", i)

_, err := verify.RetryOnAWSCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
return conn.PutBucketLogging(i)
})
if err != nil {
return fmt.Errorf("Error putting S3 logging: %s", err)
}

return nil
}

func resourceBucketInternalServerSideEncryptionConfigurationUpdate(conn *s3.S3, d *schema.ResourceData) error {
bucket := d.Get("bucket").(string)
serverSideEncryptionConfiguration := d.Get("server_side_encryption_configuration").([]interface{})
Expand Down Expand Up @@ -2067,6 +2014,23 @@ func resourceBucketLifecycleUpdate(conn *s3.S3, d *schema.ResourceData) error {
return nil
}

func flattenBucketLoggingEnabled(loggingEnabled *s3.LoggingEnabled) []interface{} {
if loggingEnabled == nil {
return []interface{}{}
}

m := make(map[string]interface{})

if loggingEnabled.TargetBucket != nil {
m["target_bucket"] = aws.StringValue(loggingEnabled.TargetBucket)
}
if loggingEnabled.TargetPrefix != nil {
m["target_prefix"] = aws.StringValue(loggingEnabled.TargetPrefix)
}

return []interface{}{m}
}

func flattenServerSideEncryptionConfiguration(c *s3.ServerSideEncryptionConfiguration) []map[string]interface{} {
var encryptionConfiguration []map[string]interface{}
rules := make([]interface{}, 0, len(c.Rules))
Expand Down
30 changes: 0 additions & 30 deletions internal/service/s3/bucket_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,6 @@ resource "aws_s3_bucket" "log_bucket" {
resource "aws_s3_bucket" "test" {
bucket = %[1]q
acl = "private"

lifecycle {
ignore_changes = [
logging
]
}
}

resource "aws_s3_bucket_logging" "test" {
Expand All @@ -418,12 +412,6 @@ func testAccBucketLoggingUpdateConfig(rName, targetBucketName, targetPrefix stri
resource "aws_s3_bucket" "test" {
bucket = %[1]q
acl = "private"

lifecycle {
ignore_changes = [
logging
]
}
}

resource "aws_s3_bucket" "log_bucket" {
Expand Down Expand Up @@ -452,12 +440,6 @@ resource "aws_s3_bucket" "log_bucket" {
resource "aws_s3_bucket" "test" {
bucket = %[1]q
acl = "private"

lifecycle {
ignore_changes = [
logging
]
}
}

resource "aws_s3_bucket_logging" "test" {
Expand Down Expand Up @@ -487,12 +469,6 @@ resource "aws_s3_bucket" "log_bucket" {
resource "aws_s3_bucket" "test" {
bucket = %[1]q
acl = "private"

lifecycle {
ignore_changes = [
logging
]
}
}

resource "aws_s3_bucket_logging" "test" {
Expand Down Expand Up @@ -524,12 +500,6 @@ resource "aws_s3_bucket" "log_bucket" {
resource "aws_s3_bucket" "test" {
bucket = %[1]q
acl = "private"

lifecycle {
ignore_changes = [
logging
]
}
}

resource "aws_s3_bucket_logging" "test" {
Expand Down
109 changes: 0 additions & 109 deletions internal/service/s3/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,33 +706,6 @@ func TestAccS3Bucket_Manage_versioningAndMfaDeleteDisabled(t *testing.T) {
})
}

func TestAccS3Bucket_Security_logging(t *testing.T) {
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
bucketName := sdkacctest.RandomWithPrefix("tf-test-bucket")
resourceName := "aws_s3_bucket.bucket"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckBucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccBucketWithLoggingConfig(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBucketExists(resourceName),
testAccCheckBucketLogging(resourceName, "aws_s3_bucket.log_bucket", "log/"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy", "acl"},
},
},
})
}

func TestAccS3Bucket_Manage_lifecycleBasic(t *testing.T) {
bucketName := sdkacctest.RandomWithPrefix("tf-test-bucket")
resourceName := "aws_s3_bucket.bucket"
Expand Down Expand Up @@ -2627,49 +2600,6 @@ func testAccCheckBucketPolicy(n string, policy string) resource.TestCheckFunc {
}
}

func testAccCheckBucketLogging(n, b, p string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs := s.RootModule().Resources[n]
conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn

out, err := conn.GetBucketLogging(&s3.GetBucketLoggingInput{
Bucket: aws.String(rs.Primary.ID),
})

if err != nil {
return fmt.Errorf("GetBucketLogging error: %v", err)
}

if out.LoggingEnabled == nil {
return fmt.Errorf("logging not enabled for bucket: %s", rs.Primary.ID)
}

tb := s.RootModule().Resources[b]

if v := out.LoggingEnabled.TargetBucket; v == nil {
if tb.Primary.ID != "" {
return fmt.Errorf("bad target bucket, found nil, expected: %s", tb.Primary.ID)
}
} else {
if *v != tb.Primary.ID {
return fmt.Errorf("bad target bucket, expected: %s, got %s", tb.Primary.ID, *v)
}
}

if v := out.LoggingEnabled.TargetPrefix; v == nil {
if p != "" {
return fmt.Errorf("bad target prefix, found nil, expected: %s", p)
}
} else {
if *v != p {
return fmt.Errorf("bad target prefix, expected: %s, got %s", p, *v)
}
}

return nil
}
}

func testAccCheckBucketReplicationRules(n string, rules []*s3.ReplicationRule) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs := s.RootModule().Resources[n]
Expand Down Expand Up @@ -3186,45 +3116,6 @@ resource "aws_s3_bucket" "bucket" {
`, bucketName, enabled, mfaDelete)
}

func testAccBucketWithLoggingConfig(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "log_bucket" {
bucket = "%[1]s-log"

lifecycle {
ignore_changes = [
grant,
]
}
}

resource "aws_s3_bucket_acl" "log_bucket" {
bucket = aws_s3_bucket.log_bucket.id
acl = "log-delivery-write"
}

resource "aws_s3_bucket" "bucket" {
bucket = %[1]q

logging {
target_bucket = aws_s3_bucket.log_bucket.id
target_prefix = "log/"
}

lifecycle {
ignore_changes = [
grant,
]
}
}

resource "aws_s3_bucket_acl" "test" {
bucket = aws_s3_bucket.bucket.id
acl = "private"
}
`, bucketName)
}

func testAccBucketWithLifecycleConfig(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "bucket" {
Expand Down
Loading