-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib/limiter: remove the Oxy rate limiter (#47257)
I've vendored in parts of gravitational/oxy under the lib/limiter/internal/ratelimit package, taking the opportunity to convert to slog and replace mailgun dependencies (ttlmap, timetools) with Teleport equivalents. This commit also removes the max_users limiter option. This setting never actually did what it claims to do. The max_users option was passed in as a capacity to a TTL map, but the TTL map doesn't reject new entries when the capacity is exceeded, it just removes old entries to make room. Removing old entries from the TTL map did not enforce any sort of user limit - it has the opposite effect. It "resets" existing limits for the old user, effectively un-limiting them! In practice, we've never actually been able to enforce a maximum number of simultaneous users, so rather than changing the behavior I've opted to remove the field from everything other than the config file.
- Loading branch information
Showing
25 changed files
with
616 additions
and
207 deletions.
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
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
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
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
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,4 @@ | ||
# ratelimit | ||
|
||
The code in this directory is derived from https://github.com/gravitational/oxy | ||
and is available under an Apache 2.0 license. |
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,60 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package ratelimit | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func ServeHTTPError(w http.ResponseWriter, r *http.Request, err error) { | ||
var le *limitExceededError | ||
if errors.As(err, &le) { | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After#delay-seconds | ||
w.Header().Set("Retry-After", strconv.Itoa(int(le.delay.Seconds())+1)) | ||
w.WriteHeader(http.StatusTooManyRequests) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
status := http.StatusInternalServerError | ||
var netError net.Error | ||
|
||
if errors.Is(err, io.EOF) { | ||
status = http.StatusBadGateway | ||
} else if errors.As(err, &netError) { | ||
if netError.Timeout() { | ||
status = http.StatusGatewayTimeout | ||
} else { | ||
status = http.StatusBadGateway | ||
} | ||
} | ||
|
||
http.Error(w, http.StatusText(status), status) | ||
} | ||
|
||
func ExtractClientIP(r *http.Request) (string, error) { | ||
// TODO: use net.SplitHostPort to be compatible with IPv6 | ||
token, _, _ := strings.Cut(r.RemoteAddr, ":") | ||
if token == "" { | ||
return "", errors.New("failed to extract source IP") | ||
} | ||
|
||
return token, nil | ||
} |
Oops, something went wrong.