Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Add isGenuine to InstanceHelper #142

Merged
merged 3 commits into from Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 45 additions & 3 deletions lib/instance-detail-helper.js
Expand Up @@ -61,7 +61,8 @@ function parseBuildInfo(resp) {
debug: resp.debug,
for_bits: resp.bits,
max_bson_object_size: resp.maxBsonObjectSize,
enterprise_module: false
enterprise_module: false,
raw: resp // Save the raw output to later determine if genuine MongoDB
};
// cover both cases of detecting enterprise module, see SERVER-18099
if (resp.gitVersion && resp.gitVersion.match(/enterprise/)) {
Expand Down Expand Up @@ -94,6 +95,43 @@ function getBuildInfo(results, done) {
});
}

function getCmdLineOpts(results, done) {
const db = results.db;

const spec = {
getCmdLineOpts: 1
};

const adminDb = db.databaseName === 'admin' ? db : db.admin();
adminDb.command(spec, {}, function(err, res) {
if (err) {
debug('getCmdLineOpts failed!', err);
return done(null, { errmsg: err.message });
}
done(null, res);
});
}

function getGenuineMongoDB(results, done) {
const buildInfo = results.build.raw;
const cmdLineOpts = results.cmdLineOpts;

const res = {
isGenuine: true,
dbType: 'mongodb'
};

if (buildInfo.hasOwnProperty('_t' )) {
res.isGenuine = false;
res.dbType = 'cosmosdb';
}
if (cmdLineOpts.hasOwnProperty('errmsg') && cmdLineOpts.errmsg.indexOf('not supported') !== -1) {
res.isGenuine = false;
res.dbType = 'documentdb';
}
done(null, res);
}

