Skip to content

Commit 657315f

Browse files
committed
wip
1 parent b68e195 commit 657315f

File tree

16 files changed

+300
-244
lines changed

16 files changed

+300
-244
lines changed

src/client-side-encryption/state_machine.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { type MongoCryptContext, type MongoCryptKMSRequest } from 'mongodb-clien
22
import * as net from 'net';
33
import * as tls from 'tls';
44

5-
import { type AutoEncrypter } from '..';
65
import {
76
type BSONSerializeOptions,
87
deserialize,
@@ -14,7 +13,7 @@ import { type ProxyOptions } from '../cmap/connection';
1413
import { CursorTimeoutContext } from '../cursor/abstract_cursor';
1514
import { getSocks, type SocksLib } from '../deps';
1615
import { MongoOperationTimeoutError } from '../error';
17-
import { type MongoClient, type MongoClientOptions } from '../mongo_client';
16+
import { type IO, type MongoClient, type MongoClientOptions } from '../mongo_client';
1817
import { type Abortable } from '../mongo_types';
1918
import { type CollectionInfo } from '../operations/list_collections';
2019
import { Timeout, type TimeoutContext, TimeoutError } from '../timeout';
@@ -25,7 +24,7 @@ import {
2524
MongoDBCollectionNamespace,
2625
promiseWithResolvers
2726
} from '../utils';
28-
import { autoSelectSocketOptions, type ClientEncryption, type DataKey } from './client_encryption';
27+
import { autoSelectSocketOptions, type DataKey } from './client_encryption';
2928
import { MongoCryptError } from './errors';
3029
import { type MongocryptdManager } from './mongocryptd_manager';
3130
import { type KMSProviders } from './providers';
@@ -186,10 +185,10 @@ export type StateMachineOptions = {
186185
*/
187186
// TODO(DRIVERS-2671): clarify CSOT behavior for FLE APIs
188187
export class StateMachine {
189-
private parent: AutoEncrypter | ClientEncryption;
188+
private parent: { _client: { io: IO } };
190189

191190
constructor(
192-
parent: AutoEncrypter | ClientEncryption,
191+
parent: { _client: { io: IO } },
193192
private options: StateMachineOptions,
194193
private bsonOptions = pluckBSONSerializeOptions(options)
195194
) {

src/cmap/auth/mongodb_oidc/machine_workflow.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { setTimeout } from 'timers/promises';
22

33
import { type Document } from '../../../bson';
4-
import { type MongoClient } from '../../../mongo_client';
4+
import { type IO } from '../../../mongo_client';
55
import { ns } from '../../../utils';
66
import type { Connection } from '../../connection';
77
import type { MongoCredentials } from '../mongo_credentials';
@@ -24,7 +24,7 @@ export interface AccessToken {
2424
/** @internal */
2525
export type OIDCTokenFunction = (
2626
credentials: MongoCredentials,
27-
client: MongoClient
27+
client: { io: IO }
2828
) => Promise<AccessToken>;
2929

3030
/**
@@ -35,12 +35,12 @@ export abstract class MachineWorkflow implements Workflow {
3535
cache: TokenCache;
3636
callback: OIDCTokenFunction;
3737
lastExecutionTime: number;
38-
client: MongoClient;
38+
client: { io: IO };
3939

4040
/**
4141
* Instantiate the machine workflow.
4242
*/
43-
constructor(client: MongoClient, cache: TokenCache) {
43+
constructor(client: { io: IO }, cache: TokenCache) {
4444
this.client = client;
4545
this.cache = cache;
4646
this.callback = this.withLock(this.getToken.bind(this));
@@ -144,5 +144,5 @@ export abstract class MachineWorkflow implements Workflow {
144144
/**
145145
* Get the token from the environment or endpoint.
146146
*/
147-
abstract getToken(credentials: MongoCredentials, client: MongoClient): Promise<AccessToken>;
147+
abstract getToken(credentials: MongoCredentials, client: { io: IO }): Promise<AccessToken>;
148148
}

src/cmap/connect.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
MongoRuntimeError,
1717
needsRetryableWriteLabel
1818
} from '../error';
19+
import { type IO } from '../mongo_client';
1920
import { type Monitor, type RTTPinger } from '../sdam/monitor';
2021
import { HostAddress, ns, promiseWithResolvers } from '../utils';
2122
import { AuthContext } from './auth/auth_provider';
@@ -38,7 +39,7 @@ import {
3839
export type Stream = Socket | TLSSocket;
3940

4041
export async function connect(
41-
parent: Monitor | RTTPinger | ConnectionPool,
42+
parent: { client: { io: IO } },
4243
options: ConnectionOptions
4344
): Promise<Connection> {
4445
let connection: Connection | null = null;
@@ -54,7 +55,7 @@ export async function connect(
5455
}
5556

5657
export function makeConnection(
57-
parent: Monitor | RTTPinger | ConnectionPool,
58+
parent: { client: { io: IO } },
5859
options: ConnectionOptions,
5960
socket: Stream
6061
): Connection {

src/cmap/connection.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
MongoServerError,
3131
MongoUnexpectedServerResponseError
3232
} from '../error';
33-
import type { MongoClient, ServerApi, SupportedNodeConnectionOptions } from '../mongo_client';
33+
import type { IO, ServerApi, SupportedNodeConnectionOptions } from '../mongo_client';
3434
import { type MongoClientAuthProviders } from '../mongo_client_auth_providers';
3535
import { MongoLoggableComponent, type MongoLogger, SeverityLevel } from '../mongo_logger';
3636
import { type Abortable, type CancellationToken, TypedEventEmitter } from '../mongo_types';
@@ -209,6 +209,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
209209
private clusterTime: Document | null = null;
210210
private error: Error | null = null;
211211
private dataEvents: AsyncGenerator<Buffer, void, void> | null = null;
212+
private parent: { client: { io: IO } };
212213

213214
private readonly socketTimeoutMS: number;
214215
private readonly monitorCommands: boolean;
@@ -230,17 +231,11 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
230231
/** @event */
231232
static readonly UNPINNED = UNPINNED;
232233

233-
private parent: Monitor | ConnectionPool | RTTPinger;
234-
235-
get client(): MongoClient {
234+
get client(): { io: IO } {
236235
return this.parent.client;
237236
}
238237

239-
constructor(
240-
parent: Monitor | ConnectionPool | RTTPinger,
241-
stream: Stream,
242-
options: ConnectionOptions
243-
) {
238+
constructor(parent: { client: { io: IO } }, stream: Stream, options: ConnectionOptions) {
244239
super();
245240
this.on('error', noop);
246241

src/cmap/handshake/client_metadata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as process from 'process';
33

44
import { BSON, type Document, Int32 } from '../../bson';
55
import { MongoInvalidArgumentError } from '../../error';
6-
import type { MongoClient, MongoOptions } from '../../mongo_client';
6+
import type { IO, MongoOptions } from '../../mongo_client';
77

88
// eslint-disable-next-line @typescript-eslint/no-require-imports
99
const NODE_DRIVER_VERSION = require('../../../package.json').version;
@@ -157,7 +157,7 @@ export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMe
157157

158158
let dockerPromise: Promise<boolean>;
159159
/** @internal */
160-
async function getContainerMetadata(client: MongoClient) {
160+
async function getContainerMetadata(client: { io: IO }) {
161161
const containerMetadata: Record<string, any> = {};
162162
dockerPromise ??= client.io.fs.access('/.dockerenv');
163163
const isDocker = await dockerPromise;
@@ -176,7 +176,7 @@ async function getContainerMetadata(client: MongoClient) {
176176
* Re-add each metadata value.
177177
* Attempt to add new env container metadata, but keep old data if it does not fit.
178178
*/
179-
export async function addContainerMetadata(client: MongoClient, originalMetadata: ClientMetadata) {
179+
export async function addContainerMetadata(client: { io: IO }, originalMetadata: ClientMetadata) {
180180
const containerMetadata = await getContainerMetadata(client);
181181
if (Object.keys(containerMetadata).length === 0) return originalMetadata;
182182

test/mongodb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export * from '../src/cmap/auth/mongodb_oidc/azure_machine_workflow';
123123
export * from '../src/cmap/auth/mongodb_oidc/callback_workflow';
124124
export * from '../src/cmap/auth/mongodb_oidc/gcp_machine_workflow';
125125
export * from '../src/cmap/auth/mongodb_oidc/machine_workflow';
126+
export * from '../src/cmap/auth/mongodb_oidc/token_cache';
126127
export * from '../src/cmap/auth/mongodb_oidc/token_machine_workflow';
127128
export * from '../src/cmap/auth/plain';
128129
export * from '../src/cmap/auth/providers';

test/unit/client-side-encryption/providers/credentialsProvider.test.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,26 @@ describe('#refreshKMSCredentials', function () {
5959
const secretKey = 'example';
6060
const sessionToken = 'example';
6161

62-
after(function () {
62+
beforeEach(function () {
63+
process.env.AWS_ACCESS_KEY_ID = accessKey;
64+
process.env.AWS_SECRET_ACCESS_KEY = secretKey;
65+
process.env.AWS_SESSION_TOKEN = sessionToken;
66+
});
67+
68+
afterEach(function () {
6369
// After the entire suite runs, set the env back for the rest of the test run.
64-
process.env.AWS_ACCESS_KEY_ID = originalAccessKeyId;
65-
process.env.AWS_SECRET_ACCESS_KEY = originalSecretAccessKey;
66-
process.env.AWS_SESSION_TOKEN = originalSessionToken;
70+
if (typeof originalAccessKeyId === 'string') {
71+
process.env.AWS_ACCESS_KEY_ID = originalAccessKeyId;
72+
}
73+
if (typeof originalSecretAccessKey === 'string') {
74+
process.env.AWS_SECRET_ACCESS_KEY = originalSecretAccessKey;
75+
}
76+
if (typeof originalSessionToken === 'string') {
77+
process.env.AWS_SESSION_TOKEN = originalSessionToken;
78+
}
6779
});
6880

6981
context('when the credential provider finds credentials', function () {
70-
before(function () {
71-
process.env.AWS_ACCESS_KEY_ID = accessKey;
72-
process.env.AWS_SECRET_ACCESS_KEY = secretKey;
73-
process.env.AWS_SESSION_TOKEN = sessionToken;
74-
});
75-
7682
context('when the credentials are empty', function () {
7783
const kmsProviders = { aws: {} };
7884

0 commit comments

Comments
 (0)