Skip to content

Commit 9c7bd36

Browse files
committed
feat(iamRole): Add lastUsedDate
1 parent 85684e9 commit 9c7bd36

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/services/iamRole/data.ts

Lines changed: 34 additions & 1 deletion
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,6 +43,35 @@ 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
@@ -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/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)