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

allows authorized clients to bypass rate limiter #599

Merged
merged 4 commits into from
Aug 15, 2023
Merged

Conversation

brunocalza
Copy link
Collaborator

@brunocalza brunocalza commented Jul 25, 2023

Summary

This PR adds a new config for the HTTP rate limiter: ApiKey. With that, authorized clients (clients that provide the same key) will not be rate limited.

Context

Our Studio application is being rate limited fairly easily.

Implementation overview

Adds a new config to be configured via env. Makes a change to the rate limiter middleware to ignore the authorized clients

@@ -28,6 +29,9 @@ func TestLimit1IP(t *testing.T) {

{name: "success", callRPS: 100, limitRPS: 500, forwardedFor: false},
{name: "block-me", callRPS: 1000, limitRPS: 500, forwardedFor: false},

{name: "allow-me", callRPS: 1000, limitRPS: 500, forwardedFor: false, allow: true},
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

adds two more test cases where rps is greater than limit but it never gets 429

Copy link
Collaborator

Choose a reason for hiding this comment

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

looks good

@@ -77,3 +81,62 @@ func extractClientIP(r *http.Request) (string, error) {
}
return ip, nil
}

type middleware struct {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

implementation borrowed from https://github.com/sethvargo/go-limiter/blob/main/httplimit/middleware.go with an extra tweak

return
}

// skip rate limiting checks if key is in allowlist
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is the additional change

cmd/api/main.go Outdated
@@ -450,11 +450,14 @@ func createAPIServer(
return nil, fmt.Errorf("parsing http ratelimiter interval: %s", err)
}

allowList := strings.Split(httpConfig.AllowList, ",")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

with this we can provide a list of addresses

@brunocalza brunocalza self-assigned this Jul 25, 2023
@brunocalza brunocalza changed the base branch from main to staging July 25, 2023 20:46
@brunocalza brunocalza marked this pull request as ready for review July 25, 2023 21:06
Interval time.Duration
MaxRPI uint64
Interval time.Duration
AllowList []string
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@@ -51,6 +51,7 @@ type HTTPConfig struct {

RateLimInterval string `default:"1s"`
MaxRequestPerInterval uint64 `default:"10"`
AllowList string `default:""`
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe a comment with an example of a list of IP addrs would be helpful here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good idea 👍

avichalp
avichalp previously approved these changes Jul 26, 2023
Copy link
Collaborator

@avichalp avichalp left a comment

Choose a reason for hiding this comment

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

LGTM!

@brunocalza brunocalza changed the title adds an allowlist for rate limiter allows authorized clients to bypass rate limiter Aug 11, 2023
@brunocalza brunocalza changed the base branch from staging to main August 11, 2023 15:12
@brunocalza brunocalza dismissed avichalp’s stale review August 11, 2023 15:12

The base branch was changed.

Signed-off-by: Bruno Calza <brunoangelicalza@gmail.com>
Signed-off-by: Bruno Calza <brunoangelicalza@gmail.com>
Signed-off-by: Bruno Calza <brunoangelicalza@gmail.com>
}

// skip rate limiting checks if secret key is provided
if key := r.Header.Get("Secret-Key"); key != "" && m.apiKey != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we name the header "Api-Key" instead of "Secret-Key" to make it consistent with the code?

Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm maybe i get your point now. this is the special "API key" that can bypass the checks. so it makes sense.

Copy link
Collaborator

@avichalp avichalp left a comment

Choose a reason for hiding this comment

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

Looks good!

Signed-off-by: Bruno Calza <brunoangelicalza@gmail.com>
@brunocalza brunocalza merged commit 0b122ac into staging Aug 15, 2023
5 checks passed
@brunocalza brunocalza deleted the bcalza/ratelim branch August 15, 2023 13:07
@brunocalza brunocalza mentioned this pull request Aug 21, 2023
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.

None yet

2 participants