Skip to content

Commit fcc9bf4

Browse files
authored
feat(cli-repl): add experimental HTTP OIDC proxying capability MONGOSH-1779 (#1995)
This is just an experiment, *not* a production-ready change with tests or documentation, with the goal being to verify that an implementation of this sort can satisfy specific customer needs.
1 parent 5ec9eb3 commit fcc9bf4

File tree

8 files changed

+272
-159
lines changed

8 files changed

+272
-159
lines changed

package-lock.json

Lines changed: 242 additions & 152 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/arg-parser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"mongodb-connection-string-url": "^3.0.1"
4141
},
4242
"devDependencies": {
43-
"@mongodb-js/devtools-connect": "^2.6.2",
43+
"@mongodb-js/devtools-connect": "^2.6.3",
4444
"@mongodb-js/eslint-config-mongosh": "^1.0.0",
4545
"@mongodb-js/prettier-config-devtools": "^1.0.1",
4646
"@mongodb-js/tsconfig-mongosh": "^1.0.0",

packages/cli-repl/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"mongodb-log-writer": "^1.4.2",
8888
"numeral": "^2.0.6",
8989
"pretty-repl": "^4.0.1",
90+
"proxy-agent": "^6.4.0",
9091
"semver": "^7.5.4",
9192
"strip-ansi": "^6.0.0",
9293
"text-table": "^0.2.0",

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,7 +2447,10 @@ describe('CliRepl', function () {
24472447
cliRepl = new CliRepl(cliReplOptions);
24482448
await cliRepl.start('', {});
24492449

2450-
const o = await cliRepl.prepareOIDCOptions({} as any);
2450+
const o = await cliRepl.prepareOIDCOptions(
2451+
'mongodb://localhost/',
2452+
{} as any
2453+
);
24512454
expect(o.oidc?.allowedFlows).to.deep.equal(['auth-code']);
24522455
expect(o.oidc?.notifyDeviceFlow).to.be.a('function');
24532456
expect(o.authMechanismProperties).to.deep.equal({});
@@ -2470,7 +2473,7 @@ describe('CliRepl', function () {
24702473
let o: DevtoolsConnectOptions;
24712474
process.env.MONGOSH_OIDC_PARENT_HANDLE = 'foo-bar';
24722475
try {
2473-
o = await cliRepl.prepareOIDCOptions({} as any);
2476+
o = await cliRepl.prepareOIDCOptions('mongodb://localhost/', {} as any);
24742477
} finally {
24752478
delete process.env.MONGOSH_OIDC_PARENT_HANDLE;
24762479
}

packages/cli-repl/src/cli-repl.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ export class CliRepl implements MongoshIOProvider {
360360
delete driverOptions.autoEncryption;
361361
}
362362

363-
driverOptions = await this.prepareOIDCOptions(driverOptions);
363+
driverOptions = await this.prepareOIDCOptions(driverUri, driverOptions);
364364
markTime(TimingCategories.DriverSetup, 'prepared OIDC options');
365365

366366
let initialServiceProvider;
@@ -1143,6 +1143,7 @@ export class CliRepl implements MongoshIOProvider {
11431143

11441144
/** Adjust `driverOptionsIn` with OIDC-specific settings from this CLI instance. */
11451145
async prepareOIDCOptions(
1146+
driverUri: string,
11461147
driverOptionsIn: Readonly<DevtoolsConnectOptions>
11471148
): Promise<DevtoolsConnectOptions> {
11481149
const driverOptions = {
@@ -1165,6 +1166,24 @@ export class CliRepl implements MongoshIOProvider {
11651166
)}\nWaiting...\n`
11661167
);
11671168
};
1169+
if (process.env.MONGOSH_EXPERIMENTAL_OIDC_PROXY_SUPPORT) {
1170+
const ProxyAgent = (await import('proxy-agent')).ProxyAgent;
1171+
const tlsCAFile =
1172+
driverOptions.tlsCAFile ??
1173+
new ConnectionString(driverUri)
1174+
.typedSearchParams<DevtoolsConnectOptions>()
1175+
.get('tlsCAFile');
1176+
const ca = tlsCAFile ? await fs.readFile(tlsCAFile) : undefined;
1177+
driverOptions.oidc.customHttpOptions = (_url, opts) => {
1178+
if (ca && !opts.ca) {
1179+
opts = { ...opts, ca };
1180+
}
1181+
return {
1182+
...opts,
1183+
agent: new ProxyAgent({ ...opts }),
1184+
};
1185+
};
1186+
}
11681187

11691188
const [redirectURI, trustedEndpoints, browser] = await Promise.all([
11701189
this.getConfig('oidcRedirectURI'),

packages/logging/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"node": ">=14.15.1"
1818
},
1919
"dependencies": {
20-
"@mongodb-js/devtools-connect": "^2.6.2",
20+
"@mongodb-js/devtools-connect": "^2.6.3",
2121
"@mongosh/errors": "0.0.0-dev.0",
2222
"@mongosh/history": "0.0.0-dev.0",
2323
"@mongosh/types": "0.0.0-dev.0",

packages/service-provider-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
}
4848
},
4949
"dependencies": {
50-
"@mongodb-js/devtools-connect": "^2.6.2",
50+
"@mongodb-js/devtools-connect": "^2.6.3",
5151
"@mongodb-js/oidc-plugin": "^0.4.0",
5252
"@mongosh/errors": "0.0.0-dev.0",
5353
"@mongosh/service-provider-core": "0.0.0-dev.0",

packages/types/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"unitTestsOnly": true
3939
},
4040
"dependencies": {
41-
"@mongodb-js/devtools-connect": "^2.6.2"
41+
"@mongodb-js/devtools-connect": "^2.6.3"
4242
},
4343
"devDependencies": {
4444
"@mongodb-js/eslint-config-mongosh": "^1.0.0",

0 commit comments

Comments
 (0)