Skip to content

Commit 561a26d

Browse files
lerouxbaddaleax
andauthored
fix(mongodb-cloud-info): if the host's IP is not in a cloud's range, check if the CNAME resolves to AWS. COMPASS-8932 (#526)
* If the host's IP is not in a cloud's range, check if the CNAME resolves to AWS. * Update packages/mongodb-cloud-info/src/index.ts Co-authored-by: Anna Henningsen <anna.henningsen@mongodb.com> * without the .only * reformat unrelated code --------- Co-authored-by: Anna Henningsen <anna.henningsen@mongodb.com>
1 parent 559968a commit 561a26d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

packages/dl-center/src/download-center.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ describe('download center client', function () {
8686
createReadStream(fixturePath('asset.txt')),
8787
{
8888
acl: 'private',
89-
}
89+
},
9090
);
9191

9292
const content = await downloadCenter.downloadAsset(
93-
'prefix-private/asset.txt'
93+
'prefix-private/asset.txt',
9494
);
9595
expect(content?.toString()).to.contain('content');
9696
});

packages/mongodb-cloud-info/src/index.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,15 @@ describe('getCloudInfo', function () {
8787
isAzure: true,
8888
});
8989
});
90+
91+
it('returns {isAws: true} if CNAME resolves to an AWS host', async function () {
92+
const cloudInfo = await getCloudInfo(
93+
'compass-data-sets-shard-00-00.e06dc.mongodb.net',
94+
);
95+
expect(cloudInfo).to.deep.equal({
96+
isAws: true,
97+
isGcp: false,
98+
isAzure: false,
99+
});
100+
});
90101
}).timeout(5000);

packages/mongodb-cloud-info/src/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type RawCloudProviderCIDRs = {
2222
let unparsedCIDRsPromise: Promise<RawCloudProviderCIDRs> | undefined;
2323

2424
const dnsLookup = util.promisify(dns.lookup.bind(dns));
25+
const dnsResolveCname = util.promisify(dns.resolveCname.bind(dns));
2526

2627
function rangesContainsIP(
2728
ipRanges: ParsedCIDRs,
@@ -41,6 +42,18 @@ function parseCIDRs(rawCidrs: RawCIDRs): ParsedCIDRs {
4142
};
4243
}
4344

45+
async function hasAWSCname(host: string) {
46+
try {
47+
const addresses = await dnsResolveCname(host);
48+
return addresses.some((address) => address.endsWith('.amazonaws.com'));
49+
} catch (err: unknown) {
50+
// This can be any of a long list of codes, but in all cases we're just
51+
// going to assume that it is not on an AWS host.
52+
// (see https://nodejs.org/api/dns.html#error-codes)
53+
return false;
54+
}
55+
}
56+
4457
export async function getCloudInfo(host?: string) {
4558
if (!host) {
4659
return {
@@ -69,9 +82,15 @@ export async function getCloudInfo(host?: string) {
6982
throw err;
7083
}
7184

72-
return {
85+
const info = {
7386
isAws: rangesContainsIP(parseCIDRs(unparsedCIDRs.aws), ip),
7487
isGcp: rangesContainsIP(parseCIDRs(unparsedCIDRs.gcp), ip),
7588
isAzure: rangesContainsIP(parseCIDRs(unparsedCIDRs.azure), ip),
7689
};
90+
91+
if (!info.isAws && !info.isGcp && !info.isAzure) {
92+
info.isAws = await hasAWSCname(host);
93+
}
94+
95+
return info;
7796
}

0 commit comments

Comments
 (0)