/
table_aws_secretsmanager_secret.go
359 lines (321 loc) · 10.8 KB
/
table_aws_secretsmanager_secret.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package aws
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
secretsmanagerv1 "github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
//// TABLE DEFINITION
func tableAwsSecretsManagerSecret(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_secretsmanager_secret",
Description: "AWS Secrets Manager Secret",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("arn"),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"ValidationException", "InvalidParameter", "ResourceNotFoundException"}),
},
Hydrate: describeSecretsManagerSecret,
Tags: map[string]string{"service": "secretsmanager", "action": "DescribeSecret"},
},
List: &plugin.ListConfig{
Hydrate: listSecretsManagerSecrets,
Tags: map[string]string{"service": "secretsmanager", "action": "ListSecrets"},
KeyColumns: []*plugin.KeyColumn{
{Name: "name", Require: plugin.Optional},
{Name: "description", Require: plugin.Optional},
{Name: "primary_region", Require: plugin.Optional},
},
},
HydrateConfig: []plugin.HydrateConfig{
{
Func: describeSecretsManagerSecret,
Tags: map[string]string{"service": "sagemaker", "action": "DescribeSecret"},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(secretsmanagerv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "name",
Description: "The friendly name of the secret.",
Type: proto.ColumnType_STRING,
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the secret.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ARN"),
},
{
Name: "created_date",
Description: "The date and time when a secret was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "description",
Description: "The user-provided description of the secret.",
Type: proto.ColumnType_STRING,
},
{
Name: "kms_key_id",
Description: "The ARN or alias of the AWS KMS customer master key (CMK) used to encrypt the SecretString and SecretBinary fields in each version of the secret.",
Type: proto.ColumnType_STRING,
Hydrate: describeSecretsManagerSecret,
},
{
Name: "deleted_date",
Description: "The date and time the deletion of the secret occurred.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "last_accessed_date",
Description: "The last date that this secret was accessed.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "last_changed_date",
Description: "The last date and time that this secret was modified in any way.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "last_rotated_date",
Description: "The most recent date and time that the Secrets Manager rotation process was successfully completed.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "owning_service",
Description: "Returns the name of the service that created the secret.",
Type: proto.ColumnType_STRING,
Hydrate: describeSecretsManagerSecret,
},
{
Name: "primary_region",
Description: "The Region where Secrets Manager originated the secret.",
Type: proto.ColumnType_STRING,
Hydrate: describeSecretsManagerSecret,
},
{
Name: "policy",
Description: "A JSON-formatted string that describes the permissions that are associated with the attached secret.",
Type: proto.ColumnType_JSON,
Hydrate: getSecretsManagerSecretPolicy,
Transform: transform.FromField("ResourcePolicy"),
},
{
Name: "policy_std",
Description: "Contains the permissions that are associated with the attached secret in a canonical form for easier searching.",
Type: proto.ColumnType_JSON,
Hydrate: getSecretsManagerSecretPolicy,
Transform: transform.FromField("ResourcePolicy").Transform(unescape).Transform(policyToCanonical),
},
{
Name: "replication_status",
Description: "Describes a list of replication status objects as InProgress, Failed or InSync.",
Type: proto.ColumnType_JSON,
Hydrate: describeSecretsManagerSecret,
},
{
Name: "rotation_enabled",
Description: "Indicates whether automatic, scheduled rotation is enabled for this secret.",
Type: proto.ColumnType_BOOL,
},
{
Name: "rotation_lambda_arn",
Description: "The ARN of an AWS Lambda function invoked by Secrets Manager to rotate and expire the secret either automatically per the schedule or manually by a call to RotateSecret.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("RotationLambdaARN"),
Hydrate: describeSecretsManagerSecret,
},
{
Name: "rotation_rules",
Description: "A structure that defines the rotation configuration for the secret.",
Type: proto.ColumnType_JSON,
Hydrate: describeSecretsManagerSecret,
},
{
Name: "secret_versions_to_stages",
Description: "A list of all of the currently assigned SecretVersionStage staging labels and the SecretVersionId attached to each one.",
Type: proto.ColumnType_JSON,
},
{
Name: "tags_src",
Description: "The list of user-defined tags associated with the secret.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Tags"),
},
// Standard columns for all tables
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: describeSecretsManagerSecret,
Transform: transform.FromField("Tags").Transform(secretsManagerSecretTagListToTurbotTags),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ARN").Transform(arnToAkas),
},
}),
}
}
//// LIST FUNCTION
func listSecretsManagerSecrets(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create session
svc, err := SecretsManagerClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_secretsmanager_secret.listSecretsManagerSecrets", "connection_error", err)
return nil, err
}
// Limiting the results
maxLimit := int32(100)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxLimit {
if limit < 1 {
maxLimit = 1
} else {
maxLimit = limit
}
}
}
input := &secretsmanager.ListSecretsInput{
MaxResults: aws.Int32(maxLimit),
}
filters := buildSecretManagerSecretFilter(d.Quals)
if len(filters) > 0 {
input.Filters = filters
}
paginator := secretsmanager.NewListSecretsPaginator(svc, input, func(o *secretsmanager.ListSecretsPaginatorOptions) {
o.Limit = maxLimit
o.StopOnDuplicateToken = true
})
// List call
for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_secretsmanager_secret.listSecretsManagerSecrets", "api_error", err)
return nil, err
}
for _, items := range output.SecretList {
d.StreamListItem(ctx, items)
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, err
}
//// HYDRATE FUNCTIONS
func describeSecretsManagerSecret(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var secretID string
if h.Item != nil {
data := secretData(h.Item)
secretID = data["ARN"]
} else {
quals := d.EqualsQuals
secretID = quals["arn"].GetStringValue()
}
// get service
svc, err := SecretsManagerClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_secretsmanager_secret.describeSecretsManagerSecret", "connection_error", err)
return nil, err
}
// Build the params
params := &secretsmanager.DescribeSecretInput{
SecretId: aws.String(secretID),
}
// Get call
op, err := svc.DescribeSecret(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_secretsmanager_secret.describeSecretsManagerSecret", "api_error", err)
return nil, err
}
return op, nil
}
func getSecretsManagerSecretPolicy(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var arn string
if h.Item != nil {
data := secretData(h.Item)
arn = data["ARN"]
}
// Create Session
svc, err := SecretsManagerClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_secretsmanager_secret.getSecretsManagerSecretPolicy", "connection_eror", err)
return nil, err
}
// Build the params
params := &secretsmanager.GetResourcePolicyInput{
SecretId: &arn,
}
// Get call
data, err := svc.GetResourcePolicy(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_secretsmanager_secret.getSecretsManagerSecretPolicy", "api_error", err)
return nil, err
}
return data, nil
}
//// TRANSFORM FUNCTION
func secretsManagerSecretTagListToTurbotTags(ctx context.Context, d *transform.TransformData) (interface{}, error) {
tagList := d.Value.([]types.Tag)
// Mapping the resource tags inside turbotTags
var turbotTagsMap map[string]string
if tagList != nil {
turbotTagsMap = map[string]string{}
for _, i := range tagList {
turbotTagsMap[*i.Key] = *i.Value
}
}
return turbotTagsMap, nil
}
func secretData(item interface{}) map[string]string {
data := map[string]string{}
switch item := item.(type) {
case *secretsmanager.DescribeSecretOutput:
data["ARN"] = *item.ARN
case types.SecretListEntry:
data["ARN"] = *item.ARN
}
return data
}
//// UTILITY FUNCTION
// Build secret manager secret list call input filter
func buildSecretManagerSecretFilter(quals plugin.KeyColumnQualMap) []types.Filter {
filters := make([]types.Filter, 0)
filterQuals := map[string]string{
"description": "description",
"name": "name",
"primary_region": "primary-region",
}
for columnName, filterName := range filterQuals {
if quals[columnName] != nil {
filter := types.Filter{
Key: types.FilterNameStringType(filterName),
}
value := getQualsValueByColumn(quals, columnName, "string")
val, ok := value.(string)
if ok {
filter.Values = []string{val}
}
filters = append(filters, filter)
}
}
return filters
}