-
Notifications
You must be signed in to change notification settings - Fork 8
feat(oidc-mock-provider,mongodb-runner): make OIDC mocks more broadly usable COMPASS-10034 #589
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
Open
addaleax
wants to merge
5
commits into
main
Choose a base branch
from
10034-dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+468
−113
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
033cd7b
feat(oidc-mock-provider,mongodb-runner): make OIDC mocks more broadly…
addaleax eaa51a8
chmod +x bin/oidc-mock-provider.js
addaleax e640985
fixup: requirements in READMEs
addaleax 9fb4455
fixup: log OIDC connection string as well when using --oidc
addaleax e699e95
fixup! fixup: log OIDC connection string as well when using --oidc
addaleax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { spawn } from 'child_process'; | ||
| import { once } from 'events'; | ||
| import { parseCLIArgs, OIDCMockProvider } from '@mongodb-js/oidc-mock-provider'; | ||
| import { debug } from './util'; | ||
|
|
||
| if (process.env.RUN_OIDC_MOCK_PROVIDER !== undefined) { | ||
| (async function main() { | ||
| const uuid = crypto.randomUUID(); | ||
| debug('starting OIDC mock provider with UUID', uuid); | ||
| const config = parseCLIArgs(process.env.RUN_OIDC_MOCK_PROVIDER); | ||
| const sampleTokenConfig = await config.getTokenPayload({ | ||
| client_id: 'cid', | ||
| scope: 'scope', | ||
| }); | ||
| debug('sample OIDC token config', sampleTokenConfig, uuid); | ||
| const audience = sampleTokenConfig.payload.aud; | ||
| const provider = await OIDCMockProvider.create({ | ||
| ...config, | ||
| overrideRequestHandler(url, req, res) { | ||
| if (new URL(url).pathname === `/shutdown/${uuid}`) { | ||
| res.on('close', () => { | ||
| process.exit(); | ||
| }); | ||
| res.writeHead(200); | ||
| res.end(); | ||
| } | ||
| }, | ||
| }); | ||
| debug('started OIDC mock provider with UUID', { | ||
| issuer: provider.issuer, | ||
| uuid, | ||
| audience, | ||
| }); | ||
| process.send?.({ | ||
| issuer: provider.issuer, | ||
| uuid, | ||
| audience, | ||
| }); | ||
| })().catch((error) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error starting OIDC mock identity provider server:', error); | ||
| process.exitCode = 1; | ||
| }); | ||
| } | ||
|
|
||
| export class OIDCMockProviderProcess { | ||
| pid?: number; | ||
| issuer?: string; | ||
| uuid?: string; | ||
| audience?: string; | ||
|
|
||
| serialize(): unknown /* JSON-serializable */ { | ||
| return { | ||
| pid: this.pid, | ||
| issuer: this.issuer, | ||
| uuid: this.uuid, | ||
| audience: this.audience, | ||
| }; | ||
| } | ||
|
|
||
| static deserialize(serialized: any): OIDCMockProviderProcess { | ||
| const process = new OIDCMockProviderProcess(); | ||
| process.pid = serialized.pid; | ||
| process.issuer = serialized.issuer; | ||
| process.uuid = serialized.uuid; | ||
| process.audience = serialized.audience; | ||
| return process; | ||
| } | ||
|
|
||
| private constructor() { | ||
| /* see .start() */ | ||
| } | ||
|
|
||
| static async start(args: string): Promise<OIDCMockProviderProcess> { | ||
| const oidcProc = new this(); | ||
| debug('spawning OIDC child process', [process.execPath, __filename], args); | ||
| const proc = spawn(process.execPath, [__filename], { | ||
| stdio: ['inherit', 'inherit', 'inherit', 'ipc'], | ||
| env: { | ||
| ...process.env, | ||
| RUN_OIDC_MOCK_PROVIDER: args, | ||
| }, | ||
| detached: true, | ||
| serialization: 'advanced', | ||
| }); | ||
| await once(proc, 'spawn'); | ||
| try { | ||
| oidcProc.pid = proc.pid; | ||
| const [msg] = await Promise.race([ | ||
| once(proc, 'message'), | ||
| once(proc, 'exit').then(() => { | ||
| throw new Error( | ||
| `OIDC mock provider process exited before sending message (${String(proc.exitCode)}, ${String(proc.signalCode)})`, | ||
| ); | ||
| }), | ||
| ]); | ||
| debug('received message from OIDC child process', msg); | ||
| oidcProc.issuer = msg.issuer; | ||
| oidcProc.uuid = msg.uuid; | ||
| oidcProc.audience = msg.audience; | ||
| } catch (err) { | ||
| proc.kill(); | ||
| throw err; | ||
| } | ||
| proc.unref(); | ||
| proc.channel?.unref(); | ||
| debug('OIDC setup complete, uuid =', oidcProc.uuid); | ||
| return oidcProc; | ||
| } | ||
|
|
||
| async close(): Promise<void> { | ||
| try { | ||
| if (this.pid) process.kill(this.pid, 0); | ||
| } catch (e) { | ||
| if (typeof e === 'object' && e && 'code' in e && e.code === 'ESRCH') | ||
| return; // process already exited | ||
| } | ||
|
|
||
| if (!this.issuer || !this.uuid) return; | ||
| await fetch(new URL(this.issuer, `/shutdown/${this.uuid}`)); | ||
| this.uuid = undefined; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't this override the
log-requestsfrom cli?