Skip to content

Commit 9a28985

Browse files
committed
update
1 parent b6c9790 commit 9a28985

File tree

5 files changed

+98
-9
lines changed

5 files changed

+98
-9
lines changed

src/common/session.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
ConnectionSettings,
1212
ConnectionStateConnected,
1313
ConnectionStateErrored,
14+
ConnectionStringAuthType,
1415
} from "./connectionManager.js";
1516
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
1617
import { ErrorCodes, MongoDBError } from "./errors.js";
@@ -182,4 +183,8 @@ export class Session extends EventEmitter<SessionEvents> {
182183
get connectedAtlasCluster(): AtlasClusterConnectionInfo | undefined {
183184
return this.connectionManager.currentConnectionState.connectedAtlasCluster;
184185
}
186+
187+
get connectionStringAuthType(): ConnectionStringAuthType | undefined {
188+
return this.connectionManager.currentConnectionState.connectionStringAuthType;
189+
}
185190
}

src/tools/atlas/connect/connectCluster.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ensureCurrentIpInAccessList } from "../../../common/atlas/accessListUti
88
import type { AtlasClusterConnectionInfo } from "../../../common/connectionManager.js";
99
import { getDefaultRoleFromConfig } from "../../../common/atlas/roles.js";
1010
import { AtlasArgs } from "../../args.js";
11-
import { AtlasLocalToolMetadata } from "../../../telemetry/types.js";
11+
import { ConnectionMetadata } from "../../../telemetry/types.js";
1212

1313
const addedIpAccessListMessage =
1414
"Note: Your current IP address has been added to the Atlas project's IP access list to enable secure connection.";
@@ -306,8 +306,8 @@ export class ConnectClusterTool extends AtlasToolBase {
306306
}
307307

