From 0e2526c705dfff7d0db3fb53122021d1c2fd31f9 Mon Sep 17 00:00:00 2001 From: "Christian R. Garcia" Date: Fri, 13 Oct 2023 08:39:09 -0700 Subject: [PATCH 1/2] Fixed some validations errors not getting sent --- service/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/service/utils.py b/service/utils.py index 152daca..6142ec4 100644 --- a/service/utils.py +++ b/service/utils.py @@ -46,8 +46,11 @@ async def error_handler(request: Request, exc): status_code = 500 elif isinstance(exc, RequestValidationError) or isinstance(exc, ValidationError): error_list = [] + logger.debug(f"Got validation error: {repr(exc)}") for error_dict in exc.errors(): error_list.append(f"{', '.join(str(err) for err in error_dict['loc'])}: {error_dict['msg']}") + if error_list is None: + response = error(msg=f'Unexpected. {repr(exc)}') response = error(msg=error_list) status_code = 400 else: From dc37d32447df58b1365b917ab7f8aac91f5e8ab4 Mon Sep 17 00:00:00 2001 From: "Christian R. Garcia" Date: Mon, 16 Oct 2023 10:24:30 -0700 Subject: [PATCH 2/2] Fix some errors being eaten by FastAPI. Better error messages now. --- configschema.json | 4 ++++ deployment-template/config.json | 1 + requirements.txt | 7 +++++-- service/api.py | 7 ++++++- service/models_pods.py | 4 ++-- service/utils.py | 3 +++ tests/test_utils.py | 6 +++--- 7 files changed, 24 insertions(+), 8 deletions(-) diff --git a/configschema.json b/configschema.json index 6da0df4..cb50aac 100644 --- a/configschema.json +++ b/configschema.json @@ -81,6 +81,10 @@ "type": "integer", "description": "Unique host_id for worker host. Each host should have at least one spawner and health check worker." }, + "test_abaco_service_password": { + "type": "string", + "description": "Abaco service password is required to run tests as it's able to generate tokens." + }, "image_allow_list": { "type": "array", "description": "Docker images that users are allowed to use.", diff --git a/deployment-template/config.json b/deployment-template/config.json index 628f0b7..39a151d 100644 --- a/deployment-template/config.json +++ b/deployment-template/config.json @@ -5,6 +5,7 @@ "service_site_id": "tacc", "service_name": "pods", "service_password": "$env{SERVICE_PASSWORD}", + "test_abaco_service_password": "FILLTHISFORTESTING", "tenants": [ "tacc", "dev", diff --git a/requirements.txt b/requirements.txt index 7ee8758..3238daa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,9 @@ # Tapis -tapipy>=1.3.2 -tapisservice>=1.3.0 +tapipy>=1.4.0 +tapisservice>=1.4.0 +jsonschema==4.17.3 # Errors after this version with error below. +# Error reading local "notifications" resource. Ensure path is absolute /usr/local/lib/python3.10/site-packages/tapipy/resources/openapi_v3-notifications.yml. e:cannot import name '_legacy_validators' from 'jsonschema' (/usr/local/lib/python3.10/site-packages/jsonschema/__init__.py) + # Service API pydantic diff --git a/service/api.py b/service/api.py index e67c55b..77ca9f4 100644 --- a/service/api.py +++ b/service/api.py @@ -5,6 +5,7 @@ from __init__ import Tenants from fastapi import FastAPI from fastapi.middleware import Middleware +from fastapi.exceptions import RequestValidationError from auth import authorization, authentication from api_pods import router as router_pods @@ -72,7 +73,11 @@ "url": "https://github.com/tapis-project/pods_service", }, debug=False, - exception_handlers={Exception: error_handler}, + exception_handlers={ + Exception: error_handler, + RequestValidationError: error_handler, + 422: error_handler + }, middleware=[ Middleware(HttpUrlRedirectMiddleware), Middleware(GlobalsMiddleware), diff --git a/service/models_pods.py b/service/models_pods.py index 895550f..18c58c7 100644 --- a/service/models_pods.py +++ b/service/models_pods.py @@ -354,9 +354,9 @@ def check_networking(cls, v): raise TypeError(f"networking key must be str. Got type {type(env_key).__name__}.") res = re.fullmatch(r'[a-z0-9]+', env_key) if not res: - raise ValueError(f"networking key must be lowercase alphanumeric. Default if 'default'.") + raise ValueError(f"networking key must be lowercase alphanumeric. Default is 'default'.") if len(env_key) > 64 or len(env_key) < 3: - raise ValueError(f"networking key length must be between 3-64 characters. Inputted length: {len(v)}") + raise ValueError(f"networking key length must be between 3-64 characters. Inputted length: {len(env_key)}") return v @root_validator(pre=False) diff --git a/service/utils.py b/service/utils.py index 152daca..1b87579 100644 --- a/service/utils.py +++ b/service/utils.py @@ -16,6 +16,7 @@ TAG = conf.version async def error_handler(request: Request, exc): + logger.debug(f"Top of Pods Service error handler. Got error: {repr(exc)}") response = None status_code: int = -1 if conf.show_traceback: @@ -48,6 +49,8 @@ async def error_handler(request: Request, exc): error_list = [] for error_dict in exc.errors(): error_list.append(f"{', '.join(str(err) for err in error_dict['loc'])}: {error_dict['msg']}") + if error_list is None: + response = error(msg=f'Unexpected. {repr(exc)}') response = error(msg=error_list) status_code = 400 else: diff --git a/tests/test_utils.py b/tests/test_utils.py index 18602a4..da6d698 100755 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -21,7 +21,7 @@ def get_service_tapis_client(): sk_url = os.environ.get('sk_url', conf.primary_site_admin_tenant_base_url) tenant_id = os.environ.get('tenant', 'admin') - service_password = os.environ.get('service_password', conf.service_password) + test_abaco_service_password = os.environ.get('test_abaco_service_password', conf.test_abaco_service_password) jwt = os.environ.get('jwt', None) resource_set = os.environ.get('resource_set', 'local') custom_spec_dict = os.environ.get('custom_spec_dict', None) @@ -29,9 +29,9 @@ def get_service_tapis_client(): # if there is no tenant_id, use the service_tenant_id and primary_site_admin_tenant_base_url configured for the service: t = Tapis(base_url=sk_url or base_url, tenant_id=tenant_id, - username='abaco', + username='abaco', ### NOTE: Must be abaco as it can generate tokens account_type='service', - service_password=service_password, + service_password=test_abaco_service_password, jwt=jwt, resource_set=resource_set, custom_spec_dict=custom_spec_dict,