forked from aquasecurity/trivy-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapt.go
202 lines (165 loc) · 5.61 KB
/
adapt.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package elasticache
import (
"github.com/aquasecurity/trivy-aws/internal/adapters/cloud/aws"
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/elasticache"
"github.com/aquasecurity/trivy/pkg/iac/state"
trivyTypes "github.com/aquasecurity/trivy/pkg/iac/types"
"github.com/aquasecurity/trivy/pkg/log"
api "github.com/aws/aws-sdk-go-v2/service/elasticache"
"github.com/aws/aws-sdk-go-v2/service/elasticache/types"
"github.com/aquasecurity/trivy-aws/pkg/concurrency"
)
type adapter struct {
*aws.RootAdapter
api *api.Client
}
func init() {
aws.RegisterServiceAdapter(&adapter{})
}
func (a *adapter) Provider() string {
return "aws"
}
func (a *adapter) Name() string {
return "elasticache"
}
func (a *adapter) Adapt(root *aws.RootAdapter, state *state.State) error {
a.RootAdapter = root
a.api = api.NewFromConfig(root.SessionConfig())
var err error
state.AWS.ElastiCache.Clusters, err = a.getClusters()
if err != nil {
return err
}
state.AWS.ElastiCache.ReplicationGroups, err = a.getReplicationGroups()
if err != nil {
return err
}
// this can error if classic resources are requested where not available
state.AWS.ElastiCache.SecurityGroups, _ = a.getSecurityGroups()
return nil
}
func (a *adapter) getClusters() ([]elasticache.Cluster, error) {
a.Tracker().SetServiceLabel("Discovering clusters...")
var input api.DescribeCacheClustersInput
var apiClusters []types.CacheCluster
for {
output, err := a.api.DescribeCacheClusters(a.Context(), &input)
if err != nil {
return nil, err
}
apiClusters = append(apiClusters, output.CacheClusters...)
a.Tracker().SetTotalResources(len(apiClusters))
if output.Marker == nil {
break
}
input.Marker = output.Marker
}
a.Tracker().SetServiceLabel("Adapting clusters...")
return concurrency.Adapt(apiClusters, a.RootAdapter, a.adaptCluster), nil
}
func (a *adapter) adaptCluster(apiCluster types.CacheCluster) (*elasticache.Cluster, error) {
metadata := a.CreateMetadataFromARN(*apiCluster.ARN)
engine := trivyTypes.StringDefault("", metadata)
if apiCluster.Engine != nil {
engine = trivyTypes.String(*apiCluster.Engine, metadata)
}
nodeType := trivyTypes.StringDefault("", metadata)
if apiCluster.CacheNodeType != nil {
nodeType = trivyTypes.String(*apiCluster.CacheNodeType, metadata)
}
limit := trivyTypes.IntDefault(0, metadata)
if apiCluster.SnapshotRetentionLimit != nil {
limit = trivyTypes.Int(int(*apiCluster.SnapshotRetentionLimit), metadata)
}
return &elasticache.Cluster{
Metadata: metadata,
Engine: engine,
NodeType: nodeType,
SnapshotRetentionLimit: limit,
}, nil
}
func (a *adapter) getReplicationGroups() ([]elasticache.ReplicationGroup, error) {
a.Tracker().SetServiceLabel("Discovering replication groups...")
var input api.DescribeReplicationGroupsInput
var apiGroups []types.ReplicationGroup
for {
output, err := a.api.DescribeReplicationGroups(a.Context(), &input)
if err != nil {
return nil, err
}
apiGroups = append(apiGroups, output.ReplicationGroups...)
a.Tracker().SetTotalResources(len(apiGroups))
if output.Marker == nil {
break
}
input.Marker = output.Marker
}
a.Tracker().SetServiceLabel("Adapting replication groups...")
var groups []elasticache.ReplicationGroup
for _, apiGroup := range apiGroups {
group, err := a.adaptReplicationGroup(apiGroup)
if err != nil {
a.Logger().Error("Failed to adapt replication group", log.String("ARN", *apiGroup.ARN), log.Err(err))
continue
}
groups = append(groups, *group)
a.Tracker().IncrementResource()
}
return groups, nil
}
func (a *adapter) adaptReplicationGroup(apiGroup types.ReplicationGroup) (*elasticache.ReplicationGroup, error) {
metadata := a.CreateMetadataFromARN(*apiGroup.ARN)
transitEncrypted := trivyTypes.BoolDefault(false, metadata)
if apiGroup.TransitEncryptionEnabled != nil {
transitEncrypted = trivyTypes.Bool(*apiGroup.TransitEncryptionEnabled, metadata)
}
atRestEncrypted := trivyTypes.BoolDefault(false, metadata)
if apiGroup.AtRestEncryptionEnabled != nil {
atRestEncrypted = trivyTypes.Bool(*apiGroup.AtRestEncryptionEnabled, metadata)
}
return &elasticache.ReplicationGroup{
Metadata: metadata,
TransitEncryptionEnabled: transitEncrypted,
AtRestEncryptionEnabled: atRestEncrypted,
}, nil
}
func (a *adapter) getSecurityGroups() ([]elasticache.SecurityGroup, error) {
a.Tracker().SetServiceLabel("Discovering security groups...")
var input api.DescribeCacheSecurityGroupsInput
var apiGroups []types.CacheSecurityGroup
for {
output, err := a.api.DescribeCacheSecurityGroups(a.Context(), &input)
if err != nil {
return nil, err
}
apiGroups = append(apiGroups, output.CacheSecurityGroups...)
a.Tracker().SetTotalResources(len(apiGroups))
if output.Marker == nil {
break
}
input.Marker = output.Marker
}
a.Tracker().SetServiceLabel("Adapting security groups...")
var groups []elasticache.SecurityGroup
for _, apiGroup := range apiGroups {
group, err := a.adaptSecurityGroup(apiGroup)
if err != nil {
a.Logger().Error("Failed to adapt security group", log.String("ARN", *apiGroup.ARN), log.Err(err))
continue
}
groups = append(groups, *group)
a.Tracker().IncrementResource()
}
return groups, nil
}
func (a *adapter) adaptSecurityGroup(apiGroup types.CacheSecurityGroup) (*elasticache.SecurityGroup, error) {
metadata := a.CreateMetadataFromARN(*apiGroup.ARN)
description := trivyTypes.StringDefault("", metadata)
if apiGroup.Description != nil {
description = trivyTypes.String(*apiGroup.Description, metadata)
}
return &elasticache.SecurityGroup{
Metadata: metadata,
Description: description,
}, nil
}