Skip to content

Commit

Permalink
reauth support for connection.established
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Jan 17, 2024
1 parent cbb5df6 commit ffeead4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/cmap/auth/auth_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ export abstract class AuthProvider {
throw new MongoRuntimeError('Reauthentication already in progress.');
}
try {
context.connection.established = false;
context.reauthenticating = true;
await this.auth(context);
} finally {
context.connection.established = true;
context.reauthenticating = false;
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
private messageStream: Readable;
private socketWrite: (buffer: Uint8Array) => Promise<void>;
private clusterTime: Document | null = null;
/** @internal */
override component = MongoLoggableComponent.COMMAND;
/** @internal */
override mongoLogger: MongoLogger | undefined;

Expand Down Expand Up @@ -461,7 +459,8 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
let started = 0;
if (
this.monitorCommands ||
(this.established && this.mongoLogger?.willLog(SeverityLevel.DEBUG, this.component))
(this.established &&
this.mongoLogger?.willLog(SeverityLevel.DEBUG, MongoLoggableComponent.COMMAND))
) {
started = now();
this.emitAndLogCommand(
Expand Down Expand Up @@ -490,7 +489,8 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {

if (
this.monitorCommands ||
(this.established && this.mongoLogger?.willLog(SeverityLevel.DEBUG, this.component))
(this.established &&
this.mongoLogger?.willLog(SeverityLevel.DEBUG, MongoLoggableComponent.COMMAND))
) {
this.emitAndLogCommand(
this.monitorCommands,
Expand All @@ -513,7 +513,8 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
} catch (error) {
if (
this.monitorCommands ||
(this.established && this.mongoLogger?.willLog(SeverityLevel.DEBUG, this.component))
(this.established &&
this.mongoLogger?.willLog(SeverityLevel.DEBUG, MongoLoggableComponent.COMMAND))
) {
if (error.name === 'MongoWriteConcernError') {
this.emitAndLogCommand(
Expand Down
6 changes: 3 additions & 3 deletions src/mongo_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ function defaultLogTransform(
return log;
case SERVER_SELECTION_FAILED:
log = attachServerSelectionFields(log, logObject, maxDocumentLength);
log.failure = logObject.failure.message;
log.failure = logObject.failure?.message;
return log;
case SERVER_SELECTION_SUCCEEDED:
log = attachServerSelectionFields(log, logObject, maxDocumentLength);
Expand All @@ -536,7 +536,7 @@ function defaultLogTransform(
log = attachCommandFields(log, logObject);
log.message = 'Command failed';
log.durationMS = logObject.duration;
log.failure = logObject.failure.message ?? '(redacted)';
log.failure = logObject.failure?.message ?? '(redacted)';
return log;
case CONNECTION_POOL_CREATED:
log = attachConnectionFields(log, logObject);
Expand Down Expand Up @@ -666,7 +666,7 @@ function defaultLogTransform(
log = attachServerHeartbeatFields(log, logObject);
log.message = 'Server heartbeat failed';
log.durationMS = logObject.duration;
log.failure = logObject.failure.message;
log.failure = logObject.failure?.message;
return log;
case TOPOLOGY_OPENING:
log = attachSDAMFields(log, logObject);
Expand Down
20 changes: 20 additions & 0 deletions test/unit/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,26 @@ describe('MongoOptions', function () {
});
});

context('when option is not a valid MongoDBLogwritable stream', function () {
it('should default to stderr', function () {
const writable = {
buffer: [],
misNamedWrite(log) {
this.buffer.push(log);
}
};
const client = new MongoClient('mongodb://a/', {
[loggerFeatureFlag]: true,
mongodbLogPath: writable
});
const log = { t: new Date(), c: 'constructorInvalidOption', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stderrStub.write).calledWith(
inspect(log, { breakLength: Infinity, compact: true })
);
});
});

context('when option is `stdout`', function () {
it('it is accessible through mongoLogger.logDestination', function () {
const client = new MongoClient('mongodb://a/', {
Expand Down

0 comments on commit ffeead4

Please sign in to comment.