From c6233568937a6b71148f153f0111a0b1609e4469 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Sun, 2 Apr 2023 21:27:12 -0400 Subject: [PATCH 1/8] FAAS metadata commit --- src/cmap/connect.ts | 7 +- src/cmap/connection.ts | 5 +- src/cmap/connection_pool.ts | 2 +- src/cmap/handshake/client_metadata.ts | 124 ++++ src/cmap/handshake/faas_provider.ts | 84 +++ src/connection_string.ts | 4 +- src/index.ts | 8 +- src/mongo_client.ts | 17 +- src/sdam/topology.ts | 9 +- src/utils.ts | 85 +-- .../connection.test.ts | 6 +- test/integration/mongodb-handshake/.gitkeep | 0 .../mongodb-handshake.prose.test.ts | 104 +++ .../node-specific/topology.test.js | 2 +- test/mongodb.ts | 2 + test/tools/cmap_spec_runner.ts | 11 +- .../cmap/handshake/client_metadata.test.ts | 602 ++++++++++++++---- test/unit/connection_string.test.ts | 34 + test/unit/sdam/topology.test.js | 2 +- 19 files changed, 905 insertions(+), 203 deletions(-) create mode 100644 src/cmap/handshake/client_metadata.ts create mode 100644 src/cmap/handshake/faas_provider.ts create mode 100644 test/integration/mongodb-handshake/.gitkeep create mode 100644 test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts diff --git a/src/cmap/connect.ts b/src/cmap/connect.ts index 7b2866adf2..324c6f7485 100644 --- a/src/cmap/connect.ts +++ b/src/cmap/connect.ts @@ -17,7 +17,7 @@ import { MongoRuntimeError, needsRetryableWriteLabel } from '../error'; -import { Callback, ClientMetadata, HostAddress, ns } from '../utils'; +import { Callback, HostAddress, ns } from '../utils'; import { AuthContext, AuthProvider } from './auth/auth_provider'; import { GSSAPI } from './auth/gssapi'; import { MongoCR } from './auth/mongocr'; @@ -28,6 +28,7 @@ import { AuthMechanism } from './auth/providers'; import { ScramSHA1, ScramSHA256 } from './auth/scram'; import { X509 } from './auth/x509'; import { CommandOptions, Connection, ConnectionOptions, CryptoConnection } from './connection'; +import type { TruncatedClientMetadata } from './handshake/client_metadata'; import { MAX_SUPPORTED_SERVER_VERSION, MAX_SUPPORTED_WIRE_VERSION, @@ -192,7 +193,7 @@ export interface HandshakeDocument extends Document { ismaster?: boolean; hello?: boolean; helloOk?: boolean; - client: ClientMetadata; + client: TruncatedClientMetadata; compression: string[]; saslSupportedMechs?: string; loadBalanced?: boolean; @@ -213,7 +214,7 @@ export async function prepareHandshakeDocument( const handshakeDoc: HandshakeDocument = { [serverApi?.version ? 'hello' : LEGACY_HELLO_COMMAND]: 1, helloOk: true, - client: options.metadata, + client: options.truncatedClientMetadata, compression: compressors }; diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 464f86d9b3..8ffaa54718 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -29,7 +29,6 @@ import { applySession, ClientSession, updateSessionFromResponse } from '../sessi import { calculateDurationInMs, Callback, - ClientMetadata, HostAddress, maxWireVersion, MongoDBNamespace, @@ -46,6 +45,7 @@ import { } from './command_monitoring_events'; import { BinMsg, Msg, Query, Response, WriteProtocolMessageType } from './commands'; import type { Stream } from './connect'; +import type { ClientMetadata, TruncatedClientMetadata } from './handshake/client_metadata'; import { MessageStream, OperationDescription } from './message_stream'; import { StreamDescription, StreamDescriptionOptions } from './stream_description'; import { getReadPreference, isSharded } from './wire_protocol/shared'; @@ -128,6 +128,9 @@ export interface ConnectionOptions socketTimeoutMS?: number; cancellationToken?: CancellationToken; metadata: ClientMetadata; + + /** @internal */ + truncatedClientMetadata: TruncatedClientMetadata; } /** @internal */ diff --git a/src/cmap/connection_pool.ts b/src/cmap/connection_pool.ts index e3d4228135..4acc1551e5 100644 --- a/src/cmap/connection_pool.ts +++ b/src/cmap/connection_pool.ts @@ -227,7 +227,7 @@ export class ConnectionPool extends TypedEventEmitter { waitQueueTimeoutMS: options.waitQueueTimeoutMS ?? 0, minPoolSizeCheckFrequencyMS: options.minPoolSizeCheckFrequencyMS ?? 100, autoEncrypter: options.autoEncrypter, - metadata: options.metadata + truncatedClientMetadata: options.truncatedClientMetadata }); if (this.options.minPoolSize > this.options.maxPoolSize) { diff --git a/src/cmap/handshake/client_metadata.ts b/src/cmap/handshake/client_metadata.ts new file mode 100644 index 0000000000..e436bb722d --- /dev/null +++ b/src/cmap/handshake/client_metadata.ts @@ -0,0 +1,124 @@ +import { calculateObjectSize } from 'bson'; +import * as os from 'os'; + +import type { MongoOptions } from '../../mongo_client'; +import { deepCopy, DeepPartial } from '../../utils'; +import { applyFaasEnvMetadata } from './faas_provider'; + +/** + * @public + * @see https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#hello-command + */ +export interface ClientMetadata { + driver: { + name: string; + version: string; + }; + os: { + type: string; + name: NodeJS.Platform; + architecture: string; + version: string; + }; + platform: string; + application?: { + name: string; + }; + + /** Data containing information about the environment, if the driver is running in a FAAS environment. */ + env?: { + name: 'aws.lambda' | 'gcp.func' | 'azure.func' | 'vercel'; + timeout_sec?: number; + memory_mb?: number; + region?: string; + url?: string; + }; +} + +/** @internal */ +export type TruncatedClientMetadata = DeepPartial; + +/** + * @internal + * truncates the client metadata according to the priority outlined here + * https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#limitations + */ +export function truncateClientMetadata(metadata: ClientMetadata): TruncatedClientMetadata { + const copiedMetadata: TruncatedClientMetadata = deepCopy(metadata); + const truncations: Array<(arg0: TruncatedClientMetadata) => void> = [ + m => delete m.platform, + m => { + if (m.env) { + m.env = { name: m.env.name }; + } + }, + m => { + if (m.os) { + m.os = { type: m.os.type }; + } + }, + m => delete m.env, + m => delete m.os, + m => delete m.driver, + m => delete m.application + ]; + + for (const truncation of truncations) { + if (calculateObjectSize(copiedMetadata) <= 512) { + return copiedMetadata; + } + truncation(copiedMetadata); + } + + return copiedMetadata; +} + +/** @public */ +export interface ClientMetadataOptions { + driverInfo?: { + name?: string; + version?: string; + platform?: string; + }; + appName?: string; +} + +// eslint-disable-next-line @typescript-eslint/no-var-requires +const NODE_DRIVER_VERSION = require('../../../package.json').version; + +export function makeClientMetadata( + options: Pick +): ClientMetadata { + const name = options.driverInfo.name ? `nodejs|${options.driverInfo.name}` : 'nodejs'; + const version = options.driverInfo.version + ? `${NODE_DRIVER_VERSION}|${options.driverInfo.version}` + : NODE_DRIVER_VERSION; + const platform = options.driverInfo.platform + ? `Node.js ${process.version}, ${os.endianness()}|${options.driverInfo.platform}` + : `Node.js ${process.version}, ${os.endianness()}`; + + const metadata: ClientMetadata = { + driver: { + name, + version + }, + os: { + type: os.type(), + name: process.platform, + architecture: process.arch, + version: os.release() + }, + platform + }; + + if (options.appName) { + // MongoDB requires the appName not exceed a byte length of 128 + const name = + Buffer.byteLength(options.appName, 'utf8') <= 128 + ? options.appName + : Buffer.from(options.appName, 'utf8').subarray(0, 128).toString('utf8'); + metadata.application = { name }; + } + + return applyFaasEnvMetadata(metadata); +} diff --git a/src/cmap/handshake/faas_provider.ts b/src/cmap/handshake/faas_provider.ts new file mode 100644 index 0000000000..ce9df91af0 --- /dev/null +++ b/src/cmap/handshake/faas_provider.ts @@ -0,0 +1,84 @@ +import { identity } from '../../utils'; +import type { ClientMetadata } from './client_metadata'; + +export type FAASProvider = 'aws' | 'gcp' | 'azure' | 'vercel' | 'none'; + +export function determineCloudProvider(): FAASProvider { + const awsPresent = process.env.AWS_EXECUTION_ENV || process.env.AWS_LAMBDA_RUNTIME_API; + const azurePresent = process.env.FUNCTIONS_WORKER_RUNTIME; + const gcpPresent = process.env.K_SERVICE || process.env.FUNCTION_NAME; + const vercelPresent = process.env.VERCEL; + + const numberOfProvidersPresent = [awsPresent, azurePresent, gcpPresent, vercelPresent].filter( + identity + ).length; + + if (numberOfProvidersPresent !== 1) { + return 'none'; + } + + if (awsPresent) return 'aws'; + if (azurePresent) return 'azure'; + if (gcpPresent) return 'gcp'; + return 'vercel'; +} + +function applyAzureMetadata(m: ClientMetadata): ClientMetadata { + m.env = { name: 'azure.func' }; + return m; +} + +function applyGCPMetadata(m: ClientMetadata): ClientMetadata { + m.env = { name: 'gcp.func' }; + + const memory_mb = Number.parseInt(process.env.FUNCTION_MEMORY_MB ?? ''); + if (!Number.isNaN(memory_mb)) { + m.env.memory_mb = memory_mb; + } + const timeout_sec = Number.parseInt(process.env.FUNCTION_TIMEOUT_SEC ?? ''); + if (!Number.isNaN(timeout_sec)) { + m.env.timeout_sec = timeout_sec; + } + if (process.env.FUNCTION_REGION) { + m.env.region = process.env.FUNCTION_REGION; + } + + return m; +} + +function applyAWSMetadata(m: ClientMetadata): ClientMetadata { + m.env = { name: 'aws.lambda' }; + if (process.env.AWS_REGION) { + m.env.region = process.env.AWS_REGION; + } + const memory_mb = Number.parseInt(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE ?? ''); + if (!Number.isNaN(memory_mb)) { + m.env.memory_mb = memory_mb; + } + return m; +} + +function applyVercelMetadata(m: ClientMetadata): ClientMetadata { + m.env = { name: 'vercel' }; + if (process.env.VERCEL_URL) { + m.env.url = process.env.VERCEL_URL; + } + if (process.env.VERCEL_REGION) { + m.env.region = process.env.VERCEL_REGION; + } + return m; +} + +export function applyFaasEnvMetadata(metadata: ClientMetadata): ClientMetadata { + const handlerMap: Record ClientMetadata> = { + aws: applyAWSMetadata, + gcp: applyGCPMetadata, + azure: applyAzureMetadata, + vercel: applyVercelMetadata, + none: identity + }; + const cloudProvider = determineCloudProvider(); + + const faasMetadataProvider = handlerMap[cloudProvider]; + return faasMetadataProvider(metadata); +} diff --git a/src/connection_string.ts b/src/connection_string.ts index 9b55437273..fe8cf90e6b 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -6,6 +6,7 @@ import { URLSearchParams } from 'url'; import type { Document } from './bson'; import { MongoCredentials } from './cmap/auth/mongo_credentials'; import { AUTH_MECHS_AUTH_SRC_EXTERNAL, AuthMechanism } from './cmap/auth/providers'; +import { makeClientMetadata, truncateClientMetadata } from './cmap/handshake/client_metadata'; import { Compressor, CompressorName } from './cmap/wire_protocol/compression'; import { Encrypter } from './encrypter'; import { @@ -32,7 +33,6 @@ import { emitWarningOnce, HostAddress, isRecord, - makeClientMetadata, parseInteger, setDifference } from './utils'; @@ -543,6 +543,8 @@ export function parseOptions( ); mongoOptions.metadata = makeClientMetadata(mongoOptions); + Object.freeze(mongoOptions.metadata); + mongoOptions.truncatedClientMetadata = truncateClientMetadata(mongoOptions.metadata); return mongoOptions; } diff --git a/src/index.ts b/src/index.ts index fac3c9c95b..da39a758ba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -238,6 +238,11 @@ export type { WaitQueueMember, WithConnectionCallback } from './cmap/connection_pool'; +export type { + ClientMetadata, + ClientMetadataOptions, + TruncatedClientMetadata +} from './cmap/handshake/client_metadata'; export type { MessageStream, MessageStreamOptions, @@ -463,8 +468,7 @@ export type { Transaction, TransactionOptions, TxnState } from './transactions'; export type { BufferPool, Callback, - ClientMetadata, - ClientMetadataOptions, + DeepPartial, EventEmitterWithState, HostAddress, List, diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 885c980fbf..d5b2511a7b 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -8,6 +8,7 @@ import type { AuthMechanismProperties, MongoCredentials } from './cmap/auth/mong import type { AuthMechanism } from './cmap/auth/providers'; import type { LEGAL_TCP_SOCKET_OPTIONS, LEGAL_TLS_SOCKET_OPTIONS } from './cmap/connect'; import type { Connection } from './cmap/connection'; +import type { ClientMetadata, TruncatedClientMetadata } from './cmap/handshake/client_metadata'; import type { CompressorName } from './cmap/wire_protocol/compression'; import { parseOptions, resolveSRVRecord } from './connection_string'; import { MONGO_CLIENT_EVENTS } from './constants'; @@ -24,7 +25,7 @@ import { readPreferenceServerSelector } from './sdam/server_selection'; import type { SrvPoller } from './sdam/srv_polling'; import { Topology, TopologyEvents } from './sdam/topology'; import { ClientSession, ClientSessionOptions, ServerSessionPool } from './sessions'; -import { ClientMetadata, HostAddress, MongoDBNamespace, ns, resolveOptions } from './utils'; +import { HostAddress, MongoDBNamespace, ns, resolveOptions } from './utils'; import type { W, WriteConcern, WriteConcernSettings } from './write_concern'; /** @public */ @@ -711,12 +712,24 @@ export interface MongoOptions compressors: CompressorName[]; writeConcern: WriteConcern; dbName: string; - metadata: ClientMetadata; autoEncrypter?: AutoEncrypter; proxyHost?: string; proxyPort?: number; proxyUsername?: string; proxyPassword?: string; + + metadata: ClientMetadata; + + /** + * @internal + * `metadata` truncated to be less than 512 bytes, if necessary, to attach to handshakes. + * `metadata` is left untouched because it is public and to provide users a document they + * inspect to confirm their metadata was parsed correctly. + * + * If `metadata` `<=` 512 bytes, these fields are the same but the driver only uses `truncatedMetadata`. + */ + truncatedClientMetadata: TruncatedClientMetadata; + /** @internal */ connectionType?: typeof Connection; diff --git a/src/sdam/topology.ts b/src/sdam/topology.ts index f545cb8847..66465e43ef 100644 --- a/src/sdam/topology.ts +++ b/src/sdam/topology.ts @@ -5,6 +5,7 @@ import type { BSONSerializeOptions, Document } from '../bson'; import type { MongoCredentials } from '../cmap/auth/mongo_credentials'; import type { ConnectionEvents, DestroyOptions } from '../cmap/connection'; import type { CloseOptions, ConnectionPoolEvents } from '../cmap/connection_pool'; +import type { ClientMetadata, TruncatedClientMetadata } from '../cmap/handshake/client_metadata'; import { DEFAULT_OPTIONS, FEATURE_FLAGS } from '../connection_string'; import { CLOSE, @@ -37,7 +38,6 @@ import type { ClientSession } from '../sessions'; import type { Transaction } from '../transactions'; import { Callback, - ClientMetadata, EventEmitterWithState, HostAddress, List, @@ -138,15 +138,14 @@ export interface TopologyOptions extends BSONSerializeOptions, ServerOptions { /** The name of the replica set to connect to */ replicaSet?: string; srvHost?: string; - /** @internal */ srvPoller?: SrvPoller; /** Indicates that a client should directly connect to a node without attempting to discover its topology type */ directConnection: boolean; loadBalanced: boolean; metadata: ClientMetadata; + truncatedClientMetadata: TruncatedClientMetadata; /** MongoDB server API version */ serverApi?: ServerApi; - /** @internal */ [featureFlag: symbol]: any; } @@ -661,8 +660,8 @@ export class Topology extends TypedEventEmitter { if (typeof callback === 'function') callback(undefined, true); } - get clientMetadata(): ClientMetadata { - return this.s.options.metadata; + get clientMetadata(): TruncatedClientMetadata { + return this.s.options.truncatedClientMetadata; } isConnected(): boolean { diff --git a/src/utils.ts b/src/utils.ts index dee4bc51ae..1793352f06 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,5 @@ import * as crypto from 'crypto'; import type { SrvRecord } from 'dns'; -import * as os from 'os'; import { URL } from 'url'; import { Document, ObjectId, resolveBSONOptions } from './bson'; @@ -20,7 +19,7 @@ import { MongoRuntimeError } from './error'; import type { Explain } from './explain'; -import type { MongoClient, MongoOptions } from './mongo_client'; +import type { MongoClient } from './mongo_client'; import type { CommandOperationOptions, OperationParent } from './operations/command'; import type { Hint, OperationOptions } from './operations/operation'; import { ReadConcern } from './read_concern'; @@ -39,6 +38,13 @@ export type Callback = (error?: AnyError, result?: T) => void; export type AnyOptions = Document; +/** @internal */ +export type DeepPartial = T extends object + ? { + [P in keyof T]?: DeepPartial; + } + : T; + export const ByteUtils = { toLocalBufferType(this: void, buffer: Buffer | Uint8Array): Buffer { return Buffer.isBuffer(buffer) @@ -513,77 +519,6 @@ export function makeStateMachine(stateTable: StateTable): StateTransitionFunctio }; } -/** - * @public - * @see https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#hello-command - */ -export interface ClientMetadata { - driver: { - name: string; - version: string; - }; - os: { - type: string; - name: NodeJS.Platform; - architecture: string; - version: string; - }; - platform: string; - application?: { - name: string; - }; -} - -/** @public */ -export interface ClientMetadataOptions { - driverInfo?: { - name?: string; - version?: string; - platform?: string; - }; - appName?: string; -} - -// eslint-disable-next-line @typescript-eslint/no-var-requires -const NODE_DRIVER_VERSION = require('../package.json').version; - -export function makeClientMetadata( - options: Pick -): ClientMetadata { - const name = options.driverInfo.name ? `nodejs|${options.driverInfo.name}` : 'nodejs'; - const version = options.driverInfo.version - ? `${NODE_DRIVER_VERSION}|${options.driverInfo.version}` - : NODE_DRIVER_VERSION; - const platform = options.driverInfo.platform - ? `Node.js ${process.version}, ${os.endianness()}|${options.driverInfo.platform}` - : `Node.js ${process.version}, ${os.endianness()}`; - - const metadata: ClientMetadata = { - driver: { - name, - version - }, - os: { - type: os.type(), - name: process.platform, - architecture: process.arch, - version: os.release() - }, - platform - }; - - if (options.appName) { - // MongoDB requires the appName not exceed a byte length of 128 - const name = - Buffer.byteLength(options.appName, 'utf8') <= 128 - ? options.appName - : Buffer.from(options.appName, 'utf8').subarray(0, 128).toString('utf8'); - metadata.application = { name }; - } - - return metadata; -} - /** @internal */ export function now(): number { const hrtime = process.hrtime(); @@ -1277,3 +1212,7 @@ export function parseUnsignedInteger(value: unknown): number | null { return parsedInt != null && parsedInt >= 0 ? parsedInt : null; } + +export function identity(obj: T): T { + return obj; +} diff --git a/test/integration/connection-monitoring-and-pooling/connection.test.ts b/test/integration/connection-monitoring-and-pooling/connection.test.ts index 7ad5bb5c59..7591aa7291 100644 --- a/test/integration/connection-monitoring-and-pooling/connection.test.ts +++ b/test/integration/connection-monitoring-and-pooling/connection.test.ts @@ -36,7 +36,7 @@ describe('Connection', function () { const connectOptions: Partial = { connectionType: Connection, ...this.configuration.options, - metadata: makeClientMetadata({ driverInfo: {} }) + truncatedClientMetadata: makeClientMetadata({ driverInfo: {} }) }; connect(connectOptions as any as ConnectionOptions, (err, conn) => { @@ -60,7 +60,7 @@ describe('Connection', function () { connectionType: Connection, monitorCommands: true, ...this.configuration.options, - metadata: makeClientMetadata({ driverInfo: {} }) + truncatedClientMetadata: makeClientMetadata({ driverInfo: {} }) }; connect(connectOptions as any as ConnectionOptions, (err, conn) => { @@ -92,7 +92,7 @@ describe('Connection', function () { const connectOptions: Partial = { connectionType: Connection, ...this.configuration.options, - metadata: makeClientMetadata({ driverInfo: {} }) + truncatedClientMetadata: makeClientMetadata({ driverInfo: {} }) }; connect(connectOptions as any as ConnectionOptions, (err, conn) => { diff --git a/test/integration/mongodb-handshake/.gitkeep b/test/integration/mongodb-handshake/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts new file mode 100644 index 0000000000..a97c25358b --- /dev/null +++ b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts @@ -0,0 +1,104 @@ +import { expect } from 'chai'; + +import { determineCloudProvider, FAASProvider, MongoClient } from '../../mongodb'; + +context('FAAS Environment Prose Tests', function () { + let client: MongoClient; + + afterEach(async function () { + await client?.close(); + }); + + type EnvironmentVariables = Array<[string, string]>; + const tests: Array<{ + context: string; + expectedProvider: FAASProvider; + env: EnvironmentVariables; + }> = [ + { + context: '1. Valid AWS', + expectedProvider: 'aws', + env: [ + ['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'], + ['AWS_REGION', 'us-east-2'], + ['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '1024'] + ] + }, + { + context: '2. Valid Azure', + expectedProvider: 'azure', + env: [['FUNCTIONS_WORKER_RUNTIME', 'node']] + }, + { + context: '3. Valid GCP', + expectedProvider: 'gcp', + env: [ + ['K_SERVICE', 'servicename'], + ['FUNCTION_MEMORY_MB', '1024'], + ['FUNCTION_TIMEOUT_SEC', '60'], + ['FUNCTION_REGION', 'us-central1'] + ] + }, + { + context: '4. Valid Vercel', + expectedProvider: 'vercel', + env: [ + ['VERCEL', '1'], + ['VERCEL_URL', '*.vercel.app'], + ['VERCEL_REGION', 'cdg1'] + ] + }, + { + expectedProvider: 'none', + context: '5. Invalid - multiple providers', + env: [ + ['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'], + ['FUNCTIONS_WORKER_RUNTIME', 'node'] + ] + }, + { + expectedProvider: 'aws', + context: '6. Invalid - long string', + env: [ + ['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'], + ['AWS_REGION', 'a'.repeat(1024)] + ] + }, + { + expectedProvider: 'aws', + context: '7. Invalid - wrong types', + env: [ + ['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'], + ['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'big'] + ] + } + ]; + + for (const { context: name, env, expectedProvider } of tests) { + context(name, function () { + before(() => { + for (const [key, value] of env) { + process.env[key] = value; + } + }); + after(() => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + for (const [key, _] of env) { + delete process.env[key]; + } + }); + + before(`metadata confirmation test for ${name}`, function () { + expect(determineCloudProvider()).to.equal( + expectedProvider, + 'determined the wrong cloud provider' + ); + }); + + it('runs a hello successfully', async function () { + client = this.configuration.newClient({ serverSelectionTimeoutMS: 3000 }); + await client.connect(); + }); + }); + } +}); diff --git a/test/integration/node-specific/topology.test.js b/test/integration/node-specific/topology.test.js index ee4393efcd..ced960c939 100644 --- a/test/integration/node-specific/topology.test.js +++ b/test/integration/node-specific/topology.test.js @@ -7,7 +7,7 @@ describe('Topology', function () { metadata: { requires: { apiVersion: false, topology: '!load-balanced' } }, // apiVersion not supported by newTopology() test: function (done) { const topology = this.configuration.newTopology({ - metadata: makeClientMetadata({ driverInfo: {} }) + truncatedClientMetadata: makeClientMetadata({ driverInfo: {} }) }); const states = []; diff --git a/test/mongodb.ts b/test/mongodb.ts index dee4e204a3..79f621d496 100644 --- a/test/mongodb.ts +++ b/test/mongodb.ts @@ -122,6 +122,8 @@ export * from '../src/cmap/connection'; export * from '../src/cmap/connection_pool'; export * from '../src/cmap/connection_pool_events'; export * from '../src/cmap/errors'; +export * from '../src/cmap/handshake/client_metadata'; +export * from '../src/cmap/handshake/faas_provider'; export * from '../src/cmap/message_stream'; export * from '../src/cmap/metrics'; export * from '../src/cmap/stream_description'; diff --git a/test/tools/cmap_spec_runner.ts b/test/tools/cmap_spec_runner.ts index 7f21a8bc34..99da831840 100644 --- a/test/tools/cmap_spec_runner.ts +++ b/test/tools/cmap_spec_runner.ts @@ -370,7 +370,10 @@ async function runCmapTest(test: CmapTest, threadContext: ThreadContext) { delete poolOptions.backgroundThreadIntervalMS; } - const metadata = makeClientMetadata({ appName: poolOptions.appName, driverInfo: {} }); + const truncatedClientMetadata = makeClientMetadata({ + appName: poolOptions.appName, + driverInfo: {} + }); delete poolOptions.appName; const operations = test.operations; @@ -382,7 +385,11 @@ async function runCmapTest(test: CmapTest, threadContext: ThreadContext) { const mainThread = threadContext.getThread(MAIN_THREAD_KEY); mainThread.start(); - threadContext.createPool({ ...poolOptions, metadata, minPoolSizeCheckFrequencyMS }); + threadContext.createPool({ + ...poolOptions, + truncatedClientMetadata, + minPoolSizeCheckFrequencyMS + }); // yield control back to the event loop so that the ConnectionPoolCreatedEvent // has a chance to be fired before any synchronously-emitted events from // the queued operations diff --git a/test/unit/cmap/handshake/client_metadata.test.ts b/test/unit/cmap/handshake/client_metadata.test.ts index f75c9cecfe..c827588a91 100644 --- a/test/unit/cmap/handshake/client_metadata.test.ts +++ b/test/unit/cmap/handshake/client_metadata.test.ts @@ -1,150 +1,536 @@ import { expect } from 'chai'; import * as os from 'os'; -import { makeClientMetadata } from '../../../mongodb'; +import { + ClientMetadata, + determineCloudProvider, + FAASProvider, + makeClientMetadata, + truncateClientMetadata, + TruncatedClientMetadata +} from '../../../mongodb'; // eslint-disable-next-line @typescript-eslint/no-var-requires const NODE_DRIVER_VERSION = require('../../../../package.json').version; -describe('makeClientMetadata()', () => { - context('when driverInfo.platform is provided', () => { - it('appends driverInfo.platform to the platform field', () => { - const options = { - driverInfo: { platform: 'myPlatform' } - }; - const metadata = makeClientMetadata(options); - expect(metadata).to.deep.equal({ - driver: { - name: 'nodejs', - version: NODE_DRIVER_VERSION - }, - os: { - type: os.type(), - name: process.platform, - architecture: process.arch, - version: os.release() - }, - platform: `Node.js ${process.version}, ${os.endianness()}|myPlatform` +describe('client metadata module', () => { + describe('determineCloudProvider()', function () { + const tests: Array<[string, FAASProvider]> = [ + ['AWS_EXECUTION_ENV', 'aws'], + ['AWS_LAMBDA_RUNTIME_API', 'aws'], + ['FUNCTIONS_WORKER_RUNTIME', 'azure'], + ['K_SERVICE', 'gcp'], + ['FUNCTION_NAME', 'gcp'], + ['VERCEL', 'vercel'] + ]; + for (const [envVariable, provider] of tests) { + context(`when ${envVariable} is in the environment`, () => { + before(() => { + process.env[envVariable] = 'non empty string'; + }); + after(() => { + delete process.env[envVariable]; + }); + it('determines the correct provider', () => { + expect(determineCloudProvider()).to.equal(provider); + }); }); - }); - }); + } - context('when driverInfo.name is provided', () => { - it('appends driverInfo.name to the driver.name field', () => { - const options = { - driverInfo: { name: 'myName' } - }; - const metadata = makeClientMetadata(options); - expect(metadata).to.deep.equal({ - driver: { - name: 'nodejs|myName', - version: NODE_DRIVER_VERSION - }, - os: { - type: os.type(), - name: process.platform, - architecture: process.arch, - version: os.release() - }, - platform: `Node.js ${process.version}, ${os.endianness()}` + context('when there is no FAAS provider data in the env', () => { + it('parses no FAAS provider', () => { + expect(determineCloudProvider()).to.equal('none'); }); }); - }); - context('when driverInfo.version is provided', () => { - it('appends driverInfo.version to the version field', () => { - const options = { - driverInfo: { version: 'myVersion' } - }; - const metadata = makeClientMetadata(options); - expect(metadata).to.deep.equal({ - driver: { - name: 'nodejs', - version: `${NODE_DRIVER_VERSION}|myVersion` - }, - os: { - type: os.type(), - name: process.platform, - architecture: process.arch, - version: os.release() - }, - platform: `Node.js ${process.version}, ${os.endianness()}` + context('when there is data from multiple cloud providers in the env', () => { + before(() => { + process.env.AWS_EXECUTION_ENV = 'non-empty-string'; + process.env.FUNCTIONS_WORKER_RUNTIME = 'non-empty-string'; + }); + after(() => { + delete process.env.AWS_EXECUTION_ENV; + delete process.env.FUNCTIONS_WORKER_RUNTIME; + }); + it('parses no FAAS provider', () => { + expect(determineCloudProvider()).to.equal('none'); }); }); }); - context('when no custom driverInfo is provided', () => { - const metadata = makeClientMetadata({ driverInfo: {} }); + describe('makeClientMetadata()', () => { + context('when no FAAS environment is detected', () => { + it('does not append FAAS metadata', () => { + const metadata = makeClientMetadata({ driverInfo: {} }); + expect(metadata).not.to.have.property( + 'env', + 'faas metadata applied in a non-faas environment' + ); + expect(metadata).to.deep.equal({ + driver: { + name: 'nodejs', + version: NODE_DRIVER_VERSION + }, + os: { + type: os.type(), + name: process.platform, + architecture: process.arch, + version: os.release() + }, + platform: `Node.js ${process.version}, ${os.endianness()}` + }); + }); + }); + context('when driverInfo.platform is provided', () => { + it('appends driverInfo.platform to the platform field', () => { + const options = { + driverInfo: { platform: 'myPlatform' } + }; + const metadata = makeClientMetadata(options); + expect(metadata).to.deep.equal({ + driver: { + name: 'nodejs', + version: NODE_DRIVER_VERSION + }, + os: { + type: os.type(), + name: process.platform, + architecture: process.arch, + version: os.release() + }, + platform: `Node.js ${process.version}, ${os.endianness()}|myPlatform` + }); + }); + }); - it('does not append the driver info to the metadata', () => { - expect(metadata).to.deep.equal({ - driver: { - name: 'nodejs', - version: NODE_DRIVER_VERSION - }, - os: { - type: os.type(), - name: process.platform, - architecture: process.arch, - version: os.release() - }, - platform: `Node.js ${process.version}, ${os.endianness()}` + context('when driverInfo.name is provided', () => { + it('appends driverInfo.name to the driver.name field', () => { + const options = { + driverInfo: { name: 'myName' } + }; + const metadata = makeClientMetadata(options); + expect(metadata).to.deep.equal({ + driver: { + name: 'nodejs|myName', + version: NODE_DRIVER_VERSION + }, + os: { + type: os.type(), + name: process.platform, + architecture: process.arch, + version: os.release() + }, + platform: `Node.js ${process.version}, ${os.endianness()}` + }); }); }); - it('does not set the application field', () => { - expect(metadata).not.to.have.property('application'); + context('when driverInfo.version is provided', () => { + it('appends driverInfo.version to the version field', () => { + const options = { + driverInfo: { version: 'myVersion' } + }; + const metadata = makeClientMetadata(options); + expect(metadata).to.deep.equal({ + driver: { + name: 'nodejs', + version: `${NODE_DRIVER_VERSION}|myVersion` + }, + os: { + type: os.type(), + name: process.platform, + architecture: process.arch, + version: os.release() + }, + platform: `Node.js ${process.version}, ${os.endianness()}` + }); + }); }); - }); - context('when app name is provided', () => { - context('when the app name is over 128 bytes', () => { - const longString = 'a'.repeat(300); - const options = { - appName: longString, - driverInfo: {} - }; - const metadata = makeClientMetadata(options); - - it('truncates the application name to <=128 bytes', () => { - expect(metadata.application?.name).to.be.a('string'); - // the above assertion fails if `metadata.application?.name` is undefined, so - // we can safely assert that it exists - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - expect(Buffer.byteLength(metadata.application!.name, 'utf8')).to.equal(128); + context('when no custom driverInto is provided', () => { + const metadata = makeClientMetadata({ driverInfo: {} }); + + it('does not append the driver info to the metadata', () => { + expect(metadata).to.deep.equal({ + driver: { + name: 'nodejs', + version: NODE_DRIVER_VERSION + }, + os: { + type: os.type(), + name: process.platform, + architecture: process.arch, + version: os.release() + }, + platform: `Node.js ${process.version}, ${os.endianness()}` + }); + }); + + it('does not set the application field', () => { + expect(metadata).not.to.have.property('application'); }); }); - context( - 'TODO(NODE-5150): fix appName truncation when multi-byte unicode charaters straddle byte 128', - () => { - const longString = '€'.repeat(300); + context('when app name is provided', () => { + context('when the app name is over 128 bytes', () => { + const longString = 'a'.repeat(300); const options = { appName: longString, driverInfo: {} }; const metadata = makeClientMetadata(options); - it('truncates the application name to 129 bytes', () => { + it('truncates the application name to <=128 bytes', () => { expect(metadata.application?.name).to.be.a('string'); // the above assertion fails if `metadata.application?.name` is undefined, so // we can safely assert that it exists // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - expect(Buffer.byteLength(metadata.application!.name, 'utf8')).to.equal(129); + expect(Buffer.byteLength(metadata.application!.name, 'utf8')).to.equal(128); }); - } - ); + }); + + context( + 'TODO(NODE-5150): fix appName truncation when multi-byte unicode charaters straddle byte 128', + () => { + const longString = '€'.repeat(300); + const options = { + appName: longString, + driverInfo: {} + }; + const metadata = makeClientMetadata(options); + + it('truncates the application name to 129 bytes', () => { + expect(metadata.application?.name).to.be.a('string'); + // the above assertion fails if `metadata.application?.name` is undefined, so + // we can safely assert that it exists + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + expect(Buffer.byteLength(metadata.application!.name, 'utf8')).to.equal(129); + }); + } + ); - context('when the app name is under 128 bytes', () => { - const options = { - appName: 'myApplication', - driverInfo: {} - }; - const metadata = makeClientMetadata(options); + context('when the app name is under 128 bytes', () => { + const options = { + appName: 'myApplication', + driverInfo: {} + }; + const metadata = makeClientMetadata(options); - it('sets the application name to the value', () => { - expect(metadata.application?.name).to.equal('myApplication'); + it('sets the application name to the value', () => { + expect(metadata.application?.name).to.equal('myApplication'); + }); }); }); }); + + describe('FAAS metadata application to handshake', () => { + const tests = { + aws: [ + { + context: 'no additional metadata', + env: [['AWS_EXECUTION_ENV', 'non-empty string']], + outcome: { + name: 'aws.lambda' + } + }, + { + context: 'AWS_REGION provided', + env: [ + ['AWS_EXECUTION_ENV', 'non-empty string'], + ['AWS_REGION', 'non-null'] + ], + outcome: { + name: 'aws.lambda', + region: 'non-null' + } + }, + { + context: 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE provided', + env: [ + ['AWS_EXECUTION_ENV', 'non-empty string'], + ['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '3'] + ], + outcome: { + name: 'aws.lambda', + memory_mb: 3 + } + } + ], + azure: [ + { + context: 'no additional metadata', + env: [['FUNCTIONS_WORKER_RUNTIME', 'non-empty']], + outcome: { + name: 'azure.func' + } + } + ], + gcp: [ + { + context: 'no additional metadata', + env: [['FUNCTION_NAME', 'non-empty']], + outcome: { + name: 'gcp.func' + } + }, + { + context: 'FUNCTION_MEMORY_MB provided', + env: [ + ['FUNCTION_NAME', 'non-empty'], + ['FUNCTION_MEMORY_MB', '1024'] + ], + outcome: { + name: 'gcp.func', + memory_mb: 1024 + } + }, + { + context: 'FUNCTION_REGION provided', + env: [ + ['FUNCTION_NAME', 'non-empty'], + ['FUNCTION_REGION', 'region'] + ], + outcome: { + name: 'gcp.func', + region: 'region' + } + } + ], + vercel: [ + { + context: 'no additional metadata', + env: [['VERCEL', 'non-empty']], + outcome: { + name: 'vercel' + } + }, + { + context: 'VERCEL_URL provided', + env: [ + ['VERCEL', 'non-empty'], + ['VERCEL_URL', 'provided-url'] + ], + outcome: { + name: 'vercel', + url: 'provided-url' + } + }, + { + context: 'VERCEL_REGION provided', + env: [ + ['VERCEL', 'non-empty'], + ['VERCEL_REGION', 'region'] + ], + outcome: { + name: 'vercel', + region: 'region' + } + } + ] + }; + + for (const [provider, _tests] of Object.entries(tests)) { + context(provider, () => { + for (const { context, env: _env, outcome } of _tests) { + it(context, () => { + for (const [k, v] of _env) { + if (v != null) { + process.env[k] = v; + } + } + + const { env } = makeClientMetadata({ driverInfo: {} }); + expect(env).to.deep.equal(outcome); + + for (const [k] of _env) { + delete process.env[k]; + } + }); + } + }); + } + + context('when a numeric FAAS env variable is not numerically parsable', () => { + before(() => { + process.env['AWS_EXECUTION_ENV'] = 'non-empty-string'; + process.env['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] = 'not numeric'; + }); + + after(() => { + delete process.env['AWS_EXECUTION_ENV']; + delete process.env['AWS_LAMBDA_FUNCTION_MEMORY_SIZE']; + }); + + it('does not attach it to the metadata', () => { + expect(makeClientMetadata({ driverInfo: {} })).not.to.have.nested.property('aws.memory_mb'); + }); + }); + }); + + describe('metadata truncation order', function () { + const longDocument = 'a'.repeat(512); + + const tests: Array<[string, ClientMetadata, TruncatedClientMetadata]> = [ + [ + 'only removes platform first', + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + platform: longDocument, + application: { name: 'applicationName' }, + env: { name: 'aws.lambda' } + }, + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + application: { name: 'applicationName' }, + env: { name: 'aws.lambda' } + } + ], + [ + 'truncates environment metadata after platform', + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + platform: 'Node.js v16.17.0, LE', + application: { name: 'applicationName' }, + env: { + name: 'aws.lambda', + region: longDocument + } + }, + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + application: { name: 'applicationName' }, + env: { name: 'aws.lambda' } + } + ], + [ + 'truncates os metadata after env metadata', + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: longDocument, + version: '21.6.0' + }, + platform: 'Node.js v16.17.0, LE', + application: { name: 'applicationName' }, + env: { name: 'aws.lambda' } + }, + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { type: 'Darwin' }, + application: { name: 'applicationName' }, + env: { name: 'aws.lambda' } + } + ], + [ + 'removes env after truncating os metadata', + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + platform: 'Node.js v16.17.0, LE', + application: { name: 'applicationName' }, + env: { + name: longDocument as any + } + }, + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { type: 'Darwin' }, + application: { name: 'applicationName' } + } + ], + [ + 'removes os after removing env', + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { + type: longDocument, + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + platform: 'Node.js v16.17.0, LE', + application: { name: 'applicationName' }, + env: { name: 'aws.lambda' } + }, + { + application: { name: 'applicationName' }, + driver: { name: 'nodejs', version: '5.1.0' } + } + ], + [ + 'removes driver after removing env', + { + driver: { + name: longDocument, + version: '5.1.0' + }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + platform: 'Node.js v16.17.0, LE', + application: { name: 'applicationName' }, + env: { name: 'aws.lambda' } + }, + { application: { name: 'applicationName' } } + ], + [ + 'returns nothing when everything is too large (should never happen)', + { + driver: { name: 'nodejs', version: '5.1.0' }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + platform: 'Node.js v16.17.0, LE', + application: { + name: longDocument + }, + env: { name: 'aws.lambda' } + }, + {} + ] + ]; + + for (const [description, input, expected] of tests) { + it(description, function () { + expect(truncateClientMetadata(input)).to.deep.equal(expected); + }); + } + }); }); diff --git a/test/unit/connection_string.test.ts b/test/unit/connection_string.test.ts index 4a33259bb9..9d2ac852c2 100644 --- a/test/unit/connection_string.test.ts +++ b/test/unit/connection_string.test.ts @@ -641,4 +641,38 @@ describe('Connection String', function () { ]); }); }); + + context('when the metadata is >512 bytes', () => { + it('truncates the metadata', () => { + const client = new MongoClient('mongodb://localhost:27017', { + appName: 'my app', + driverInfo: { name: 'a'.repeat(512) } + }); + expect(client.options.truncatedClientMetadata).to.deep.equal({ + application: { name: 'my app' } + }); + }); + + it('preserves the untruncated metadata on the `metadata` property', () => { + const client = new MongoClient('mongodb://localhost:27017', { + appName: 'my app', + driverInfo: { name: 'a'.repeat(512) } + }); + console.error(client.options.metadata); + expect(client.options.metadata).to.deep.equal({ + driver: { + name: 'nodejs|mongodb-legacy|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + version: '5.1.0|5.0.0' + }, + os: { + type: 'Darwin', + name: 'darwin', + architecture: 'x64', + version: '21.6.0' + }, + platform: 'Node.js v16.17.0, LE', + application: { name: 'my app' } + }); + }); + }); }); diff --git a/test/unit/sdam/topology.test.js b/test/unit/sdam/topology.test.js index 8f9dd6e984..244df35296 100644 --- a/test/unit/sdam/topology.test.js +++ b/test/unit/sdam/topology.test.js @@ -36,7 +36,7 @@ describe('Topology (unit)', function () { it('should correctly pass appname', function (done) { const server = new Topology([`localhost:27017`], { - metadata: makeClientMetadata({ + truncatedClientMetadata: makeClientMetadata({ appName: 'My application name', driverInfo: {} }) From 1b86b2df429bbfa2d3561786a8ffae914413c131 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 3 Apr 2023 08:30:52 -0400 Subject: [PATCH 2/8] fix lint --- test/unit/connection_string.test.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test/unit/connection_string.test.ts b/test/unit/connection_string.test.ts index 9d2ac852c2..c5cdf3f533 100644 --- a/test/unit/connection_string.test.ts +++ b/test/unit/connection_string.test.ts @@ -1,5 +1,6 @@ import { expect } from 'chai'; import * as dns from 'dns'; +import * as os from 'os'; import * as sinon from 'sinon'; import { @@ -658,19 +659,22 @@ describe('Connection String', function () { appName: 'my app', driverInfo: { name: 'a'.repeat(512) } }); - console.error(client.options.metadata); + + // eslint-disable-next-line @typescript-eslint/no-var-requires + const NODE_DRIVER_VERSION = require('../../package.json').version; + expect(client.options.metadata).to.deep.equal({ driver: { name: 'nodejs|mongodb-legacy|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', - version: '5.1.0|5.0.0' + version: `${NODE_DRIVER_VERSION}|5.0.0` }, os: { - type: 'Darwin', - name: 'darwin', - architecture: 'x64', - version: '21.6.0' + type: os.type(), + name: process.platform, + architecture: process.arch, + version: os.release() }, - platform: 'Node.js v16.17.0, LE', + platform: `Node.js ${process.version}, ${os.endianness()}`, application: { name: 'my app' } }); }); From ed44549f0511c20d00180b90838594cb4416d3cd Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 3 Apr 2023 08:43:52 -0400 Subject: [PATCH 3/8] misc fixes --- src/utils.ts | 5 +++++ test/unit/cmap/handshake/client_metadata.test.ts | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index 1793352f06..6f2bf1261b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1213,6 +1213,11 @@ export function parseUnsignedInteger(value: unknown): number | null { return parsedInt != null && parsedInt >= 0 ? parsedInt : null; } +/** + * returns the object that was provided + * + * @internal + */ export function identity(obj: T): T { return obj; } diff --git a/test/unit/cmap/handshake/client_metadata.test.ts b/test/unit/cmap/handshake/client_metadata.test.ts index c827588a91..3aa295fedf 100644 --- a/test/unit/cmap/handshake/client_metadata.test.ts +++ b/test/unit/cmap/handshake/client_metadata.test.ts @@ -368,6 +368,16 @@ describe('client metadata module', () => { }); describe('metadata truncation order', function () { + /** + * These tests demonstrate that the order in which metadata truncation occurs is spec + * compliant. There are tests in `connection_string.test.ts` that demonstrate that when + * the metadata is greater than 512 bytes, the metadata is truncated. + * + * Together, these tests demonstrate that + * - truncation happens in the correct order + * - truncation occurs when necessary + */ + const longDocument = 'a'.repeat(512); const tests: Array<[string, ClientMetadata, TruncatedClientMetadata]> = [ From da62ec4dd6ca60d897205e0a60b88a69fc644d03 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 3 Apr 2023 12:30:10 -0400 Subject: [PATCH 4/8] remove truncated client metadata --- src/cmap/connect.ts | 6 +-- src/cmap/connection.ts | 5 +-- src/cmap/connection_pool.ts | 2 +- src/cmap/handshake/client_metadata.ts | 37 ++++--------------- src/connection_string.ts | 2 - src/index.ts | 6 +-- src/mongo_client.ts | 12 +----- src/sdam/topology.ts | 7 ++-- .../connection.test.ts | 6 +-- .../node-specific/topology.test.js | 2 +- test/tools/cmap_spec_runner.ts | 4 +- .../cmap/handshake/client_metadata.test.ts | 5 +-- test/unit/connection_string.test.ts | 2 +- test/unit/sdam/topology.test.js | 2 +- 14 files changed, 28 insertions(+), 70 deletions(-) diff --git a/src/cmap/connect.ts b/src/cmap/connect.ts index 324c6f7485..7789527f9d 100644 --- a/src/cmap/connect.ts +++ b/src/cmap/connect.ts @@ -28,7 +28,7 @@ import { AuthMechanism } from './auth/providers'; import { ScramSHA1, ScramSHA256 } from './auth/scram'; import { X509 } from './auth/x509'; import { CommandOptions, Connection, ConnectionOptions, CryptoConnection } from './connection'; -import type { TruncatedClientMetadata } from './handshake/client_metadata'; +import type { ClientMetadata } from './handshake/client_metadata'; import { MAX_SUPPORTED_SERVER_VERSION, MAX_SUPPORTED_WIRE_VERSION, @@ -193,7 +193,7 @@ export interface HandshakeDocument extends Document { ismaster?: boolean; hello?: boolean; helloOk?: boolean; - client: TruncatedClientMetadata; + client: ClientMetadata; compression: string[]; saslSupportedMechs?: string; loadBalanced?: boolean; @@ -214,7 +214,7 @@ export async function prepareHandshakeDocument( const handshakeDoc: HandshakeDocument = { [serverApi?.version ? 'hello' : LEGACY_HELLO_COMMAND]: 1, helloOk: true, - client: options.truncatedClientMetadata, + client: options.metadata, compression: compressors }; diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 8ffaa54718..66e80aa95b 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -45,7 +45,7 @@ import { } from './command_monitoring_events'; import { BinMsg, Msg, Query, Response, WriteProtocolMessageType } from './commands'; import type { Stream } from './connect'; -import type { ClientMetadata, TruncatedClientMetadata } from './handshake/client_metadata'; +import type { ClientMetadata } from './handshake/client_metadata'; import { MessageStream, OperationDescription } from './message_stream'; import { StreamDescription, StreamDescriptionOptions } from './stream_description'; import { getReadPreference, isSharded } from './wire_protocol/shared'; @@ -128,9 +128,6 @@ export interface ConnectionOptions socketTimeoutMS?: number; cancellationToken?: CancellationToken; metadata: ClientMetadata; - - /** @internal */ - truncatedClientMetadata: TruncatedClientMetadata; } /** @internal */ diff --git a/src/cmap/connection_pool.ts b/src/cmap/connection_pool.ts index 4acc1551e5..e3d4228135 100644 --- a/src/cmap/connection_pool.ts +++ b/src/cmap/connection_pool.ts @@ -227,7 +227,7 @@ export class ConnectionPool extends TypedEventEmitter { waitQueueTimeoutMS: options.waitQueueTimeoutMS ?? 0, minPoolSizeCheckFrequencyMS: options.minPoolSizeCheckFrequencyMS ?? 100, autoEncrypter: options.autoEncrypter, - truncatedClientMetadata: options.truncatedClientMetadata + metadata: options.metadata }); if (this.options.minPoolSize > this.options.maxPoolSize) { diff --git a/src/cmap/handshake/client_metadata.ts b/src/cmap/handshake/client_metadata.ts index e436bb722d..e77203dfca 100644 --- a/src/cmap/handshake/client_metadata.ts +++ b/src/cmap/handshake/client_metadata.ts @@ -2,7 +2,7 @@ import { calculateObjectSize } from 'bson'; import * as os from 'os'; import type { MongoOptions } from '../../mongo_client'; -import { deepCopy, DeepPartial } from '../../utils'; +import { deepCopy } from '../../utils'; import { applyFaasEnvMetadata } from './faas_provider'; /** @@ -35,40 +35,19 @@ export interface ClientMetadata { }; } -/** @internal */ -export type TruncatedClientMetadata = DeepPartial; - /** * @internal * truncates the client metadata according to the priority outlined here * https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#limitations */ -export function truncateClientMetadata(metadata: ClientMetadata): TruncatedClientMetadata { - const copiedMetadata: TruncatedClientMetadata = deepCopy(metadata); - const truncations: Array<(arg0: TruncatedClientMetadata) => void> = [ - m => delete m.platform, - m => { - if (m.env) { - m.env = { name: m.env.name }; - } - }, - m => { - if (m.os) { - m.os = { type: m.os.type }; - } - }, - m => delete m.env, - m => delete m.os, - m => delete m.driver, - m => delete m.application - ]; +export function truncateClientMetadata(metadata: ClientMetadata): ClientMetadata { + const copiedMetadata: ClientMetadata = deepCopy(metadata); - for (const truncation of truncations) { - if (calculateObjectSize(copiedMetadata) <= 512) { - return copiedMetadata; - } - truncation(copiedMetadata); - } + // if () + + // 1. Truncate ``platform``. + // 2. Omit fields from ``env`` except ``env.name``. + // 3. Omit the ``env`` document entirely. return copiedMetadata; } diff --git a/src/connection_string.ts b/src/connection_string.ts index fe8cf90e6b..9c4f67c6ad 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -543,8 +543,6 @@ export function parseOptions( ); mongoOptions.metadata = makeClientMetadata(mongoOptions); - Object.freeze(mongoOptions.metadata); - mongoOptions.truncatedClientMetadata = truncateClientMetadata(mongoOptions.metadata); return mongoOptions; } diff --git a/src/index.ts b/src/index.ts index da39a758ba..c945fbbcd7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -238,11 +238,7 @@ export type { WaitQueueMember, WithConnectionCallback } from './cmap/connection_pool'; -export type { - ClientMetadata, - ClientMetadataOptions, - TruncatedClientMetadata -} from './cmap/handshake/client_metadata'; +export type { ClientMetadata, ClientMetadataOptions } from './cmap/handshake/client_metadata'; export type { MessageStream, MessageStreamOptions, diff --git a/src/mongo_client.ts b/src/mongo_client.ts index d5b2511a7b..651e4b00e9 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -8,7 +8,7 @@ import type { AuthMechanismProperties, MongoCredentials } from './cmap/auth/mong import type { AuthMechanism } from './cmap/auth/providers'; import type { LEGAL_TCP_SOCKET_OPTIONS, LEGAL_TLS_SOCKET_OPTIONS } from './cmap/connect'; import type { Connection } from './cmap/connection'; -import type { ClientMetadata, TruncatedClientMetadata } from './cmap/handshake/client_metadata'; +import type { ClientMetadata } from './cmap/handshake/client_metadata'; import type { CompressorName } from './cmap/wire_protocol/compression'; import { parseOptions, resolveSRVRecord } from './connection_string'; import { MONGO_CLIENT_EVENTS } from './constants'; @@ -720,16 +720,6 @@ export interface MongoOptions metadata: ClientMetadata; - /** - * @internal - * `metadata` truncated to be less than 512 bytes, if necessary, to attach to handshakes. - * `metadata` is left untouched because it is public and to provide users a document they - * inspect to confirm their metadata was parsed correctly. - * - * If `metadata` `<=` 512 bytes, these fields are the same but the driver only uses `truncatedMetadata`. - */ - truncatedClientMetadata: TruncatedClientMetadata; - /** @internal */ connectionType?: typeof Connection; diff --git a/src/sdam/topology.ts b/src/sdam/topology.ts index 66465e43ef..9a4629b50e 100644 --- a/src/sdam/topology.ts +++ b/src/sdam/topology.ts @@ -5,7 +5,7 @@ import type { BSONSerializeOptions, Document } from '../bson'; import type { MongoCredentials } from '../cmap/auth/mongo_credentials'; import type { ConnectionEvents, DestroyOptions } from '../cmap/connection'; import type { CloseOptions, ConnectionPoolEvents } from '../cmap/connection_pool'; -import type { ClientMetadata, TruncatedClientMetadata } from '../cmap/handshake/client_metadata'; +import type { ClientMetadata } from '../cmap/handshake/client_metadata'; import { DEFAULT_OPTIONS, FEATURE_FLAGS } from '../connection_string'; import { CLOSE, @@ -143,7 +143,6 @@ export interface TopologyOptions extends BSONSerializeOptions, ServerOptions { directConnection: boolean; loadBalanced: boolean; metadata: ClientMetadata; - truncatedClientMetadata: TruncatedClientMetadata; /** MongoDB server API version */ serverApi?: ServerApi; [featureFlag: symbol]: any; @@ -660,8 +659,8 @@ export class Topology extends TypedEventEmitter { if (typeof callback === 'function') callback(undefined, true); } - get clientMetadata(): TruncatedClientMetadata { - return this.s.options.truncatedClientMetadata; + get clientMetadata(): ClientMetadata { + return this.s.options.metadata; } isConnected(): boolean { diff --git a/test/integration/connection-monitoring-and-pooling/connection.test.ts b/test/integration/connection-monitoring-and-pooling/connection.test.ts index 7591aa7291..7ad5bb5c59 100644 --- a/test/integration/connection-monitoring-and-pooling/connection.test.ts +++ b/test/integration/connection-monitoring-and-pooling/connection.test.ts @@ -36,7 +36,7 @@ describe('Connection', function () { const connectOptions: Partial = { connectionType: Connection, ...this.configuration.options, - truncatedClientMetadata: makeClientMetadata({ driverInfo: {} }) + metadata: makeClientMetadata({ driverInfo: {} }) }; connect(connectOptions as any as ConnectionOptions, (err, conn) => { @@ -60,7 +60,7 @@ describe('Connection', function () { connectionType: Connection, monitorCommands: true, ...this.configuration.options, - truncatedClientMetadata: makeClientMetadata({ driverInfo: {} }) + metadata: makeClientMetadata({ driverInfo: {} }) }; connect(connectOptions as any as ConnectionOptions, (err, conn) => { @@ -92,7 +92,7 @@ describe('Connection', function () { const connectOptions: Partial = { connectionType: Connection, ...this.configuration.options, - truncatedClientMetadata: makeClientMetadata({ driverInfo: {} }) + metadata: makeClientMetadata({ driverInfo: {} }) }; connect(connectOptions as any as ConnectionOptions, (err, conn) => { diff --git a/test/integration/node-specific/topology.test.js b/test/integration/node-specific/topology.test.js index ced960c939..ee4393efcd 100644 --- a/test/integration/node-specific/topology.test.js +++ b/test/integration/node-specific/topology.test.js @@ -7,7 +7,7 @@ describe('Topology', function () { metadata: { requires: { apiVersion: false, topology: '!load-balanced' } }, // apiVersion not supported by newTopology() test: function (done) { const topology = this.configuration.newTopology({ - truncatedClientMetadata: makeClientMetadata({ driverInfo: {} }) + metadata: makeClientMetadata({ driverInfo: {} }) }); const states = []; diff --git a/test/tools/cmap_spec_runner.ts b/test/tools/cmap_spec_runner.ts index 99da831840..6292eaae77 100644 --- a/test/tools/cmap_spec_runner.ts +++ b/test/tools/cmap_spec_runner.ts @@ -370,7 +370,7 @@ async function runCmapTest(test: CmapTest, threadContext: ThreadContext) { delete poolOptions.backgroundThreadIntervalMS; } - const truncatedClientMetadata = makeClientMetadata({ + const metadata = makeClientMetadata({ appName: poolOptions.appName, driverInfo: {} }); @@ -387,7 +387,7 @@ async function runCmapTest(test: CmapTest, threadContext: ThreadContext) { threadContext.createPool({ ...poolOptions, - truncatedClientMetadata, + metadata, minPoolSizeCheckFrequencyMS }); // yield control back to the event loop so that the ConnectionPoolCreatedEvent diff --git a/test/unit/cmap/handshake/client_metadata.test.ts b/test/unit/cmap/handshake/client_metadata.test.ts index 3aa295fedf..80bebd2ba3 100644 --- a/test/unit/cmap/handshake/client_metadata.test.ts +++ b/test/unit/cmap/handshake/client_metadata.test.ts @@ -6,8 +6,7 @@ import { determineCloudProvider, FAASProvider, makeClientMetadata, - truncateClientMetadata, - TruncatedClientMetadata + truncateClientMetadata } from '../../../mongodb'; // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -380,7 +379,7 @@ describe('client metadata module', () => { const longDocument = 'a'.repeat(512); - const tests: Array<[string, ClientMetadata, TruncatedClientMetadata]> = [ + const tests: Array<[string, ClientMetadata, ClientMetadata]> = [ [ 'only removes platform first', { diff --git a/test/unit/connection_string.test.ts b/test/unit/connection_string.test.ts index c5cdf3f533..2dfc3debe2 100644 --- a/test/unit/connection_string.test.ts +++ b/test/unit/connection_string.test.ts @@ -649,7 +649,7 @@ describe('Connection String', function () { appName: 'my app', driverInfo: { name: 'a'.repeat(512) } }); - expect(client.options.truncatedClientMetadata).to.deep.equal({ + expect(client.options.metadata).to.deep.equal({ application: { name: 'my app' } }); }); diff --git a/test/unit/sdam/topology.test.js b/test/unit/sdam/topology.test.js index 244df35296..8f9dd6e984 100644 --- a/test/unit/sdam/topology.test.js +++ b/test/unit/sdam/topology.test.js @@ -36,7 +36,7 @@ describe('Topology (unit)', function () { it('should correctly pass appname', function (done) { const server = new Topology([`localhost:27017`], { - truncatedClientMetadata: makeClientMetadata({ + metadata: makeClientMetadata({ appName: 'My application name', driverInfo: {} }) From a98a423fcf03c1d72a6af08efc52756fa2654614 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 3 Apr 2023 14:30:17 -0400 Subject: [PATCH 5/8] chore: everything but truncation --- src/cmap/handshake/faas_provider.ts | 29 ++++++++++++------- src/index.ts | 1 - src/utils.ts | 7 ----- .../mongodb-handshake.prose.test.ts | 11 +++++-- .../cmap/handshake/client_metadata.test.ts | 10 +++---- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/cmap/handshake/faas_provider.ts b/src/cmap/handshake/faas_provider.ts index ce9df91af0..ee5b8d5049 100644 --- a/src/cmap/handshake/faas_provider.ts +++ b/src/cmap/handshake/faas_provider.ts @@ -3,11 +3,18 @@ import type { ClientMetadata } from './client_metadata'; export type FAASProvider = 'aws' | 'gcp' | 'azure' | 'vercel' | 'none'; -export function determineCloudProvider(): FAASProvider { - const awsPresent = process.env.AWS_EXECUTION_ENV || process.env.AWS_LAMBDA_RUNTIME_API; - const azurePresent = process.env.FUNCTIONS_WORKER_RUNTIME; - const gcpPresent = process.env.K_SERVICE || process.env.FUNCTION_NAME; - const vercelPresent = process.env.VERCEL; +function isNonEmptyString(s: string | undefined): s is string { + return typeof s === 'string' && s.length > 0; +} + +export function determineFAASProvider(): FAASProvider { + const awsPresent = + isNonEmptyString(process.env.AWS_EXECUTION_ENV) || + isNonEmptyString(process.env.AWS_LAMBDA_RUNTIME_API); + const azurePresent = isNonEmptyString(process.env.FUNCTIONS_WORKER_RUNTIME); + const gcpPresent = + isNonEmptyString(process.env.K_SERVICE) || isNonEmptyString(process.env.FUNCTION_NAME); + const vercelPresent = isNonEmptyString(process.env.VERCEL); const numberOfProvidersPresent = [awsPresent, azurePresent, gcpPresent, vercelPresent].filter( identity @@ -39,7 +46,7 @@ function applyGCPMetadata(m: ClientMetadata): ClientMetadata { if (!Number.isNaN(timeout_sec)) { m.env.timeout_sec = timeout_sec; } - if (process.env.FUNCTION_REGION) { + if (isNonEmptyString(process.env.FUNCTION_REGION)) { m.env.region = process.env.FUNCTION_REGION; } @@ -48,7 +55,7 @@ function applyGCPMetadata(m: ClientMetadata): ClientMetadata { function applyAWSMetadata(m: ClientMetadata): ClientMetadata { m.env = { name: 'aws.lambda' }; - if (process.env.AWS_REGION) { + if (isNonEmptyString(process.env.AWS_REGION)) { m.env.region = process.env.AWS_REGION; } const memory_mb = Number.parseInt(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE ?? ''); @@ -60,10 +67,10 @@ function applyAWSMetadata(m: ClientMetadata): ClientMetadata { function applyVercelMetadata(m: ClientMetadata): ClientMetadata { m.env = { name: 'vercel' }; - if (process.env.VERCEL_URL) { + if (isNonEmptyString(process.env.VERCEL_URL)) { m.env.url = process.env.VERCEL_URL; } - if (process.env.VERCEL_REGION) { + if (isNonEmptyString(process.env.VERCEL_REGION)) { m.env.region = process.env.VERCEL_REGION; } return m; @@ -77,8 +84,8 @@ export function applyFaasEnvMetadata(metadata: ClientMetadata): ClientMetadata { vercel: applyVercelMetadata, none: identity }; - const cloudProvider = determineCloudProvider(); + const faasProvider = determineFAASProvider(); - const faasMetadataProvider = handlerMap[cloudProvider]; + const faasMetadataProvider = handlerMap[faasProvider]; return faasMetadataProvider(metadata); } diff --git a/src/index.ts b/src/index.ts index c945fbbcd7..057047684e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -464,7 +464,6 @@ export type { Transaction, TransactionOptions, TxnState } from './transactions'; export type { BufferPool, Callback, - DeepPartial, EventEmitterWithState, HostAddress, List, diff --git a/src/utils.ts b/src/utils.ts index 6f2bf1261b..7434c7e659 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -38,13 +38,6 @@ export type Callback = (error?: AnyError, result?: T) => void; export type AnyOptions = Document; -/** @internal */ -export type DeepPartial = T extends object - ? { - [P in keyof T]?: DeepPartial; - } - : T; - export const ByteUtils = { toLocalBufferType(this: void, buffer: Buffer | Uint8Array): Buffer { return Buffer.isBuffer(buffer) diff --git a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts index a97c25358b..9f81bf78d9 100644 --- a/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts +++ b/test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { determineCloudProvider, FAASProvider, MongoClient } from '../../mongodb'; +import { determineFAASProvider, FAASProvider, MongoClient } from '../../mongodb'; context('FAAS Environment Prose Tests', function () { let client: MongoClient; @@ -89,14 +89,19 @@ context('FAAS Environment Prose Tests', function () { }); before(`metadata confirmation test for ${name}`, function () { - expect(determineCloudProvider()).to.equal( + expect(determineFAASProvider()).to.equal( expectedProvider, 'determined the wrong cloud provider' ); }); it('runs a hello successfully', async function () { - client = this.configuration.newClient({ serverSelectionTimeoutMS: 3000 }); + client = this.configuration.newClient({ + // if the handshake is not truncated, the `hello`s fail and the client does + // not connect. Lowering the server selection timeout causes the tests + // to fail more quickly in that scenario. + serverSelectionTimeoutMS: 3000 + }); await client.connect(); }); }); diff --git a/test/unit/cmap/handshake/client_metadata.test.ts b/test/unit/cmap/handshake/client_metadata.test.ts index 80bebd2ba3..412debdf3f 100644 --- a/test/unit/cmap/handshake/client_metadata.test.ts +++ b/test/unit/cmap/handshake/client_metadata.test.ts @@ -3,7 +3,7 @@ import * as os from 'os'; import { ClientMetadata, - determineCloudProvider, + determineFAASProvider, FAASProvider, makeClientMetadata, truncateClientMetadata @@ -12,7 +12,7 @@ import { // eslint-disable-next-line @typescript-eslint/no-var-requires const NODE_DRIVER_VERSION = require('../../../../package.json').version; -describe('client metadata module', () => { +describe.only('client metadata module', () => { describe('determineCloudProvider()', function () { const tests: Array<[string, FAASProvider]> = [ ['AWS_EXECUTION_ENV', 'aws'], @@ -31,14 +31,14 @@ describe('client metadata module', () => { delete process.env[envVariable]; }); it('determines the correct provider', () => { - expect(determineCloudProvider()).to.equal(provider); + expect(determineFAASProvider()).to.equal(provider); }); }); } context('when there is no FAAS provider data in the env', () => { it('parses no FAAS provider', () => { - expect(determineCloudProvider()).to.equal('none'); + expect(determineFAASProvider()).to.equal('none'); }); }); @@ -52,7 +52,7 @@ describe('client metadata module', () => { delete process.env.FUNCTIONS_WORKER_RUNTIME; }); it('parses no FAAS provider', () => { - expect(determineCloudProvider()).to.equal('none'); + expect(determineFAASProvider()).to.equal('none'); }); }); }); From ef58d3f87cbb86728eff9663579e1b2589b35772 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 3 Apr 2023 14:47:47 -0400 Subject: [PATCH 6/8] chore: misc updates --- src/cmap/handshake/client_metadata.ts | 23 +++- .../cmap/handshake/client_metadata.test.ts | 125 ++---------------- test/unit/connection_string.test.ts | 37 ------ 3 files changed, 24 insertions(+), 161 deletions(-) diff --git a/src/cmap/handshake/client_metadata.ts b/src/cmap/handshake/client_metadata.ts index e77203dfca..5cab4bf12a 100644 --- a/src/cmap/handshake/client_metadata.ts +++ b/src/cmap/handshake/client_metadata.ts @@ -2,7 +2,6 @@ import { calculateObjectSize } from 'bson'; import * as os from 'os'; import type { MongoOptions } from '../../mongo_client'; -import { deepCopy } from '../../utils'; import { applyFaasEnvMetadata } from './faas_provider'; /** @@ -41,15 +40,25 @@ export interface ClientMetadata { * https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#limitations */ export function truncateClientMetadata(metadata: ClientMetadata): ClientMetadata { - const copiedMetadata: ClientMetadata = deepCopy(metadata); - - // if () + if (calculateObjectSize(metadata) <= 512) { + return metadata; + } + // 1. Truncate ``platform``. + // no-op - we don't truncate because the `platform` field is essentially a fixed length in Node + // and there isn't anything we can truncate that without removing useful information. - // 1. Truncate ``platform``. // 2. Omit fields from ``env`` except ``env.name``. + if (metadata.env) { + metadata.env = { name: metadata.env.name }; + } + if (calculateObjectSize(metadata) <= 512) { + return metadata; + } + // 3. Omit the ``env`` document entirely. + delete metadata.env; - return copiedMetadata; + return metadata; } /** @public */ @@ -99,5 +108,5 @@ export function makeClientMetadata( metadata.application = { name }; } - return applyFaasEnvMetadata(metadata); + return truncateClientMetadata(applyFaasEnvMetadata(metadata)); } diff --git a/test/unit/cmap/handshake/client_metadata.test.ts b/test/unit/cmap/handshake/client_metadata.test.ts index 412debdf3f..e5a2e24159 100644 --- a/test/unit/cmap/handshake/client_metadata.test.ts +++ b/test/unit/cmap/handshake/client_metadata.test.ts @@ -12,7 +12,7 @@ import { // eslint-disable-next-line @typescript-eslint/no-var-requires const NODE_DRIVER_VERSION = require('../../../../package.json').version; -describe.only('client metadata module', () => { +describe('client metadata module', () => { describe('determineCloudProvider()', function () { const tests: Array<[string, FAASProvider]> = [ ['AWS_EXECUTION_ENV', 'aws'], @@ -366,48 +366,12 @@ describe.only('client metadata module', () => { }); }); - describe('metadata truncation order', function () { - /** - * These tests demonstrate that the order in which metadata truncation occurs is spec - * compliant. There are tests in `connection_string.test.ts` that demonstrate that when - * the metadata is greater than 512 bytes, the metadata is truncated. - * - * Together, these tests demonstrate that - * - truncation happens in the correct order - * - truncation occurs when necessary - */ - + describe('metadata truncation', function () { const longDocument = 'a'.repeat(512); const tests: Array<[string, ClientMetadata, ClientMetadata]> = [ [ - 'only removes platform first', - { - driver: { name: 'nodejs', version: '5.1.0' }, - os: { - type: 'Darwin', - name: 'darwin', - architecture: 'x64', - version: '21.6.0' - }, - platform: longDocument, - application: { name: 'applicationName' }, - env: { name: 'aws.lambda' } - }, - { - driver: { name: 'nodejs', version: '5.1.0' }, - os: { - type: 'Darwin', - name: 'darwin', - architecture: 'x64', - version: '21.6.0' - }, - application: { name: 'applicationName' }, - env: { name: 'aws.lambda' } - } - ], - [ - 'truncates environment metadata after platform', + 'removes extra fields in `env` first', { driver: { name: 'nodejs', version: '5.1.0' }, os: { @@ -418,10 +382,7 @@ describe.only('client metadata module', () => { }, platform: 'Node.js v16.17.0, LE', application: { name: 'applicationName' }, - env: { - name: 'aws.lambda', - region: longDocument - } + env: { name: 'aws.lambda', region: longDocument } }, { driver: { name: 'nodejs', version: '5.1.0' }, @@ -431,33 +392,13 @@ describe.only('client metadata module', () => { architecture: 'x64', version: '21.6.0' }, - application: { name: 'applicationName' }, - env: { name: 'aws.lambda' } - } - ], - [ - 'truncates os metadata after env metadata', - { - driver: { name: 'nodejs', version: '5.1.0' }, - os: { - type: 'Darwin', - name: 'darwin', - architecture: longDocument, - version: '21.6.0' - }, platform: 'Node.js v16.17.0, LE', application: { name: 'applicationName' }, env: { name: 'aws.lambda' } - }, - { - driver: { name: 'nodejs', version: '5.1.0' }, - os: { type: 'Darwin' }, - application: { name: 'applicationName' }, - env: { name: 'aws.lambda' } - } + }) ], [ - 'removes env after truncating os metadata', + 'removes `env` entirely next', { driver: { name: 'nodejs', version: '5.1.0' }, os: { @@ -472,67 +413,17 @@ describe.only('client metadata module', () => { name: longDocument as any } }, - { - driver: { name: 'nodejs', version: '5.1.0' }, - os: { type: 'Darwin' }, - application: { name: 'applicationName' } - } - ], - [ - 'removes os after removing env', { driver: { name: 'nodejs', version: '5.1.0' }, - os: { - type: longDocument, - name: 'darwin', - architecture: 'x64', - version: '21.6.0' - }, - platform: 'Node.js v16.17.0, LE', - application: { name: 'applicationName' }, - env: { name: 'aws.lambda' } - }, - { - application: { name: 'applicationName' }, - driver: { name: 'nodejs', version: '5.1.0' } - } - ], - [ - 'removes driver after removing env', - { - driver: { - name: longDocument, - version: '5.1.0' - }, os: { type: 'Darwin', name: 'darwin', architecture: 'x64', version: '21.6.0' }, - platform: 'Node.js v16.17.0, LE', application: { name: 'applicationName' }, - env: { name: 'aws.lambda' } - }, - { application: { name: 'applicationName' } } - ], - [ - 'returns nothing when everything is too large (should never happen)', - { - driver: { name: 'nodejs', version: '5.1.0' }, - os: { - type: 'Darwin', - name: 'darwin', - architecture: 'x64', - version: '21.6.0' - }, - platform: 'Node.js v16.17.0, LE', - application: { - name: longDocument - }, - env: { name: 'aws.lambda' } - }, - {} + platform: 'Node.js v16.17.0, LE' + } ] ]; diff --git a/test/unit/connection_string.test.ts b/test/unit/connection_string.test.ts index 2dfc3debe2..e85f379cf8 100644 --- a/test/unit/connection_string.test.ts +++ b/test/unit/connection_string.test.ts @@ -642,41 +642,4 @@ describe('Connection String', function () { ]); }); }); - - context('when the metadata is >512 bytes', () => { - it('truncates the metadata', () => { - const client = new MongoClient('mongodb://localhost:27017', { - appName: 'my app', - driverInfo: { name: 'a'.repeat(512) } - }); - expect(client.options.metadata).to.deep.equal({ - application: { name: 'my app' } - }); - }); - - it('preserves the untruncated metadata on the `metadata` property', () => { - const client = new MongoClient('mongodb://localhost:27017', { - appName: 'my app', - driverInfo: { name: 'a'.repeat(512) } - }); - - // eslint-disable-next-line @typescript-eslint/no-var-requires - const NODE_DRIVER_VERSION = require('../../package.json').version; - - expect(client.options.metadata).to.deep.equal({ - driver: { - name: 'nodejs|mongodb-legacy|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', - version: `${NODE_DRIVER_VERSION}|5.0.0` - }, - os: { - type: os.type(), - name: process.platform, - architecture: process.arch, - version: os.release() - }, - platform: `Node.js ${process.version}, ${os.endianness()}`, - application: { name: 'my app' } - }); - }); - }); }); From 51ae18b6ce42c2dec3b6f644c5480a567398bf61 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 3 Apr 2023 14:59:04 -0400 Subject: [PATCH 7/8] misc changes --- src/connection_string.ts | 2 +- src/mongo_client.ts | 3 +-- test/unit/cmap/handshake/client_metadata.test.ts | 2 +- test/unit/connection_string.test.ts | 1 - 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/connection_string.ts b/src/connection_string.ts index 9c4f67c6ad..c7ba4e028c 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -6,7 +6,7 @@ import { URLSearchParams } from 'url'; import type { Document } from './bson'; import { MongoCredentials } from './cmap/auth/mongo_credentials'; import { AUTH_MECHS_AUTH_SRC_EXTERNAL, AuthMechanism } from './cmap/auth/providers'; -import { makeClientMetadata, truncateClientMetadata } from './cmap/handshake/client_metadata'; +import { makeClientMetadata } from './cmap/handshake/client_metadata'; import { Compressor, CompressorName } from './cmap/wire_protocol/compression'; import { Encrypter } from './encrypter'; import { diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 651e4b00e9..21bf61618a 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -712,14 +712,13 @@ export interface MongoOptions compressors: CompressorName[]; writeConcern: WriteConcern; dbName: string; + metadata: ClientMetadata; autoEncrypter?: AutoEncrypter; proxyHost?: string; proxyPort?: number; proxyUsername?: string; proxyPassword?: string; - metadata: ClientMetadata; - /** @internal */ connectionType?: typeof Connection; diff --git a/test/unit/cmap/handshake/client_metadata.test.ts b/test/unit/cmap/handshake/client_metadata.test.ts index e5a2e24159..0c904a07e8 100644 --- a/test/unit/cmap/handshake/client_metadata.test.ts +++ b/test/unit/cmap/handshake/client_metadata.test.ts @@ -395,7 +395,7 @@ describe('client metadata module', () => { platform: 'Node.js v16.17.0, LE', application: { name: 'applicationName' }, env: { name: 'aws.lambda' } - }) + } ], [ 'removes `env` entirely next', diff --git a/test/unit/connection_string.test.ts b/test/unit/connection_string.test.ts index e85f379cf8..4a33259bb9 100644 --- a/test/unit/connection_string.test.ts +++ b/test/unit/connection_string.test.ts @@ -1,6 +1,5 @@ import { expect } from 'chai'; import * as dns from 'dns'; -import * as os from 'os'; import * as sinon from 'sinon'; import { From 6853019d3fd4fd91682d7bc3f6ebdcfc42b59136 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 3 Apr 2023 15:44:22 -0400 Subject: [PATCH 8/8] use `Int32` --- src/cmap/handshake/client_metadata.ts | 6 +++--- src/cmap/handshake/faas_provider.ts | 20 ++++++++++--------- .../cmap/handshake/client_metadata.test.ts | 5 +++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/cmap/handshake/client_metadata.ts b/src/cmap/handshake/client_metadata.ts index 5cab4bf12a..c19e8b3776 100644 --- a/src/cmap/handshake/client_metadata.ts +++ b/src/cmap/handshake/client_metadata.ts @@ -1,4 +1,4 @@ -import { calculateObjectSize } from 'bson'; +import { calculateObjectSize, Int32 } from 'bson'; import * as os from 'os'; import type { MongoOptions } from '../../mongo_client'; @@ -27,8 +27,8 @@ export interface ClientMetadata { /** Data containing information about the environment, if the driver is running in a FAAS environment. */ env?: { name: 'aws.lambda' | 'gcp.func' | 'azure.func' | 'vercel'; - timeout_sec?: number; - memory_mb?: number; + timeout_sec?: Int32; + memory_mb?: Int32; region?: string; url?: string; }; diff --git a/src/cmap/handshake/faas_provider.ts b/src/cmap/handshake/faas_provider.ts index ee5b8d5049..c1bfdc9ad1 100644 --- a/src/cmap/handshake/faas_provider.ts +++ b/src/cmap/handshake/faas_provider.ts @@ -1,3 +1,5 @@ +import { Int32 } from 'bson'; + import { identity } from '../../utils'; import type { ClientMetadata } from './client_metadata'; @@ -38,13 +40,13 @@ function applyAzureMetadata(m: ClientMetadata): ClientMetadata { function applyGCPMetadata(m: ClientMetadata): ClientMetadata { m.env = { name: 'gcp.func' }; - const memory_mb = Number.parseInt(process.env.FUNCTION_MEMORY_MB ?? ''); - if (!Number.isNaN(memory_mb)) { - m.env.memory_mb = memory_mb; + const memory_mb = Number(process.env.FUNCTION_MEMORY_MB); + if (Number.isInteger(memory_mb)) { + m.env.memory_mb = new Int32(memory_mb); } - const timeout_sec = Number.parseInt(process.env.FUNCTION_TIMEOUT_SEC ?? ''); - if (!Number.isNaN(timeout_sec)) { - m.env.timeout_sec = timeout_sec; + const timeout_sec = Number(process.env.FUNCTION_TIMEOUT_SEC); + if (Number.isInteger(timeout_sec)) { + m.env.timeout_sec = new Int32(timeout_sec); } if (isNonEmptyString(process.env.FUNCTION_REGION)) { m.env.region = process.env.FUNCTION_REGION; @@ -58,9 +60,9 @@ function applyAWSMetadata(m: ClientMetadata): ClientMetadata { if (isNonEmptyString(process.env.AWS_REGION)) { m.env.region = process.env.AWS_REGION; } - const memory_mb = Number.parseInt(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE ?? ''); - if (!Number.isNaN(memory_mb)) { - m.env.memory_mb = memory_mb; + const memory_mb = Number(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE); + if (Number.isInteger(memory_mb)) { + m.env.memory_mb = new Int32(memory_mb); } return m; } diff --git a/test/unit/cmap/handshake/client_metadata.test.ts b/test/unit/cmap/handshake/client_metadata.test.ts index 0c904a07e8..42d9cf5ca7 100644 --- a/test/unit/cmap/handshake/client_metadata.test.ts +++ b/test/unit/cmap/handshake/client_metadata.test.ts @@ -5,6 +5,7 @@ import { ClientMetadata, determineFAASProvider, FAASProvider, + Int32, makeClientMetadata, truncateClientMetadata } from '../../../mongodb'; @@ -251,7 +252,7 @@ describe('client metadata module', () => { ], outcome: { name: 'aws.lambda', - memory_mb: 3 + memory_mb: new Int32(3) } } ], @@ -280,7 +281,7 @@ describe('client metadata module', () => { ], outcome: { name: 'gcp.func', - memory_mb: 1024 + memory_mb: new Int32(1024) } }, {