Created
August 30, 2021 08:00
-
-
Save christianhaller3000/429bf3809e2f70d780ee1b1f6adac7c3 to your computer and use it in GitHub Desktop.
fetch iam users and sort by last password used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User, IAMClient, ListUsersCommand } from "@aws-sdk/client-iam"; | |
import { formatDistance } from "date-fns"; | |
const getUsers = async (m: string | undefined, iamUsers: User[] = []) => { | |
const { Users, Marker, IsTruncated } = await new IAMClient({}).send( | |
new ListUsersCommand({ | |
Marker: m, | |
}) | |
); | |
iamUsers.push(...Users); | |
if (IsTruncated && Marker) { | |
await getUsers(Marker, iamUsers); | |
} | |
return iamUsers; | |
}; | |
const [, , filter] = process.argv; | |
filter | |
? console.log(`filter for aws iam users starting with: ${filter}`) | |
: null; | |
(async () => { | |
const users = (await getUsers(undefined)).filter(({ UserName }) => { | |
if (filter) { | |
return UserName?.startsWith(filter); | |
} | |
return true; | |
}); | |
const sortedByPasswordLastedUsed = users.sort( | |
({ PasswordLastUsed: pwA = 0 }, { PasswordLastUsed: pwB = 0 }) => { | |
return new Date(pwB).getTime() - new Date(pwA).getTime(); | |
} | |
); | |
console.log( | |
sortedByPasswordLastedUsed.map(({ UserName, PasswordLastUsed = 0 }) => { | |
return `${UserName}: ${ | |
!PasswordLastUsed | |
? "🤷🏼♂️" | |
: formatDistance(new Date(), PasswordLastUsed as Date) | |
}`; | |
}) | |
); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment