|
| 1 | +import { spawn } from 'child_process'; |
| 2 | +import { once } from 'events'; |
| 3 | +import { parseCLIArgs, OIDCMockProvider } from '@mongodb-js/oidc-mock-provider'; |
| 4 | +import { debug } from './util'; |
| 5 | + |
| 6 | +if (process.env.RUN_OIDC_MOCK_PROVIDER !== undefined) { |
| 7 | + (async function main() { |
| 8 | + const uuid = crypto.randomUUID(); |
| 9 | + debug('starting OIDC mock provider with UUID', uuid); |
| 10 | + const config = parseCLIArgs(process.env.RUN_OIDC_MOCK_PROVIDER); |
| 11 | + const sampleTokenConfig = await config.getTokenPayload({ |
| 12 | + client_id: 'cid', |
| 13 | + scope: 'scope', |
| 14 | + }); |
| 15 | + debug('sample OIDC token config', sampleTokenConfig, uuid); |
| 16 | + const audience = sampleTokenConfig.payload.aud; |
| 17 | + const provider = await OIDCMockProvider.create({ |
| 18 | + ...config, |
| 19 | + overrideRequestHandler(url, req, res) { |
| 20 | + if (new URL(url).pathname === `/shutdown/${uuid}`) { |
| 21 | + res.on('close', () => { |
| 22 | + process.exit(); |
| 23 | + }); |
| 24 | + res.writeHead(200); |
| 25 | + res.end(); |
| 26 | + } |
| 27 | + }, |
| 28 | + }); |
| 29 | + debug('started OIDC mock provider with UUID', { |
| 30 | + issuer: provider.issuer, |
| 31 | + uuid, |
| 32 | + audience, |
| 33 | + }); |
| 34 | + process.send?.({ |
| 35 | + issuer: provider.issuer, |
| 36 | + uuid, |
| 37 | + audience, |
| 38 | + }); |
| 39 | + })().catch((error) => { |
| 40 | + // eslint-disable-next-line no-console |
| 41 | + console.error('Error starting OIDC mock identity provider server:', error); |
| 42 | + process.exitCode = 1; |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +export class OIDCMockProviderProcess { |
| 47 | + pid?: number; |
| 48 | + issuer?: string; |
| 49 | + uuid?: string; |
| 50 | + audience?: string; |
| 51 | + |
| 52 | + serialize(): unknown /* JSON-serializable */ { |
| 53 | + return { |
| 54 | + pid: this.pid, |
| 55 | + issuer: this.issuer, |
| 56 | + uuid: this.uuid, |
| 57 | + audience: this.audience, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + static deserialize(serialized: any): OIDCMockProviderProcess { |
| 62 | + const process = new OIDCMockProviderProcess(); |
| 63 | + process.pid = serialized.pid; |
| 64 | + process.issuer = serialized.issuer; |
| 65 | + process.uuid = serialized.uuid; |
| 66 | + process.audience = serialized.audience; |
| 67 | + return process; |
| 68 | + } |
| 69 | + |
| 70 | + private constructor() { |
| 71 | + /* see .start() */ |
| 72 | + } |
| 73 | + |
| 74 | + static async start(args: string): Promise<OIDCMockProviderProcess> { |
| 75 | + const oidcProc = new this(); |
| 76 | + debug('spawning OIDC child process', [process.execPath, __filename], args); |
| 77 | + const proc = spawn(process.execPath, [__filename], { |
| 78 | + stdio: ['inherit', 'inherit', 'inherit', 'ipc'], |
| 79 | + env: { |
| 80 | + ...process.env, |
| 81 | + RUN_OIDC_MOCK_PROVIDER: args, |
| 82 | + }, |
| 83 | + detached: true, |
| 84 | + serialization: 'advanced', |
| 85 | + }); |
| 86 | + await once(proc, 'spawn'); |
| 87 | + try { |
| 88 | + oidcProc.pid = proc.pid; |
| 89 | + const [msg] = await Promise.race([ |
| 90 | + once(proc, 'message'), |
| 91 | + once(proc, 'exit').then(() => { |
| 92 | + throw new Error( |
| 93 | + `OIDC mock provider process exited before sending message (${String(proc.exitCode)}, ${String(proc.signalCode)})`, |
| 94 | + ); |
| 95 | + }), |
| 96 | + ]); |
| 97 | + debug('received message from OIDC child process', msg); |
| 98 | + oidcProc.issuer = msg.issuer; |
| 99 | + oidcProc.uuid = msg.uuid; |
| 100 | + oidcProc.audience = msg.audience; |
| 101 | + } catch (err) { |
| 102 | + proc.kill(); |
| 103 | + throw err; |
| 104 | + } |
| 105 | + proc.unref(); |
| 106 | + proc.channel?.unref(); |
| 107 | + debug('OIDC setup complete, uuid =', oidcProc.uuid); |
| 108 | + return oidcProc; |
| 109 | + } |
| 110 | + |
| 111 | + async close(): Promise<void> { |
| 112 | + try { |
| 113 | + if (this.pid) process.kill(this.pid, 0); |
| 114 | + } catch (e) { |
| 115 | + if (typeof e === 'object' && e && 'code' in e && e.code === 'ESRCH') |
| 116 | + return; // process already exited |
| 117 | + } |
| 118 | + |
| 119 | + if (!this.issuer || !this.uuid) return; |
| 120 | + await fetch(new URL(this.issuer, `/shutdown/${this.uuid}`)); |
| 121 | + this.uuid = undefined; |
| 122 | + } |
| 123 | +} |
0 commit comments