Add rate limit to Duo direct access token endpoints

What does this MR do and why?

This MR adds a ip based rate limit to Duo direct access token endpoints

It's introduced behind two default-off feature flags so it can be rolled out in stages:

  • ai_users_per_ip_limit records and logs requests over the limit
  • enforce_ai_users_per_ip_limit returns 429 when the limit is reached

Related to https://gitlab.com/groups/gitlab-org/-/work_items/24032

References

Screenshots or screen recordings

Before After

How to set up and validate locally

Create tokens

Feature.enable(:ai_users_per_ip_limit)   # counting + logging only
Feature.disable(:enforce_ai_users_per_ip_limit)

tokens = (1..6).map do |i|
  user = User.find_by(username: "ipfree#{i}") ||
    Users::CreateService.new(User.admins.first,
      username: "ipfree#{i}", name: "IP Free #{i}", email: "ipfree#{i}@example.com",
      password: SecureRandom.hex(16), skip_confirmation: true,
      organization_id: Organizations::Organization.first.id).execute.payload[:user]
  user.personal_access_tokens.create!(name: 'ip-test', scopes: [:api],
    expires_at: 1.week.from_now, organization: Organizations::Organization.first).token
end
puts tokens

Call endpoint

for t in <token1> <token2> <token3> <token4> <token5> <token6>; do
  curl -s -o /dev/null -D - -X POST -H "PRIVATE-TOKEN: $t" \
    http://gdk.test:3000/api/v4/ai/duo_workflows/direct_access | grep -iE '^HTTP|ratelimit-name|retry-after'
done

You should get 403

Call endpoint again

You should get 429

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Halil Coban

Merge request reports

Loading
Loading