Skip to content

Commit 75ddafe

Browse files
committed
feat(NODE-6988): require aws sdk for aws auth
1 parent 14303bc commit 75ddafe

File tree

3 files changed

+3
-51
lines changed

3 files changed

+3
-51
lines changed

src/cmap/auth/aws_temporary_credentials.ts

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { type AWSCredentials, getAwsCredentialProvider } from '../../deps';
22
import { MongoAWSError } from '../../error';
3-
import { request } from '../../utils';
4-
5-
const AWS_RELATIVE_URI = 'http://169.254.170.2';
6-
const AWS_EC2_URI = 'http://169.254.169.254';
7-
const AWS_EC2_PATH = '/latest/meta-data/iam/security-credentials';
83

94
/**
105
* @internal
@@ -32,7 +27,7 @@ export type AWSCredentialProvider = () => Promise<AWSCredentials>;
3227
export abstract class AWSTemporaryCredentialProvider {
3328
abstract getCredentials(): Promise<AWSTempCredentials>;
3429
private static _awsSDK: ReturnType<typeof getAwsCredentialProvider>;
35-
protected static get awsSDK() {
30+
static get awsSDK() {
3631
AWSTemporaryCredentialProvider._awsSDK ??= getAwsCredentialProvider();
3732
return AWSTemporaryCredentialProvider._awsSDK;
3833
}
@@ -144,42 +139,3 @@ export class AWSSDKCredentialProvider extends AWSTemporaryCredentialProvider {
144139
}
145140
}
146141
}
147-
148-
/**
149-
* @internal
150-
* 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)
151-
* section of the Auth spec.
152-
*/
153-
export class LegacyAWSTemporaryCredentialProvider extends AWSTemporaryCredentialProvider {
154-
override async getCredentials(): Promise<AWSTempCredentials> {
155-
// If the environment variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
156-
// is set then drivers MUST assume that it was set by an AWS ECS agent
157-
if (process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) {
158-
return await request(
159-
`${AWS_RELATIVE_URI}${process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}`
160-
);
161-
}
162-
163-
// Otherwise assume we are on an EC2 instance
164-
165-
// get a token
166-
const token = await request(`${AWS_EC2_URI}/latest/api/token`, {
167-
method: 'PUT',
168-
json: false,
169-
headers: { 'X-aws-ec2-metadata-token-ttl-seconds': 30 }
170-
});
171-
172-
// get role name
173-
const roleName = await request(`${AWS_EC2_URI}/${AWS_EC2_PATH}`, {
174-
json: false,
175-
headers: { 'X-aws-ec2-metadata-token': token }
176-
});
177-
178-
// get temp credentials
179-
const creds = await request(`${AWS_EC2_URI}/${AWS_EC2_PATH}/${roleName}`, {
180-
headers: { 'X-aws-ec2-metadata-token': token }
181-
});
182-
183-
return creds;
184-
}
185-
}

src/cmap/auth/mongodb_aws.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
type AWSCredentialProvider,
1313
AWSSDKCredentialProvider,
1414
type AWSTempCredentials,
15-
AWSTemporaryCredentialProvider,
16-
LegacyAWSTemporaryCredentialProvider
15+
type AWSTemporaryCredentialProvider
1716
} from './aws_temporary_credentials';
1817
import { MongoCredentials } from './mongo_credentials';
1918
import { AuthMechanism } from './providers';
@@ -41,9 +40,7 @@ export class MongoDBAWS extends AuthProvider {
4140
super();
4241

4342
this.credentialProvider = credentialProvider;
44-
this.credentialFetcher = AWSTemporaryCredentialProvider.isAWSSDKInstalled
45-
? new AWSSDKCredentialProvider(credentialProvider)
46-
: new LegacyAWSTemporaryCredentialProvider();
43+
this.credentialFetcher = new AWSSDKCredentialProvider(credentialProvider);
4744
}
4845

4946
override async auth(authContext: AuthContext): Promise<void> {

test/integration/auth/mongodb_aws.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ describe('MONGODB-AWS', function () {
153153
});
154154

155155
it('authenticates with a user provided credentials provider', async function () {
156-
// @ts-expect-error We intentionally access a protected variable.
157156
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
158157
const provider = async () => {
159158
providerCount++;

0 commit comments

Comments
 (0)