Skip to content

Commit

Permalink
feat: add OIDC auth support, enable in new form VSCODE-354 (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anemy authored Jan 4, 2024
1 parent 4e2cbeb commit cd95065
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actions/test-and-build/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ runs:

- name: Build .vsix
env:
NODE_OPTIONS: "--require ./scripts/no-npm-list-fail.js"
NODE_OPTIONS: "--require ./scripts/no-npm-list-fail.js --max_old_space_size=4096"
# NOTE: --githubBranch is "The GitHub branch used to infer relative links in README.md."
run: |
npx vsce package --githubBranch main
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ If you use Terraform to manage your infrastructure, MongoDB for VS Code helps yo
| `mdb.defaultLimit` | The number of documents to fetch when viewing documents from a collection. | `10` |
| `mdb.confirmRunAll` | Show a confirmation message before running commands in a playground. | `true` |
| `mdb.confirmDeleteDocument` | Show a confirmation message before deleting a document in the tree view. | `true` |
| `mdb.persistOIDCTokens` | Remain logged in when using the MONGODB-OIDC authentication mechanism for MongoDB server connection. Access tokens are encrypted using the system keychain before being stored. | `true` |
| `mdb.excludeFromPlaygroundsSearch` | Exclude files and folders while searching for playground files in the current workspace. | Refer to [`package.json`](https://github.com/mongodb-js/vscode/blob/7b10092db4c8c10c4aa9c45b443c8ed3d5f37d5c/package.json) |
| `mdb.connectionSaving.` `hideOptionToChooseWhereToSaveNewConnections` | When a connection is added, a prompt is shown that let's the user decide where the new connection should be saved. When this setting is checked, the prompt is not shown and the default connection saving location setting is used. | `true` |
| `mdb.connectionSaving.` `defaultConnectionSavingLocation` | When the setting that hides the option to choose where to save new connections is checked, this setting sets if and where new connections are saved. | `Global` |
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,11 @@
"default": true,
"description": "Show a confirmation message before deleting a document from the tree view."
},
"mdb.persistOIDCTokens": {
"type": "boolean",
"default": true,
"description": "Remain logged in when using the MONGODB-OIDC authentication mechanism for MongoDB server connection. Access tokens are encrypted using the system keychain before being stored."
},
"mdb.sendTelemetry": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -991,6 +996,7 @@
"classnames": "^2.3.2",
"debug": "^4.3.4",
"dotenv": "^16.3.1",
"lodash": "^4.17.21",
"micromatch": "^4.0.5",
"mongodb": "^6.0.0",
"mongodb-build-info": "^1.6.2",
Expand Down
2 changes: 1 addition & 1 deletion scripts/check-vsix-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const vsixFileName = path.resolve(
);
const size = fs.statSync(vsixFileName).size;

const maxSize = 8 * 1000000; // 8 MB
const maxSize = 8_500_000; // 8.5 MB

if (size >= maxSize) {
throw new Error(
Expand Down
85 changes: 40 additions & 45 deletions src/commands/launchMongoShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,47 @@ import * as vscode from 'vscode';

import type ConnectionController from '../connectionController';

const launchMongoDBShellWithEnv = (
shellCommand: string,
mdbConnectionString: string,
envVariableString: string
) => {
const launchMongoDBShellWithEnv = ({
shellCommand,
mdbConnectionString,
envVariableString,
parentHandle,
}: {
shellCommand: string;
mdbConnectionString: string;
envVariableString: string;
parentHandle?: string;
}) => {
const mongoDBShell = vscode.window.createTerminal({
name: 'MongoDB Shell',
env: {
MDB_CONNECTION_STRING: mdbConnectionString,
...(parentHandle
? {
MONGOSH_OIDC_PARENT_HANDLE: parentHandle, // For OIDC to share the state and avoid extra logins.
}
: {}),
},
});

mongoDBShell.sendText(`${shellCommand} ${envVariableString};`);
mongoDBShell.show();
};

const launchMongoDBShellOnPowershell = (
shellCommand: string,
mdbConnectionString: string
): void => {
launchMongoDBShellWithEnv(
shellCommand,
mdbConnectionString,
'$Env:MDB_CONNECTION_STRING'
);
const getPowershellEnvString = () => {
return '$Env:MDB_CONNECTION_STRING';
};

const launchMongoDBShellOnCmd = (
shellCommand: string,
mdbConnectionString: string
): void => {
launchMongoDBShellWithEnv(
shellCommand,
mdbConnectionString,
'%MDB_CONNECTION_STRING%'
);
const getCmdEnvString = () => {
return '%MDB_CONNECTION_STRING%';
};

const launchMongoDBShellOnGitBash = (
shellCommand: string,
mdbConnectionString: string
): void => {
launchMongoDBShellWithEnv(
shellCommand,
mdbConnectionString,
'$MDB_CONNECTION_STRING'
);
const getGitBashEnvString = () => {
return '$MDB_CONNECTION_STRING';
};

const launchMongoDBShellOnBash = (
shellCommand: string,
mdbConnectionString: string
): void => {
launchMongoDBShellWithEnv(
shellCommand,
mdbConnectionString,
'$MDB_CONNECTION_STRING'
);
const getBashEnvString = () => {
return '$MDB_CONNECTION_STRING';
};

const openMongoDBShell = (
Expand Down Expand Up @@ -94,19 +77,31 @@ const openMongoDBShell = (
}

const mdbConnectionString = connectionController.getActiveConnectionString();
const parentHandle =
connectionController.getMongoClientConnectionOptions()?.options
.parentHandle;

let envVariableString = '';

if (userShell.includes('powershell.exe')) {
launchMongoDBShellOnPowershell(shellCommand, mdbConnectionString);
envVariableString = getPowershellEnvString();
} else if (userShell.includes('cmd.exe')) {
launchMongoDBShellOnCmd(shellCommand, mdbConnectionString);
envVariableString = getCmdEnvString();
} else if (userShell.toLocaleLowerCase().includes('git\\bin\\bash.exe')) {
launchMongoDBShellOnGitBash(shellCommand, mdbConnectionString);
envVariableString = getGitBashEnvString();
} else {
// Assume it's a bash environment. This may fail on certain
// shells but should cover most cases.
launchMongoDBShellOnBash(shellCommand, mdbConnectionString);
envVariableString = getBashEnvString();
}

launchMongoDBShellWithEnv({
shellCommand,
mdbConnectionString,
parentHandle,
envVariableString,
});

return Promise.resolve(true);
};

Expand Down
Loading

0 comments on commit cd95065

Please sign in to comment.