-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat(cli): instance proxy server #635
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02567be
feat(cli): instance proxy server
mediremi 515b79b
chore: merge branch 'master' into feat/proxy
mediremi bbaf791
docs: add proxy docs
mediremi 7efe6b4
fix: rewrite URLs in JSON responses
mediremi 72abac8
fix: support node v12
mediremi 905acf1
test(cli): add proxy location rewrite edge case tests
mediremi 1422f63
refactor: remove DHIS2_DEFAULT_BASE_URL env
mediremi 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
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,127 @@ | ||
const url = require('url') | ||
const { reporter } = require('@dhis2/cli-helpers-engine') | ||
const httpProxy = require('http-proxy') | ||
const _ = require('lodash') | ||
const transformProxyResponse = require('node-http-proxy-json') | ||
|
||
const stripCookieSecure = cookie => { | ||
return cookie | ||
.split(';') | ||
.filter(v => v.trim().toLowerCase() !== 'secure') | ||
.join('; ') | ||
} | ||
|
||
const rewriteLocation = ({ location, target, baseUrl }) => { | ||
const parsedLocation = url.parse(location) | ||
const parsedTarget = url.parse(target) | ||
const parsedBaseUrl = url.parse(baseUrl) | ||
|
||
if ( | ||
parsedLocation.host === parsedTarget.host && | ||
parsedLocation.pathname.startsWith(parsedTarget.pathname) | ||
) { | ||
return url.format({ | ||
...parsedBaseUrl, | ||
pathname: parsedLocation.pathname.replace( | ||
parsedTarget.pathname, | ||
'' | ||
), | ||
search: parsedLocation.search, | ||
}) | ||
} | ||
return location | ||
} | ||
|
||
const isUrl = string => { | ||
try { | ||
const { protocol } = new URL(string) | ||
return protocol === 'http:' || protocol === 'https:' | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
|
||
const transformJsonResponse = (res, { target, baseUrl }) => { | ||
switch (typeof res) { | ||
case 'string': | ||
if (isUrl(res)) { | ||
return rewriteLocation({ location: res, target, baseUrl }) | ||
} | ||
return res | ||
case 'object': | ||
if (Array.isArray(res)) { | ||
return res.map(r => | ||
transformJsonResponse(r, { target, baseUrl }) | ||
) | ||
} | ||
return _.transform( | ||
res, | ||
(result, value, key) => { | ||
result[key] = transformJsonResponse(value, { | ||
target, | ||
baseUrl, | ||
}) | ||
}, | ||
{} | ||
) | ||
default: | ||
return res | ||
} | ||
} | ||
|
||
exports = module.exports = ({ target, baseUrl, port, shellPort }) => { | ||
const proxyServer = httpProxy.createProxyServer({ | ||
target, | ||
changeOrigin: true, | ||
secure: false, | ||
protocolRewrite: 'http', | ||
cookieDomainRewrite: '', | ||
cookiePathRewrite: '/', | ||
}) | ||
|
||
proxyServer.on('proxyRes', (proxyRes, req, res) => { | ||
if (proxyRes.headers['access-control-allow-origin']) { | ||
res.setHeader( | ||
'access-control-allow-origin', | ||
`http://localhost:${shellPort}` | ||
) | ||
} | ||
|
||
if (proxyRes.headers.location) { | ||
proxyRes.headers.location = rewriteLocation({ | ||
location: proxyRes.headers.location, | ||
target, | ||
baseUrl, | ||
}) | ||
} | ||
|
||
const sc = proxyRes.headers['set-cookie'] | ||
if (Array.isArray(sc)) { | ||
proxyRes.headers['set-cookie'] = sc.map(stripCookieSecure) | ||
} | ||
|
||
if ( | ||
proxyRes.headers['content-type'] && | ||
proxyRes.headers['content-type'].includes('application/json') | ||
) { | ||
transformProxyResponse(res, proxyRes, body => { | ||
if (body) { | ||
return transformJsonResponse(body, { | ||
target, | ||
baseUrl, | ||
}) | ||
} | ||
return body | ||
}) | ||
} | ||
}) | ||
|
||
proxyServer.on('error', error => { | ||
reporter.warn(error) | ||
}) | ||
|
||
proxyServer.listen(port) | ||
} | ||
|
||
exports.rewriteLocation = rewriteLocation | ||
exports.transformJsonResponse = transformJsonResponse |
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,91 @@ | ||
const { rewriteLocation, transformJsonResponse } = require('./proxy') | ||
|
||
describe('transformJsonResponse', () => { | ||
it('rewrites URLs in responses if they match the proxy target', () => { | ||
const transformedResponse = transformJsonResponse( | ||
{ | ||
a: { | ||
b: { | ||
c: 'https://play.dhis2.org/dev/api/endpoint', | ||
}, | ||
}, | ||
}, | ||
{ | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl: 'http://localhost:8080', | ||
} | ||
) | ||
|
||
expect(transformedResponse.a.b.c).toBe( | ||
'http://localhost:8080/api/endpoint' | ||
) | ||
}) | ||
}) | ||
|
||
describe('rewriteLocation', () => { | ||
it('rewrites locations if they match the proxy target', () => { | ||
const baseUrl = 'http://localhost:8080' | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://play.dhis2.org/dev/login.action', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl, | ||
}) | ||
).toBe(`${baseUrl}/login.action`) | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://play.dhis2.org/dev/page?param=value', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl, | ||
}) | ||
).toBe(`${baseUrl}/page?param=value`) | ||
mediremi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://server.com:1234', | ||
target: 'https://server.com:5678', | ||
baseUrl, | ||
}) | ||
).toBe('https://server.com:1234') | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://server.com', | ||
target: 'http://server.com', | ||
baseUrl, | ||
}) | ||
).toBe(baseUrl) | ||
|
||
expect( | ||
rewriteLocation({ | ||
location: 'https://play.dhis2.org/dev/api/dev', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl, | ||
}) | ||
).toBe(`${baseUrl}/api/dev`) | ||
}) | ||
|
||
it('does not rewrite locations if they do not match the proxy target', () => { | ||
;[ | ||
{ | ||
location: 'https://example.com/path', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
{ | ||
location: 'https://play.dhis2.org/2.35dev', | ||
target: 'https://play.dhis2.org/dev', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
{ | ||
location: 'http://server.com:1234', | ||
target: 'http://server.com:5678', | ||
baseUrl: 'http://localhost:8080', | ||
}, | ||
].forEach(args => { | ||
expect(rewriteLocation(args)).toBe(args.location) | ||
}) | ||
}) | ||
}) |
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,14 @@ | ||
# Proxy | ||
|
||
When developing against a remote instance, some browsers may block cookies due | ||
to the cross-site nature of requests. | ||
|
||
As a workaround, the [`start`](scripts/start.md) command provides a `--proxy` | ||
option. The value of this option is the remote instance that a local proxy will | ||
route requests to. | ||
|
||
## Usage | ||
|
||
``` | ||
d2-app-scripts start --proxy <remote instance URL> | ||
``` |
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 |
---|---|---|
|
@@ -8,8 +8,11 @@ A DHIS2 application needs to talk to a [DHIS2 server](https://www.dhis2.org/down | |
|
||
You should specify the fully-qualified base URL of a running DHIS2 server (including the `http://` or `https://` protocol) in the **Server** field of the login dialog. | ||
|
||
Make sure that the the URL of your application (`localhost:3000` in this case) is included in the DHIS2 server's CORS whitelist (System Settings -> Access). Also ensure that the server | ||
you specify doesn't have any domain-restricting proxy settings (such as the `SameSite=Lax`). | ||
Make sure that the the URL of your application (`localhost:3000` in this case) | ||
is included in the DHIS2 server's CORS whitelist (System Settings -> Access). If | ||
your server or browser has domain-restricting settings (such as the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually the case in modern Chrome even if the cookies haven't been explicitly set to restrict domains, since they are now SameSite by default (which is also the DHIS2 default) |
||
`SameSite=Lax`), the [`--proxy`](proxy.md) option of the | ||
[`start`](scripts/start.md) command may be helpful. | ||
|
||
For testing purposes, `https://play.dhis2.org/dev` will NOT work because it is configured for secure same-site access, while `https://debug.dhis2.org/dev` should work. | ||
|
||
|
Oops, something went wrong.
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.
Won't this have issues if i.e. the target pathname is
/dev
and there's a/dev
somewhere OTHER than at the start of the string? I.e. we should probably replace/^${pathname}/
instead, so that only the first match is replacedThere 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.
When a string is passed to
String.prototype.replace
, only the first match is replaced (e.g.'aaa'.replace('a', 'b') === 'baa'
). For it to replace multiple matches, a regex with theg
flag must be passed ('aaa'.replace(/a/g, 'b') === 'bbb'
).