-
Notifications
You must be signed in to change notification settings - Fork 2.1k
athena audit logs - query rate limiter #24918
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
Merged
tobiaszheller
merged 1 commit into
master
from
tobiaszheller/auditevents-athena-ratelimit-query
Apr 28, 2023
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
| // Copyright 2023 Gravitational, Inc | ||
| // | ||
| // 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 events | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "golang.org/x/time/rate" | ||
|
|
||
| "github.com/gravitational/teleport/api/types" | ||
| apievents "github.com/gravitational/teleport/api/types/events" | ||
| ) | ||
|
|
||
| // SearchEventsLimiter allows to wrap any AuditLogger with rate limit on | ||
| // search events endpoints. | ||
| // Note it share limiter for both SearchEvents and SearchSessionEvents. | ||
| type SearchEventsLimiter struct { | ||
| limiter *rate.Limiter | ||
| AuditLogger | ||
| } | ||
|
|
||
| // SearchEventsLimiterConfig is configuration for SearchEventsLimiter. | ||
| type SearchEventsLimiterConfig struct { | ||
| // RefillTime determines the duration of time between the addition of tokens to the bucket. | ||
| RefillTime time.Duration | ||
| // RefillAmount is the number of tokens that are added to the bucket during interval | ||
| // specified by RefillTime. | ||
| RefillAmount int | ||
| // Burst defines number of available tokens. It's initially full and refilled | ||
| // based on RefillAmount and RefillTime. | ||
| Burst int | ||
| // AuditLogger is auditLogger that will be wrapped with limiter on search endpoints. | ||
| AuditLogger AuditLogger | ||
| } | ||
|
|
||
| func (cfg *SearchEventsLimiterConfig) CheckAndSetDefaults() error { | ||
| if cfg.AuditLogger == nil { | ||
| return trace.BadParameter("empty auditLogger") | ||
| } | ||
| if cfg.Burst <= 0 { | ||
| return trace.BadParameter("Burst cannot be less or equal to 0") | ||
| } | ||
| if cfg.RefillAmount <= 0 { | ||
| return trace.BadParameter("RefillAmount cannot be less or equal to 0") | ||
| } | ||
| if cfg.RefillTime == 0 { | ||
| // Default to seconds so it can be just used as rate. | ||
| cfg.RefillTime = time.Second | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // NewSearchEventLimiter returns instance of new SearchEventsLimiter. | ||
| func NewSearchEventLimiter(cfg SearchEventsLimiterConfig) (*SearchEventsLimiter, error) { | ||
| if err := cfg.CheckAndSetDefaults(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| return &SearchEventsLimiter{ | ||
| limiter: rate.NewLimiter(rate.Every(cfg.RefillTime/time.Duration(cfg.RefillAmount)), cfg.Burst), | ||
| AuditLogger: cfg.AuditLogger, | ||
| }, nil | ||
| } | ||
|
|
||
| func (s *SearchEventsLimiter) SearchEvents(fromUTC, toUTC time.Time, namespace string, | ||
| eventTypes []string, limit int, order types.EventOrder, startKey string, | ||
| ) ([]apievents.AuditEvent, string, error) { | ||
| if !s.limiter.Allow() { | ||
| return nil, "", trace.LimitExceeded("rate limit exceeded for searching events") | ||
| } | ||
| out, keyset, err := s.AuditLogger.SearchEvents(fromUTC, toUTC, namespace, eventTypes, limit, order, startKey) | ||
| return out, keyset, trace.Wrap(err) | ||
| } | ||
|
|
||
| func (s *SearchEventsLimiter) SearchSessionEvents(fromUTC, toUTC time.Time, limit int, | ||
| order types.EventOrder, startKey string, cond *types.WhereExpr, sessionID string, | ||
| ) ([]apievents.AuditEvent, string, error) { | ||
| if !s.limiter.Allow() { | ||
| return nil, "", trace.LimitExceeded("rate limit exceeded for searching events") | ||
| } | ||
| out, keyset, err := s.AuditLogger.SearchSessionEvents(fromUTC, toUTC, limit, order, startKey, cond, sessionID) | ||
| return out, keyset, trace.Wrap(err) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.