From 75337af99e4a35771e84e359950dfd08847f04c8 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 4 Mar 2021 11:59:59 +0100 Subject: [PATCH] fix(shell-api): forbid exit/quit in embedded environments MONGOSH-616 --- packages/shell-api/src/helpers.ts | 6 ++---- packages/shell-api/src/shell-api.spec.ts | 4 ++-- packages/shell-api/src/shell-api.ts | 18 +++++++++--------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/shell-api/src/helpers.ts b/packages/shell-api/src/helpers.ts index 4bfc4a5a79..38ab86ffac 100644 --- a/packages/shell-api/src/helpers.ts +++ b/packages/shell-api/src/helpers.ts @@ -600,14 +600,12 @@ export async function setHideIndex(coll: Collection, index: string | Document, h ); } -export function assertCLI(platform: ReplPlatform): void { +export function assertCLI(platform: ReplPlatform, features: string): void { if ( platform !== ReplPlatform.CLI ) { throw new MongoshUnimplementedError( - `new Mongo connection are not supported for current platform: ${ - ReplPlatform[platform] - }`, + `${features} are not supported for current platform: ${ReplPlatform[platform]}`, CommonErrors.NotImplemented ); } diff --git a/packages/shell-api/src/shell-api.spec.ts b/packages/shell-api/src/shell-api.spec.ts index 3757c0ecc8..6964cbe3d2 100644 --- a/packages/shell-api/src/shell-api.spec.ts +++ b/packages/shell-api/src/shell-api.spec.ts @@ -55,7 +55,7 @@ describe('ShellApi', () => { returnsPromise: true, deprecated: false, returnType: { type: 'unknown', attributes: {} }, - platforms: ALL_PLATFORMS, + platforms: [ ReplPlatform.CLI ], topologies: ALL_TOPOLOGIES, serverVersions: ALL_SERVER_VERSIONS }); @@ -516,7 +516,7 @@ describe('ShellApi', () => { expect.fail('missed exception'); } catch (e) { // We should be getting an exception because we’re not actually exiting. - expect(e.message).to.contain('exit not supported for current platform'); + expect(e.message).to.contain('onExit listener returned'); } expect(evaluationListener.onExit).to.have.been.calledWith(); }); diff --git a/packages/shell-api/src/shell-api.ts b/packages/shell-api/src/shell-api.ts index 05b011a2a8..b766ba681a 100644 --- a/packages/shell-api/src/shell-api.ts +++ b/packages/shell-api/src/shell-api.ts @@ -16,7 +16,7 @@ import { CommandResult, CursorIterationResult } from './result'; import ShellInternalState from './shell-internal-state'; import { assertArgsDefined, assertCLI } from './helpers'; import { DEFAULT_DB, ReplPlatform } from '@mongosh/service-provider-core'; -import { CommonErrors, MongoshUnimplementedError } from '@mongosh/errors'; +import { CommonErrors, MongoshUnimplementedError, MongoshInternalError } from '@mongosh/errors'; import { DBQuery } from './deprecated'; import { promisify } from 'util'; import { ClientSideFieldLevelEncryptionOptions } from './field-level-encryption'; @@ -46,19 +46,19 @@ export default class ShellApi extends ShellApiClass { @directShellCommand @returnsPromise - async exit(): Promise { + @platforms([ ReplPlatform.CLI ] ) + async exit(): Promise { + assertCLI(this.internalState.initialServiceProvider.platform, 'the exit/quit commands'); await this.internalState.close(true); // This should never actually return. await this.internalState.evaluationListener.onExit?.(); - throw new MongoshUnimplementedError( - `exit not supported for current platform: ${ReplPlatform[this.internalState.initialServiceProvider.platform]}`, - CommonErrors.NotImplemented - ); + throw new MongoshInternalError('.onExit listener returned'); } @directShellCommand @returnsPromise - async quit(): Promise { + @platforms([ ReplPlatform.CLI ] ) + async quit(): Promise { return await this.exit(); } @@ -66,7 +66,7 @@ export default class ShellApi extends ShellApiClass { @returnType('Mongo') @platforms([ ReplPlatform.CLI ] ) public async Mongo(uri?: string, options?: ClientSideFieldLevelEncryptionOptions): Promise { - assertCLI(this.internalState.initialServiceProvider.platform); + assertCLI(this.internalState.initialServiceProvider.platform, 'new Mongo connections'); const mongo = new Mongo(this.internalState, uri, options); await mongo.connect(); this.internalState.mongos.push(mongo); @@ -78,7 +78,7 @@ export default class ShellApi extends ShellApiClass { @platforms([ ReplPlatform.CLI ] ) async connect(uri: string, user?: string, pwd?: string): Promise { assertArgsDefined(uri); - assertCLI(this.internalState.initialServiceProvider.platform); + assertCLI(this.internalState.initialServiceProvider.platform, 'new Mongo connections'); const mongo = new Mongo(this.internalState, uri); await mongo.connect(user, pwd); this.internalState.mongos.push(mongo);