Skip to content

Commit 8112cdf

Browse files
authored
feat(misconf): adapt AWS::DynamoDB::Table (#8529)
Signed-off-by: nikpivkin <[email protected]>
1 parent 124e161 commit 8112cdf

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

pkg/iac/adapters/cloudformation/aws/dynamodb/dynamodb.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ import (
99
func Adapt(cfFile parser.FileContext) dynamodb.DynamoDB {
1010
return dynamodb.DynamoDB{
1111
DAXClusters: getClusters(cfFile),
12+
Tables: getTables(cfFile),
1213
}
1314
}

pkg/iac/adapters/cloudformation/aws/dynamodb/dynamodb_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ Resources:
2323
Properties:
2424
SSESpecification:
2525
SSEEnabled: true
26+
DDBTable:
27+
Type: AWS::DynamoDB::Table
28+
Properties:
29+
SSESpecification:
30+
SSEEnabled: true
31+
KMSMasterKeyId: "test"
32+
PointInTimeRecoverySpecification:
33+
PointInTimeRecoveryEnabled: true
34+
2635
`,
2736
expected: dynamodb.DynamoDB{
2837
DAXClusters: []dynamodb.DAXCluster{
@@ -32,6 +41,15 @@ Resources:
3241
},
3342
},
3443
},
44+
Tables: []dynamodb.Table{
45+
{
46+
ServerSideEncryption: dynamodb.ServerSideEncryption{
47+
Enabled: types.BoolTest(true),
48+
KMSKeyID: types.StringTest("test"),
49+
},
50+
PointInTimeRecovery: types.BoolTest(true),
51+
},
52+
},
3553
},
3654
},
3755
{
@@ -40,9 +58,18 @@ Resources:
4058
Resources:
4159
daxCluster:
4260
Type: AWS::DAX::Cluster
61+
DDBTable:
62+
Type: AWS::DynamoDB::Table
4363
`,
4464
expected: dynamodb.DynamoDB{
4565
DAXClusters: []dynamodb.DAXCluster{{}},
66+
Tables: []dynamodb.Table{
67+
{
68+
ServerSideEncryption: dynamodb.ServerSideEncryption{
69+
KMSKeyID: types.StringTest(dynamodb.DefaultKMSKeyID),
70+
},
71+
},
72+
},
4673
},
4774
},
4875
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dynamodb
2+
3+
import (
4+
"github.com/samber/lo"
5+
6+
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/dynamodb"
7+
"github.com/aquasecurity/trivy/pkg/iac/scanners/cloudformation/parser"
8+
)
9+
10+
func getTables(fctx parser.FileContext) []dynamodb.Table {
11+
return lo.Map(fctx.GetResourcesByType("AWS::DynamoDB::Table"), func(
12+
resource *parser.Resource, _ int,
13+
) dynamodb.Table {
14+
sseSpec := resource.GetProperty("SSESpecification")
15+
return dynamodb.Table{
16+
Metadata: resource.Metadata(),
17+
ServerSideEncryption: dynamodb.ServerSideEncryption{
18+
Metadata: sseSpec.Metadata(),
19+
Enabled: sseSpec.GetBoolProperty("SSEEnabled"),
20+
KMSKeyID: sseSpec.GetStringProperty("KMSMasterKeyId", dynamodb.DefaultKMSKeyID),
21+
},
22+
PointInTimeRecovery: resource.GetBoolProperty("PointInTimeRecoverySpecification.PointInTimeRecoveryEnabled"),
23+
}
24+
})
25+
}

pkg/iac/adapters/terraform/aws/dynamodb/adapt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func adaptTable(resource *terraform.Block, module *terraform.Module) dynamodb.Ta
7777
table.ServerSideEncryption.Enabled = enabledAttr.AsBoolValueOrDefault(false, ssEncryptionBlock)
7878

7979
kmsKeyIdAttr := ssEncryptionBlock.GetAttribute("kms_key_arn")
80-
table.ServerSideEncryption.KMSKeyID = kmsKeyIdAttr.AsStringValueOrDefault("alias/aws/dynamodb", ssEncryptionBlock)
80+
table.ServerSideEncryption.KMSKeyID = kmsKeyIdAttr.AsStringValueOrDefault(dynamodb.DefaultKMSKeyID, ssEncryptionBlock)
8181

8282
kmsBlock, err := module.GetReferencedBlock(kmsKeyIdAttr, resource)
8383
if err == nil && kmsBlock.IsNotNil() {

pkg/iac/adapters/terraform/aws/dynamodb/adapt_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func Test_adaptTable(t *testing.T) {
9494
ServerSideEncryption: dynamodb.ServerSideEncryption{
9595
Metadata: iacTypes.NewTestMetadata(),
9696
Enabled: iacTypes.Bool(true, iacTypes.NewTestMetadata()),
97-
KMSKeyID: iacTypes.String("alias/aws/dynamodb", iacTypes.NewTestMetadata()),
97+
KMSKeyID: iacTypes.String(dynamodb.DefaultKMSKeyID, iacTypes.NewTestMetadata()),
9898
},
9999
PointInTimeRecovery: iacTypes.Bool(false, iacTypes.NewTestMetadata()),
100100
},

0 commit comments

Comments
 (0)