-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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/aws_s3_bucket: support default server side encryption configuration #2472
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
897ac16
#2217: updated r/aws_s3_bucket to support default server side encrypt…
trung c74d8af
#2217: make fmt
trung f03a0a1
Merge branch 'master' into f-r-2217
trung 3a3c6da
Merge branch 'master' into f-r-2217
trung aeb4624
#2217: wrote typical acceptance test
trung aaa2357
#2217: wrote additional acceptance test
trung 1698f93
Merge branch 'master' into f-r-2217
trung 1f3fd42
#2217: re-enable default encryption after disabling in via UI
trung f19e809
#2217: documentation
trung bfa7843
Merge branch 'master' into f-r-2217
trung df3405e
#2217: fixed when encryption is AES256
trung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,6 +392,42 @@ func resourceAwsS3Bucket() *schema.Resource { | |
}, | ||
}, | ||
|
||
"server_side_encryption_configuration": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"rule": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"apply_server_side_encryption_by_default": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"kms_master_key_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"sse_algorithm": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"tags": tagsSchema(), | ||
}, | ||
} | ||
|
@@ -531,6 +567,12 @@ func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error { | |
} | ||
} | ||
|
||
if d.HasChange("server_side_encryption_configuration") { | ||
if err := resourceAwsS3BucketServerSideEncryptionConfigurationUpdate(s3conn, d); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return resourceAwsS3BucketRead(d, meta) | ||
} | ||
|
||
|
@@ -941,6 +983,31 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { | |
} | ||
} | ||
|
||
// Read the bucket server side encryption configuration | ||
|
||
encryptionResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { | ||
return s3conn.GetBucketEncryption(&s3.GetBucketEncryptionInput{ | ||
Bucket: aws.String(d.Id()), | ||
}) | ||
}) | ||
if err != nil { | ||
if isAWSErr(err, "ServerSideEncryptionConfigurationNotFoundError", "encryption configuration was not found") { | ||
log.Printf("[DEBUG] Default encryption is not enabled for %s", d.Id()) | ||
d.Set("server_side_encryption_configuration", []map[string]interface{}{}) | ||
} else { | ||
return err | ||
} | ||
} else { | ||
encryption := encryptionResponse.(*s3.GetBucketEncryptionOutput) | ||
log.Printf("[DEBUG] S3 Bucket: %s, read encryption configuration: %v", d.Id(), encryption) | ||
if c := encryption.ServerSideEncryptionConfiguration; c != nil { | ||
if err := d.Set("server_side_encryption_configuration", flatternAwsS3ServerSideEncryptionConfiguration(c)); err != nil { | ||
log.Printf("[DEBUG] Error setting server side encryption configuration: %s", err) | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// Add the region as an attribute | ||
|
||
locationResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { | ||
|
@@ -1493,6 +1560,68 @@ func resourceAwsS3BucketRequestPayerUpdate(s3conn *s3.S3, d *schema.ResourceData | |
return nil | ||
} | ||
|
||
func resourceAwsS3BucketServerSideEncryptionConfigurationUpdate(s3conn *s3.S3, d *schema.ResourceData) error { | ||
bucket := d.Get("bucket").(string) | ||
serverSideEncryptionConfiguration := d.Get("server_side_encryption_configuration").([]interface{}) | ||
if len(serverSideEncryptionConfiguration) == 0 { | ||
log.Printf("[DEBUG] Delete server side encryption configuration: %#v", serverSideEncryptionConfiguration) | ||
i := &s3.DeleteBucketEncryptionInput{ | ||
Bucket: aws.String(bucket), | ||
} | ||
|
||
err := resource.Retry(1*time.Minute, func() *resource.RetryError { | ||
if _, err := s3conn.DeleteBucketEncryption(i); err != nil { | ||
return resource.NonRetryableError(err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error removing S3 bucket server side encryption: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
c := serverSideEncryptionConfiguration[0].(map[string]interface{}) | ||
|
||
rc := &s3.ServerSideEncryptionConfiguration{} | ||
|
||
rcRules := c["rule"].([]interface{}) | ||
var rules []*s3.ServerSideEncryptionRule | ||
for _, v := range rcRules { | ||
rr := v.(map[string]interface{}) | ||
rrDefault := rr["apply_server_side_encryption_by_default"].([]interface{}) | ||
sseAlgorithm := rrDefault[0].(map[string]interface{})["sse_algorithm"].(string) | ||
kmsMasterKeyId := rrDefault[0].(map[string]interface{})["kms_master_key_id"].(string) | ||
rcDefaultRule := &s3.ServerSideEncryptionByDefault{ | ||
SSEAlgorithm: aws.String(sseAlgorithm), | ||
} | ||
if kmsMasterKeyId != "" { | ||
rcDefaultRule.KMSMasterKeyID = aws.String(kmsMasterKeyId) | ||
} | ||
rcRule := &s3.ServerSideEncryptionRule{ | ||
ApplyServerSideEncryptionByDefault: rcDefaultRule, | ||
} | ||
|
||
rules = append(rules, rcRule) | ||
} | ||
|
||
rc.Rules = rules | ||
i := &s3.PutBucketEncryptionInput{ | ||
Bucket: aws.String(bucket), | ||
ServerSideEncryptionConfiguration: rc, | ||
} | ||
log.Printf("[DEBUG] S3 put bucket replication configuration: %#v", i) | ||
|
||
_, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { | ||
return s3conn.PutBucketEncryption(i) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error putting S3 server side encryption configuration: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsS3BucketReplicationConfigurationUpdate(s3conn *s3.S3, d *schema.ResourceData) error { | ||
bucket := d.Get("bucket").(string) | ||
replicationConfiguration := d.Get("replication_configuration").([]interface{}) | ||
|
@@ -1739,6 +1868,25 @@ func resourceAwsS3BucketLifecycleUpdate(s3conn *s3.S3, d *schema.ResourceData) e | |
return nil | ||
} | ||
|
||
func flatternAwsS3ServerSideEncryptionConfiguration(c *s3.ServerSideEncryptionConfiguration) []map[string]interface{} { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||
var encryptionConfiguration []map[string]interface{} | ||
rules := make([]interface{}, 0, len(c.Rules)) | ||
for _, v := range c.Rules { | ||
if v.ApplyServerSideEncryptionByDefault != nil { | ||
r := make(map[string]interface{}) | ||
d := make(map[string]interface{}) | ||
d["kms_master_key_id"] = aws.StringValue(v.ApplyServerSideEncryptionByDefault.KMSMasterKeyID) | ||
d["sse_algorithm"] = aws.StringValue(v.ApplyServerSideEncryptionByDefault.SSEAlgorithm) | ||
r["apply_server_side_encryption_by_default"] = []map[string]interface{}{d} | ||
rules = append(rules, r) | ||
} | ||
} | ||
encryptionConfiguration = append(encryptionConfiguration, map[string]interface{}{ | ||
"rule": rules, | ||
}) | ||
return encryptionConfiguration | ||
} | ||
|
||
func flattenAwsS3BucketReplicationConfiguration(r *s3.ReplicationConfiguration) []map[string]interface{} { | ||
replication_configuration := make([]map[string]interface{}, 0, 1) | ||
m := make(map[string]interface{}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should consider adding a validation function here - the valid values are
aws:kms
orAES256
. It's conceivable that this list will expand in the future, but additional values can easily be added. This will move a potential error from apply time to plan time, which is desirable.