Skip to content

Commit aa6dfb8

Browse files
committed
fix: switch to native-machine-id
1 parent 97237f5 commit aa6dfb8

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

package-lock.json

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/build/src/compile/signable-compiler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export class SignableCompiler {
132132
path: await findModulePath('cli-repl', 'glibc-version'),
133133
requireRegexp: /\bglibc_version\.node$/,
134134
};
135+
const nativeMachineIdAddon = {
136+
path: await findModulePath('logging', 'native-machine-id'),
137+
requireRegexp: /\bnative_machine_id\.node$/,
138+
};
135139
// Warning! Until https://jira.mongodb.org/browse/MONGOSH-990,
136140
// packages/service-provider-node-driver *also* has a copy of these.
137141
// We use the versions included in packages/cli-repl here, so these
@@ -186,6 +190,7 @@ export class SignableCompiler {
186190
kerberosAddon,
187191
cryptLibraryVersionAddon,
188192
glibcVersionAddon,
193+
nativeMachineIdAddon,
189194
]
190195
.concat(winCAAddon ? [winCAAddon] : [])
191196
.concat(winConsoleProcessListAddon ? [winConsoleProcessListAddon] : [])

packages/cli-repl/webpack.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ const config = {
8787
'commonjs2 ../build/Release/mongocrypt.node',
8888
'../build/Debug/mongocrypt.node':
8989
'commonjs2 ../build/Debug/mongocrypt.node',
90+
'../build/Release/native_machine_id.node':
91+
'commonjs2 ../build/Release/native_machine_id.node',
92+
'../build/Debug/native_machine_id.node':
93+
'commonjs2 ../build/Debug/native_machine_id.node',
94+
'./build/Release/native_machine_id.node':
95+
'commonjs2 ./build/Release/native_machine_id.node',
9096
},
9197

9298
externalsPresets: {

packages/logging/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@mongosh/types": "3.6.0",
2424
"mongodb-log-writer": "^2.3.1",
2525
"mongodb-redact": "^1.1.5",
26-
"node-machine-id": "^1.1.12"
26+
"native-machine-id": "^0.0.11"
2727
},
2828
"devDependencies": {
2929
"@mongodb-js/eslint-config-mongosh": "^1.0.0",

packages/logging/src/logging-and-telemetry.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ describe('MongoshLoggingAndTelemetry', function () {
217217

218218
it('uses device ID "unknown" and logs error if it fails to resolve it', async function () {
219219
// eslint-disable-next-line @typescript-eslint/no-var-requires
220-
sinon.stub(require('child_process'), 'exec').throws(new Error('Test'));
220+
sinon
221+
// eslint-disable-next-line @typescript-eslint/no-var-requires
222+
.stub(require('native-machine-id'), 'getMachineId')
223+
.rejects(new Error('Test'));
221224
const loggingAndTelemetry = setupLoggingAndTelemetry({
222225
...testLoggingArguments,
223226
bus,
@@ -289,7 +292,7 @@ describe('MongoshLoggingAndTelemetry', function () {
289292
});
290293
sinon
291294
// eslint-disable-next-line @typescript-eslint/no-var-requires
292-
.stub(require('node-machine-id'), 'machineId')
295+
.stub(require('native-machine-id'), 'getMachineId')
293296
.resolves(delayedTelemetry);
294297

295298
const loggingAndTelemetry = setupLoggingAndTelemetry({

packages/logging/src/logging-and-telemetry.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import type {
5353
MongoshLoggingAndTelemetryArguments,
5454
MongoshTrackingProperties,
5555
} from './types';
56-
import { machineId } from 'node-machine-id';
56+
import { getMachineId } from 'native-machine-id';
5757
import { createHmac } from 'crypto';
5858

5959
export function setupLoggingAndTelemetry(
@@ -66,13 +66,15 @@ export function setupLoggingAndTelemetry(
6666
}
6767

6868
/**
69-
* @returns A hashed, unique identifier for the running device.
70-
* @throws If something goes wrong when getting the device ID.
69+
* @returns A hashed, unique identifier for the running device or `undefined` if not known.
7170
*/
72-
export async function getDeviceId(): Promise<string> {
71+
export async function getDeviceId(): Promise<string | 'unknown'> {
7372
// Create a hashed format from the all uppercase version of the machine ID
7473
// to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
75-
const originalId = (await machineId(true)).toUpperCase();
74+
const originalId = (await getMachineId({ raw: true }))?.toUpperCase();
75+
if (!originalId) {
76+
return 'unknown';
77+
}
7678
const hmac = createHmac('sha256', originalId);
7779

7880
/** This matches the message used to create the hashes in Atlas CLI */

0 commit comments

Comments
 (0)