Skip to content

Commit 068ef9b

Browse files
committed
chore: improve logging around local HTTP server listening MONGOSH-1917
1 parent 6d44d9c commit 068ef9b

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

src/log-hook.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ export function hookLoggerToMongoLogWriter(
9292
);
9393
});
9494

95+
emitter.on('mongodb-oidc-plugin:local-listen-resolved-hostname', (ev) => {
96+
log.info(
97+
'OIDC-PLUGIN',
98+
mongoLogId(1_002_000_028),
99+
`${contextPrefix}-oidc`,
100+
'Resolved hostnames for local server',
101+
{
102+
...ev,
103+
}
104+
);
105+
});
106+
95107
emitter.on('mongodb-oidc-plugin:local-listen-failed', (ev) => {
96108
log.error(
97109
'OIDC-PLUGIN',

src/rfc-8252-http-server.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,18 @@ export class RFC8252HTTPServer {
300300
*/
301301
public get listeningPort(): number | undefined {
302302
if (this.servers.length === 0) return undefined;
303-
const ports = new Set(
304-
this.servers.map((srv) => (srv.address() as AddressInfo)?.port)
303+
const addresses: AddressInfo[] = this.servers.map(
304+
(srv) => srv.address() as AddressInfo
305305
);
306+
const ports = new Set(addresses.map((addr) => addr.port));
306307
const port = ports.size === 1 && [...ports][0];
307308
if (typeof port !== 'number') {
309+
const addressesDebugInfo = addresses
310+
.map((addr) => JSON.stringify(addr))
311+
.join(',');
308312
// Should never happen
309313
throw new MongoDBOIDCError(
310-
`Server is listening in inconsistent state: ${[...ports].join(',')}`
314+
`Server is listening in inconsistent state: ${addressesDebugInfo}`
311315
);
312316
}
313317
return port;
@@ -343,8 +347,11 @@ export class RFC8252HTTPServer {
343347
);
344348
}
345349

350+
const urlPort = this.redirectUrl.port === '' ? 80 : +this.redirectUrl.port;
351+
346352
this.logger.emit('mongodb-oidc-plugin:local-listen-started', {
347353
url: this.redirectUrl.toString(),
354+
urlPort,
348355
});
349356

350357
// https://www.rfc-editor.org/rfc/rfc8252#section-7.3 states:
@@ -363,9 +370,18 @@ export class RFC8252HTTPServer {
363370
let hostname = this.redirectUrl.hostname;
364371
if (hostname.startsWith('[') && hostname.endsWith(']'))
365372
hostname = hostname.slice(1, -1);
366-
const dnsResults = await dns.lookup(hostname, {
367-
all: true,
368-
hints: ADDRCONFIG,
373+
const dnsResults = (
374+
await dns.lookup(hostname, {
375+
all: true,
376+
hints: ADDRCONFIG,
377+
})
378+
).map(({ address, family }) => ({ address, family }));
379+
380+
this.logger.emit('mongodb-oidc-plugin:local-listen-resolved-hostname', {
381+
url: this.redirectUrl.toString(),
382+
urlPort,
383+
hostname,
384+
interfaces: dnsResults,
369385
});
370386

371387
if (dnsResults.length === 0) {
@@ -375,9 +391,6 @@ export class RFC8252HTTPServer {
375391
}
376392

377393
try {
378-
const urlPort =
379-
this.redirectUrl.port === '' ? 80 : +this.redirectUrl.port;
380-
381394
// Two scenarios: Either we are listening on an arbitrary port here,
382395
// or listening on a specific port. Using an arbitrary port has the
383396
// advantage that the OS will allocate a free one for us, while a
@@ -406,7 +419,7 @@ export class RFC8252HTTPServer {
406419
if (typeof port !== 'number') {
407420
// Should never happen
408421
throw new MongoDBOIDCError(
409-
`Listening on ${dnsResults[0].address} did not return a port`
422+
`Listening on ${dnsResults[0].address} (family = ${dnsResults[0].family}) did not return a port`
410423
);
411424
}
412425
}
@@ -429,13 +442,16 @@ export class RFC8252HTTPServer {
429442
await this.close();
430443
this.logger.emit('mongodb-oidc-plugin:local-listen-failed', {
431444
url: this.redirectUrl.toString(),
445+
error: String(
446+
err && typeof err === 'object' && 'message' in err ? err.message : err
447+
),
432448
});
433449
throw err;
434450
}
435451

436452
this.logger.emit('mongodb-oidc-plugin:local-listen-succeeded', {
437453
url: this.listeningRedirectUrl || '',
438-
interfaces: dnsResults.map((dnsResult) => dnsResult.address),
454+
interfaces: dnsResults,
439455
});
440456
}
441457

src/types.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,23 @@ export interface MongoDBOIDCLogEventsMap {
2525
method: string;
2626
path: string;
2727
}) => void;
28-
'mongodb-oidc-plugin:local-listen-started': (event: { url: string }) => void;
29-
'mongodb-oidc-plugin:local-listen-failed': (event: { url: string }) => void;
28+
'mongodb-oidc-plugin:local-listen-started': (event: {
29+
url: string;
30+
urlPort: number;
31+
}) => void;
32+
'mongodb-oidc-plugin:local-listen-resolved-hostname': (event: {
33+
url: string;
34+
urlPort: number;
35+
hostname: string;
36+
interfaces: { family: number; address: string }[];
37+
}) => void;
38+
'mongodb-oidc-plugin:local-listen-failed': (event: {
39+
url: string;
40+
error: string;
41+
}) => void;
3042
'mongodb-oidc-plugin:local-listen-succeeded': (event: {
3143
url: string;
32-
interfaces: string[];
44+
interfaces: { family: number; address: string }[];
3345
}) => void;
3446
'mongodb-oidc-plugin:local-server-close': (event: { url: string }) => void;
3547
'mongodb-oidc-plugin:open-browser': (event: {

0 commit comments

Comments
 (0)