Skip to content

Commit 6458439

Browse files
authored
fix(cli): mfa code is not requested when $AWS_PROFILE is used (#32313)
We only passed in the `mfaCode` function if we got a profile from `--profile`, not when configured using `$AWS_PROFILE`. Reduce a miss in the duplicated code by moving the `clientConfig` to a single initialization point. Fixes #32312. Unfortunately this cannot be unit tested (I cannot mock the function that I need to mock), nor integ tested because it needs human input. I'm open to ideas. In the mean time, tested manually. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent f271168 commit 6458439

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

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

+11-12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export class AwsCliCompatible {
2727
public static async credentialChainBuilder(
2828
options: CredentialChainOptions = {},
2929
): Promise<AwsCredentialIdentityProvider> {
30+
const clientConfig = {
31+
requestHandler: AwsCliCompatible.requestHandlerBuilder(options.httpOptions),
32+
customUserAgent: 'aws-cdk',
33+
logger: options.logger,
34+
};
35+
3036
/**
3137
* The previous implementation matched AWS CLI behavior:
3238
*
@@ -41,16 +47,12 @@ export class AwsCliCompatible {
4147
profile: options.profile,
4248
ignoreCache: true,
4349
mfaCodeProvider: tokenCodeFn,
44-
clientConfig: {
45-
requestHandler: AwsCliCompatible.requestHandlerBuilder(options.httpOptions),
46-
customUserAgent: 'aws-cdk',
47-
logger: options.logger,
48-
},
50+
clientConfig,
4951
logger: options.logger,
5052
});
5153
}
5254

53-
const profile = options.profile || process.env.AWS_PROFILE || process.env.AWS_DEFAULT_PROFILE;
55+
const envProfile = process.env.AWS_PROFILE || process.env.AWS_DEFAULT_PROFILE;
5456

5557
/**
5658
* Env AWS - EnvironmentCredentials with string AWS
@@ -74,13 +76,10 @@ export class AwsCliCompatible {
7476
* fromInstanceMetadata()
7577
*/
7678
const nodeProviderChain = fromNodeProviderChain({
77-
profile: profile,
78-
clientConfig: {
79-
requestHandler: AwsCliCompatible.requestHandlerBuilder(options.httpOptions),
80-
customUserAgent: 'aws-cdk',
81-
logger: options.logger,
82-
},
79+
profile: envProfile,
80+
clientConfig,
8381
logger: options.logger,
82+
mfaCodeProvider: tokenCodeFn,
8483
ignoreCache: true,
8584
});
8685

0 commit comments

Comments
 (0)