-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BIOMAGE-1824] Missing clusters issue #339
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1543d55
added log
kafkasl 6bb4005
added logs
kafkasl 04eb085
more logging & debuggin
kafkasl 029578e
moved isReqFrom outside to be properly tested
kafkasl 65ea7cd
removed logs
kafkasl e9f9eb3
Merge branch 'master' into patch-cs
kafkasl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could have also one
describe('isRequestFromCluster')
and anotherdescribe('isRequestFromLocalhost')
(in the same.test.js
file).It's not a PR-blocking thing though, but just leaving this as another option.