From a07c37a927c393570bb0034adf5bb98cfaf5f9c4 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 19 Apr 2024 12:36:29 +0200 Subject: [PATCH] fix(cli-repl): throw error when changing telemetry setting while `forceDisableTelemetry: true` MONGOSH-1762 --- packages/cli-repl/src/cli-repl.spec.ts | 37 ++++++++++++++++++++++++++ packages/cli-repl/src/cli-repl.ts | 5 ++++ 2 files changed, 42 insertions(+) diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index 3116c07db..06515d1f7 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -1594,6 +1594,43 @@ describe('CliRepl', function () { expect(requests).to.have.lengthOf(0); }); + it('does not let the user modify telemetry settings with global force-disable-telemetry config', async function () { + const globalConfigFile = path.join(tmpdir.path, 'globalconfig.conf'); + await fs.writeFile( + globalConfigFile, + 'mongosh:\n forceDisableTelemetry: true' + ); + + cliReplOptions.globalConfigPaths = [globalConfigFile]; + cliRepl = new CliRepl(cliReplOptions); + await cliRepl.start(await testServer.connectionString(), {}); + + output = ''; + input.write('enableTelemetry()\n'); + await waitEval(cliRepl.bus); + expect(output).to.include( + "Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true" + ); + + output = ''; + input.write('disableTelemetry()\n'); + await waitEval(cliRepl.bus); + expect(output).to.include( + "Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true" + ); + + output = ''; + input.write('config.set("enableTelemetry", true)\n'); + await waitEval(cliRepl.bus); + expect(output).to.include( + "Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true" + ); + + input.write('exit\n'); + await waitBus(cliRepl.bus, 'mongosh:closed'); + expect(requests).to.have.lengthOf(0); + }); + it('does not send out telemetry if the user only runs a script for disabling telemetry', async function () { cliReplOptions.shellCliOptions.eval = ['disableTelemetry()']; cliRepl = new CliRepl(cliReplOptions); diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index cc30106fb..8b88ceac9 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -844,6 +844,11 @@ export class CliRepl implements MongoshIOProvider { } this.config[key] = value; if (key === 'enableTelemetry') { + if (this.forceDisableTelemetry) { + throw new MongoshRuntimeError( + "Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true" + ); + } this.setTelemetryEnabled(this.config.enableTelemetry); this.bus.emit('mongosh:update-user', { userId: this.config.userId,