Skip to content

Commit 6f4e98a

Browse files
authored
fix(cli): fails to load credentials if both containerToken and containerTokenFile are set (#829)
If both `$AWS_CONTAINER_AUTHORIZATION_TOKEN` and `$AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE` are set, the SDK wants to emit a warning. Because of the way it calls the `logger.warn()` function, the reference to `this` is lost and we try to look up a member on `undefined` which then fails and no credentials are loaded at all. Reported as an upstream bug to SDKv3, but in order to expedite the fix we also guard against it ourselvses by locally binding `this` before we pass our logger object to the SDK. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent aec389c commit 6f4e98a

File tree

1 file changed

+22
-4
lines changed
  • packages/@aws-cdk/toolkit-lib/lib/api/aws-auth

1 file changed

+22
-4
lines changed

packages/@aws-cdk/toolkit-lib/lib/api/aws-auth/sdk.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,6 @@ export class SDK {
579579

580580
public readonly config: ConfigurationOptions;
581581

582-
protected readonly logger?: ISdkLogger;
583-
584582
private readonly accountCache;
585583

586584
/**
@@ -618,9 +616,8 @@ export class SDK {
618616
requestHandler,
619617
retryStrategy: new ConfiguredRetryStrategy(7, (attempt) => 300 * (2 ** attempt)),
620618
customUserAgent: defaultCliUserAgent(),
621-
logger,
619+
logger: logger ? makeSdkLoggerSafeByBindingThis(logger) : undefined,
622620
};
623-
this.logger = logger;
624621
this.currentRegion = region;
625622
}
626623

@@ -1075,3 +1072,24 @@ export class SDK {
10751072
}
10761073

10771074
const CURRENT_ACCOUNT_KEY = Symbol('current_account_key');
1075+
1076+
/**
1077+
* Make the SDK logger safe against raw function invocations
1078+
*
1079+
* The SDK expects the logger to be an object with a number of functions, but it
1080+
* doesn't necessarily keep 'this' bound all the time; sometimes it will copy
1081+
* functions off of the object and call them from a variable.
1082+
*
1083+
* By how JavaScript works, this drops the 'this' reference. Make sure 'this' is
1084+
* bound at all times.
1085+
*
1086+
* @see https://github.com/aws/aws-sdk-js-v3/issues/7297
1087+
*/
1088+
function makeSdkLoggerSafeByBindingThis(logger: ISdkLogger): ISdkLogger {
1089+
return {
1090+
debug: logger.debug.bind(logger),
1091+
info: logger.info.bind(logger),
1092+
warn: logger.warn.bind(logger),
1093+
error: logger.error.bind(logger),
1094+
};
1095+
}

0 commit comments

Comments
 (0)