Skip to content
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

Feature: Add enforce HTTPS middleware #27

Merged
merged 3 commits into from
Aug 31, 2022
Merged

Conversation

kokokuo
Copy link
Contributor

@kokokuo kokokuo commented Aug 2, 2022

Description

Provide the enforce HTTPS middleware, the enforce https provide two different solution:

With Reverse Proxy

If you have a reverse proxy in the front of vulcan-sql server, then you could choose the solution to enforce request must be https.
In yaml, you could setup options in enforce-https ( Will move out to the options for following Unite extension loaders dicussion after first extension loader finished), like below:

enforce-https:
   options:
      type: X_FORWARDED_PROTO,
      redirectMethods:
         - GET

The above detail options could see in sslify. Also we change the resolver options to type and add proto for CUSTOM type.

Otherwise, the all type shown below to match different reverse proxies:

export enum ResolverType {
  /* use local server to run https server, suit for local usage. */
  LOCAL = 'LOCAL',
  /*
   * RFC standard header (RFC7239) to carry information in a organized way for reverse proxy used.
   *  However, currently only little reverse proxies support it. e.g: nginx supported.
   *  refer: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
   *  refer: https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/
   */
  FORWARDED = 'FORWARDED',
  /*
   * X-Forwarded-Proto header flag is one of the de-facto standard (But not RFC standard) to check and enforce https or not, almost reverse proxies supported.
   * e.g: Heroku, GKE ingress, AWS ELB, nginx.
   */
  X_FORWARDED_PROTO = 'X_FORWARDED_PROTO',
  /*
   * if use Azure Application Request Routing as reverse proxy, then it use X-ARR-SSL header flag to check and enforce https.
   * refer: https://abhimantiwari.github.io/blog/ARR/
   */
  AZURE_ARR = 'AZURE_ARR',
  /* customize the header flag to check and enforce https, when use the type, need to define an custom header flag for checking and enforcing https */
  CUSTOM = 'CUSTOM',
}

Without Reverse Proxy

If you only would like to run in the local, you could set type to LOCAL and provide ssl options in the yaml:

ssl: 
   keyFile: './server.key'
   certFile: './server.cert'

enforce-https:
   options:
      type: LOCAL,

Then the server will open HTTP and HTTP server to make you redirect when request with HTTP to the server, the HTTP default port is 3000, the HTTPS default port is 3001

How To Test / Expected Results

For the test result, please see the below test cases that passed the unit test:

Serve Package

螢幕快照 2022-08-02 下午3 46 37

Commit Message

  • b5c89ae - feat(serve): add enforcing https middleware.
    • add EnforceHttpsMiddlewares to support enforcing https request.
    • add enforcing HTTPS middleware test cases.
    • add ssl-sslify package.
    • provide runServer method for "VulcanApplication" to start server for local https according to config provide ssl file and set LOCAL mode or not.
  • 9f17631 - fix(serve): make the local enforce https provide http and https server.
    • move the run server method from app to server class.
    • make the local enforce https provide http and https server.
    • update app test cases.

@kokokuo kokokuo changed the base branch from develop to feature/authenticator August 2, 2022 07:56
@kokokuo kokokuo changed the title Feature: Provide enforce HTTPS middleware [WIP] Feature: Provide enforce HTTPS middleware Aug 2, 2022
@kokokuo kokokuo force-pushed the feature/enforce-https branch 2 times, most recently from 5ed6c17 to 6738279 Compare August 2, 2022 09:39
@kokokuo kokokuo marked this pull request as ready for review August 2, 2022 09:41
@kokokuo kokokuo changed the title [WIP] Feature: Provide enforce HTTPS middleware Feature: Provide enforce HTTPS middleware Aug 2, 2022
@kokokuo kokokuo requested a review from oscar60310 August 2, 2022 09:41
@kokokuo kokokuo changed the title Feature: Provide enforce HTTPS middleware Feature: Add enforce HTTPS middleware Aug 2, 2022
@kokokuo kokokuo force-pushed the feature/authenticator branch 2 times, most recently from a22b01b to 02a19de Compare August 17, 2022 06:44
@kokokuo kokokuo force-pushed the feature/enforce-https branch from 6738279 to 9f17631 Compare August 17, 2022 07:57
@kokokuo
Copy link
Contributor Author

kokokuo commented Aug 17, 2022

Rebased done from #26 which based on #23

@kokokuo kokokuo force-pushed the feature/authenticator branch 3 times, most recently from 05e1464 to 6b094ef Compare August 19, 2022 01:25
@kokokuo kokokuo force-pushed the feature/enforce-https branch from 9f17631 to c35b4b2 Compare August 19, 2022 01:33
@codecov-commenter
Copy link

Codecov Report

Base: 91.78% // Head: 91.52% // Decreases project coverage by -0.25% ⚠️

