/
table_aws_iam_credential_report.go
276 lines (259 loc) · 11.3 KB
/
table_aws_iam_credential_report.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
package aws
import (
"context"
"errors"
"time"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/smithy-go"
"github.com/gocarina/gocsv"
"github.com/turbot/go-kit/types"
"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"
)
type awsIamCredentialReportResult struct {
GeneratedTime *time.Time `csv:"-"`
UserArn string `csv:"arn"`
UserName string `csv:"user"`
UserCreationTime string `csv:"user_creation_time"`
AccessKey1Active bool `csv:"access_key_1_active"`
AccessKey1LastRotated string `csv:"access_key_1_last_rotated"`
AccessKey1LastUsedDate string `csv:"access_key_1_last_used_date"`
AccessKey1LastUsedRegion string `csv:"access_key_1_last_used_region"`
AccessKey1LastUsedService string `csv:"access_key_1_last_used_service"`
AccessKey2Active bool `csv:"access_key_2_active"`
AccessKey2LastRotated string `csv:"access_key_2_last_rotated"`
AccessKey2LastUsedDate string `csv:"access_key_2_last_used_date"`
AccessKey2LastUsedRegion string `csv:"access_key_2_last_used_region"`
AccessKey2LastUsedService string `csv:"access_key_2_last_used_service"`
Cert1Active bool `csv:"cert_1_active"`
Cert1LastRotated string `csv:"cert_1_last_rotated"`
Cert2Active bool `csv:"cert_2_active"`
Cert2LastRotated string `csv:"cert_2_last_rotated"`
MFAActive bool `csv:"mfa_active"`
PasswordEnabled string `csv:"password_enabled"`
PasswordLastChanged string `csv:"password_last_changed"`
PasswordLastUsed string `csv:"password_last_used"`
PasswordNextRotation string `csv:"password_next_rotation"`
}
func tableAwsIamCredentialReport(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_iam_credential_report",
Description: "AWS IAM Credential Report",
List: &plugin.ListConfig{
Hydrate: listCredentialReports,
Tags: map[string]string{"service": "iam", "action": "GetCredentialReport"},
},
Columns: awsGlobalRegionColumns([]*plugin.Column{
{
Name: "user_name",
Description: "The friendly name of the user.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "user_arn",
Description: "The Amazon Resource Name (ARN) of the user.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "user_creation_time",
Description: "The date and time when the user was created.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo(),
},
{
Name: "generated_time",
Description: "The date and time when the credential report was created, in ISO 8601 date-time format (http://www.iso.org/iso/iso8601).",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo(),
},
{
Name: "access_key_1_active",
Description: "Does the user have an access key and is the access key's status Active.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromGo(),
},
{
Name: "access_key_1_last_rotated",
Description: "The date and time when the user's access key was created or last changed.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "access_key_1_last_used_date",
Description: "The date and time when the user's access key was most recently used to sign an AWS API request. When an access key is used more than once in a 15-minute span, only the first use is recorded in this field.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "access_key_1_last_used_region",
Description: "The AWS Region in which the access key was most recently used. When an access key is used more than once in a 15-minute span, only the first use is recorded in this field.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "access_key_1_last_used_service",
Description: "The AWS service that was most recently accessed with the access key. The value in this field uses the service's namespace—for example, s3 for Amazon S3 and ec2 for Amazon EC2. When an access key is used more than once in a 15-minute span, only the first use is recorded in this field.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "access_key_2_active",
Description: "Does the user have a second access key and is the access key's status Active.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromGo(),
},
{
Name: "access_key_2_last_rotated",
Description: "The date and time when the user's second access key was created or last changed.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "access_key_2_last_used_date",
Description: "The date and time when the user's second access key was most recently used to sign an AWS API request. When an access key is used more than once in a 15-minute span, only the first use is recorded in this field.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "access_key_2_last_used_region",
Description: "The AWS Region in which the user's second access key was most recently used. When an access key is used more than once in a 15-minute span, only the first use is recorded in this field.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "access_key_2_last_used_service",
Description: "The AWS service that was most recently accessed with the user's second access key. The value in this field uses the service's namespace—for example, s3 for Amazon S3 and ec2 for Amazon EC2. When an access key is used more than once in a 15-minute span, only the first use is recorded in this field.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "cert_1_active",
Description: "Does the user have an X.509 signing certificate and is that certificate's status Active.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromGo(),
},
{
Name: "cert_1_last_rotated",
Description: "The date and time when the user's signing certificate was created or last changed.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "cert_2_active",
Description: "Does the user have a second X.509 signing certificate and is that certificate's status Active.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromGo(),
},
{
Name: "cert_2_last_rotated",
Description: "The date and time when the user's second signing certificate was created or last changed.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A"),
},
{
Name: "mfa_active",
Description: "Whether a multi-factor authentication (MFA) device has been enabled for the user.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromField("MFAActive"),
},
{
Name: "password_enabled",
Description: "When the user has a password, this value is true. Otherwise it is false. The value for the AWS account root user is always false.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromGo().Transform(passwordEnabledToBool),
},
{
Name: "password_last_changed",
Description: "The date and time when the user's password was last set.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A").NullIfEqual("not_supported"),
},
{
Name: "password_last_used",
Description: "The date and time when the AWS account root user or IAM user's password was last used to sign in to an AWS website.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A").NullIfEqual("no_information"),
},
{
Name: "password_status",
Description: "The status of an user password. Password status can be one of used, never_used and not_set.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PasswordLastUsed").Transform(passwordStatus),
},
{
Name: "password_next_rotation",
Description: "When the account has a password policy that requires password rotation, this field contains the date and time.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromGo().NullIfEqual("N/A").NullIfEqual("not_supported"),
},
}),
}
}
//// LIST FUNCTION
func listCredentialReports(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Get client
svc, err := IAMClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_iam_credential_report.listCredentialReports", "client_error", err)
return nil, err
}
resp, err := svc.GetCredentialReport(ctx, &iam.GetCredentialReportInput{})
if err != nil {
var ae smithy.APIError
if errors.As(err, &ae) {
if ae.ErrorCode() == "ReportNotPresent" {
return nil, errors.New("Credential report not available. Please run 'aws iam generate-credential-report' to generate it and try again.")
}
}
plugin.Logger(ctx).Error("aws_iam_credential_report.listCredentialReports", "api_error", err)
return nil, err
}
//if err != nil {
// if err.Error() == "ReportExpired" {
// return nil, errors.New("Existing credential report expired. Requested generation of report - please try again shortly")
// }
// if err.Error() == "ReportInProgress" {
// return nil, errors.New("Credential report generation in progress. Please try again shortly")
// }
// return nil, err
//}
content := string(resp.Content[:])
rows := []*awsIamCredentialReportResult{}
if err := gocsv.UnmarshalString(content, &rows); err != nil {
return nil, err
}
for _, row := range rows {
row.GeneratedTime = resp.GeneratedTime
d.StreamListItem(ctx, row)
// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
break
}
}
return nil, nil
}
//// TRANSFORM FUNCTIONS
func passwordEnabledToBool(_ context.Context, d *transform.TransformData) (interface{}, error) {
enabled := types.SafeString(d.Value)
// The value for the AWS root account <root_account> is always returned as not_supported for password_enabled by AWS API. The root password can not be disabled.
if enabled == "not_supported" {
return true, nil
}
if enabled == "true" {
return true, nil
}
return false, nil
}
func passwordStatus(_ context.Context, d *transform.TransformData) (interface{}, error) {
used := types.SafeString(d.Value)
pwdStatus := "used"
if used == "no_information" {
pwdStatus = "never_used"
} else if used == "N/A" {
pwdStatus = "not_set"
}
return pwdStatus, nil
}