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

Add rate limiter for login endpoint #4062

Merged
merged 8 commits into from Jun 16, 2023

Conversation

JohnNiang
Copy link
Member

@JohnNiang JohnNiang commented Jun 12, 2023

What type of PR is this?

/kind feature
/area core

What this PR does / why we need it:

This PR introduces https://github.com/resilience4j/resilience4j to archive the feature. The login endpoint has limited login failures at a rate of 3 per minute.

See #4044 for more.

Which issue(s) this PR fixes:

Fixes #4044

Special notes for your reviewer:

  1. Start Halo.
  2. Try to login with incorrect credential 4 times
  3. Check the response.

Does this PR introduce a user-facing change?

增加登录失败次数限制功能。

@f2c-ci-robot f2c-ci-robot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. labels Jun 12, 2023
@f2c-ci-robot f2c-ci-robot bot requested a review from guqing June 12, 2023 06:21
@f2c-ci-robot f2c-ci-robot bot added the area/core Issues or PRs related to the Halo Core label Jun 12, 2023
@f2c-ci-robot f2c-ci-robot bot requested a review from minliacom June 12, 2023 06:21
@codecov
Copy link

codecov bot commented Jun 12, 2023

Codecov Report

Merging #4062 (7ee2d77) into main (997a73d) will decrease coverage by 0.34%.
The diff coverage is 20.23%.

@@             Coverage Diff              @@
##               main    #4062      +/-   ##
============================================
- Coverage     60.56%   60.22%   -0.34%     
+ Complexity     2386     2385       -1     
============================================
  Files           356      357       +1     
  Lines         12356    12392      +36     
  Branches        894      892       -2     
============================================
- Hits           7483     7463      -20     
- Misses         4432     4492      +60     
+ Partials        441      437       -4     
Impacted Files Coverage Δ
.../java/run/halo/app/infra/exception/Exceptions.java 0.00% <0.00%> (ø)
...p/infra/exception/ThemeAlreadyExistsException.java 0.00% <0.00%> (ø)
...in/java/run/halo/app/plugin/BasePluginFactory.java 11.76% <0.00%> (ø)
...in/java/run/halo/app/plugin/HaloPluginManager.java 6.82% <0.00%> (-0.43%) ⬇️
...ntication/login/UsernamePasswordAuthenticator.java 35.55% <13.20%> (-16.30%) ⬇️
.../java/run/halo/app/infra/utils/IpAddressUtils.java 50.00% <61.53%> (+3.57%) ⬆️
...pp/core/extension/reconciler/PluginReconciler.java 53.14% <100.00%> (-0.11%) ⬇️

... and 5 files with indirect coverage changes

@JackyLiang522
Copy link
Contributor

once this PR is merged, i will open a PR adding rate limiter for the comment endpoint, as we discussed in #4044

@ruibaby ruibaby added this to the 2.7.x milestone Jun 14, 2023
@ruibaby
Copy link
Member

ruibaby commented Jun 14, 2023

How should the login page display error messages?

image

@JohnNiang
Copy link
Member Author

How should the login page display error messages?

image

Thanks for reminding me. I made some changes to support unified error response for login endpoint, please see:

{
  "type": "https://halo.run/probs/invalid-credential",
  "title": "无效凭据",
  "status": 401,
  "detail": "用户名或密码错误。",
  "instance": "http://localhost:8090/login",
  "requestId": "ab463fb8-431",
  "timestamp": "2023-06-14T07:51:09.529533Z"
}
{
  "type": "https://halo.run/probs/request-not-permitted",
  "title": "请求限制",
  "status": 429,
  "detail": "请求过于频繁,请稍候再试。",
  "instance": "http://localhost:8090/login",
  "requestId": "0b14362b-434",
  "timestamp": "2023-06-14T07:51:31.110746Z"
}

Copy link
Member

@ruibaby ruibaby left a comment

Choose a reason for hiding this comment

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

/lgtm

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Jun 15, 2023
Copy link
Member

@guqing guqing left a comment

Choose a reason for hiding this comment

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

/approve

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Jun 15, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: guqing

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jun 15, 2023
@f2c-ci-robot f2c-ci-robot bot removed lgtm Indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jun 16, 2023
Copy link
Member

@ruibaby ruibaby left a comment

Choose a reason for hiding this comment

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

/lgtm

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Jun 16, 2023
@f2c-ci-robot f2c-ci-robot bot merged commit 02369fb into halo-dev:main Jun 16, 2023
4 checks passed
@ruibaby ruibaby modified the milestones: 2.7.x, 2.7.0 Jun 19, 2023
@JohnNiang JohnNiang deleted the feat/rate-limit branch June 20, 2023 04:18
f2c-ci-robot bot pushed a commit that referenced this pull request Jun 21, 2023
…ted (#4101)

#### What type of PR is this?

/kind bug
/area core

#### What this PR does / why we need it:

This is a bug introduced from #4062. I have overridden onAuthenticationSuccess to create rate limiter in advance instead of invoking `securityContextRepository#save` before.

See #4099 (comment) for more.

#### Special notes for your reviewer:

1. Try to log in with incorrect password three times
2. Log in with correct password and check if the response headers contain `Set-Cookie`

#### Does this PR introduce a user-facing change?

```release-note
None
```
@JohnNiang JohnNiang mentioned this pull request Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/core Issues or PRs related to the Halo Core kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request to support rate limit mechanism
4 participants