Skip to content

Commit

Permalink
fix: add better logging (bcgov#25)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason C. Leach <[email protected]>
  • Loading branch information
jleach authored Feb 3, 2024
1 parent 5858a94 commit fdbeea6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion devops/charts/controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 18 additions & 14 deletions src/controller.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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)


Expand Down Expand Up @@ -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"]

Expand Down
41 changes: 22 additions & 19 deletions src/traction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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):
Expand All @@ -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

Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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()

0 comments on commit fdbeea6

Please sign in to comment.