Skip to content

Commit 4f93a3a

Browse files
authored
Merge pull request #82 from M1kep/feat/last-used-date-iam-roles
feat(iamRole): Add lastUsedDate
2 parents 0da99a4 + f7433bc commit 4f93a3a

6 files changed

Lines changed: 65 additions & 28 deletions

File tree

src/services/iamPolicy/data.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const tagsByPolicyArn = async (
4747
iam: IAM,
4848
{ Arn }: Policy
4949
): Promise<{ Arn: string; Tags: TagMap }> =>
50-
new Promise(resolveUserPolicies => {
50+
new Promise(resolve => {
5151
iam.listPolicyTags(
5252
{ PolicyArn: Arn },
5353
(err: AWSError, data: ListPolicyTagsResponse) => {
@@ -60,13 +60,13 @@ const tagsByPolicyArn = async (
6060

6161
if (!isEmpty(data)) {
6262
const { Tags: tags = [] } = data
63-
resolveUserPolicies({
63+
resolve({
6464
Arn,
6565
Tags: convertAwsTagsToTagMap(tags),
6666
})
6767
}
6868

69-
resolveUserPolicies(null)
69+
resolve(null)
7070
}
7171
)
7272
})
@@ -75,7 +75,7 @@ const policyVersionByPolicyArn = async (
7575
iam: IAM,
7676
{ Arn, DefaultVersionId }: Policy
7777
): Promise<{ Arn: string; Content: string }> =>
78-
new Promise(resolveUserPolicies => {
78+
new Promise(resolve => {
7979
iam.getPolicyVersion(
8080
{ PolicyArn: Arn, VersionId: DefaultVersionId },
8181
(err: AWSError, data: GetPolicyVersionResponse) => {
@@ -88,13 +88,13 @@ const policyVersionByPolicyArn = async (
8888

8989
if (!isEmpty(data)) {
9090
const { PolicyVersion = { Document: '' } } = data
91-
resolveUserPolicies({
91+
resolve({
9292
Arn,
9393
Content: decodeURIComponent(PolicyVersion.Document),
9494
})
9595
}
9696

97-
resolveUserPolicies(null)
97+
resolve(null)
9898
}
9999
)
100100
})

src/services/iamRole/data.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import isEmpty from 'lodash/isEmpty'
55
import { AWSError } from 'aws-sdk/lib/error'
66

77
import IAM, {
8-
AttachedPolicy,
8+
AttachedPolicy, GetRoleResponse,
99
ListAttachedRolePoliciesResponse,
1010
ListRolePoliciesResponse,
1111
ListRolesResponse,
@@ -43,11 +43,40 @@ export interface RawAwsIamRole extends Omit<Role, 'Tags'> {
4343
Tags?: TagMap
4444
}
4545

46+
const roleByRoleName = async (
47+
iam: IAM,
48+
{ RoleName }: Role
49+
): Promise<{RoleName: string; Role: Role}> =>
50+
new Promise(resolve => {
51+
iam.getRole(
52+
{ RoleName },
53+
(err: AWSError, data: GetRoleResponse) => {
54+
if (err) {
55+
errorLog.generateAwsErrorLog({
56+
err,
57+
functionName: 'iam:getRole',
58+
})
59+
}
60+
61+
if (!isEmpty(data)) {
62+
const {Role} = data
63+
64+
resolve({
65+
RoleName,
66+
Role,
67+
})
68+
}
69+
70+
resolve(null)
71+
}
72+
)
73+
})
74+
4675
const tagsByRoleName = async (
4776
iam: IAM,
4877
{ RoleName }: Role
4978
): Promise<{ RoleName: string; Tags: TagMap }> =>
50-
new Promise(resolveUserPolicies => {
79+
new Promise(resolve => {
5180
iam.listRoleTags(
5281
{ RoleName },
5382
(err: AWSError, data: ListRoleTagsResponse) => {
@@ -61,13 +90,13 @@ const tagsByRoleName = async (
6190
if (!isEmpty(data)) {
6291
const { Tags: tags = [] } = data
6392

64-
resolveUserPolicies({
93+
resolve({
6594
RoleName,
6695
Tags: convertAwsTagsToTagMap(tags),
6796
})
6897
}
6998

70-
resolveUserPolicies(null)
99+
resolve(null)
71100
}
72101
)
73102
})
@@ -76,7 +105,7 @@ const policiesByRoleName = async (
76105
iam: IAM,
77106
{ RoleName }: Role
78107
): Promise<{ RoleName: string; Policies: string[] }> =>
79-
new Promise(resolveUserPolicies => {
108+
new Promise(resolve => {
80109
iam.listRolePolicies(
81110
{ RoleName },
82111
(err: AWSError, data: ListRolePoliciesResponse) => {
@@ -90,10 +119,10 @@ const policiesByRoleName = async (
90119
if (!isEmpty(data)) {
91120
const { PolicyNames = [] } = data
92121

93-
resolveUserPolicies({ RoleName, Policies: PolicyNames })
122+
resolve({ RoleName, Policies: PolicyNames })
94123
}
95124

96-
resolveUserPolicies(null)
125+
resolve(null)
97126
}
98127
)
99128
})
@@ -102,7 +131,7 @@ const managedPoliciesByRoleName = async (
102131
iam: IAM,
103132
{ RoleName }: Role
104133
): Promise<{ RoleName: string; ManagedPolicies: AttachedPolicy[] }> =>
105-
new Promise(resolveUserPolicies => {
134+
new Promise(resolve => {
106135
iam.listAttachedRolePolicies(
107136
{ RoleName },
108137
(err: AWSError, data: ListAttachedRolePoliciesResponse) => {
@@ -116,13 +145,13 @@ const managedPoliciesByRoleName = async (
116145
if (!isEmpty(data)) {
117146
const { AttachedPolicies = [] } = data
118147

119-
resolveUserPolicies({
148+
resolve({
120149
RoleName,
121150
ManagedPolicies: AttachedPolicies,
122151
})
123152
}
124153

125-
resolveUserPolicies(null)
154+
resolve(null)
126155
}
127156
)
128157
})
@@ -136,6 +165,7 @@ export const listIamRoles = async (
136165
const policiesByRoleNamePromises = []
137166
const tagsByRoleNamePromises = []
138167
const managedPoliciesByRoleNamePromises = []
168+
const roleByRoleNamePromises: Promise<{RoleName: string; Role: Role}>[] = []
139169

140170
iam.listRoles(
141171
{ Marker: marker },
@@ -155,13 +185,15 @@ export const listIamRoles = async (
155185
managedPoliciesByRoleNamePromises.push(
156186
managedPoliciesByRoleName(iam, role)
157187
)
188+
roleByRoleNamePromises.push(roleByRoleName(iam, role))
158189
})
159190

160191
const tags = await Promise.all(tagsByRoleNamePromises)
161192
const policies = await Promise.all(policiesByRoleNamePromises)
162193
const managedPolicies = await Promise.all(
163194
managedPoliciesByRoleNamePromises
164195
)
196+
const detailedRoles = await Promise.all(roleByRoleNamePromises)
165197

166198
result.push(
167199
...roles.map(
@@ -173,6 +205,7 @@ export const listIamRoles = async (
173205
),
174206
...role,
175207
region: globalRegionName,
208+
RoleLastUsed: detailedRoles?.find(r => r.RoleName === RoleName)?.Role.RoleLastUsed,
176209
Policies:
177210
policies
178211
?.filter(p => p?.RoleName === RoleName)

src/services/iamRole/format.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default ({
2121
Path: path = '',
2222
CreateDate: createdAt,
2323
Description: description = '',
24+
RoleLastUsed,
2425
AssumeRolePolicyDocument: assumeRolePolicy = '',
2526
MaxSessionDuration: maxSessionDuration = 0,
2627
Policies: inlinePolicies = [],
@@ -38,6 +39,7 @@ export default ({
3839
path,
3940
createdAt: createdAt?.toISOString() || '',
4041
description,
42+
lastUsedDate: RoleLastUsed?.LastUsedDate?.toISOString() || null,
4143
rawPolicy: assumeRolePolicy,
4244
assumeRolePolicy: formatIamJsonPolicy(assumeRolePolicy),
4345
maxSessionDuration,

src/services/iamRole/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type awsIamRole implements awsBaseService @key(fields: "id") {
55
assumeRolePolicy: awsIamJSONPolicy
66
description: String @search(by: [hash, regexp])
77
createdAt: String @search(by: [hash, regexp])
8+
lastUsedDate: DateTime @search(by: [day])
89
maxSessionDuration: Int @search
910
tags: [awsRawTag]
1011
inlinePolicies: [String]

src/services/iamUser/data.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const tagsByUsername = async (
9191
iam: IAM,
9292
{ UserName }: User
9393
): Promise<{ UserName: string; Tags: TagMap }> =>
94-
new Promise(resolveUserPolicies => {
94+
new Promise(resolve => {
9595
iam.listUserTags(
9696
{ UserName },
9797
(err: AWSError, data: ListUserTagsResponse) => {
@@ -105,10 +105,10 @@ const tagsByUsername = async (
105105
if (!isEmpty(data)) {
106106
const { Tags: tags = [] } = data
107107

108-
resolveUserPolicies({ UserName, Tags: convertAwsTagsToTagMap(tags) })
108+
resolve({ UserName, Tags: convertAwsTagsToTagMap(tags) })
109109
}
110110

111-
resolveUserPolicies(null)
111+
resolve(null)
112112
}
113113
)
114114
})
@@ -117,7 +117,7 @@ const groupsByUsername = async (
117117
iam: IAM,
118118
{ UserName }: User
119119
): Promise<{ UserName: string; Groups: string[] }> =>
120-
new Promise(resolveUserGroups => {
120+
new Promise(resolve => {
121121
iam.listGroupsForUser(
122122
{ UserName },
123123
(err: AWSError, data: ListGroupsForUserResponse) => {
@@ -133,10 +133,10 @@ const groupsByUsername = async (
133133

134134
const userGroups = Groups.map(({ GroupId }) => GroupId)
135135

136-
resolveUserGroups({ UserName, Groups: userGroups })
136+
resolve({ UserName, Groups: userGroups })
137137
}
138138

139-
resolveUserGroups(null)
139+
resolve(null)
140140
}
141141
)
142142
})
@@ -145,7 +145,7 @@ const policiesByUsername = async (
145145
iam: IAM,
146146
{ UserName }: User
147147
): Promise<{ UserName: string; Policies: string[] }> =>
148-
new Promise(resolveUserPolicies => {
148+
new Promise(resolve => {
149149
iam.listUserPolicies(
150150
{ UserName },
151151
(err: AWSError, data: ListUserPoliciesResponse) => {
@@ -159,10 +159,10 @@ const policiesByUsername = async (
159159
if (!isEmpty(data)) {
160160
const { PolicyNames = [] } = data
161161

162-
resolveUserPolicies({ UserName, Policies: PolicyNames })
162+
resolve({ UserName, Policies: PolicyNames })
163163
}
164164

165-
resolveUserPolicies(null)
165+
resolve(null)
166166
}
167167
)
168168
})
@@ -171,7 +171,7 @@ const managedPoliciesByUsername = async (
171171
iam: IAM,
172172
{ UserName }: User
173173
): Promise<{ UserName: string; ManagedPolicies: AttachedPolicy[] }> =>
174-
new Promise(resolveUserPolicies => {
174+
new Promise(resolve => {
175175
iam.listAttachedUserPolicies(
176176
{ UserName },
177177
(err: AWSError, data: ListAttachedUserPoliciesResponse) => {
@@ -185,13 +185,13 @@ const managedPoliciesByUsername = async (
185185
if (!isEmpty(data)) {
186186
const { AttachedPolicies = [] } = data
187187

188-
resolveUserPolicies({
188+
resolve({
189189
UserName,
190190
ManagedPolicies: AttachedPolicies,
191191
})
192192
}
193193

194-
resolveUserPolicies(null)
194+
resolve(null)
195195
}
196196
)
197197
})

src/types/generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,6 +3182,7 @@ export type AwsIamRole = AwsBaseService & {
31823182
inlinePolicies?: Maybe<Array<Maybe<Scalars['String']>>>;
31833183
kinesisFirehose?: Maybe<Array<Maybe<AwsKinesisFirehose>>>;
31843184
lambda?: Maybe<Array<Maybe<AwsLambda>>>;
3185+
lastUsedDate?: Maybe<Scalars['DateTime']>;
31853186
managedAirflows?: Maybe<Array<Maybe<AwsManagedAirflow>>>;
31863187
maxSessionDuration?: Maybe<Scalars['Int']>;
31873188
name?: Maybe<Scalars['String']>;

0 commit comments

Comments
 (0)