-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
instance.go
133 lines (109 loc) · 4.79 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package rds
import (
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/rds"
"github.com/aquasecurity/trivy/pkg/iac/scanners/cloudformation/parser"
"github.com/aquasecurity/trivy/pkg/iac/types"
)
func getClustersAndInstances(ctx parser.FileContext) ([]rds.Cluster, []rds.Instance) {
clusterMap := getClusters(ctx)
var orphans []rds.Instance
for _, r := range ctx.GetResourcesByType("AWS::RDS::DBInstance") {
instance := rds.Instance{
Metadata: r.Metadata(),
BackupRetentionPeriodDays: r.GetIntProperty("BackupRetentionPeriod", 1),
ReplicationSourceARN: r.GetStringProperty("SourceDBInstanceIdentifier"),
PerformanceInsights: rds.PerformanceInsights{
Metadata: r.Metadata(),
Enabled: r.GetBoolProperty("EnablePerformanceInsights"),
KMSKeyID: r.GetStringProperty("PerformanceInsightsKMSKeyId"),
},
Encryption: rds.Encryption{
Metadata: r.Metadata(),
EncryptStorage: r.GetBoolProperty("StorageEncrypted"),
KMSKeyID: r.GetStringProperty("KmsKeyId"),
},
PublicAccess: r.GetBoolProperty("PubliclyAccessible", true),
Engine: r.GetStringProperty("Engine"),
IAMAuthEnabled: r.GetBoolProperty("EnableIAMDatabaseAuthentication"),
DeletionProtection: r.GetBoolProperty("DeletionProtection", false),
DBInstanceArn: r.GetStringProperty("DBInstanceArn"),
StorageEncrypted: r.GetBoolProperty("StorageEncrypted", false),
DBInstanceIdentifier: r.GetStringProperty("DBInstanceIdentifier"),
DBParameterGroups: getDBParameterGroups(ctx, r),
TagList: getTagList(r),
EnabledCloudwatchLogsExports: getEnabledCloudwatchLogsExports(r),
EngineVersion: r.GetStringProperty("EngineVersion"),
AutoMinorVersionUpgrade: r.GetBoolProperty("AutoMinorVersionUpgrade"),
MultiAZ: r.GetBoolProperty("MultiAZ"),
PubliclyAccessible: r.GetBoolProperty("PubliclyAccessible"),
LatestRestorableTime: types.TimeUnresolvable(r.Metadata()),
ReadReplicaDBInstanceIdentifiers: getReadReplicaDBInstanceIdentifiers(r),
}
if clusterID := r.GetProperty("DBClusterIdentifier"); clusterID.IsString() {
if cluster, exist := clusterMap[clusterID.AsString()]; exist {
cluster.Instances = append(cluster.Instances, rds.ClusterInstance{
Instance: instance,
ClusterIdentifier: clusterID.AsStringValue(),
})
clusterMap[clusterID.AsString()] = cluster
}
} else {
orphans = append(orphans, instance)
}
}
clusters := make([]rds.Cluster, 0, len(clusterMap))
for _, cluster := range clusterMap {
clusters = append(clusters, cluster)
}
return clusters, orphans
}
func getDBParameterGroups(ctx parser.FileContext, r *parser.Resource) (dbParameterGroup []rds.DBParameterGroupsList) {
var parameterGroupList []rds.DBParameterGroupsList
dbParameterGroupName := r.GetStringProperty("DBParameterGroupName")
for _, r := range ctx.GetResourcesByType("AWS::RDS::DBParameterGroup") {
name := r.GetStringProperty("DBParameterGroupName")
// TODO: find by resource logical id
if !dbParameterGroupName.EqualTo(name.Value()) {
continue
}
dbpmgl := rds.DBParameterGroupsList{
Metadata: r.Metadata(),
DBParameterGroupName: name,
KMSKeyID: types.StringUnresolvable(r.Metadata()),
}
parameterGroupList = append(dbParameterGroup, dbpmgl)
}
return parameterGroupList
}
func getEnabledCloudwatchLogsExports(r *parser.Resource) (enabledcloudwatchlogexportslist []types.StringValue) {
enabledCloudwatchLogExportList := r.GetProperty("EnableCloudwatchLogsExports")
if enabledCloudwatchLogExportList.IsNil() || enabledCloudwatchLogExportList.IsNotList() {
return enabledcloudwatchlogexportslist
}
for _, ecle := range enabledCloudwatchLogExportList.AsList() {
enabledcloudwatchlogexportslist = append(enabledcloudwatchlogexportslist, ecle.AsStringValue())
}
return enabledcloudwatchlogexportslist
}
func getTagList(r *parser.Resource) (taglist []rds.TagList) {
tagLists := r.GetProperty("Tags")
if tagLists.IsNil() || tagLists.IsNotList() {
return taglist
}
for _, tl := range tagLists.AsList() {
taglist = append(taglist, rds.TagList{
Metadata: tl.Metadata(),
})
}
return taglist
}
func getReadReplicaDBInstanceIdentifiers(r *parser.Resource) (readreplicadbidentifier []types.StringValue) {
readReplicaDBIdentifier := r.GetProperty("SourceDBInstanceIdentifier")
if readReplicaDBIdentifier.IsNil() || readReplicaDBIdentifier.IsNotList() {
return readreplicadbidentifier
}
for _, rr := range readReplicaDBIdentifier.AsList() {
readreplicadbidentifier = append(readreplicadbidentifier, rr.AsStringValue())
}
return readreplicadbidentifier
}