Skip to content

Commit

Permalink
fix(cli-repl): Detect containerized environment and disable telemetry M…
Browse files Browse the repository at this point in the history
…ONGOSH-1278 (#1326)

* fix(cli-repl): Detect containerized environment and disable telemetry MONGOSH-1278

* test(cli-repl): Add tests for telemetry disabling

* fix(cli-repl): Explicit return types and stricter regexp

Co-authored-by: Anna Henningsen <[email protected]>

Co-authored-by: Anna Henningsen <[email protected]>
  • Loading branch information
gribnoysup and addaleax authored Jul 27, 2022
1 parent 567160b commit 22b07d8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 0 additions & 1 deletion package-lock.json

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

25 changes: 25 additions & 0 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,18 @@ describe('CliRepl', () => {
expect(requests).to.have.lengthOf(2);
});

it('sends out telemetry if the repl is running in an interactive mode in a containerized environment', async() => {
cliRepl = new CliRepl(cliReplOptions);
cliRepl.getIsContainerizedEnvironment = () => {
return Promise.resolve(true);
};
await cliRepl.start(await testServer.connectionString(), {});
input.write('db.hello()\n');
input.write('exit\n');
await waitBus(cliRepl.bus, 'mongosh:closed');
expect(requests).to.have.lengthOf(2);
});

it('does not send out telemetry if the user starts with a no-telemetry config', async() => {
await fs.writeFile(path.join(tmpdir.path, 'config'), EJSON.stringify({ enableTelemetry: false }));
await cliRepl.start(await testServer.connectionString(), {});
Expand Down Expand Up @@ -986,6 +998,19 @@ describe('CliRepl', () => {
expect(requests).to.have.lengthOf(0);
});

it('does not send out telemetry if the repl is running in non-interactive mode in a containerized environment', async() => {
cliReplOptions.shellCliOptions.eval = ['db.hello()'];
cliRepl = new CliRepl(cliReplOptions);
cliRepl.getIsContainerizedEnvironment = () => {
return Promise.resolve(true);
};
await startWithExpectedImmediateExit(
cliRepl,
await testServer.connectionString()
);
expect(requests).to.have.lengthOf(0);
});

context('with a 5.0+ server', () => {
skipIfServerVersion(testServer, '<= 4.4');

Expand Down
32 changes: 30 additions & 2 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class CliRepl implements MongoshIOProvider {
warnedAboutInaccessibleFiles = false;
onExit: (code?: number) => Promise<never>;
closing = false;
isContainerizedEnvironment = false;

/**
* Instantiate the new CLI Repl.
Expand Down Expand Up @@ -155,6 +156,29 @@ class CliRepl implements MongoshIOProvider {
});
}

async getIsContainerizedEnvironment() {
// Check for dockerenv file first
try {
await fs.stat('/.dockerenv');
return true;
} catch {
try {
// Check if there is any mention of docker / lxc / k8s in control groups
const cgroup = await fs.readFile('/proc/self/cgroup', 'utf8');
return /\b(docker|lxc|kubepods)\b/.test(cgroup);
} catch {
return false;
}
}
}

get forceDisableTelemetry(): boolean {
return (
this.globalConfig?.forceDisableTelemetry ||
(this.isContainerizedEnvironment && !this.mongoshRepl.isInteractive)
);
}

/**
* Setup CLI environment: serviceProvider, ShellEvaluator, log connection
* information, external editor, and finally start the repl.
Expand All @@ -167,6 +191,9 @@ class CliRepl implements MongoshIOProvider {
const { version } = require('../package.json');
await this.verifyNodeVersion();

this.isContainerizedEnvironment =
await this.getIsContainerizedEnvironment();

if (!this.cliOptions.nodb) {
const cs = new ConnectionString(driverUri);
const searchParams = cs.typedSearchParams<DevtoolsConnectOptions>();
Expand Down Expand Up @@ -215,7 +242,8 @@ class CliRepl implements MongoshIOProvider {
this.toggleableAnalytics,
{
platform: process.platform,
arch: process.arch
arch: process.arch,
is_containerized: this.isContainerizedEnvironment,
},
require('../package.json').version);

Expand Down Expand Up @@ -329,7 +357,7 @@ class CliRepl implements MongoshIOProvider {
// case.
return;
}
if (enabled && !this.globalConfig.forceDisableTelemetry) {
if (enabled && !this.forceDisableTelemetry) {
this.toggleableAnalytics.enable();
} else {
this.toggleableAnalytics.disable();
Expand Down
4 changes: 4 additions & 0 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class MongoshNodeRepl implements EvaluationListener {
this.runtimeState().instanceState.isInteractive = value;
}

get isInteractive(): boolean {
return this.runtimeState().instanceState.isInteractive;
}

/**
* Create a Node.js REPL instance that can run mongosh commands,
* print greeting messages, and set up autocompletion and
Expand Down

0 comments on commit 22b07d8

Please sign in to comment.