Skip to content

Commit a73de1f

Browse files
committed
chore: switch to @mongodb-js/device-id
This package is meant to minimize code replication and to make sure the approach we're taking with hashing is consistent.
1 parent 59e2511 commit a73de1f

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
},
6262
"dependencies": {
6363
"@modelcontextprotocol/sdk": "^1.8.0",
64+
"@mongodb-js/device-id": "^0.2.0",
6465
"@mongodb-js/devtools-connect": "^3.7.2",
6566
"@mongosh/service-provider-node-driver": "^3.6.0",
6667
"bson": "^6.10.3",

src/telemetry/telemetry.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import logger, { LogId } from "../logger.js";
55
import { ApiClient } from "../common/atlas/apiClient.js";
66
import { MACHINE_METADATA } from "./constants.js";
77
import { EventCache } from "./eventCache.js";
8-
import { createHmac } from "crypto";
98
import nodeMachineId from "node-machine-id";
109
import { DeferredPromise } from "../deferred-promise.js";
10+
import { getDeviceId as extractDeviceId } from "@mongodb-js/device-id";
1111

1212
type EventResult = {
1313
success: boolean;
@@ -80,26 +80,13 @@ export class Telemetry {
8080
* @returns A hashed, unique identifier for the running device or `"unknown"` if not known.
8181
*/
8282
private async getDeviceId(): Promise<string> {
83-
try {
84-
if (this.commonProperties.device_id) {
85-
return this.commonProperties.device_id;
86-
}
87-
88-
const originalId: string = await this.getRawMachineId();
89-
90-
// Create a hashed format from the all uppercase version of the machine ID
91-
// to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
92-
const hmac = createHmac("sha256", originalId.toUpperCase());
93-
94-
/** This matches the message used to create the hashes in Atlas CLI */
95-
const DEVICE_ID_HASH_MESSAGE = "atlascli";
96-
97-
hmac.update(DEVICE_ID_HASH_MESSAGE);
98-
return hmac.digest("hex");
99-
} catch (error) {
100-
logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", String(error));
101-
return "unknown";
83+
if (this.commonProperties.device_id) {
84+
return this.commonProperties.device_id;
10285
}
86+
return extractDeviceId({
87+
getMachineId: () => this.getRawMachineId(),
88+
isNodeMachineId: true,
89+
}).value;
10390
}
10491

10592
/**

0 commit comments

Comments
 (0)