forked from kamranahmedse/aws-cost-cli
-
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.
Merge pull request #16 from route06/issue-6-add-rbac
Enable use case with RBAC to improve security
- Loading branch information
Showing
8 changed files
with
1,225 additions
and
24 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,78 @@ | ||
import { | ||
STSClient, | ||
AssumeRoleCommand, | ||
AssumeRoleCommandOutput, | ||
Credentials, | ||
} from '@aws-sdk/client-sts'; | ||
import 'aws-sdk-client-mock-jest'; | ||
import { AwsClientStub, mockClient } from 'aws-sdk-client-mock'; | ||
import { getAwsConfigFromOptionsOrFile } from './config'; | ||
|
||
let stsMock: AwsClientStub<STSClient>; | ||
|
||
beforeEach(() => { | ||
stsMock = mockClient(STSClient) as AwsClientStub<STSClient>; | ||
}); | ||
|
||
afterEach(() => { | ||
stsMock.restore(); | ||
}); | ||
|
||
describe('should assume role if roleArn is provided', (): void => { | ||
const mockCredentials: Credentials = { | ||
AccessKeyId: 'mockAccessKeyId', | ||
SecretAccessKey: 'mockSecretAccessKey', | ||
SessionToken: 'mockSessionToken', | ||
Expiration: new Date(), | ||
}; | ||
|
||
it('should assume role if `roleArn` is provided', async () => { | ||
const roleArn = 'arn:aws:iam::123456789012:role/test-role'; | ||
stsMock.on(AssumeRoleCommand).resolves({ | ||
Credentials: mockCredentials, | ||
$metadata: {}, | ||
} as AssumeRoleCommandOutput); | ||
|
||
const awsConfig = await getAwsConfigFromOptionsOrFile({ | ||
profile: 'default', | ||
accessKey: '', | ||
secretKey: '', | ||
sessionToken: '', | ||
region: 'us-east-1', | ||
roleArn, | ||
}); | ||
|
||
expect(stsMock).toHaveReceivedCommandWith(AssumeRoleCommand, { | ||
RoleArn: roleArn, | ||
}); | ||
|
||
expect(awsConfig.credentials).toEqual({ | ||
accessKeyId: mockCredentials.AccessKeyId, | ||
secretAccessKey: mockCredentials.SecretAccessKey, | ||
sessionToken: mockCredentials.SessionToken, | ||
}); | ||
}); | ||
|
||
it('should allow ABAC if `{accessKey, secretKey, sessionToken}` provided', async () => { | ||
const accessKey = 'testAccessKey'; | ||
const secretKey = 'testSecretKey'; | ||
const sessionToken = 'testSessionToken'; | ||
|
||
const awsConfig = await getAwsConfigFromOptionsOrFile({ | ||
profile: 'default', | ||
accessKey: accessKey, | ||
secretKey: secretKey, | ||
sessionToken: sessionToken, | ||
region: 'us-east-1', | ||
roleArn: '', | ||
}); | ||
|
||
expect(stsMock).toHaveReceivedCommandTimes(AssumeRoleCommand, 0); | ||
|
||
expect(awsConfig.credentials).toEqual({ | ||
accessKeyId: accessKey, | ||
secretAccessKey: secretKey, | ||
sessionToken: sessionToken, | ||
}); | ||
}); | ||
}); |
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