Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 82 additions & 68 deletions packages/mongodb-cloud-info/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,100 @@ import path from 'path';

const expect = chai.expect;

describe('getCloudInfo', function () {
beforeEach(function () {
nock('https://raw.githubusercontent.com', {
reqheaders: {
accept: '*/*',
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
'accept-encoding': 'gzip,deflate',
},
})
.get('/mongodb-js/devtools-shared/main/resources/cidrs.json')
.replyWithFile(
200,
path.resolve(__dirname, '../../../resources/cidrs.json'),
);
});
describe
.only('getCloudInfo', function () {
beforeEach(function () {
nock('https://raw.githubusercontent.com', {
reqheaders: {
accept: '*/*',
'user-agent':
'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
'accept-encoding': 'gzip,deflate',
},
})
.get('/mongodb-js/devtools-shared/main/resources/cidrs.json')
.replyWithFile(
200,
path.resolve(__dirname, '../../../resources/cidrs.json'),
);
});

afterEach(function () {
nock.cleanAll();
});

afterEach(function () {
nock.cleanAll();
});
it('returns all false for undefined', async function () {
const cloudInfo = await getCloudInfo();
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: false,
});
});

it('returns all false for undefined', async function () {
const cloudInfo = await getCloudInfo();
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: false,
it('returns all false for localhost', async function () {
const cloudInfo = await getCloudInfo('localhost');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: false,
});
});
});

it('returns all false for localhost', async function () {
const cloudInfo = await getCloudInfo('localhost');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: false,
it('works with local ip address (127.0.0.1)', async function () {
const cloudInfo = await getCloudInfo('127.0.0.1');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: false,
});
});
});

it('works with local ip address (127.0.0.1)', async function () {
const cloudInfo = await getCloudInfo('127.0.0.1');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: false,
it('works with local ipv6 address (::1)', async function () {
const cloudInfo = await getCloudInfo('::1');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: false,
});
});
});

it('works with local ipv6 address (::1)', async function () {
const cloudInfo = await getCloudInfo('::1');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: false,
it('returns {isAws: true} if hostname is an AWS ip', async function () {
const cloudInfo = await getCloudInfo('13.248.118.1');
expect(cloudInfo).to.deep.equal({
isAws: true,
isGcp: false,
isAzure: false,
});
});
});

it('returns {isAws: true} if hostname is an AWS ip', async function () {
const cloudInfo = await getCloudInfo('13.248.118.1');
expect(cloudInfo).to.deep.equal({
isAws: true,
isGcp: false,
isAzure: false,
it('returns {isGcp: true} if hostname is a GCP ip', async function () {
const cloudInfo = await getCloudInfo('8.34.208.1');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: true,
isAzure: false,
});
});
});

it('returns {isGcp: true} if hostname is a GCP ip', async function () {
const cloudInfo = await getCloudInfo('8.34.208.1');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: true,
isAzure: false,
it('returns {isAzure: true} if hostname is an Azure ip', async function () {
const cloudInfo = await getCloudInfo('13.64.151.161');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: true,
});
});
});

it('returns {isAzure: true} if hostname is an Azure ip', async function () {
const cloudInfo = await getCloudInfo('13.64.151.161');
expect(cloudInfo).to.deep.equal({
isAws: false,
isGcp: false,
isAzure: true,
it('returns {isAws: true} if CNAME resolves to an AWS host', async function () {
const cloudInfo = await getCloudInfo(
'compass-data-sets-shard-00-00.e06dc.mongodb.net',
);
expect(cloudInfo).to.deep.equal({
isAws: true,
isGcp: false,
isAzure: false,
});
});
});
}).timeout(5000);
})
.timeout(5000);
21 changes: 20 additions & 1 deletion packages/mongodb-cloud-info/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type RawCloudProviderCIDRs = {
let unparsedCIDRsPromise: Promise<RawCloudProviderCIDRs> | undefined;

const dnsLookup = util.promisify(dns.lookup.bind(dns));
const dnsResolveCname = util.promisify(dns.resolveCname.bind(dns));

function rangesContainsIP(
ipRanges: ParsedCIDRs,
Expand All @@ -41,6 +42,18 @@ function parseCIDRs(rawCidrs: RawCIDRs): ParsedCIDRs {
};
}

async function hasAWSCname(host: string) {
try {
const addresses = await dnsResolveCname(host);
return addresses.some((address) => address.endsWith('.amazonaws.com'));
} catch (err: unknown) {
// This can be any of a long list of codes, but in all cases we're just
// going to assume that it is not one an AWS host.
// (see https://nodejs.org/api/dns.html#error-codes)
return false;
}
}

export async function getCloudInfo(host?: string) {
if (!host) {
return {
Expand Down Expand Up @@ -69,9 +82,15 @@ export async function getCloudInfo(host?: string) {
throw err;
}

return {
const info = {
isAws: rangesContainsIP(parseCIDRs(unparsedCIDRs.aws), ip),
isGcp: rangesContainsIP(parseCIDRs(unparsedCIDRs.gcp), ip),
isAzure: rangesContainsIP(parseCIDRs(unparsedCIDRs.azure), ip),
};

if (!info.isAws && !info.isGcp && !info.isAzure) {
info.isAws = await hasAWSCname(host);
}

return info;
}
Loading