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

chore: improve logging around local HTTP server listening MONGOSH-1917 #198

Merged
merged 3 commits into from
Nov 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/log-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ export function hookLoggerToMongoLogWriter(
);
});

emitter.on('mongodb-oidc-plugin:local-listen-resolved-hostname', (ev) => {
log.info(
'OIDC-PLUGIN',
mongoLogId(1_002_000_028),
`${contextPrefix}-oidc`,
'Resolved hostnames for local server',
{
...ev,
}
);
});

emitter.on('mongodb-oidc-plugin:local-listen-failed', (ev) => {
log.error(
'OIDC-PLUGIN',
Expand Down
38 changes: 27 additions & 11 deletions src/rfc-8252-http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,18 @@ export class RFC8252HTTPServer {
*/
public get listeningPort(): number | undefined {
if (this.servers.length === 0) return undefined;
const ports = new Set(
this.servers.map((srv) => (srv.address() as AddressInfo)?.port)
const addresses: AddressInfo[] = this.servers.map(
(srv) => srv.address() as AddressInfo
);
const ports = new Set(addresses.map((addr) => addr.port));
const port = ports.size === 1 && [...ports][0];
if (typeof port !== 'number') {
const addressesDebugInfo = addresses
.map((addr) => JSON.stringify(addr))
.join(',');
// Should never happen
throw new MongoDBOIDCError(
`Server is listening in inconsistent state: ${[...ports].join(',')}`
`Server is listening in inconsistent state: ${addressesDebugInfo}`
);
}
return port;
Expand Down Expand Up @@ -343,8 +347,11 @@ export class RFC8252HTTPServer {
);
}

const urlPort = this.redirectUrl.port === '' ? 80 : +this.redirectUrl.port;

this.logger.emit('mongodb-oidc-plugin:local-listen-started', {
url: this.redirectUrl.toString(),
urlPort,
});

// https://www.rfc-editor.org/rfc/rfc8252#section-7.3 states:
Expand All @@ -363,9 +370,18 @@ export class RFC8252HTTPServer {
let hostname = this.redirectUrl.hostname;
if (hostname.startsWith('[') && hostname.endsWith(']'))
hostname = hostname.slice(1, -1);
const dnsResults = await dns.lookup(hostname, {
all: true,
hints: ADDRCONFIG,
const dnsResults = (
await dns.lookup(hostname, {
all: true,
hints: ADDRCONFIG,
})
).map(({ address, family }) => ({ address, family }));

this.logger.emit('mongodb-oidc-plugin:local-listen-resolved-hostname', {
url: this.redirectUrl.toString(),
urlPort,
hostname,
interfaces: dnsResults,
});

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

try {
const urlPort =
this.redirectUrl.port === '' ? 80 : +this.redirectUrl.port;

// Two scenarios: Either we are listening on an arbitrary port here,
// or listening on a specific port. Using an arbitrary port has the
// advantage that the OS will allocate a free one for us, while a
Expand Down Expand Up @@ -406,7 +419,7 @@ export class RFC8252HTTPServer {
if (typeof port !== 'number') {
// Should never happen
throw new MongoDBOIDCError(
`Listening on ${dnsResults[0].address} did not return a port`
`Listening on ${dnsResults[0].address} (family = ${dnsResults[0].family}) did not return a port`
);
}
}
Expand All @@ -429,13 +442,16 @@ export class RFC8252HTTPServer {
await this.close();
this.logger.emit('mongodb-oidc-plugin:local-listen-failed', {
url: this.redirectUrl.toString(),
error: String(
err && typeof err === 'object' && 'message' in err ? err.message : err
),
});
throw err;
}

this.logger.emit('mongodb-oidc-plugin:local-listen-succeeded', {
url: this.listeningRedirectUrl || '',
interfaces: dnsResults.map((dnsResult) => dnsResult.address),
interfaces: dnsResults,
});
}

Expand Down
18 changes: 15 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ export interface MongoDBOIDCLogEventsMap {
method: string;
path: string;
}) => void;
'mongodb-oidc-plugin:local-listen-started': (event: { url: string }) => void;
'mongodb-oidc-plugin:local-listen-failed': (event: { url: string }) => void;
'mongodb-oidc-plugin:local-listen-started': (event: {
url: string;
urlPort: number;
}) => void;
'mongodb-oidc-plugin:local-listen-resolved-hostname': (event: {
url: string;
urlPort: number;
hostname: string;
interfaces: { family: number; address: string }[];
}) => void;
'mongodb-oidc-plugin:local-listen-failed': (event: {
url: string;
error: string;
}) => void;
'mongodb-oidc-plugin:local-listen-succeeded': (event: {
url: string;
interfaces: string[];
interfaces: { family: number; address: string }[];
}) => void;
'mongodb-oidc-plugin:local-server-close': (event: { url: string }) => void;
'mongodb-oidc-plugin:open-browser': (event: {
Expand Down
9 changes: 7 additions & 2 deletions test/log-hook-verification-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ export function verifySuccessfulAuthCodeFlowLog(entries: any[]): void {
expect(attr.url).to.match(/http:\/\/localhost.*\/redirect/);
expect(attr.interfaces).to.be.an('array');
expect(attr.interfaces).to.have.lengthOf.greaterThanOrEqual(1);
for (const item of attr.interfaces as unknown[])
expect(item).to.be.a('string');
for (const item of attr.interfaces as {
address: string;
family: number;
}[]) {
expect(item.address).to.be.a('string');
expect(item.family).to.be.a('number');
}
},
},
{
Expand Down