-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Moved test stuff to _/ rather than api/ - Moved some APISIX stuff to a middleware - Reworked logging to universally be "log" and to use "debug" (maybe not done yet tho) - Regenerated OpenAPI stuff
- Loading branch information
Showing
9 changed files
with
139 additions
and
95 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,27 @@ | ||
"""Private URLs for the system_meta app.""" | ||
|
||
from django.urls import re_path | ||
|
||
from system_meta.views import ( | ||
apisix_test_request, | ||
authed_traefik_test_request, | ||
traefik_test_request, | ||
) | ||
|
||
urlpatterns = [ | ||
re_path( | ||
r"^apisix_test_request/$", | ||
apisix_test_request, | ||
name="apisix_test_request", | ||
), | ||
re_path( | ||
r"^traefik_test_request/$", | ||
traefik_test_request, | ||
name="traefik_test_request", | ||
), | ||
re_path( | ||
r"^authed_traefik_test_request/$", | ||
authed_traefik_test_request, | ||
name="authed_traefik_test_request", | ||
), | ||
] |
This file contains 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 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 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,63 @@ | ||
"""Middleware for Unified Ecommerce.""" | ||
|
||
import base64 | ||
import json | ||
import logging | ||
|
||
from django.contrib.auth.middleware import RemoteUserMiddleware | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class ApisixUserMiddleware: | ||
"""Checks for and processes APISIX-specific headers.""" | ||
|
||
def decode_apisix_headers(self, request): | ||
"""Decode the APISIX-specific headers.""" | ||
user_info = request.META.get("HTTP_X_USERINFO", False) | ||
|
||
if not user_info: | ||
return None | ||
|
||
try: | ||
apisix_result = json.loads(base64.b64decode(user_info)) | ||
except json.JSONDecodeError: | ||
log_message = ( | ||
"ApisixUserMiddleware: Result from HTTP_X_USERINFO " | ||
f"is not valid JSON: {user_info}" | ||
) | ||
log.debug(log_message) | ||
|
||
return None | ||
|
||
log_message = f"ApisixUserMiddleware: Got {apisix_result}" | ||
log.debug(log_message) | ||
|
||
return { | ||
"email": apisix_result["email"], | ||
"preferred_username": apisix_result["preferred_username"], | ||
"given_name": apisix_result["given_name"], | ||
"family_name": apisix_result["family_name"], | ||
} | ||
|
||
def __init__(self, get_response): | ||
"""Initialize the middleware.""" | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
""" | ||
Process any APISIX headers and put them in the request. | ||
If valid data is found, then it will be put into the "api_gateway_userdata" | ||
attribute of the request. Otherwise, it'll be set to None. | ||
""" | ||
|
||
request.api_gateway_userdata = self.decode_apisix_headers(request) | ||
|
||
return self.get_response(request) | ||
|
||
|
||
class ForwardUserMiddleware(RemoteUserMiddleware): | ||
"""RemoteUserMiddleware, but looks at X-Forwarded-User""" | ||
|
||
header = "HTTP_X_FORWARDED_USER" |
This file contains 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 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