From fdbeea6fe862f23777f25c43c04f3ca5b4f3d17e Mon Sep 17 00:00:00 2001 From: "Jason C. Leach" Date: Fri, 2 Feb 2024 17:48:24 -0800 Subject: [PATCH] fix: add better logging (#25) Signed-off-by: Jason C. Leach --- devops/charts/controller/values.yaml | 2 +- src/controller.py | 32 ++++++++++++---------- src/traction.py | 41 +++++++++++++++------------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/devops/charts/controller/values.yaml b/devops/charts/controller/values.yaml index cfcf072..c0ac44e 100644 --- a/devops/charts/controller/values.yaml +++ b/devops/charts/controller/values.yaml @@ -14,7 +14,7 @@ image: registry: ghcr.io repository: bcgov/mobile-attestation-vc-controller/controller # Overrides the image tag whose default is the chart appVersion. - tag: "a5a33c9" + tag: "5858a94" env: TRACTION_BASE_URL: "https://traction-tenant-proxy-dev.apps.silver.devops.gov.bc.ca" diff --git a/src/controller.py b/src/controller.py index a0e393a..300ca17 100644 --- a/src/controller.py +++ b/src/controller.py @@ -1,6 +1,7 @@ import base64 import json import secrets +import logging from flask import Flask, request, make_response from traction import get_connection, send_message, offer_attestation_credential from apple import verify_attestation_statement @@ -14,6 +15,9 @@ load_dotenv() server = Flask(__name__) +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + def handle_message(message, content): action = content.get("action") @@ -33,17 +37,17 @@ def report_failure(connection_id): json_str = json.dumps(report_failure) base64_str = base64.b64encode(json_str.encode("utf-8")).decode("utf-8") - print(f"sending report failure message to {connection_id}") + logger.info(f"sending report failure message to {connection_id}") send_message(connection_id, base64_str) def handle_request_nonce(connection_id, content): - print("handle_request_nonce") + logger.info("handle_request_nonce") connection = get_connection(connection_id) - print(f"fetched connection = {connection}") + logger.info(f"fetched connection = {connection}") if connection["rfc23_state"] != "completed": - print("connection is not completed") + logger.info("connection is not completed") return message_templates_path = os.getenv("MESSAGE_TEMPLATES_PATH") @@ -61,42 +65,42 @@ def handle_request_nonce(connection_id, content): json_str = json.dumps(request_attestation) base64_str = base64.b64encode(json_str.encode("utf-8")).decode("utf-8") - print(f"sending request attestation message to {connection_id}") + logger.info(f"sending request attestation message to {connection_id}") send_message(connection_id, base64_str) def handle_challenge_response(connection_id, content): - print("handle_attestation_challenge") + logger.info("handle_attestation_challenge") platform = content.get("platform") # fetch nonce from cache using connection id as key nonce = redis_instance.get(connection_id) if not nonce: - print("No cached nonce") + logger.info("No cached nonce") report_failure(connection_id) return if platform == "apple": is_valid_challenge = verify_attestation_statement(content, nonce) if is_valid_challenge: - print("valid apple challenge") + logger.info("valid apple challenge") offer_attestation_credential(connection_id) else: - print("invalid apple challenge") + logger.info("invalid apple challenge") report_failure(connection_id) elif platform == "google": token = content.get("attestation_object") is_valid_challenge = verify_integrity_token(token, nonce) if is_valid_challenge: - print("valid google integrity verdict") + logger.info("valid google integrity verdict") offer_attestation_credential(connection_id) else: - print("invalid google integrity verdict") + logger.info("invalid google integrity verdict") report_failure(connection_id) else: - print("unsupported platform") + logger.info("unsupported platform") report_failure(connection_id) @@ -125,13 +129,13 @@ def decode_base64_to_json(s): @server.route("/topic/ping/", methods=["POST"]) def ping(): - print("Run POST /ping/") + logger.info("Run POST /ping/") return make_response("", 204) @server.route("/topic/basicmessages/", methods=["POST"]) def basicmessages(): - print("Run POST /topic/basicmessages/") + logger.info("Run POST /topic/basicmessages/") message = request.get_json() content = message["content"] diff --git a/src/traction.py b/src/traction.py index 1742a32..999f43c 100644 --- a/src/traction.py +++ b/src/traction.py @@ -3,11 +3,14 @@ import os from urllib.parse import urljoin from dotenv import load_dotenv +import logging if os.getenv("FLASK_ENV") == "development": load_dotenv() bearer_token = None +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) def fetch_bearer_token(): @@ -24,17 +27,17 @@ def fetch_bearer_token(): headers = {"Content-Type": "application/json", "accept": "application/json"} data = {"wallet_key": wallet_key} - print(f"Requesting bearer token for walletId {wallet_id}") + logger.info(f"Requesting bearer token for walletId {wallet_id}") response = requests.post(url, headers=headers, data=json.dumps(data)) if response.status_code == 200: - print("Token fetched successfully") + logger.info("Token fetched successfully") response_data = json.loads(response.text) bearer_token = response_data["token"] return bearer_token else: - print(f"Error fetcing token: {response.status_code}") + logger.info(f"Error fetcing token: {response.status_code}") def get_connection(conn_id): @@ -50,15 +53,15 @@ def get_connection(conn_id): "Authorization": f"Bearer {token}", } - print(f"Fetching connection {conn_id}") + logger.info(f"Fetching connection {conn_id}") response = requests.get(url, headers=headers) if response.status_code == 200: - print("Conneciton fetched successfully") + logger.info("Conneciton fetched successfully") return json.loads(response.text) else: - print(f"Error fetcing conneciton message: {response.status_code}") + logger.info(f"Error fetcing conneciton message: {response.status_code}") return None @@ -77,18 +80,18 @@ def send_message(conn_id, content): } data = {"content": content} - print(f"Sending message to {conn_id}, message = {content}") + logger.info(f"Sending message to {conn_id}, message = {content}") response = requests.post(url, headers=headers, data=json.dumps(data)) if response.status_code == 200: - print("Message sent successfully") + logger.info("Message sent successfully") else: - print(f"Error sending message: {response.status_code}") + logger.info(f"Error sending message: {response.status_code}") def offer_attestation_credential(conn_id): - print("issue_attestation_credential") + logger.info("issue_attestation_credential") base_url = os.environ.get("TRACTION_BASE_URL") endpoint = "/issue-credential/send-offer" @@ -108,18 +111,18 @@ def offer_attestation_credential(conn_id): offer["connection_id"] = conn_id - print(f"Sending offer to {conn_id}, offer = {offer}") + logger.info(f"Sending offer to {conn_id}, offer = {offer}") response = requests.post(url, headers=headers, data=json.dumps(offer)) if response.status_code == 200: - print("Offer sent successfully") + logger.info("Offer sent successfully") else: - print(f"Error sending offer: {response.status_code}") + logger.info(f"Error sending offer: {response.status_code}") def get_schema(schema_id): - print("get_schema") + logger.info("get_schema") base_url = os.environ.get("TRACTION_BASE_URL") endpoint = "/schemas/created" @@ -136,15 +139,15 @@ def get_schema(schema_id): response = requests.get(url, headers=headers, params={"schema_id": schema_id}) if response.status_code == 200: - print("Schema queried successfully") + logger.info("Schema queried successfully") else: - print(f"Error quering schema: {response.status_code}") + logger.info(f"Error quering schema: {response.status_code}") return response.json() def create_schema(schema_name, schema_version, attributes): - print("create_schema") + logger.info("create_schema") base_url = os.environ.get("TRACTION_BASE_URL") endpoint = "/schemas" @@ -167,8 +170,8 @@ def create_schema(schema_name, schema_version, attributes): response = requests.post(url, headers=headers, data=json.dumps(schema)) if response.status_code == 200: - print("Schema created successfully") + logger.info("Schema created successfully") else: - print(f"Error creating schema: {response.status_code}") + logger.info(f"Error creating schema: {response.status_code}") return response.json()