Coverage data is based on head (c35b4b2) compared to base (6b094ef).
Patch coverage: 83.07% of modified lines in pull request are covered.

Additional details and impacted files
@@                    Coverage Diff                    @@
##           feature/authenticator      #27      +/-   ##
=========================================================
- Coverage                  91.78%   91.52%   -0.26%     
=========================================================
  Files                        192      193       +1     
  Lines                       2494     2548      +54     
  Branches                     293      304      +11     
=========================================================
+ Hits                        2289     2332      +43     
- Misses                       158      168      +10     
- Partials                      47       48       +1     
Impacted Files Coverage Δ
packages/serve/src/lib/server.ts 75.00% <61.90%> (-11.96%) ⬇️
...serve/src/lib/middleware/enforceHttpsMiddleware.ts 92.50% <92.50%> (ø)
packages/serve/src/lib/app.ts 83.33% <100.00%> (+2.68%) ⬆️
packages/serve/src/lib/middleware/index.ts 100.00% <100.00%> (ø)
...e/src/lib/middleware/response-format/middleware.ts 76.19% <0.00%> (-23.81%) ⬇️
...ges/serve/src/models/extensions/routeMiddleware.ts 91.66% <0.00%> (+16.66%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@kokokuo kokokuo force-pushed the feature/authenticator branch 5 times, most recently from 9f31a85 to c95709b Compare August 25, 2022 12:01
@kokokuo kokokuo force-pushed the feature/enforce-https branch from c35b4b2 to 8f5e50d Compare August 26, 2022 01:57
Copy link
Contributor

@oscar60310 oscar60310 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

*/
private runServer(app: VulcanApplication, port: number, httpsPort: number) {
const options = getEnforceHttpsOptions(this.config['enforce-https']);
if (options && this.config.ssl) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create https server without enforce-https middleware enabled?

Copy link
Contributor Author

@kokokuo kokokuo Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @oscar60310 for reviewing and suggestion, but I could not understand why we need also to create https server when enforcec-https options are disabled ? ( I need you tell more reason ~ )

In general, we usually open http server in the local machine, if user would like to create https server, it could use the reverse proxy ?

If you would like to create local https server, it should set the LOCAL undertans the enforce-https options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @oscar60310 thanks for discussing with me, after discussion, I have made the HTTPS server open when enforce-https options use defualt value ( default is LOCAL type with enabled` ).

Besides user setup the disable for enforce-https options, or the http server and https server will run both.

* @param port the http port for server start, default is 3000
* @param httpsPort the https port for https server start when you set "type" = LOCAL in "enforce-https" middleware and provide ssl file, default port is 3001
*/
public async start(port = 3000, httpsPort = 3001) {
Copy link
Contributor

@oscar60310 oscar60310 Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: We'll need to set https port here and the option of koa sslify, maybe we can find a way the share it. e.g. store them in config file ...

NIT: Would you update CLI too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @oscar60310 for reviewing and suggestion, I have moved the httpsPort setting into config under enforce-https options, because the https server is only created when enforce-https set LOCAL type.

Btw, after I moved to the config, seems CLI no need to update too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @oscar60310, thanks for discussing with me, I have also move the http port into config for reading!

Comment on lines 75 to 78
const options = {
key: fs.readFileSync(this.config.ssl.keyFile),
cert: fs.readFileSync(this.config.ssl.certFile),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @oscar60310 for reviewing and suggestion, also thanks for sharing the document to let me know we should also provide CA bundle chain!

Base automatically changed from feature/authenticator to develop August 30, 2022 05:55
- add "EnforceHttpsMiddleware" to support enforcing https request.
- add enforcing https middleware test cases.
- add "ssl-sslify" package.
- provide "runServer" method for "VulcanApplication" to start server for local https according config provide ssl file and set LOCAL mode or not.
- move the run server  method from app to server class.
- make the local enforce https provide http and https server.
- update app test cases.
@kokokuo kokokuo force-pushed the feature/enforce-https branch from 8f5e50d to 1a62af2 Compare August 30, 2022 06:23
- setup the https port by config when enable enforce-https with LOCAL type.
- make the https server open when enforce-https options is enabled and type is LOCAL
- add ca bundle file for creating https server.
@kokokuo kokokuo force-pushed the feature/enforce-https branch from 0351e1f to 1437c32 Compare August 31, 2022 04:02
@kokokuo
Copy link
Contributor Author

kokokuo commented Aug 31, 2022

Hi @oscar60310 thanks for reviewing, suggesting and discussed with me, the PR has been fixed, please check it :)

@oscar60310 oscar60310 merged commit a608f57 into develop Aug 31, 2022
@oscar60310 oscar60310 deleted the feature/enforce-https branch August 31, 2022 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants