-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #339 from hms-dbmi-cellenics/patch-cs
[BIOMAGE-1824] Missing clusters issue
- Loading branch information
Showing
6 changed files
with
155 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const dns = require('dns').promises; | ||
const ipaddr = require('ipaddr.js'); | ||
|
||
// eslint-disable-next-line no-useless-escape | ||
const INTERNAL_DOMAINS_REGEX = new RegExp('((\.compute\.internal)|(\.svc\.local))$'); | ||
|
||
const isReqFromLocalhost = async (req) => { | ||
const ip = req.connection.remoteAddress; | ||
const host = req.get('host'); | ||
|
||
if (ip === '127.0.0.1' || ip === '::ffff:127.0.0.1' || ip === '::1' || host.indexOf('localhost') !== -1) { | ||
return true; | ||
} | ||
|
||
throw new Error('ip address is not localhost'); | ||
}; | ||
|
||
const isReqFromCluster = async (req) => { | ||
let remoteAddress = req.ip; | ||
const addr = ipaddr.parse(req.ip); | ||
// req.ip returns IPv4 addresses mapped to IPv6, e.g.: | ||
// 127.0.0.1 (IPv4) -> ::ffff:127.0.0.1 (IPv6) | ||
// dns.reverse is not capable of dealing with them, | ||
// it either uses IPv4 or IPv6, so we need to map those | ||
// IPs back to IPv4 before. | ||
if (addr.kind() === 'ipv6' && addr.isIPv4MappedAddress()) { | ||
remoteAddress = addr.toIPv4Address().toString(); | ||
} | ||
|
||
const domains = await dns.reverse(remoteAddress); | ||
if (domains.some((domain) => INTERNAL_DOMAINS_REGEX.test(domain))) { | ||
return true; | ||
} | ||
|
||
throw new Error('ip address does not come from internal sources'); | ||
}; | ||
|
||
module.exports = { | ||
isReqFromCluster, | ||
isReqFromLocalhost, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
|
||
|
||
jest.mock('dns', () => ({ | ||
promises: { | ||
reverse: jest.fn((ip) => { | ||
if (ip === '127.0.0.1') return []; | ||
return ['ip-192-168-905-123.region.compute.internal']; | ||
}), | ||
}, | ||
})); | ||
|
||
const { | ||
isReqFromCluster, | ||
isReqFromLocalhost, | ||
} = require('../../src/utils/isReqFrom'); | ||
const fake = require('../test-utils/constants'); | ||
|
||
describe('Tests for isRequestFromCluster and isRequestFromLocalhost', () => { | ||
it('isReqFromLocalhost returns true for localhost', async () => { | ||
const req = { | ||
params: { experimentId: fake.EXPERIMENT_ID }, | ||
user: fake.USER, | ||
url: `/v1/experiments/${fake.EXPERIMENT_ID}/cellSets`, | ||
method: 'PATCH', | ||
connection: { | ||
address: '::ffff:127.0.0.1', | ||
}, | ||
get: () => 'localhost', | ||
}; | ||
|
||
expect(await isReqFromLocalhost(req)).toEqual(true); | ||
}); | ||
|
||
it('isRequestFromLocalhost throws with cluster addr', async () => { | ||
const req = { | ||
params: { experimentId: fake.EXPERIMENT_ID }, | ||
user: fake.USER, | ||
url: `/v1/experiments/${fake.EXPERIMENT_ID}/cellSets`, | ||
method: 'PATCH', | ||
ip: '::ffff:192.0.0.1', | ||
connection: { | ||
address: '::ffff:192.0.0.1', | ||
}, | ||
get: () => 'whatever.internal.compute', | ||
}; | ||
|
||
await expect(isReqFromLocalhost(req)) | ||
.rejects | ||
.toThrow(); | ||
}); | ||
|
||
it('isReqFromCluster returns true for cluster addr', async () => { | ||
const req = { | ||
params: { experimentId: fake.EXPERIMENT_ID }, | ||
user: fake.USER, | ||
url: `/v1/experiments/${fake.EXPERIMENT_ID}/cellSets`, | ||
method: 'PATCH', | ||
ip: '::ffff:192.0.0.1', | ||
}; | ||
|
||
expect(await isReqFromCluster(req)).toEqual(true); | ||
}); | ||
|
||
it('isRequestFromCluster throws with localhost', async () => { | ||
const req = { | ||
params: { experimentId: fake.EXPERIMENT_ID }, | ||
user: fake.USER, | ||
url: `/v1/experiments/${fake.EXPERIMENT_ID}/cellSets`, | ||
method: 'PATCH', | ||
ip: '::ffff:127.0.0.1', | ||
}; | ||
|
||
await expect(isReqFromCluster(req)) | ||
.rejects | ||
.toThrow(); | ||
}); | ||
}); |