Skip to content

Commit 1ee40c1

Browse files
committed
Use identifyServerName in parallel with requesting build info
1 parent 4c31948 commit 1ee40c1

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

packages/service-provider-core/src/connect-info.spec.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ chai.use(chaiAsPromised);
66
import getConnectExtraInfo from './connect-info';
77
import { ConnectionString } from 'mongodb-connection-string-url';
88

9+
const mockedAdminCommand = () =>
10+
Promise.reject(new Error('adminCommand was mocked'));
11+
912
describe('getConnectInfo', function () {
1013
const BUILD_INFO = {
1114
version: '3.2.0-rc2',
@@ -79,9 +82,10 @@ describe('getConnectInfo', function () {
7982
getConnectExtraInfo({
8083
connectionString: new ConnectionString(ATLAS_URI_WITH_AUTH),
8184
buildInfo: Promise.resolve(BUILD_INFO),
82-
atlasVersion: Promise.resolve(ATLAS_VERSION),
85+
atlasVersion: ATLAS_VERSION,
8386
resolvedHostname: 'test-data-sets-00-02-a011bb.mongodb.net',
84-
isLocalAtlas: Promise.resolve(false),
87+
isLocalAtlas: false,
88+
adminCommand: mockedAdminCommand,
8589
})
8690
).to.eventually.deep.equal(output);
8791
});
@@ -111,9 +115,10 @@ describe('getConnectInfo', function () {
111115
getConnectExtraInfo({
112116
connectionString: new ConnectionString(ATLAS_URI),
113117
buildInfo: Promise.resolve(BUILD_INFO),
114-
atlasVersion: Promise.resolve(ATLAS_VERSION),
118+
atlasVersion: ATLAS_VERSION,
115119
resolvedHostname: 'test-data-sets-00-02-a011bb.mongodb.net',
116-
isLocalAtlas: Promise.resolve(false),
120+
isLocalAtlas: false,
121+
adminCommand: mockedAdminCommand,
117122
})
118123
).to.eventually.deep.equal(output);
119124
});
@@ -145,10 +150,11 @@ describe('getConnectInfo', function () {
145150
getConnectExtraInfo({
146151
connectionString: new ConnectionString(streamUri),
147152
buildInfo: Promise.resolve(BUILD_INFO),
148-
atlasVersion: Promise.resolve(undefined),
153+
atlasVersion: undefined,
149154
resolvedHostname:
150155
'atlas-stream-67b8e1cd6d60357be377be7b-1dekw.virginia-usa.a.query.mongodb-dev.net',
151-
isLocalAtlas: Promise.resolve(false),
156+
isLocalAtlas: false,
157+
adminCommand: mockedAdminCommand,
152158
})
153159
).to.eventually.deep.equal(output);
154160
});
@@ -177,9 +183,10 @@ describe('getConnectInfo', function () {
177183
await expect(
178184
getConnectExtraInfo({
179185
buildInfo: Promise.resolve(BUILD_INFO),
180-
atlasVersion: Promise.resolve(undefined),
186+
atlasVersion: undefined,
181187
resolvedHostname: 'localhost',
182-
isLocalAtlas: Promise.resolve(true),
188+
isLocalAtlas: true,
189+
adminCommand: mockedAdminCommand,
183190
})
184191
).to.eventually.deep.equal(output);
185192
});
@@ -208,8 +215,9 @@ describe('getConnectInfo', function () {
208215
await expect(
209216
getConnectExtraInfo({
210217
buildInfo: Promise.resolve(null),
211-
atlasVersion: Promise.resolve(undefined),
212-
isLocalAtlas: Promise.resolve(false),
218+
atlasVersion: undefined,
219+
isLocalAtlas: false,
220+
adminCommand: mockedAdminCommand,
213221
})
214222
).to.eventually.deep.equal(output);
215223
});

