-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Support encryption at rest for DynamoDB tables #3303
Changes from 1 commit
2114a3b
1aa8400
774d415
80af905
5223968
b4ddab7
5c58790
0eb8914
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,21 @@ func resourceAwsDynamoDbTable() *schema.Resource { | |
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"encrypt_at_rest": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"enabled": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"tags": tagsSchema(), | ||
}, | ||
} | ||
|
@@ -250,6 +265,16 @@ func resourceAwsDynamoDbTableCreate(d *schema.ResourceData, meta interface{}) er | |
} | ||
} | ||
|
||
if v, ok := d.GetOk("encrypt_at_rest"); ok { | ||
options := v.([]interface{}) | ||
if options[0] == nil { | ||
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. I think this needs to check s := make([]interface{}, 0)
if s[0] == nil {
log.Println("All is well")
}
# panic: runtime error: index out of range
s := make([]interface{}, 0)
if len(s) == 0 {
log.Println("All is well")
}
# 2018/03/01 15:19:00 All is well 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. Generally speaking though, we forego error checking against our own schema. 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. Doesn't |
||
return fmt.Errorf("At least one field is expected inside encrypt_at_rest") | ||
} | ||
|
||
s := options[0].(map[string]interface{}) | ||
req.SSESpecification = expandDynamoDbEncryptAtRestOptions(s) | ||
} | ||
|
||
var output *dynamodb.CreateTableOutput | ||
err := resource.Retry(2*time.Minute, func() *resource.RetryError { | ||
var err error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,7 +295,7 @@ func TestAccAWSDynamoDbTable_basic(t *testing.T) { | |
Config: testAccAWSDynamoDbConfigInitialState(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckInitialAWSDynamoDbTableExists("aws_dynamodb_table.basic-dynamodb-table", &conf), | ||
testAccCheckInitialAWSDynamoDbTableConf("aws_dynamodb_table.basic-dynamodb-table"), | ||
testAccCheckInitialAWSDynamoDbTableConf("aws_dynamodb_table.basic-dynamodb-table", false), | ||
), | ||
}, | ||
{ | ||
|
@@ -368,7 +368,7 @@ func TestAccAWSDynamoDbTable_tags(t *testing.T) { | |
Config: testAccAWSDynamoDbConfigTags(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckInitialAWSDynamoDbTableExists("aws_dynamodb_table.basic-dynamodb-table", &conf), | ||
testAccCheckInitialAWSDynamoDbTableConf("aws_dynamodb_table.basic-dynamodb-table"), | ||
testAccCheckInitialAWSDynamoDbTableConf("aws_dynamodb_table.basic-dynamodb-table", false), | ||
resource.TestCheckResourceAttr( | ||
"aws_dynamodb_table.basic-dynamodb-table", "tags.%", "3"), | ||
), | ||
|
@@ -625,6 +625,27 @@ func testAccCheckDynamoDbTableTimeToLiveWasUpdated(n string) resource.TestCheckF | |
} | ||
} | ||
|
||
func TestAccAWSDynamoDbTable_encryption(t *testing.T) { | ||
var conf dynamodb.DescribeTableOutput | ||
|
||
rName := acctest.RandomWithPrefix("TerraformTestTable-") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSDynamoDbTableDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSDynamoDbConfigInitialStateWithEncryption(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckInitialAWSDynamoDbTableExists("aws_dynamodb_table.basic-dynamodb-table", &conf), | ||
testAccCheckInitialAWSDynamoDbTableConf("aws_dynamodb_table.basic-dynamodb-table", true), | ||
), | ||
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. Can you please check the Terraform state here? resource.TestCheckResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "server_side_encryption.#", "1"),
resource.TestCheckResourceAttr("aws_dynamodb_table.basic-dynamodb-table", "server_side_encryption.0.enabled", "true"), 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. Will do 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. By doing this and removing the extraneous attributes I can now revert the modifications to |
||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestResourceAWSDynamoDbTableStreamViewType_validation(t *testing.T) { | ||
cases := []struct { | ||
Value string | ||
|
@@ -725,7 +746,7 @@ func testAccCheckInitialAWSDynamoDbTableExists(n string, table *dynamodb.Describ | |
} | ||
} | ||
|
||
func testAccCheckInitialAWSDynamoDbTableConf(n string) resource.TestCheckFunc { | ||
func testAccCheckInitialAWSDynamoDbTableConf(n string, sse bool) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
log.Printf("[DEBUG] Trying to create initial table state!") | ||
rs, ok := s.RootModule().Resources[n] | ||
|
@@ -761,6 +782,16 @@ func testAccCheckInitialAWSDynamoDbTableConf(n string) resource.TestCheckFunc { | |
return fmt.Errorf("Provisioned read capacity was %d, not 10!", table.ProvisionedThroughput.ReadCapacityUnits) | ||
} | ||
|
||
if sse { | ||
if table.SSEDescription == nil || *table.SSEDescription.Status != dynamodb.SSEStatusEnabled { | ||
return fmt.Errorf("SSE status was not %s", dynamodb.SSEStatusEnabled) | ||
} | ||
} else { | ||
if table.SSEDescription != nil && *table.SSEDescription.Status != dynamodb.SSEStatusDisabled { | ||
return fmt.Errorf("SSE status was %s, not %s", *table.SSEDescription.Status, dynamodb.SSEStatusDisabled) | ||
} | ||
} | ||
|
||
attrCount := len(table.AttributeDefinitions) | ||
gsiCount := len(table.GlobalSecondaryIndexes) | ||
lsiCount := len(table.LocalSecondaryIndexes) | ||
|
@@ -927,6 +958,57 @@ resource "aws_dynamodb_table" "basic-dynamodb-table" { | |
`, rName) | ||
} | ||
|
||
func testAccAWSDynamoDbConfigInitialStateWithEncryption(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_dynamodb_table" "basic-dynamodb-table" { | ||
name = "%s" | ||
read_capacity = 10 | ||
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. Can you please set the test capacities to 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. Will do |
||
write_capacity = 20 | ||
hash_key = "TestTableHashKey" | ||
range_key = "TestTableRangeKey" | ||
|
||
attribute { | ||
name = "TestTableHashKey" | ||
type = "S" | ||
} | ||
|
||
attribute { | ||
name = "TestTableRangeKey" | ||
type = "S" | ||
} | ||
|
||
attribute { | ||
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. Can you please remove extraneous attributes/indexes here? We only really care about the encryption setting. 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. Will do |
||
name = "TestLSIRangeKey" | ||
type = "N" | ||
} | ||
|
||
attribute { | ||
name = "TestGSIRangeKey" | ||
type = "S" | ||
} | ||
|
||
local_secondary_index { | ||
name = "TestTableLSI" | ||
range_key = "TestLSIRangeKey" | ||
projection_type = "ALL" | ||
} | ||
|
||
global_secondary_index { | ||
name = "InitialTestTableGSI" | ||
hash_key = "TestTableHashKey" | ||
range_key = "TestGSIRangeKey" | ||
write_capacity = 10 | ||
read_capacity = 10 | ||
projection_type = "KEYS_ONLY" | ||
} | ||
|
||
encrypt_at_rest { | ||
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. Nitpick: Indentation got thrown off here 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. Will do, copy and paste flub |
||
enabled = true | ||
} | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAWSDynamoDbConfigAddSecondaryGSI(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_dynamodb_table" "basic-dynamodb-table" { | ||
|
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.
😅 Sorry for this, but could you rename all these
encrypt_at_rest
references toserver_side_encryption
so they better match the DynamoDB APISSE
?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.
Will do