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

Add JWT roles based throttling mechanism #4506

Merged
merged 7 commits into from
Dec 4, 2024

Conversation

zawan-ila
Copy link
Contributor

@zawan-ila zawan-ila commented Dec 3, 2024

PROD-4139

This PR adds the ADR for JWT-role based throttling. The code is mostly copied from 4394 Thanks to @adamstankiewicz for the idea and the POC!

course_discovery/apps/api/tests/jwt_utils.py Show resolved Hide resolved
course_discovery/apps/core/throttles.py Outdated Show resolved Hide resolved

# Keywords that will be searched for inside the `roles` key of the JWT in case a user uses JWT authentication.
# If the keyword is found, the user has more lenient throttling limits.
ENHANCED_THROTTLE_JWT_ROLE_KEYWORDS = ['enterprise']
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unsure if this should be a list or string.

Copy link
Member

Choose a reason for hiding this comment

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

I think keeping this as a list makes sense, as it's fairly generic as is and could theoretically be used for non-enterprise purposes, too.

Copy link
Member

@adamstankiewicz adamstankiewicz left a comment

Choose a reason for hiding this comment

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

LGTM, with a few minor nits/suggestions to consider. Edit: nice job providing an ADR to go along with the change as well!

Note, it looks like CI is currently failing at the moment.


# Keywords that will be searched for inside the `roles` key of the JWT in case a user uses JWT authentication.
# If the keyword is found, the user has more lenient throttling limits.
ENHANCED_THROTTLE_JWT_ROLE_KEYWORDS = ['enterprise']
Copy link
Member

Choose a reason for hiding this comment

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

I think keeping this as a list makes sense, as it's fairly generic as is and could theoretically be used for non-enterprise purposes, too.

@@ -16,6 +18,23 @@ def throttling_cache():
return caches['default']


def is_enterprise_user(request):
Copy link
Member

Choose a reason for hiding this comment

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

nit: if using settings (i.e., settings.ENHANCED_THROTTLE_JWT_ROLE_KEYWORDS and settings.ENHANCED_THROTTLE_LIMIT, where these settings could theoretically be configured for other non-enterprise use cases, too, might it make sense to rename this method more generically, e.g. is_priviledged_user_with_enhanced_throttle or similar?

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.


# Keywords that will be searched for inside the `roles` key of the JWT in case a user uses JWT authentication.
# If the keyword is found, the user has more lenient throttling limits.
ENHANCED_THROTTLE_JWT_ROLE_KEYWORDS = ['enterprise']
Copy link
Member

Choose a reason for hiding this comment

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

Also worth considering: it might make sense to define the base setting as an empty list (i.e., no change for the Open edX platform), and configure the setting via edx-internal such that the enhanced throttle rate change is only applicable for the edX.org instance. The ENHANCED_THROTTLE_LIMIT setting could stay hardcoded, though.

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 +21 to +22
in the JWT to identify them. Enterprise customers are guaranteed to have one of a small number of fixed roles assigned to them.
Once we identify that an incoming request's user has one of these roles in their JWT, we will enable higher rate limits for them.
Copy link
Member

Choose a reason for hiding this comment

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

It may make sense to expand on the decision to capture the configurable settings in the ADR related to the change, and while the immediate use case is for enterprise learners, the approach is generic for non-enterprise use cases, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Documented the two new settings and their usage.

@zawan-ila zawan-ila merged commit e5584c0 into openedx:master Dec 4, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants