|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import assert from 'assert' |
| 7 | +import { SharedCredentialsProvider } from '../../../auth/providers/sharedCredentialsProvider' |
| 8 | +import { createTestSections } from '../../credentials/testUtil' |
| 9 | +import { DefaultStsClient } from '../../../shared/clients/stsClient' |
| 10 | +import { oneDay } from '../../../shared/datetime' |
| 11 | +import sinon from 'sinon' |
| 12 | +import { SsoAccessTokenProvider } from '../../../auth/sso/ssoAccessTokenProvider' |
| 13 | +import { SsoClient } from '../../../auth/sso/clients' |
| 14 | + |
| 15 | +describe('SharedCredentialsProvider - Role Chaining with SSO', function () { |
| 16 | + let sandbox: sinon.SinonSandbox |
| 17 | + |
| 18 | + beforeEach(function () { |
| 19 | + sandbox = sinon.createSandbox() |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(function () { |
| 23 | + sandbox.restore() |
| 24 | + }) |
| 25 | + |
| 26 | + it('should handle role chaining from SSO profile', async function () { |
| 27 | + // Mock the SSO authentication |
| 28 | + sandbox.stub(SsoAccessTokenProvider.prototype, 'getToken').resolves({ |
| 29 | + accessToken: 'test-token', |
| 30 | + expiresAt: new Date(Date.now() + oneDay), |
| 31 | + }) |
| 32 | + |
| 33 | + // Mock SSO getRoleCredentials |
| 34 | + sandbox.stub(SsoClient.prototype, 'getRoleCredentials').resolves({ |
| 35 | + accessKeyId: 'sso-access-key', |
| 36 | + secretAccessKey: 'sso-secret-key', |
| 37 | + sessionToken: 'sso-session-token', |
| 38 | + expiration: new Date(Date.now() + oneDay), |
| 39 | + }) |
| 40 | + |
| 41 | + // Mock STS assumeRole |
| 42 | + sandbox.stub(DefaultStsClient.prototype, 'assumeRole').callsFake(async (request) => { |
| 43 | + assert.strictEqual(request.RoleArn, 'arn:aws:iam::123456789012:role/dev') |
| 44 | + return { |
| 45 | + Credentials: { |
| 46 | + AccessKeyId: 'assumed-access-key', |
| 47 | + SecretAccessKey: 'assumed-secret-key', |
| 48 | + SessionToken: 'assumed-session-token', |
| 49 | + Expiration: new Date(Date.now() + oneDay), |
| 50 | + }, |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + const sections = await createTestSections(` |
| 55 | + [sso-session aws1_session] |
| 56 | + sso_start_url = https://example.awsapps.com/start |
| 57 | + sso_region = us-east-1 |
| 58 | + sso_registration_scopes = sso:account:access |
| 59 | +
|
| 60 | + [profile Landing] |
| 61 | + sso_session = aws1_session |
| 62 | + sso_account_id = 111111111111 |
| 63 | + sso_role_name = Landing |
| 64 | + region = us-east-1 |
| 65 | +
|
| 66 | + [profile dev] |
| 67 | + region = us-east-1 |
| 68 | + role_arn = arn:aws:iam::123456789012:role/dev |
| 69 | + source_profile = Landing |
| 70 | + `) |
| 71 | + |
| 72 | + const provider = new SharedCredentialsProvider('dev', sections) |
| 73 | + const credentials = await provider.getCredentials() |
| 74 | + |
| 75 | + assert.strictEqual(credentials.accessKeyId, 'assumed-access-key') |
| 76 | + assert.strictEqual(credentials.secretAccessKey, 'assumed-secret-key') |
| 77 | + assert.strictEqual(credentials.sessionToken, 'assumed-session-token') |
| 78 | + }) |
| 79 | +}) |
0 commit comments