This repository has been archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add whoami command * Fix expired or invalid session error log * Add configured region to whoami command output * Pad start of whoami output keys * Move whoami command to new file and reformat output * Fix lint errors * Make status an alias for whoami and refactor utils * Minor adjustment to CHANGELOG * Minor adjustment to README
- Loading branch information
Showing
9 changed files
with
84 additions
and
40 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "awsx", | ||
"description": "AWS CLI profile switcher with MFA support", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"author": "Neo Financial Engineering <[email protected]>", | ||
"license": "MIT", | ||
"repository": { | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import chalk from 'chalk'; | ||
import AWS, { STS, IAM } from 'aws-sdk'; | ||
|
||
import { getCurrentProfile } from '../lib/profile'; | ||
import { timeout } from '../lib/time'; | ||
|
||
const assumedRole = (arn?: string): string | undefined => { | ||
return arn ? arn.split('/')[1] : undefined; | ||
}; | ||
|
||
const whoami = async (): Promise<void> => { | ||
const sts = new STS(); | ||
const iam = new IAM(); | ||
|
||
const identity = await Promise.race([sts.getCallerIdentity().promise(), timeout(1500)]); | ||
|
||
if (!identity) { | ||
console.log(chalk.red(`Session is expired or invalid`)); | ||
process.exit(); | ||
} | ||
|
||
// Should be available if the identity call returns truthy, so shouldn't need a second timed-out race. | ||
const aliases = await iam.listAccountAliases().promise(); | ||
|
||
const whoAmI = { | ||
Account: identity.Account, | ||
Aliases: aliases?.AccountAliases, | ||
Arn: identity.Arn, | ||
AssumedRole: assumedRole(identity.Arn), | ||
Profile: getCurrentProfile(), | ||
Region: AWS.config.region, | ||
UserId: identity.UserId, | ||
}; | ||
|
||
const maxKeyLength = Math.max.apply( | ||
null, | ||
Object.keys(whoAmI).map((k) => k.length) | ||
); | ||
|
||
for (const [key, value] of Object.entries(whoAmI)) { | ||
console.log(chalk.green(`${key.padEnd(maxKeyLength)} -> ${value}`)); | ||
} | ||
}; | ||
|
||
export { assumedRole, whoami }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const getCurrentProfile = (): string => { | ||
return process.env.AWS_PROFILE || ''; | ||
}; | ||
|
||
export { getCurrentProfile }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { promisify } from 'util'; | ||
|
||
const timeout = promisify(setTimeout); | ||
|
||
export { timeout }; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { assumedRole } from '../../src/command/whoami'; | ||
|
||
describe('whoami', () => { | ||
test('assumedRole', () => { | ||
expect( | ||
assumedRole('arn:aws:sts::111111111111:assumed-role/foo-bar/aws-sdk-js-1111111111111') | ||
).toEqual('foo-bar'); | ||
}); | ||
}); |