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

Handle unauthorized exceptions in a custom handler #1255

Closed
wants to merge 1 commit into from

Conversation

lubosmj
Copy link
Member

@lubosmj lubosmj commented Apr 11, 2023

closes #1254

@lubosmj lubosmj force-pushed the fix-keycloak-401 branch 2 times, most recently from 8080e46 to 431810f Compare April 11, 2023 12:55

REST_FRAMEWORK = settings.REST_FRAMEWORK
REST_FRAMEWORK.update(
{"EXCEPTION_HANDLER": "pulp_container.app.exceptions.unauthorized_exception_handler"}
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this set the exception handler for all plugins?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, that is a good question. I think it probably will. 😭

Copy link
Member Author

Choose a reason for hiding this comment

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

I can try cloning this to make it available only to pulp_container.

Comment on lines +2 to +11
from django.conf import settings

DRF_ACCESS_POLICY = {
"dynaconf_merge_unique": True,
"reusable_conditions": ["pulp_container.app.global_access_conditions"],
}

REST_FRAMEWORK = deepcopy(settings.REST_FRAMEWORK)
REST_FRAMEWORK.update(
{"EXCEPTION_HANDLER": "pulp_container.app.exceptions.unauthorized_exception_handler"}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
from django.conf import settings
DRF_ACCESS_POLICY = {
"dynaconf_merge_unique": True,
"reusable_conditions": ["pulp_container.app.global_access_conditions"],
}
REST_FRAMEWORK = deepcopy(settings.REST_FRAMEWORK)
REST_FRAMEWORK.update(
{"EXCEPTION_HANDLER": "pulp_container.app.exceptions.unauthorized_exception_handler"}
DRF_ACCESS_POLICY__reusable_conditions = "@merge_unique pulp_container.app.global_access_conditions"
REST_FRAMEWORK__EXCEPTION_HANDLER = "pulp_container.app.exceptions.unauthorized_exception_handler"

@@ -1,4 +1,12 @@
from copy import deepcopy
from django.conf import settings
Copy link
Member

@rochacbruno rochacbruno Apr 11, 2023

Choose a reason for hiding this comment

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

I don' t recommend importing django.conf.settings here, you can use dynaconf inline merging with @ and __ (as in my other comment)

DRF_ACCESS_POLICY__reusable_conditions = "@merge_unique pulp_container.app.global_access_conditions"
REST_FRAMEWORK__EXCEPTION_HANDLER = "pulp_container.app.exceptions.unauthorized_exception_handler"

Or you can add a pulp_container/app/dynaconf_hooks.py and then do any conditionals or updates there.

dynaconf_hooks.py

def post(settings):
    data = {"dynaconf_merge": True}
    data["REST_FRAMEWORK"]["EXCEPTION_HANDLER"] = "..."
    return data

The reason is that as settings is formed by the merging of multiple sources the final state is not the one you get grom django.conf at this point here.

@lubosmj
Copy link
Member Author

lubosmj commented Apr 11, 2023

I am closing this PR in favour of #1256

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.

Can't configure authentication on token API
3 participants