Skip to content

Commit eba6052

Browse files
authored
feat(cli): support SSO (#19454)
Adds support for SSO. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d59bee9 commit eba6052

File tree

9 files changed

+48
-21
lines changed

9 files changed

+48
-21
lines changed

packages/aws-cdk/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,11 @@ role_arn=arn:aws:iam::123456789123:role/role_to_be_assumed
622622
mfa_serial=arn:aws:iam::123456789123:mfa/my_user
623623
```
624624

625+
## SSO support
626+
627+
If you create an SSO profile with `aws configure sso` and run `aws sso login`, the CDK can use those credentials
628+
if you set the profile name as the value of `AWS_PROFILE` or pass it to `--profile`.
629+
625630
## Configuration
626631

627632
On top of passing configuration through command-line arguments, it is possible to use JSON configuration files. The

packages/aws-cdk/THIRD_PARTY_LICENSES

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH RE
268268

269269
----------------
270270

271-
** aws-sdk@2.1094.0 - https://www.npmjs.com/package/aws-sdk/v/2.1094.0 | Apache-2.0
271+
** aws-sdk@2.1095.0 - https://www.npmjs.com/package/aws-sdk/v/2.1095.0 | Apache-2.0
272272
AWS SDK for JavaScript
273273
Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
274274

packages/aws-cdk/lib/api/aws-auth/awscli-compatible.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,25 @@ export class AwsCliCompatible {
3333
* 4. Respects $AWS_DEFAULT_PROFILE in addition to $AWS_PROFILE.
3434
*/
3535
public static async credentialChain(options: CredentialChainOptions = {}) {
36+
// Force reading the `config` file if it exists by setting the appropriate
37+
// environment variable.
38+
await forceSdkToReadConfigIfPresent();
3639

3740
// To match AWS CLI behavior, if a profile is explicitly given using --profile,
3841
// we use that to the exclusion of everything else (note: this does not apply
3942
// to AWS_PROFILE, environment credentials still take precedence over AWS_PROFILE)
4043
if (options.profile) {
41-
await forceSdkToReadConfigIfPresent();
42-
const theProfile = options.profile;
43-
return new AWS.CredentialProviderChain([
44-
() => profileCredentials(theProfile),
45-
() => new AWS.ProcessCredentials({ profile: theProfile }),
46-
]);
44+
return new AWS.CredentialProviderChain(iniFileCredentialFactories(options.profile));
4745
}
4846

4947
const implicitProfile = process.env.AWS_PROFILE || process.env.AWS_DEFAULT_PROFILE || 'default';
5048

5149
const sources = [
5250
() => new AWS.EnvironmentCredentials('AWS'),
5351
() => new AWS.EnvironmentCredentials('AMAZON'),
52+
...iniFileCredentialFactories(implicitProfile),
5453
];
5554

56-
if (await fs.pathExists(credentialsFileName())) {
57-
// Force reading the `config` file if it exists by setting the appropriate
58-
// environment variable.
59-
await forceSdkToReadConfigIfPresent();
60-
sources.push(() => profileCredentials(implicitProfile));
61-
sources.push(() => new AWS.ProcessCredentials({ profile: implicitProfile }));
62-
}
63-
6455
if (options.containerCreds ?? hasEcsCredentials()) {
6556
sources.push(() => new AWS.ECSCredentials());
6657
} else if (hasWebIdentityCredentials()) {
@@ -83,6 +74,14 @@ export class AwsCliCompatible {
8374
tokenCodeFn,
8475
});
8576
}
77+
78+
function iniFileCredentialFactories(theProfile: string) {
79+
return [
80+
() => profileCredentials(theProfile),
81+
() => new AWS.SsoCredentials({ profile: theProfile }),
82+
() => new AWS.ProcessCredentials({ profile: theProfile }),
83+
];
84+
}
8685
}
8786

8887
/**

packages/aws-cdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"@aws-cdk/region-info": "0.0.0",
9595
"@jsii/check-node": "1.55.0",
9696
"archiver": "^5.3.0",
97-
"aws-sdk": "^2.979.0",
97+
"aws-sdk": "^2.1093.0",
9898
"camelcase": "^6.3.0",
9999
"cdk-assets": "0.0.0",
100100
"chokidar": "^3.5.3",

packages/aws-cdk/test/context-providers/amis.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import * as AWS from 'aws-sdk-mock';
33
import { AmiContextProviderPlugin } from '../../lib/context-providers/ami';
44
import { MockSdkProvider } from '../util/mock-sdk';
55

6-
AWS.setSDKInstance(aws);
6+
// If the 'aws-sdk' package imported here and the 'aws-sdk' package imported by 'aws-sdk-mock' aren't
7+
// the same physical package on disk (if version mismatches cause hoisting/deduping to not happen),
8+
// the type check here takes too long and makes the TypeScript compiler fail.
9+
// Suppress the type check using 'as any' to make this more robust.
10+
AWS.setSDKInstance(aws as any);
711

812
afterEach(done => {
913
AWS.restore();

packages/aws-cdk/test/context-providers/asymmetric-vpcs.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import * as AWS from 'aws-sdk-mock';
33
import { VpcNetworkContextProviderPlugin } from '../../lib/context-providers/vpcs';
44
import { MockSdkProvider } from '../util/mock-sdk';
55

6-
AWS.setSDKInstance(aws);
6+
// If the 'aws-sdk' package imported here and the 'aws-sdk' package imported by 'aws-sdk-mock' aren't
7+
// the same physical package on disk (if version mismatches cause hoisting/deduping to not happen),
8+
// the type check here takes too long and makes the TypeScript compiler fail.
9+
// Suppress the type check using 'as any' to make this more robust.
10+
AWS.setSDKInstance(aws as any);
711

812
afterEach(done => {
913
AWS.restore();

packages/aws-cdk/test/util/awscli-compatible.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ test('Use web identity when available', async () => {
4242
const providers = (await AwsCliCompatible.credentialChain()).providers;
4343

4444
// make sure the web identity provider is in the chain
45-
const webIdentify = (providers[2] as Function)();
45+
const webIdentify = (providers[5] as Function)();
4646
expect(webIdentify).toBeInstanceOf(AWS.TokenFileWebIdentityCredentials);
4747
});

packages/cdk-assets/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"@aws-cdk/cloud-assembly-schema": "0.0.0",
4848
"@aws-cdk/cx-api": "0.0.0",
4949
"archiver": "^5.3.0",
50-
"aws-sdk": "^2.848.0",
50+
"aws-sdk": "^2.1093.0",
5151
"glob": "^7.2.0",
5252
"mime": "^2.6.0",
5353
"yargs": "^16.2.0"

yarn.lock

+16-1
Original file line numberDiff line numberDiff line change
@@ -2562,7 +2562,22 @@ [email protected]:
25622562
sinon "^11.1.1"
25632563
traverse "^0.6.6"
25642564

2565-
aws-sdk@^2.596.0, aws-sdk@^2.848.0, aws-sdk@^2.928.0, aws-sdk@^2.979.0:
2565+
aws-sdk@^2.1093.0:
2566+
version "2.1095.0"
2567+
resolved "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.1095.0.tgz#7847493b09a326a0613010ed9db53302f760edf6"
2568+
integrity sha512-OrZq2pTDsnfOJYsAdRlw+NXTGLQYqWldSZR3HugW8JT4JPWyFZrgB2yPP2ElFHX+4J4SZg5QvkAXl/7s9gLTgA==
2569+
dependencies:
2570+
buffer "4.9.2"
2571+
events "1.1.1"
2572+
ieee754 "1.1.13"
2573+
jmespath "0.16.0"
2574+
querystring "0.2.0"
2575+
sax "1.2.1"
2576+
url "0.10.3"
2577+
uuid "3.3.2"
2578+
xml2js "0.4.19"
2579+
2580+
aws-sdk@^2.596.0, aws-sdk@^2.848.0, aws-sdk@^2.928.0:
25662581
version "2.1094.0"
25672582
resolved "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.1094.0.tgz#85cc5fb416ce7af356f1dd1b14fbb714cd923800"
25682583
integrity sha512-g/pjEl1JKs8+UZSdfdTMwUh7oNSWy6LXkjd0WfI3TBVgU5+yE5bd1VtAiJxJ/kIOFwcWyGPy0fNkGjAqL6NAGw==

0 commit comments

Comments
 (0)