-
Notifications
You must be signed in to change notification settings - Fork 55
feat: implement plan-based API rate limits #696
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
Merged
Changes from 10 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
6e67085
feat: implement plan-based API rate limits
rohan-chaturvedi ac1c030
feat: disable default throttling for self-hosted environments
rohan-chaturvedi 427d080
fix: apply default rate to unauthenticated requests
rohan-chaturvedi 88b0d4f
feat: add test coverage for rate limits
rohan-chaturvedi e9418a6
feat: add throttling selectively to public APIs
rohan-chaturvedi 94da683
Merge branch 'main' into feat--api-rate-limits
rohan-chaturvedi 6072d8d
chore: add pytest-django
rohan-chaturvedi d53d29e
chore: cleanup
rohan-chaturvedi 5b36b44
fix: centralize redis env var parsing
rohan-chaturvedi d03cd04
Merge branch 'main' into feat--api-rate-limits
nimish-ks 7775600
fix: add support for legacy service tokens in PlanBasedRateThrottle
rohan-chaturvedi c62b692
Merge branch 'main' into feat--api-rate-limits
rohan-chaturvedi fbc4cd3
fix: update rate limits and Redis SSL configuration in settings.py
nimish-ks 2a9f219
Merge branch 'main' into feat--api-rate-limits
nimish-ks 194808f
feat: cast pgport as integer
nimish-ks 13a39b4
feat: cast redis port as integrer
nimish-ks debce9f
feat: enhance Redis SSL configuration in settings.py
nimish-ks 9fb5869
feat: enhance Redis authentication configuration in settings.py
nimish-ks 9eb321a
Merge branch 'main' into feat--api-rate-limits
nimish-ks 7a8c5bb
feat: update Redis username handling in settings.py
nimish-ks 9658ea7
feat: enhance PostgreSQL SSL configuration in settings.py
nimish-ks 5980d35
refactor: clean up settings.py by removing commented-out code
nimish-ks 6874524
chore: update Dockerfile to include ca-certificates
nimish-ks 2323d30
feat: add AWS RDS CA bundle to Dockerfile
nimish-ks 22ea5b6
refactor: remove redundant SSL validation in settings.py
nimish-ks 3b68e4c
refactor: update SSL configuration in settings.py
nimish-ks dc9de3e
refactor: improve SSL configuration handling in settings.py
nimish-ks d65d36c
Merge branch 'main' into feat--api-rate-limits
rohan-chaturvedi 00548fa
Merge branch 'main' into feat--api-rate-limits
nimish-ks 9c72103
Merge branch 'main' into feat--api-rate-limits
nimish-ks f843ec5
refactor: rename REDIS_USERNAME to REDIS_USER for consistency in settβ¦
nimish-ks cca7e66
Merge branch 'main' into feat--api-rate-limits
nimish-ks 7314530
fix: update Redis cache location configuration to remove database index
nimish-ks 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| from rest_framework.throttling import SimpleRateThrottle | ||
| from django.conf import settings | ||
|
|
||
| CLOUD_HOSTED = settings.APP_HOST == "cloud" | ||
|
|
||
|
|
||
| class PlanBasedRateThrottle(SimpleRateThrottle): | ||
| """ | ||
| Limits the rate of API calls based on the Organisation's plan. | ||
| Uses the pre-fetched organisation data from request.auth to avoid DB lookups. | ||
| """ | ||
|
|
||
| scope = "plan_based" | ||
|
|
||
| def get_cache_key(self, request, view): | ||
| # Identify the user or service account | ||
| ident = self.get_ident(request) | ||
|
|
||
| if request.user.is_authenticated and request.auth: | ||
| if request.auth.get("org_member"): | ||
| ident = f"user_{request.auth['org_member'].id}" | ||
| elif request.auth.get("service_account"): | ||
| ident = f"sa_{request.auth['service_account'].id}" | ||
| else: | ||
| ident = f"anon_{ident}" | ||
nimish-ks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return self.cache_format % {"scope": self.scope, "ident": ident} | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def allow_request(self, request, view): | ||
| """ | ||
| Override allow_request to dynamically set the rate based on the request user's plan. | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| # Default fallback (reads from REST_FRAMEWORK['DEFAULT_THROTTLE_RATES']['plan_based']) | ||
| new_rate = self.get_rate() | ||
|
|
||
| if request.user.is_authenticated and request.auth: | ||
| env = request.auth.get("environment") | ||
| if env: | ||
| try: | ||
| plan = env.app.organisation.plan | ||
| new_rate = self.get_rate_for_plan(plan) | ||
| except AttributeError: | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pass | ||
rohan-chaturvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Update the throttle configuration for this specific request | ||
| self.rate = new_rate | ||
| self.num_requests, self.duration = self.parse_rate(self.rate) | ||
|
|
||
| return super().allow_request(request, view) | ||
|
|
||
| @staticmethod | ||
| def get_rate_for_plan(plan): | ||
| # If self-hosted return the default rate limit. If not set, this will disable throttling | ||
| if not CLOUD_HOSTED: | ||
| return settings.PLAN_RATE_LIMITS["DEFAULT"] | ||
| return settings.PLAN_RATE_LIMITS.get(plan, settings.PLAN_RATE_LIMITS["DEFAULT"]) | ||
nimish-ks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,23 @@ | ||
| import os | ||
| import django | ||
|
|
||
| # Set environment variables required for settings.py to import successfully | ||
| os.environ.setdefault("ALLOWED_HOSTS", "localhost") | ||
| os.environ.setdefault("ALLOWED_ORIGINS", "http://localhost") | ||
|
|
||
| # Set dummy Redis values so settings.py generates a valid URL (e.g. redis://localhost:6379/1) | ||
| os.environ.setdefault("REDIS_HOST", "localhost") | ||
| os.environ.setdefault("REDIS_PORT", "6379") | ||
|
|
||
| # Set dummy database config | ||
| os.environ.setdefault("DATABASE_HOST", "localhost") | ||
| os.environ.setdefault("DATABASE_PORT", "5432") | ||
| os.environ.setdefault("DATABASE_NAME", "dummy_db") | ||
| os.environ.setdefault("DATABASE_USER", "dummy_user") | ||
| os.environ.setdefault("DATABASE_PASSWORD", "dummy_password") | ||
|
|
||
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") | ||
|
|
||
|
|
||
| def pytest_configure(): | ||
| django.setup() |
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pytest==8.3.4 | ||
| pytest-django==4.11.1 | ||
| pytest-cov==7.0.0 | ||
| Faker==37.4.0 | ||
| colorama==0.4.6 | ||
|
|
||
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,2 @@ | ||
| [pytest] | ||
| python_files = tests.py test_*.py *_tests.py |
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.