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

Configurable Authorization Request Limits #9

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ hana:
access_token_ttl: 86400 # 1 day
refresh_token_ttl: 604800 # 1 week
requests_per_minute: 60
auth_requests_per_minute: 6 # This counts valid and invalid requests from an IP address
access_token_secret: a800445648142061fc238d1f84e96200da87f4f9fa7835cac90db8b4391b117b
refresh_token_secret: 833d369ac73d883123743a44b4a7fe21203cffc956f4c8fec712e71aafa8e1aa
fastapi_title: "My HANA API Host"
Expand Down
7 changes: 4 additions & 3 deletions neon_hana/auth/client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, config: dict):
self._access_secret = config.get("access_token_secret")
self._refresh_secret = config.get("refresh_token_secret")
self._rpm = config.get("requests_per_minute", 60)
self._auth_rpm = config.get("auth_requests_per_minute", 6)
self._disable_auth = config.get("disable_auth")
self._jwt_algo = "HS256"

Expand All @@ -71,12 +72,12 @@ def check_auth_request(self, client_id: str, username: str,

if not self.rate_limiter.get_all_buckets(f"auth{origin_ip}"):
self.rate_limiter.add_bucket(f"auth{origin_ip}",
TokenBucket(replenish_time=30,
max_tokens=3))
TokenBucket(replenish_time=60,
max_tokens=self._auth_rpm))
if not self.rate_limiter.consume(f"auth{origin_ip}"):
raise HTTPException(status_code=429,
detail=f"Too many auth requests from: "
f"{origin_ip}. Wait 30 seconds.")
f"{origin_ip}. Wait 1 minute.")
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, giving the exact time to wait helps people infer the rate limit, and they can script it more accurately to stay within the window. No matter what the approach is, it's probably best to make sure there's monitoring in place for multiple failed requests inside a time window.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added some logic and update the return to give the time until the rate-limit counter resets.

When we implement some actual auth validation I agree we should also monitor failed requests and impose some stricter limits for bad credentials


if username != "guest":
# TODO: Validate password here
Expand Down
Loading