Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli-repl): throw error when changing telemetry setting while forceDisableTelemetry: true MONGOSH-1762 #1955

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading