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

x-pack/filebeat/input/entityanalytics/provider/okta: Rate limiting fixes #41583

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

chrisberkhout
Copy link
Contributor

@chrisberkhout chrisberkhout commented Nov 11, 2024

Proposed commit message

x-pack/filebeat/input/entityanalytics/provider/okta: Rate limiting fixes

- Separate rate limits by endpoint.
- Stop requests until reset when `x-rate-limit-remaining: 0`.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Discussion

Can be reviewed commit-by-commit.

The reason a new rate.Limiter needs to be created when x-rate-limit-remaining: 0 is that while rate.Limiter allows updating of the rate and burst, it doesn't allow clearing of existing tokens.

Disruptive User Impact

None.

Related issues

@chrisberkhout chrisberkhout added bugfix Team:Security-Service Integrations Security Service Integrations Team labels Nov 11, 2024
@chrisberkhout chrisberkhout self-assigned this Nov 11, 2024
@chrisberkhout chrisberkhout requested a review from a team as a code owner November 11, 2024 12:26
@elasticmachine
Copy link
Collaborator

Pinging @elastic/security-service-integrations (Team:Security-Service Integrations)

@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Nov 11, 2024
Copy link
Contributor

mergify bot commented Nov 11, 2024

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @chrisberkhout? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit

Copy link
Contributor

mergify bot commented Nov 11, 2024

backport-8.x has been added to help with the transition to the new branch 8.x.
If you don't need it please use backport-skip label and remove the backport-8.x label.

@mergify mergify bot added the backport-8.x Automated backport to the 8.x branch with mergify label Nov 11, 2024
@chrisberkhout chrisberkhout added backport-8.15 Automated backport to the 8.15 branch with mergify backport-8.16 Automated backport with mergify labels Nov 11, 2024
@chrisberkhout chrisberkhout force-pushed the okta-rate-limiting-logic branch 3 times, most recently from 3c56801 to 317c45e Compare November 11, 2024 17:27
endpoint = "/api/v1/users/{user}"
path = strings.Replace(endpoint, "{user}", user, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we not just use path.Join here rather than a search/replace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do also need endpoint now, without the ID inserted, for the rate limiter.

I think it's good that the final path is derived from the endpoint pattern. If there's a change they're less likely to diverge.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't particularly like it, but OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. Leaving it like that.

Comment on lines 195 to 196
var endpoint string
var path string
Copy link
Contributor

Choose a reason for hiding this comment

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

	var endpoint, path string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

u := &url.URL{
Scheme: "https",
Host: host,
Path: path.Join(endpoint, user, "factors"),
Path: path,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think keeping the use if path.Join would be better (also below).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As above. Leaving it.

}
if limiter.lim.Burst() != 1 {
t.Errorf("unexpected burst: got:%d want:1", limiter.lim.Burst())
for _, l := range *limiter {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this needs a comment, or preferably since we have constructed the system here, use the key that we expect to exist using limiter[<key>].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went with the comment.

type RateLimiter struct {
lim *rate.Limiter
}
type RateLimiter map[string]*rate.Limiter

func NewRateLimiter() *RateLimiter {
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not need to be on a pointer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to:

func NewRateLimiter() RateLimiter

@@ -444,7 +444,7 @@ var nextTests = []struct {
func TestNext(t *testing.T) {
for i, test := range nextTests {
got, err := Next(test.header)
if err != test.wantErr {
if !errors.Is(err, test.wantErr) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not right. The two cases are nil and io.EOF and the latter is never wrapped.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted.

@@ -478,7 +478,7 @@ func (p *oktaInput) doFetchUsers(ctx context.Context, state *stateStore, fullSyn

next, err := okta.Next(h)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment here and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted 3 cases.

r.Update(endpoint, headers, window, logp.L())
err := r.Update(endpoint, headers, window, logp.L())
if err != nil {
t.Errorf("Update returned error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

t.Errorf("unexpected error from Update(): %v", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 401 to 403
if _, copyErr := io.Copy(io.Discard, resp.Body); copyErr != nil {
err = errors.Join(err, copyErr)
}
Copy link
Contributor

@efd6 efd6 Nov 11, 2024

Choose a reason for hiding this comment

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

If I were going to do this, I would have it as

		_, cerr := io.Copy(io.Discard, resp.Body)
		return nil, nil, errors.Join(err, cerr)

but we are just discarding the the body to recover the conn's usability, so this is really only an optimisation and as such should not change the behaviour qualitatively; I would leave this as it was originally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted it.

return &r
}

func (r RateLimiter) get(path string) *rate.Limiter {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (r RateLimiter) get(path string) *rate.Limiter {
func (r RateLimiter) Limiter(path string) *rate.Limiter {

with necessary changes elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or unexported? I think this should only be used internally and for testing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, unexported is fine. If this becomes something we need to share later, we can export it then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

mergify bot commented Nov 18, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b okta-rate-limiting-logic upstream/okta-rate-limiting-logic
git merge upstream/main
git push upstream okta-rate-limiting-logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.x Automated backport to the 8.x branch with mergify backport-8.15 Automated backport to the 8.15 branch with mergify backport-8.16 Automated backport with mergify bugfix Team:Security-Service Integrations Security Service Integrations Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants