Skip to content

Commit 3ba6c54

Browse files
last code changes and integration tests passing
1 parent 716e926 commit 3ba6c54

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

src/cmap/auth/aws_temporary_credentials.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@ const AWS_RELATIVE_URI = 'http://169.254.170.2';
66
const AWS_EC2_URI = 'http://169.254.169.254';
77
const AWS_EC2_PATH = '/latest/meta-data/iam/security-credentials';
88

9-
/** @internal */
9+
/**
10+
* @internal
11+
* This interface matches the final result of fetching temporary credentials manually, outlined
12+
* in the spec [here](https://github.com/mongodb/specifications/blob/master/source/auth/auth.md#ec2-endpoint).
13+
*
14+
* When we use the AWS SDK, we map the response from the SDK to conform to this interface.
15+
*/
1016
export interface AWSTempCredentials {
1117
AccessKeyId?: string;
1218
SecretAccessKey?: string;
13-
SessionToken?: string;
19+
Token?: string;
1420
RoleArn?: string;
1521
Expiration?: Date;
1622
}
1723

18-
/** @internal */
24+
/**
25+
* @internal
26+
*
27+
* Fetches temporary AWS credentials.
28+
*/
1929
export abstract class AWSTemporaryCredentialProvider {
2030
abstract getCredentials(): Promise<AWSTempCredentials>;
2131
private static _credentialProvider: ReturnType<typeof getAwsCredentialProvider>;
@@ -32,6 +42,10 @@ export abstract class AWSTemporaryCredentialProvider {
3242
/** @internal */
3343
export class AWSSDKCredentialProvider extends AWSTemporaryCredentialProvider {
3444
private _provider?: () => Promise<AWSCredentials>;
45+
/**
46+
* The AWS SDK caches credentials automatically and handles refresh when the credentials have expired.
47+
* To ensure this occurs, we need to cache the `provider` returned by the AWS sdk and re-use it when fetching credentials.
48+
*/
3549
private get provider(): () => Promise<AWSCredentials> {
3650
if ('kModuleError' in AWSTemporaryCredentialProvider.credentialProvider) {
3751
throw AWSTemporaryCredentialProvider.credentialProvider.kModuleError;
@@ -106,7 +120,7 @@ export class AWSSDKCredentialProvider extends AWSTemporaryCredentialProvider {
106120
return {
107121
AccessKeyId: creds.accessKeyId,
108122
SecretAccessKey: creds.secretAccessKey,
109-
SessionToken: creds.sessionToken,
123+
Token: creds.sessionToken,
110124
Expiration: creds.expiration
111125
};
112126
} catch (error) {
@@ -115,7 +129,11 @@ export class AWSSDKCredentialProvider extends AWSTemporaryCredentialProvider {
115129
}
116130
}
117131

118-
/** @internal */
132+
/**
133+
* @internal
134+
* Fetches credentials manually (without the AWS SDK), as outlined in the [Obtaining Credentials](https://github.com/mongodb/specifications/blob/master/source/auth/auth.md#obtaining-credentials)
135+
* section of the Auth spec.
136+
*/
119137
export class LegacyAWSTemporaryCredentialProvider extends AWSTemporaryCredentialProvider {
120138
override async getCredentials(): Promise<AWSTempCredentials> {
121139
// If the environment variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI

src/cmap/auth/mongodb_aws.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ async function makeTempCredentials(
172172
source: credentials.source,
173173
mechanism: AuthMechanism.MONGODB_AWS,
174174
mechanismProperties: {
175-
AWS_SESSION_TOKEN: creds.SessionToken
175+
AWS_SESSION_TOKEN: creds.Token
176176
}
177177
});
178178
}

test/integration/auth/mongodb_aws.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { performance } from 'perf_hooks';
66
import * as sinon from 'sinon';
77

88
import {
9+
AWSTemporaryCredentialProvider,
910
MongoAWSError,
1011
type MongoClient,
1112
MongoDBAWS,
@@ -268,7 +269,8 @@ describe('MONGODB-AWS', function () {
268269

269270
numberOfFromNodeProviderChainCalls = 0;
270271

271-
MongoDBAWS.credentialProvider = {
272+
// @ts-expect-error We intentionally access a protected variable.
273+
AWSTemporaryCredentialProvider._credentialProvider = {
272274
fromNodeProviderChain(...args) {
273275
calledArguments = args;
274276
numberOfFromNodeProviderChainCalls += 1;
@@ -289,7 +291,8 @@ describe('MONGODB-AWS', function () {
289291
if (typeof storedEnv.AWS_STS_REGIONAL_ENDPOINTS === 'string') {
290292
process.env.AWS_REGION = storedEnv.AWS_REGION;
291293
}
292-
MongoDBAWS.credentialProvider = credentialProvider;
294+
// @ts-expect-error We intentionally access a protected variable.
295+
AWSTemporaryCredentialProvider._credentialProvider = credentialProvider;
293296
calledArguments = [];
294297
});
295298

test/mongodb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export * from '../src/bulk/ordered';
101101
export * from '../src/bulk/unordered';
102102
export * from '../src/change_stream';
103103
export * from '../src/cmap/auth/auth_provider';
104+
export * from '../src/cmap/auth/aws_temporary_credentials';
104105
export * from '../src/cmap/auth/gssapi';
105106
export * from '../src/cmap/auth/mongo_credentials';
106107
export * from '../src/cmap/auth/mongocr';

0 commit comments

Comments
 (0)