308308
// eslint-disable-next-line @typescript-eslint/no-unused-vars
309-
protected override resolveTelemetryMetadata(_args: ToolArgs<typeof this.argsShape>): AtlasLocalToolMetadata {
310-
const metadata: AtlasLocalToolMetadata = {};
309+
protected override resolveTelemetryMetadata(_args: ToolArgs<typeof this.argsShape>): ConnectionMetadata {
310+
const metadata: ConnectionMetadata = {};
311311
const connectionStringAuthType = this.session.connectionManager.currentConnectionState.connectionStringAuthType;
312312
if (connectionStringAuthType) {
313313
metadata.connection_auth_type = connectionStringAuthType;

src/tools/mongodb/connect/connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ToolArgs, OperationType, ToolConstructorParams } from "../../tool.
55
import assert from "assert";
66
import type { Server } from "../../../server.js";
77
import { LogId } from "../../../common/logger.js";
8-
import { AtlasLocalToolMetadata } from "../../../telemetry/types.js";
8+
import { ConnectionMetadata } from "../../../telemetry/types.js";
99

1010
const disconnectedSchema = z
1111
.object({

src/tools/tool.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,15 @@ export abstract class ToolBase {
306306

307307
protected getConnectionInfoMetadata(): ConnectionMetadata {
308308
const metadata: ConnectionMetadata = {};
309-
const connectionStringAuthType = this.session.connectionManager.currentConnectionState.connectionStringAuthType;
310-
if (connectionStringAuthType) {
311-
metadata.connection_auth_type = connectionStringAuthType;
312-
}
313-
314309
if (this.session.connectedAtlasCluster?.projectId) {
315310
metadata.project_id = this.session.connectedAtlasCluster.projectId;
316311
}
317312

313+
const connectionStringAuthType = this.session.connectionStringAuthType;
314+
if (connectionStringAuthType !== undefined) {
315+
metadata.connection_auth_type = connectionStringAuthType;
316+
}
317+
318318
return metadata;
319319
}
320320
}

tests/unit/toolBase.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,90 @@ describe("ToolBase", () => {
175175
expect(event.properties).toHaveProperty("test_param2", "three");
176176
});
177177
});
178+
179+
describe("getConnectionInfoMetadata", () => {
180+
it("should return empty metadata when neither connectedAtlasCluster nor connectionStringAuthType are set", () => {
181+
(mockSession as { connectedAtlasCluster?: unknown }).connectedAtlasCluster = undefined;
182+
(mockSession as { connectionStringAuthType?: unknown }).connectionStringAuthType = undefined;
183+
184+
const metadata = testTool["getConnectionInfoMetadata"]();
185+
186+
expect(metadata).toEqual({});
187+
expect(metadata).not.toHaveProperty("project_id");
188+
expect(metadata).not.toHaveProperty("connection_auth_type");
189+
});
190+
191+
it("should return metadata with project_id when connectedAtlasCluster.projectId is set", () => {
192+
(mockSession as { connectedAtlasCluster?: unknown }).connectedAtlasCluster = {
193+
projectId: "test-project-id",
194+
username: "test-user",
195+
clusterName: "test-cluster",
196+
expiryDate: new Date(),
197+
};
198+
(mockSession as { connectionStringAuthType?: unknown }).connectionStringAuthType = undefined;
199+
200+
const metadata = testTool["getConnectionInfoMetadata"]();
201+
202+
expect(metadata).toEqual({
203+
project_id: "test-project-id",
204+
});
205+
expect(metadata).not.toHaveProperty("connection_auth_type");
206+
});
207+
208+
it("should return empty metadata when connectedAtlasCluster exists but projectId is falsy", () => {
209+
(mockSession as { connectedAtlasCluster?: unknown }).connectedAtlasCluster = {
210+
projectId: "",
211+
username: "test-user",
212+
clusterName: "test-cluster",
213+
expiryDate: new Date(),
214+
};
215+
(mockSession as { connectionStringAuthType?: unknown }).connectionStringAuthType = undefined;
216+
217+
const metadata = testTool["getConnectionInfoMetadata"]();
218+
219+
expect(metadata).toEqual({});
220+
expect(metadata).not.toHaveProperty("project_id");
221+
});
222+
223+
it("should return metadata with connection_auth_type when connectionStringAuthType is set", () => {
224+
(mockSession as { connectedAtlasCluster?: unknown }).connectedAtlasCluster = undefined;
225+
(mockSession as { connectionStringAuthType?: unknown }).connectionStringAuthType = "scram";
226+
227+
const metadata = testTool["getConnectionInfoMetadata"]();
228+
229+
expect(metadata).toEqual({
230+
connection_auth_type: "scram",
231+
});
232+
expect(metadata).not.toHaveProperty("project_id");
233+
});
234+
235+
it("should return metadata with both project_id and connection_auth_type when both are set", () => {
236+
(mockSession as { connectedAtlasCluster?: unknown }).connectedAtlasCluster = {
237+
projectId: "test-project-id",
238+
username: "test-user",
239+
clusterName: "test-cluster",
240+
expiryDate: new Date(),
241+
};
242+
(mockSession as { connectionStringAuthType?: unknown }).connectionStringAuthType = "oidc-auth-flow";
243+
244+
const metadata = testTool["getConnectionInfoMetadata"]();
245+
246+
expect(metadata).toEqual({
247+
project_id: "test-project-id",
248+
connection_auth_type: "oidc-auth-flow",
249+
});
250+
});
251+
252+
it("should handle different connectionStringAuthType values", () => {
253+
const authTypes = ["scram", "ldap", "kerberos", "oidc-auth-flow", "oidc-device-flow", "x.509"] as const;
254+
255+
for (const authType of authTypes) {
256+
(mockSession as { connectionStringAuthType?: unknown }).connectionStringAuthType = authType;
257+
const metadata = testTool["getConnectionInfoMetadata"]();
258+
expect(metadata.connection_auth_type).toBe(authType);
259+
}
260+
});
261+
});
178262
});
179263

180264
class TestTool extends ToolBase {

0 commit comments

Comments
 (0)