From 827443480d4fc29406ba86239e3fa32b0ba73cb4 Mon Sep 17 00:00:00 2001 From: Mo Sohani Date: Thu, 11 Jul 2024 09:35:24 -0400 Subject: [PATCH] Local status template --- tdrs-backend/tdpservice/settings/local.py | 2 + .../users/api/login_redirect_oidc.py | 21 ++++++--- .../test/test_api/test_login_redirect_oidc.py | 44 +++++++++++++++++++ tdrs-frontend/Dockerfile | 1 + tdrs-frontend/nginx/local/locations.conf | 8 ++++ tdrs-frontend/nginx/src/50x.html | 17 +++++++ 6 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tdrs-backend/tdpservice/users/test/test_api/test_login_redirect_oidc.py create mode 100644 tdrs-frontend/nginx/src/50x.html diff --git a/tdrs-backend/tdpservice/settings/local.py b/tdrs-backend/tdpservice/settings/local.py index 171608fe5..198f1ced4 100644 --- a/tdrs-backend/tdpservice/settings/local.py +++ b/tdrs-backend/tdpservice/settings/local.py @@ -43,3 +43,5 @@ class Local(Common): } REDIS_SERVER_LOCAL = bool(strtobool(os.getenv("REDIS_SERVER_LOCAL", "TRUE"))) + + AMS_CONFIGURATION_ENDPOINT = "http://localhost:8080/.well-known/openid-configuration" diff --git a/tdrs-backend/tdpservice/users/api/login_redirect_oidc.py b/tdrs-backend/tdpservice/users/api/login_redirect_oidc.py index 585c0ff47..192757a0e 100644 --- a/tdrs-backend/tdpservice/users/api/login_redirect_oidc.py +++ b/tdrs-backend/tdpservice/users/api/login_redirect_oidc.py @@ -7,7 +7,7 @@ from urllib.parse import quote_plus, urlencode from django.conf import settings -from django.http import HttpResponseRedirect +from django.http import HttpResponseRedirect, HttpResponse from django.views.generic.base import RedirectView logger = logging.getLogger(__name__) @@ -93,19 +93,30 @@ def get_ams_configuration(): Includes currently published URLs for authorization, token, etc. """ + return None r = requests.get(settings.AMS_CONFIGURATION_ENDPOINT) - data = r.json() - return data + if r.status_code != 200: + logger.error( + f"Failed to get AMS configuration: {r.status_code} - {r.text}" + ) + return None + else: + data = r.json() + return data def get(self, request, *args, **kwargs): """Handle login workflow based on request origin.""" # Create state and nonce to track requests + logger.info("--------------- 1") state = secrets.token_hex(32) nonce = secrets.token_hex(32) - + logger.info("--------------- 2") """Get request and manage login information with AMS OpenID.""" configuration = self.get_ams_configuration() - + logger.info("--------------- 3") + if not configuration: + return HttpResponse("Failed to get AMS configuration", status=500) + logger.info("--------------- 4") auth_params = { "client_id": settings.AMS_CLIENT_ID, "nonce": nonce, diff --git a/tdrs-backend/tdpservice/users/test/test_api/test_login_redirect_oidc.py b/tdrs-backend/tdpservice/users/test/test_api/test_login_redirect_oidc.py new file mode 100644 index 000000000..42b7ac0c4 --- /dev/null +++ b/tdrs-backend/tdpservice/users/test/test_api/test_login_redirect_oidc.py @@ -0,0 +1,44 @@ +#LoginRedirectAMS +from unittest import mock +from tdpservice.users.api.login_redirect_oidc import LoginRedirectAMS + +@mock.patch("requests.get") +def test_get_ams_configuration(requests_get_mock): + """Test the LoginRedirectAMS class.""" + requests_get_mock.return_value.status_code = 200 + requests_get_mock.return_value.json.return_value = {"key": "test"} + returned_value = LoginRedirectAMS.get_ams_configuration() + assert returned_value == {"key": "test"} + + # Test if the configuration is not returned + requests_get_mock.return_value.status_code = 500 + returned_value = LoginRedirectAMS.get_ams_configuration() + assert returned_value is None + +@mock.patch("requests.get") +@mock.patch("secrets.token_hex") +def test_LoginRedirectAMS_get(secrets_token_hex_mock, requests_get_mock): + """Test the LoginRedirectAMS class.""" + class DummyRequest: + session = { + "state_nonce_tracker": "dummy_state_nonce_tracker" + } + requests_get_mock.return_value.status_code = 200 + requests_get_mock.return_value.json.return_value = {"authorization_endpoint": "dummy_authorization_endpoint"} + + secrets_token_hex_mock.return_value = "dummy_state_nonce" + + login_redirect_ams = LoginRedirectAMS() + + response = login_redirect_ams.get(DummyRequest) + assert response.url == "dummy_authorization_endpoint?client_id=&nonce=dummy_state_nonce&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fv1%2Foidc%2Fams&response_type=code&state=dummy_state_nonce&scope=openid+email" + + + # Test if the AMS server is down + requests_get_mock.return_value.status_code = 500 + login_redirect_ams = LoginRedirectAMS() + response = login_redirect_ams.get("request") + print('____ response', response.__dict__) + assert response.status_code == 500 + + \ No newline at end of file diff --git a/tdrs-frontend/Dockerfile b/tdrs-frontend/Dockerfile index 2eed83667..06282c1b4 100644 --- a/tdrs-frontend/Dockerfile +++ b/tdrs-frontend/Dockerfile @@ -52,4 +52,5 @@ COPY nginx/local/default.conf.template /etc/nginx/default.conf.template COPY nginx/local/locations.conf /etc/nginx/locations.conf #COPY nginx/local/under_maintenance.html /etc/nginx/under_maintenance.html COPY nginx/src/503.html /usr/share/nginx/html/503_.html +COPY nginx/src/50x.html /usr/share/nginx/html/50x.html COPY nginx/src/static/ /usr/share/nginx/html/static/ diff --git a/tdrs-frontend/nginx/local/locations.conf b/tdrs-frontend/nginx/local/locations.conf index 8c95a9908..c9c2cf253 100644 --- a/tdrs-frontend/nginx/local/locations.conf +++ b/tdrs-frontend/nginx/local/locations.conf @@ -20,6 +20,7 @@ location ~ ^/(v1|admin|static/admin|static/drf-yasg|swagger|redocs) { proxy_buffer_size 4k; proxy_temp_file_write_size 64k; proxy_pass_header x-csrftoken; + } location ~* ^/kibana/(.*)$ { @@ -86,6 +87,13 @@ location = / { try_files $uri $uri/ /index.html; } +proxy_intercept_errors on; error_page 500 502 504 /50x.html; +location = /50x.html { + sub_filter '##STATUS##' $status; + root /usr/share/nginx/html; + try_files /50x.html @error; + internal; +} error_page 404 /index.html; diff --git a/tdrs-frontend/nginx/src/50x.html b/tdrs-frontend/nginx/src/50x.html new file mode 100644 index 000000000..10e844dd9 --- /dev/null +++ b/tdrs-frontend/nginx/src/50x.html @@ -0,0 +1,17 @@ + + + + + Your Business + + + + +

We are updating our website "##STATUS##"

+

This is only for a few seconds, you will be redirected. $status

+ +

+ + + + \ No newline at end of file