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

feat(manager): add API throttling per account #1558

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(self):
self.public_port = 8000
self.private_port = 8000
self.max_request_size = int(os.getenv("MAX_REQUEST_SIZE_MB", "2"))
self.api_max_rps = int(os.getenv("API_MAX_RPS", "20")) # maximum requests per second and account

self.is_fedramp = strtobool(os.getenv("IS_FEDRAMP", "FALSE"))

Expand Down
1 change: 1 addition & 0 deletions conf/manager.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ DISABLE_RBAC=FALSE
GRANULAR_RBAC=FALSE
MAX_REQUEST_SIZE_MB=2
MAXIMUM_PAGE_SIZE=1000
API_MAX_RPS=100
10 changes: 10 additions & 0 deletions manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import connexion
from connexion.resolver import RestyResolver
from flask import abort
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from peewee import OperationalError
from prometheus_client import CollectorRegistry
from prometheus_client import generate_latest
Expand Down Expand Up @@ -46,6 +48,8 @@ def create_app(spec_files, wait_on_cyndi=True):
app.add_error_handler(MissingEntitlementException, forbidden_missing_entitlement)
app.add_error_handler(RbacException, forbidden_rbac)

limiter = Limiter(app=app.app, key_func=get_remote_address)

@app.app.route('/metrics', methods=['GET'])
def metrics(): # pylint: disable=unused-variable
# /metrics API shouldn't be visible in the API documentation, hence it's added here in the create_app step
Expand All @@ -69,6 +73,12 @@ def set_default_headers(response): # pylint: disable=unused-variable

return response

# Apply global throttling for all routes
@app.app.before_request
@limiter.limit(f"{CFG.api_max_rps} per second", key_func=lambda: connexion.request.headers.get("x-rh-identity"))
def account_level_throttle():
return

# This hook ensures that a connection is opened to handle any queries
# generated by the request.
@app.app.before_request
Expand Down
Loading
Loading