packages/service-provider-core/src/connect-info.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,38 @@ export default async function getConnectExtraInfo({
6767
atlasVersion,
6868
resolvedHostname,
6969
isLocalAtlas,
70+
adminCommand,
7071
}: {
7172
connectionString?: ConnectionString;
7273
buildInfo: Promise<Document | null>;
73-
atlasVersion: Promise<string | undefined>;
74+
atlasVersion: string | undefined;
7475
resolvedHostname?: string;
75-
isLocalAtlas: Promise<boolean>;
76+
isLocalAtlas: boolean;
77+
adminCommand: (document: Document) => Promise<Document>;
7678
}): Promise<ConnectionExtraInfo> {
7779
const auth_type =
7880
connectionString?.searchParams.get('authMechanism') ?? undefined;
7981
const uri = connectionString?.toString() ?? '';
8082

81-
const { isGenuine: is_genuine, serverName: non_genuine_server_name } =
82-
getBuildInfo.getGenuineMongoDB(uri);
83+
const serverName = await getBuildInfo.identifyServerName({
84+
connectionString: uri,
85+
buildInfo: buildInfo.then((info) => info ?? {}),
86+
adminCommand,
87+
});
8388
// Atlas Data Lake has been renamed to Atlas Data Federation
8489
const { isDataLake: is_data_federation, dlVersion } =
8590
getBuildInfo.getDataLake(await buildInfo);
8691

8792
const { serverOs, serverArch } = getBuildInfo.getBuildEnv(await buildInfo);
88-
const isAtlas = !!(await atlasVersion) || getBuildInfo.isAtlas(uri);
93+
const isAtlas = !!atlasVersion || getBuildInfo.isAtlas(uri);
94+
const serverVersion = await buildInfo.then((info) =>
95+
typeof info?.version === 'string' ? info.version : undefined
96+
);
8997

9098
return {
9199
...getHostInformation(resolvedHostname || uri),
92100
is_atlas: isAtlas,
93-
server_version: await buildInfo.then((info) =>
94-
typeof info?.version === 'string' ? info.version : undefined
95-
),
101+
server_version: serverVersion,
96102
node_version: process.version,
97103
server_os: serverOs || undefined,
98104
uri,
@@ -102,9 +108,10 @@ export default async function getConnectExtraInfo({
102108
is_data_federation,
103109
is_stream: getBuildInfo.isAtlasStream(uri),
104110
dl_version: dlVersion || undefined,
105-
atlas_version: await atlasVersion,
106-
is_genuine,
107-
non_genuine_server_name,
108-
is_local_atlas: await isLocalAtlas,
111+
atlas_version: atlasVersion,
112+
// When the server name is 'unknown', we cannot be certain that it's non-genuine
113+
is_genuine: serverName === 'mongodb' || serverName === 'unknown',
114+
non_genuine_server_name: serverName,
115+
is_local_atlas: isLocalAtlas,
109116
};
110117
}

packages/service-provider-node-driver/src/node-driver-service-provider.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ describe('NodeDriverServiceProvider', function () {
10071007
expect(info.extraInfo?.is_local_atlas).to.equal(false);
10081008
expect(info.extraInfo?.is_localhost).to.equal(true);
10091009
expect(info.extraInfo?.fcv).to.equal(undefined);
1010-
expect(dbStub.command).to.have.callCount(3);
1010+
expect(dbStub.command).to.have.callCount(4);
10111011
expect(
10121012
dbStub.collection,
10131013
'calls countDocument on collection to check local atlas cli support'

packages/service-provider-node-driver/src/node-driver-service-provider.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -472,42 +472,53 @@ export class NodeDriverServiceProvider
472472
}
473473

474474
async getConnectionInfo(): Promise<ConnectionInfo> {
475-
const [buildInfo = null, atlasVersion = null, fcv = null, atlascliInfo] =
476-
await Promise.all([
477-
this.runCommandWithCheck(
478-
'admin',
479-
{ buildInfo: 1 },
480-
this.baseCmdOptions
481-
).catch(() => {}),
482-
this.runCommandWithCheck(
483-
'admin',
484-
{ atlasVersion: 1 },
485-
this.baseCmdOptions
486-
).catch(() => {}),
487-
this.runCommandWithCheck(
488-
'admin',
489-
{ getParameter: 1, featureCompatibilityVersion: 1 },
490-
this.baseCmdOptions
491-
).catch(() => {}),
492-
this.countDocuments('admin', 'atlascli', {
493-
managedClusterType: 'atlasCliLocalDevCluster',
494-
}).catch(() => 0),
495-
]);
475+
const buildInfoPromise = this.runCommandWithCheck(
476+
'admin',
477+
{ buildInfo: 1 },
478+
this.baseCmdOptions
479+
).catch(() => null);
496480

497481
const resolvedHostname = this._getHostnameForConnection(
498482
this._lastSeenTopology
499483
);
500484

501-
const extraConnectionInfo = getConnectExtraInfo({
485+
const [isLocalAtlas, atlasVersion, fcv] = await Promise.all([
486+
this.countDocuments('admin', 'atlascli', {
487+
managedClusterType: 'atlasCliLocalDevCluster',
488+
}).then(
489+
(result) => !!result,
490+
() => false
491+
),
492+
this.runCommandWithCheck(
493+
'admin',
494+
{ atlasVersion: 1 },
495+
this.baseCmdOptions
496+
).then(
497+
(response) =>
498+
typeof response.atlasVersion === 'string'
499+
? response.atlasVersion
500+
: undefined,
501+
() => undefined
502+
),
503+
this.runCommandWithCheck(
504+
'admin',
505+
{ getParameter: 1, featureCompatibilityVersion: 1 },
506+
this.baseCmdOptions
507+
).catch(() => null),
508+
]);
509+
510+
const extraConnectionInfo = await getConnectExtraInfo({
502511
connectionString: this.uri,
503-
buildInfo,
512+
buildInfo: buildInfoPromise,
504513
atlasVersion,
505514
resolvedHostname,
506-
isLocalAtlas: !!atlascliInfo,
515+
isLocalAtlas,
516+
adminCommand: (request: Document) =>
517+
this.runCommandWithCheck('admin', request, this.baseCmdOptions),
507518
});
508519

509520
return {
510-
buildInfo,
521+
buildInfo: await buildInfoPromise,
511522
resolvedHostname,
512523
extraInfo: {
513524
...extraConnectionInfo,

0 commit comments

Comments
 (0)