Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
Anemy committed Jan 12, 2024
2 parents 65335f2 + 00a875e commit 0c0837c
Show file tree
Hide file tree
Showing 12 changed files with 680 additions and 43 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/actions/test-and-build/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ runs:
if: ${{ runner.os != 'Windows' }}
shell: bash

- name: Set BROWSER_AUTH_COMMAND
run: |
BROWSER_AUTH_COMMAND=$(echo "$(which node) $(pwd)/src/test/fixture/curl.js")
echo "BROWSER_AUTH_COMMAND=$BROWSER_AUTH_COMMAND" >> $GITHUB_ENV
shell: bash

- name: Run Tests
env:
BROWSER_AUTH_COMMAND: ${{ env.BROWSER_AUTH_COMMAND }}
run: |
npm run test
shell: bash
Expand Down
110 changes: 104 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -658,15 +658,18 @@
},
{
"command": "mdb.startStreamProcessor",
"when": "view == mongoDBConnectionExplorer && viewItem == streamProcessorTreeItem"
"when": "view == mongoDBConnectionExplorer && viewItem == streamProcessorTreeItem",
"group": "6@1"
},
{
"command": "mdb.stopStreamProcessor",
"when": "view == mongoDBConnectionExplorer && viewItem == streamProcessorTreeItem"
"when": "view == mongoDBConnectionExplorer && viewItem == streamProcessorTreeItem",
"group": "6@2"
},
{
"command": "mdb.dropStreamProcessor",
"when": "view == mongoDBConnectionExplorer && viewItem == streamProcessorTreeItem"
"when": "view == mongoDBConnectionExplorer && viewItem == streamProcessorTreeItem",
"group": "6@3"
}
],
"editor/title": [
Expand Down Expand Up @@ -1042,6 +1045,11 @@
"type": "boolean",
"default": false,
"description": "The default behavior is to generate a single ObjectId and insert it on all cursors. Set to true to generate a unique ObjectId per cursor instead."
},
"mdb.browserCommandForOIDCAuth": {
"type": "string",
"default": "",
"description": "Specify a shell command that is run to start the browser for authenticating with the OIDC identity provider for the server connection. Leave this empty for default browser."
}
}
}
Expand Down Expand Up @@ -1084,6 +1092,7 @@
},
"devDependencies": {
"@babel/preset-typescript": "^7.22.5",
"@mongodb-js/oidc-mock-provider": "^0.6.9",
"@mongodb-js/oidc-plugin": "^0.3.0",
"@mongodb-js/prettier-config-compass": "^1.0.0",
"@mongodb-js/sbom-tools": "^0.5.4",
Expand Down Expand Up @@ -1135,6 +1144,7 @@
"mocha-multi": "^1.1.7",
"mongodb-client-encryption": "^6.0.0",
"mongodb-runner": "^5.4.5",
"node-fetch": "^2.7.0",
"node-loader": "^0.6.0",
"npm-run-all": "^4.1.5",
"ora": "^5.4.1",
Expand Down
90 changes: 57 additions & 33 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export default class ConnectionController {
private _currentConnectionId: null | string = null;

_connectionAttempt: null | ConnectionAttempt = null;
_connectionStringInputCancellationToken: null | vscode.CancellationTokenSource =
null;
private _connectingConnectionId: null | string = null;
private _disconnecting = false;

Expand Down Expand Up @@ -144,33 +146,44 @@ export default class ConnectionController {

log.info('connectWithURI command called');

const cancellationToken = new vscode.CancellationTokenSource();
this._connectionStringInputCancellationToken = cancellationToken;

try {
connectionString = await vscode.window.showInputBox({
value: '',
ignoreFocusOut: true,
placeHolder:
'e.g. mongodb+srv://username:[email protected]/admin',
prompt: 'Enter your connection string (SRV or standard)',
validateInput: (uri: string) => {
if (
!uri.startsWith('mongodb://') &&
!uri.startsWith('mongodb+srv://')
) {
return 'MongoDB connection strings begin with "mongodb://" or "mongodb+srv://"';
}
connectionString = await vscode.window.showInputBox(
{
value: '',
ignoreFocusOut: true,
placeHolder:
'e.g. mongodb+srv://username:[email protected]/admin',
prompt: 'Enter your connection string (SRV or standard)',
validateInput: (uri: string) => {
if (
!uri.startsWith('mongodb://') &&
!uri.startsWith('mongodb+srv://')
) {
return 'MongoDB connection strings begin with "mongodb://" or "mongodb+srv://"';
}

try {
// eslint-disable-next-line no-new
new ConnectionString(uri);
} catch (error) {
return formatError(error).message;
}
try {
// eslint-disable-next-line no-new
new ConnectionString(uri);
} catch (error) {
return formatError(error).message;
}

return null;
return null;
},
},
});
cancellationToken.token
);
} catch (e) {
return false;
} finally {
if (this._connectionStringInputCancellationToken === cancellationToken) {
this._connectionStringInputCancellationToken.dispose();
this._connectionStringInputCancellationToken = null;
}
}

if (!connectionString) {
Expand Down Expand Up @@ -255,6 +268,7 @@ export default class ConnectionController {
return this._connect(connection.id, connectionType);
}

// eslint-disable-next-line complexity
async _connect(
connectionId: string,
connectionType: ConnectionTypes
Expand Down Expand Up @@ -321,22 +335,27 @@ export default class ConnectionController {
browserCommandForOIDCAuth: undefined, // We overwrite this below.
},
});
const browserAuthCommand = vscode.workspace
.getConfiguration('mdb')
.get('browserCommandForOIDCAuth');
dataService = await connectionAttempt.connect({
...connectionOptions,
oidc: {
...cloneDeep(connectionOptions.oidc),
openBrowser: async ({ signal, url }) => {
try {
await openLink(url);
} catch (err) {
if (signal.aborted) return;
// If opening the link fails we default to regular link opening.
await vscode.commands.executeCommand(
'vscode.open',
vscode.Uri.parse(url)
);
}
},
openBrowser: browserAuthCommand
? { command: browserAuthCommand }
: async ({ signal, url }) => {
try {
await openLink(url);
} catch (err) {
if (signal.aborted) return;
// If opening the link fails we default to regular link opening.
await vscode.commands.executeCommand(
'vscode.open',
vscode.Uri.parse(url)
);
}
},
},
});

Expand Down Expand Up @@ -410,6 +429,7 @@ export default class ConnectionController {
);

if (removeConfirmationResponse !== 'Confirm') {
await this.disconnect();
throw new Error('Reauthentication declined by user');
}
}
Expand Down Expand Up @@ -733,6 +753,10 @@ export default class ConnectionController {
this.eventEmitter.removeListener(eventType, listener);
}

closeConnectionStringInput() {
this._connectionStringInputCancellationToken?.cancel();
}

isConnecting(): boolean {
return !!this._connectionAttempt;
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/fixture/curl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node
/* eslint-disable */
'use strict';
const fetch = require('node-fetch');

// fetch() an URL and ignore the response body
(async function () {
(await fetch(process.argv[2])).body?.resume();
})().catch((err) => {
process.nextTick(() => {
throw err;
});
});
Loading

0 comments on commit 0c0837c

Please sign in to comment.