diff --git a/functions-python/tokens/.coveragerc b/functions-python/tokens/.coveragerc index f893c7cd9..db33d586b 100644 --- a/functions-python/tokens/.coveragerc +++ b/functions-python/tokens/.coveragerc @@ -1,6 +1,8 @@ [run] omit = */test*/* + */helpers/* + */shared/* [report] exclude_lines = diff --git a/functions-python/tokens/function_config.json b/functions-python/tokens/function_config.json index 2fa5a1652..9f97718ca 100644 --- a/functions-python/tokens/function_config.json +++ b/functions-python/tokens/function_config.json @@ -6,7 +6,8 @@ "timeout": 20, "memory": "128Mi", "trigger_http": true, - "include_folders": [], + "include_folders": ["helpers"], + "include_api_folders": ["common"], "environment_variables": [], "secret_environment_variables": [ { diff --git a/functions-python/tokens/requirements.txt b/functions-python/tokens/requirements.txt index a85da6966..7ab572db0 100644 --- a/functions-python/tokens/requirements.txt +++ b/functions-python/tokens/requirements.txt @@ -1,5 +1,18 @@ +# Common packages functions-framework==3.* +google-cloud-logging +psycopg2-binary==2.9.6 +aiohttp~=3.10.5 +asyncio~=3.4.3 +urllib3~=2.2.2 +requests~=2.32.3 +attrs~=23.1.0 +pluggy~=1.3.0 +certifi~=2024.7.4 + +# Flask dependencies for OpenAPI implementation flask -requests -PyJWT -werkzeug \ No newline at end of file +werkzeug + +# JWT library for JWT token verification +PyJWT \ No newline at end of file diff --git a/functions-python/tokens/src/main.py b/functions-python/tokens/src/main.py index 1d975db7c..2c311334e 100644 --- a/functions-python/tokens/src/main.py +++ b/functions-python/tokens/src/main.py @@ -17,6 +17,7 @@ import json import os import jwt +import logging import requests import flask import functions_framework @@ -25,6 +26,7 @@ from datetime import datetime from datetime import timezone from werkzeug.exceptions import UnsupportedMediaType, BadRequest +from shared.helpers.logger import init_logger IDP_TOKEN_URL: Final[str] = "https://securetoken.googleapis.com/v1/token" HEADERS: Final[dict[str, str]] = { @@ -33,6 +35,9 @@ } +init_logger() + + class TokenPostResponse: def __init__( self, access_token: str, expiration_datetime_utc: str, token_type: str @@ -117,7 +122,7 @@ def extract_refresh_token(request): except UnsupportedMediaType as e: raise e except Exception as e: - print(f"Error extracting refresh token : {e}") + logging.error("Error extracting refresh token : %s", e) raise BadRequest() @@ -154,7 +159,7 @@ def tokens_post(request: flask.Request) -> Response: idp_response = get_idp_response(request.get_json().get("refresh_token")) if idp_response.status_code != 200: - print(f"Error retrieving refresh token : {idp_response.json()}") + logging.error("Error retrieving refresh token : %s", idp_response.json()) return Response( status=500, mimetype="application/json", @@ -166,7 +171,7 @@ def tokens_post(request: flask.Request) -> Response: return create_response_from_idp(idp_response) except UnsupportedMediaType as e: - print(f"Error creating response from idp : {e}") + logging.error("Error creating response from idp : %s", e) return Response( status=e.code, mimetype="application/json", @@ -183,7 +188,7 @@ def tokens_post(request: flask.Request) -> Response: response=json.dumps(TokenPostResponseError("Bad Request.").__dict__), ) except Exception as e: - print(f"Error creating response from idp : {e}") + logging.error("Error creating response from idp : %s", e) return Response( status=500, mimetype="application/json",