Skip to content

Commit cbaf47a

Browse files
fix(NODE-5945): make AWS session token optional (#4006)
Co-authored-by: Neal Beeken <[email protected]>
1 parent 60bfc48 commit cbaf47a

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/cmap/auth/mongodb_aws.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export class MongoDBAWS extends AuthProvider {
116116

117117
const accessKeyId = credentials.username;
118118
const secretAccessKey = credentials.password;
119+
// Allow the user to specify an AWS session token for authentication with temporary credentials.
119120
const sessionToken = credentials.mechanismProperties.AWS_SESSION_TOKEN;
120121

121122
// If all three defined, include sessionToken, else include username and pass, else no credentials
@@ -129,6 +130,8 @@ export class MongoDBAWS extends AuthProvider {
129130
const db = credentials.source;
130131
const nonce = await randomBytes(32);
131132

133+
// All messages between MongoDB clients and servers are sent as BSON objects
134+
// in the payload field of saslStart and saslContinue.
132135
const saslStart = {
133136
saslStart: 1,
134137
mechanism: 'MONGODB-AWS',
@@ -212,7 +215,8 @@ async function makeTempCredentials(
212215
provider?: () => Promise<AWSCredentials>
213216
): Promise<MongoCredentials> {
214217
function makeMongoCredentialsFromAWSTemp(creds: AWSTempCredentials) {
215-
if (!creds.AccessKeyId || !creds.SecretAccessKey || !creds.Token) {
218+
// The AWS session token (creds.Token) may or may not be set.
219+
if (!creds.AccessKeyId || !creds.SecretAccessKey) {
216220
throw new MongoMissingCredentialsError('Could not obtain temporary MONGODB-AWS credentials');
217221
}
218222

test/integration/auth/mongodb_aws.test.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import * as http from 'http';
55
import { performance } from 'perf_hooks';
66
import * as sinon from 'sinon';
77

8-
import { MongoAWSError, type MongoClient, MongoDBAWS, MongoServerError } from '../../mongodb';
8+
import {
9+
MongoAWSError,
10+
type MongoClient,
11+
MongoDBAWS,
12+
MongoMissingCredentialsError,
13+
MongoServerError
14+
} from '../../mongodb';
915

1016
function awsSdk() {
1117
try {
@@ -81,6 +87,37 @@ describe('MONGODB-AWS', function () {
8187
expect(provider).to.be.instanceOf(MongoDBAWS);
8288
});
8389

90+
describe('with missing aws token', () => {
91+
let awsSessionToken: string | undefined;
92+
93+
beforeEach(() => {
94+
awsSessionToken = process.env.AWS_SESSION_TOKEN;
95+
delete process.env.AWS_SESSION_TOKEN;
96+
});
97+
98+
afterEach(() => {
99+
if (awsSessionToken != null) {
100+
process.env.AWS_SESSION_TOKEN = awsSessionToken;
101+
}
102+
});
103+
104+
it('should not throw an exception when aws token is missing', async function () {
105+
client = this.configuration.newClient(process.env.MONGODB_URI);
106+
107+
const result = await client
108+
.db('aws')
109+
.collection('aws_test')
110+
.estimatedDocumentCount()
111+
.catch(error => error);
112+
113+
// We check only for the MongoMissingCredentialsError
114+
// and do check for the MongoServerError as the error or numeric result
115+
// that can be returned depending on different types of environments
116+
// getting credentials from different sources.
117+
expect(result).to.not.be.instanceOf(MongoMissingCredentialsError);
118+
});
119+
});
120+
84121
describe('EC2 with missing credentials', () => {
85122
let client;
86123

0 commit comments

Comments
 (0)