/**
* @param {Object} resp - Result of `db.db('admin').command({hostInfo: 1})`.
* @return {Object}
Expand Down Expand Up @@ -487,6 +525,8 @@ function getInstanceDetail(client, db, done) {

host: ['client', 'db', getHostInfo],
build: ['client', 'db', getBuildInfo],
cmdLineOpts: ['client', 'db', getCmdLineOpts],
genuineMongoDB: ['build', 'cmdLineOpts', getGenuineMongoDB],

listDatabases: ['client', 'db', 'userInfo', listDatabases],
allowedDatabases: ['userInfo', getAllowedDatabases],
Expand All @@ -508,7 +548,7 @@ function getInstanceDetail(client, db, done) {
}
// cleanup
results = omit(results, ['db', 'listDatabases', 'allowedDatabases',
'userInfo', 'listCollections', 'allowedCollections']);
'userInfo', 'listCollections', 'allowedCollections', 'cmdLineOpts']);
return done(null, results);
});
}
Expand Down Expand Up @@ -545,7 +585,7 @@ function getInstance(client, db, done) {
}

res._id = [hostname, port].join(':');
debug('instance.get returning %j', res);
debug('instance.get returning', res);
done(null, res);
});
}
Expand All @@ -554,7 +594,9 @@ module.exports = {
getAllowedCollections,
getAllowedDatabases,
getBuildInfo,
getCmdLineOpts,
getDatabaseCollections,
getGenuineMongoDB,
getHostInfo,
getInstance,
listCollections,
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

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

49 changes: 48 additions & 1 deletion test/fixtures.js
Expand Up @@ -493,11 +493,58 @@ var BUILD_INFO_3_2 = {
"ok" : 1
};

var CMD_LINE_OPTS = {
"argv" : [
"/opt/mongodb-osx-x86_64-enterprise-3.6.3/bin/mongod",
"--dbpath=/Users/user/testdata"
],
"parsed" : {
"storage" : {
"dbPath" : "/Users/user/testdata"
}
},
"ok" : 1
};

var DOCUMENTDB_CMD_LINE_OPTS = {
"ok" : 0,
"errmsg" : "Feature not supported: getCmdLineOpts",
"code" : 303
};

var COSMOSDB_BUILD_INFO = {
"_t" : "BuildInfoResponse",
"ok" : 1,
"version" : "3.2.0",
"gitVersion" : "45d947729a0315accb6d4f15a6b06be6d9c19fe7",
"targetMinOS" : "Windows 7/Windows Server 2008 R2",
"modules" : [ ],
"allocator" : "tcmalloc",
"javascriptEngine" : "Chakra",
"sysInfo" : "deprecated",
"versionArray" : [
3,
2,
0,
0
],
"bits" : 64,
"debug" : false,
"maxBsonObjectSize" : 524288,
"openssl" : {
"running" : "OpenSSL 1.0.1p-fips 9 Jul 2015",
"compiled" : "OpenSSL 1.0.1p-fips 9 Jul 2015"
}
};

module.exports = {
HOST_INFO: HOST_INFO,
BUILD_INFO_OLD: BUILD_INFO_OLD,
BUILD_INFO_3_2: BUILD_INFO_3_2,
USER_INFO_JOHN: USER_INFO_JOHN,
USER_INFO_LISTDB_ONLY: USER_INFO_LISTDB_ONLY,
USER_INFO_COLL_ONLY: USER_INFO_COLL_ONLY
USER_INFO_COLL_ONLY: USER_INFO_COLL_ONLY,
CMD_LINE_OPTS: CMD_LINE_OPTS,
DOCUMENTDB_CMD_LINE_OPTS: DOCUMENTDB_CMD_LINE_OPTS,
COSMOSDB_BUILD_INFO: COSMOSDB_BUILD_INFO
};
99 changes: 99 additions & 0 deletions test/instance-detail-helper-mocked.test.js
Expand Up @@ -3,7 +3,9 @@ const {
getAllowedCollections,
getAllowedDatabases,
getBuildInfo,
getCmdLineOpts,
getDatabaseCollections,
getGenuineMongoDB,
getHostInfo,
listCollections,
listDatabases
Expand Down Expand Up @@ -109,8 +111,105 @@ describe('instance-detail-helper-mocked', function() {
done();
});
});
it('should save a copy of the raw output', function(done) {
const results = {
db: makeMockDB(null, {tester: 1})
};
getBuildInfo(results, function(err, res) {
assert.equal(err, null);
assert.deepEqual(res.raw, {tester: 1});
done();
});
});
});

describe('getCmdLineOpts', function() {
it('should not pass on any error that getCmdLineOpts returns', function(done) {
// instead of the real db handle, pass in the mocked one
const results = {
db: makeMockDB(new Error('some strange error'), null)
};
getCmdLineOpts(results, function(err, res) {
assert.equal(err, null);
assert.equal(res.errmsg, 'some strange error');
done();
});
});
it('should return results if they error but do not throw', function(done) {
// instead of the real db handle, pass in the mocked one
const results = {
db: makeMockDB(null, fixtures.DOCUMENTDB_CMD_LINE_OPTS)
};
getCmdLineOpts(results, function(err, res) {
assert.equal(err, null);
assert.equal(res.errmsg, 'Feature not supported: getCmdLineOpts');
done();
});
});
it('should return results if no error', function(done) {
const results = {
db: makeMockDB(null, fixtures.CMD_LINE_OPTS)
};
getCmdLineOpts(results, function(err, res) {
assert.equal(err, null);
assert.deepEqual(res, fixtures.CMD_LINE_OPTS);
done();
});
});
});

describe('getGenuineMongoDB', function() {
it('reports on CosmosDB', function(done) {
const results = {
build: {raw: fixtures.COSMOSDB_BUILD_INFO},
cmdLineOpts: fixtures.CMD_LINE_OPTS
};
getGenuineMongoDB(results, function(err, res) {
assert.equal(err, null);
assert.equal(res.dbType, 'cosmosdb');
assert.equal(res.isGenuine, false);
done();
});
});
it('reports on DocumentDB', function(done) {
const results = {
build: {raw: fixtures.BUILD_INFO_3_2},
cmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS
};
getGenuineMongoDB(results, function(err, res) {
assert.equal(err, null);
assert.equal(res.dbType, 'documentdb');
assert.equal(res.isGenuine, false);
done();
});
});
it('should not report on 3.2', function(done) {
const results = {
build: {raw: fixtures.BUILD_INFO_3_2},
cmdLineOpts: fixtures.CMD_LINE_OPTS
};
getGenuineMongoDB(results, function(err, res) {
assert.equal(err, null);
assert.equal(res.dbType, 'mongodb');
assert.equal(res.isGenuine, true);
done();
});
});
it('should not report on older versions', function(done) {
const results = {
build: {raw: fixtures.BUILD_INFO_OLD},
cmdLineOpts: fixtures.CMD_LINE_OPTS
};
getGenuineMongoDB(results, function(err, res) {
assert.equal(err, null);
assert.equal(res.dbType, 'mongodb');
assert.equal(res.isGenuine, true);
done();
});
});
});


describe('getHostInfo', function() {
it('should ignore auth errors gracefully', function(done) {
// instead of the real db handle, pass in the mocked one
Expand Down