Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-5537): remove credentials from ConnectionPoolCreatedEvent options #3813

Merged
merged 2 commits into from Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.d.ts
@@ -1,4 +1,4 @@
import { OneOrMore } from './src/mongo_types';
import { type OneOrMore } from './src/mongo_types';
import type { TestConfiguration } from './test/tools/runner/config';

declare global {
Expand Down
9 changes: 7 additions & 2 deletions src/cmap/connection_pool_events.ts
Expand Up @@ -54,14 +54,19 @@ export abstract class ConnectionPoolMonitoringEvent {
*/
export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
/** The options used to create this connection pool */
options?: ConnectionPoolOptions;
options: Omit<ConnectionPoolOptions, 'credentials'> & { credentials?: Record<never, never> };
/** @internal */
name = CONNECTION_POOL_CREATED;

/** @internal */
constructor(pool: ConnectionPool) {
super(pool);
this.options = pool.options;
if (pool.options.credentials != null) {
// Intentionally remove credentials: NODE-5460
this.options = { ...pool.options, credentials: {} };
} else {
this.options = pool.options;
}
}
}

Expand Down
@@ -1,3 +1,7 @@
import { expect } from 'chai';
import { once } from 'events';

import { type MongoClient } from '../../mongodb';
import { loadSpecTests } from '../../spec';
import { type CmapTest, runCmapTestSuite } from '../../tools/cmap_spec_runner';
import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';
Expand All @@ -23,4 +27,49 @@ describe('Connection Monitoring and Pooling (Node Driver)', function () {
'../integration/connection-monitoring-and-pooling/unified-cmap-node-specs'
);
runUnifiedSuite(unifiedTests);

describe('ConnectionPoolCreatedEvent', () => {
let client: MongoClient;
beforeEach(async function () {
client = this.configuration.newClient();
});

afterEach(async function () {
await client.close();
});

describe('constructor()', () => {
it('when auth is enabled redacts credentials from options', {
metadata: { requires: { auth: 'enabled' } },
async test() {
const poolCreated = once(client, 'connectionPoolCreated');
await client.connect();
const [event] = await poolCreated;
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
expect(event).to.have.deep.nested.property('options.credentials', {});

const poolOptions = Array.from(client.topology?.s.servers.values() ?? []).map(
s => s.pool.options
);
expect(poolOptions).to.have.length.of.at.least(1);

for (const { credentials = {} } of poolOptions) {
expect(
Object.keys(credentials),
'pool.options.credentials must exist and have keys'
).to.not.equal(0);
}
}
});

it('when auth is disabled does not add a credentials property to options', {
metadata: { requires: { auth: 'disabled' } },
async test() {
const poolCreated = once(client, 'connectionPoolCreated');
await client.connect();
const [event] = await poolCreated;
expect(event).to.not.have.nested.property('options.credentials');
}
});
});
});
});