Skip to content

Commit

Permalink
Fix connecting to emulator via Mongo shell (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenWeatherford authored Jun 30, 2018
1 parent 4e0de7a commit e64c5b0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/mongo/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ export class Shell {

private onResult: EventEmitter<{ exitCode, result, stderr, code?: string, message?: string }> = new EventEmitter<{ exitCode, result, stderr, code?: string, message?: string }>();

public static create(execPath: string, connectionString: string): Promise<Shell> {
public static create(execPath: string, connectionString: string, isEmulator: boolean): Promise<Shell> {
return new Promise((c, e) => {
try {
const shellProcess = cp.spawn(execPath, ['--quiet', connectionString]);
let args = ['--quiet', connectionString];
if (isEmulator) {
// Without this the connection will fail due to the self-signed DocDB certificate
args.push("--ssl");
args.push("--sslAllowInvalidCertificates");
}
const shellProcess = cp.spawn(execPath, args);
return c(new Shell(execPath, shellProcess));
} catch (error) {
e(`Error while creating mongo shell with path '${execPath}': ${error}`);
Expand Down
4 changes: 2 additions & 2 deletions src/mongo/tree/MongoAccountTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class MongoAccountTreeItem implements IAzureParentTreeItem {
}
return databases
.filter((database: IDatabaseInfo) => !(database.name && database.name.toLowerCase() === "admin" && database.empty)) // Filter out the 'admin' database if it's empty
.map(database => new MongoDatabaseTreeItem(database.name, this.connectionString, node.id));
.map(database => new MongoDatabaseTreeItem(database.name, this.connectionString, this.isEmulator, node.id));

} catch (error) {
return [{
Expand Down Expand Up @@ -95,7 +95,7 @@ export class MongoAccountTreeItem implements IAzureParentTreeItem {
if (collectionName) {
showCreatingNode(databaseName);

const databaseTreeItem = new MongoDatabaseTreeItem(databaseName, this.connectionString, node.id);
const databaseTreeItem = new MongoDatabaseTreeItem(databaseName, this.connectionString, this.isEmulator, node.id);
await databaseTreeItem.createCollection(collectionName);
return databaseTreeItem;
}
Expand Down
14 changes: 8 additions & 6 deletions src/mongo/tree/MongoDatabaseTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ export class MongoDatabaseTreeItem implements IAzureParentTreeItem {
public readonly contextValue: string = MongoDatabaseTreeItem.contextValue;
public readonly childTypeLabel: string = "Collection";
public readonly connectionString: string;
public readonly isEmulator: boolean;
public readonly databaseName: string;

private _parentId: string;

constructor(databaseName: string, connectionString: string, parentId: string) {
constructor(databaseName: string, connectionString: string, isEmulator: boolean, parentId: string) {
this.databaseName = databaseName;
this.connectionString = connectionString;
this.isEmulator = isEmulator;
this._parentId = parentId;
}

Expand Down Expand Up @@ -100,14 +102,14 @@ export class MongoDatabaseTreeItem implements IAzureParentTreeItem {
return result;
}
}
return reportProgress(this.executeCommandInShell(command, context), 'Executing command');
return withProgress(this.executeCommandInShell(command, context), 'Executing command');
});
}

if (command.name === 'createCollection') {
return reportProgress(this.createCollection(stripQuotes(command.arguments.join(','))).then(() => JSON.stringify({ 'Created': 'Ok' })), 'Creating collection');
return withProgress(this.createCollection(stripQuotes(command.arguments.join(','))).then(() => JSON.stringify({ 'Created': 'Ok' })), 'Creating collection');
} else {
return reportProgress(this.executeCommandInShell(command, context), 'Executing command');
return withProgress(this.executeCommandInShell(command, context), 'Executing command');
}
}

Expand Down Expand Up @@ -152,7 +154,7 @@ export class MongoDatabaseTreeItem implements IAzureParentTreeItem {
}

private async createShell(shellPath: string): Promise<Shell> {
return <Promise<null>>Shell.create(shellPath, this.connectionString)
return <Promise<null>>Shell.create(shellPath, this.connectionString, this.isEmulator)
.then(
shell => {
return shell.useDatabase(this.databaseName).then(() => shell);
Expand All @@ -176,7 +178,7 @@ export function validateMongoCollectionName(collectionName: string): string | un
return undefined;
}

function reportProgress<T>(promise: Thenable<T>, title: string): Thenable<T> {
function withProgress<T>(promise: Thenable<T>, title: string): Thenable<T> {
return vscode.window.withProgress<T>(
{
location: vscode.ProgressLocation.Window,
Expand Down
3 changes: 2 additions & 1 deletion src/tree/AttachedAccountsTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export class AttachedAccountsTreeItem implements IAzureParentTreeItem {
}
if (port) {
if (defaultExperience.api === API.MongoDB) {
connectionString = `mongodb://localhost:C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==@localhost:${port}?ssl=true`;
// Mongo shell doesn't parse passwords with slashes, so we need to URI encode it
connectionString = `mongodb://localhost:${encodeURIComponent('C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==')}@localhost:${port}/?ssl=true`;
}
else {
connectionString = `AccountEndpoint=https://localhost:${port}/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;`;
Expand Down

0 comments on commit e64c5b0

Please sign in to comment.