bcgov/mines-digital-trust

View on GitHub
services/ghg-orgbook-issuer-controller/app/credential.py

Summary

Maintainability
F
2 wks
Test Coverage
Identical blocks of code found in 2 locations. Consider refactoring.
import threading, time, os, json, requests
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
from app import logging
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
USE_LOCK = os.getenv('USE_LOCK', 'True').lower() == 'true'
Identical blocks of code found in 2 locations. Consider refactoring.
# max 15 second wait for a credential response (prevents blocking forever)
Identical blocks of code found in 2 locations. Consider refactoring.
MAX_CRED_RESPONSE_TIMEOUT = int(os.getenv('MAX_CRED_RESPONSE_TIMEOUT', '120'))
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock = threading.Lock()
Identical blocks of code found in 2 locations. Consider refactoring.
credential_requests = {}
Identical blocks of code found in 2 locations. Consider refactoring.
credential_responses = {}
Identical blocks of code found in 2 locations. Consider refactoring.
credential_threads = {}
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
def set_credential_thread_id(cred_exch_id, thread_id):
Identical blocks of code found in 2 locations. Consider refactoring.
start_time = time.perf_counter()
Identical blocks of code found in 2 locations. Consider refactoring.
if USE_LOCK:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock.acquire()
Identical blocks of code found in 2 locations. Consider refactoring.
try:
Identical blocks of code found in 2 locations. Consider refactoring.
# add 2 records so we can x-ref
Identical blocks of code found in 2 locations. Consider refactoring.
credential_threads[thread_id] = cred_exch_id
Identical blocks of code found in 2 locations. Consider refactoring.
credential_threads[cred_exch_id] = thread_id
Identical blocks of code found in 2 locations. Consider refactoring.
finally:
Identical blocks of code found in 2 locations. Consider refactoring.
if USE_LOCK:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock.release()
Identical blocks of code found in 2 locations. Consider refactoring.
processing_time = time.perf_counter() - start_time
Identical blocks of code found in 2 locations. Consider refactoring.
if processing_time > 0.001:
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.warning(">>> lock time = %s", str(processing_time))
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
def add_credential_request(cred_exch_id):
Identical blocks of code found in 2 locations. Consider refactoring.
start_time = time.perf_counter()
Identical blocks of code found in 2 locations. Consider refactoring.
if USE_LOCK:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock.acquire()
Identical blocks of code found in 2 locations. Consider refactoring.
try:
Identical blocks of code found in 2 locations. Consider refactoring.
# short circuit if we already have the response
Identical blocks of code found in 2 locations. Consider refactoring.
if cred_exch_id in credential_responses:
Identical blocks of code found in 2 locations. Consider refactoring.
return None
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
result_available = threading.Event()
Identical blocks of code found in 2 locations. Consider refactoring.
credential_requests[cred_exch_id] = result_available
Identical blocks of code found in 2 locations. Consider refactoring.
return result_available
Identical blocks of code found in 2 locations. Consider refactoring.
finally:
Identical blocks of code found in 2 locations. Consider refactoring.
if USE_LOCK:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock.release()
Identical blocks of code found in 2 locations. Consider refactoring.
processing_time = time.perf_counter() - start_time
Identical blocks of code found in 2 locations. Consider refactoring.
if processing_time > 0.001:
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.warning(">>> lock time = %s", str(processing_time))
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
def add_credential_response(cred_exch_id, response):
Identical blocks of code found in 2 locations. Consider refactoring.
start_time = time.perf_counter()
Identical blocks of code found in 2 locations. Consider refactoring.
if USE_LOCK:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock.acquire()
Identical blocks of code found in 2 locations. Consider refactoring.
try:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_responses[cred_exch_id] = response
Identical blocks of code found in 2 locations. Consider refactoring.
if cred_exch_id in credential_requests:
Identical blocks of code found in 2 locations. Consider refactoring.
result_available = credential_requests[cred_exch_id]
Identical blocks of code found in 2 locations. Consider refactoring.
result_available.set()
Identical blocks of code found in 2 locations. Consider refactoring.
del credential_requests[cred_exch_id]
Identical blocks of code found in 2 locations. Consider refactoring.
finally:
Identical blocks of code found in 2 locations. Consider refactoring.
if USE_LOCK:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock.release()
Identical blocks of code found in 2 locations. Consider refactoring.
processing_time = time.perf_counter() - start_time
Identical blocks of code found in 2 locations. Consider refactoring.
if processing_time > 0.001:
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.warning(">>> lock time = %s", str(processing_time))
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
def add_credential_problem_report(thread_id, response):
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error("get problem report for thread %s %s", thread_id, str(len(credential_requests)))
Identical blocks of code found in 2 locations. Consider refactoring.
if thread_id in credential_threads:
Identical blocks of code found in 2 locations. Consider refactoring.
cred_exch_id = credential_threads[thread_id]
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error(" ... cred_exch_id is %s: %s", cred_exch_id, str(response))
Identical blocks of code found in 2 locations. Consider refactoring.
add_credential_response(cred_exch_id, response)
Identical blocks of code found in 2 locations. Consider refactoring.
else:
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error("thread_id not found %s", thread_id)
Identical blocks of code found in 2 locations. Consider refactoring.
# hack for now
Identical blocks of code found in 2 locations. Consider refactoring.
if 1 == len(list(credential_requests.keys())):
Identical blocks of code found in 2 locations. Consider refactoring.
cred_exch_id = list(credential_requests.keys())[0]
Identical blocks of code found in 2 locations. Consider refactoring.
add_credential_response(cred_exch_id, response)
Identical blocks of code found in 2 locations. Consider refactoring.
elif 0 == len(list(credential_requests.keys())):
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error("NO outstanding requests, can't map problem report to request :-(")
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error(credential_requests)
Identical blocks of code found in 2 locations. Consider refactoring.
else:
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error("Too many outstanding requests, can't map problem report to request :-(")
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error(credential_requests)
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
def add_credential_timeout_report(cred_exch_id, thread_id):
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error("add timeout report for cred %s %s", thread_id, cred_exch_id)
Identical blocks of code found in 2 locations. Consider refactoring.
response = {"success": False, "result": thread_id + "::Error thread timeout"}
Identical blocks of code found in 2 locations. Consider refactoring.
add_credential_response(cred_exch_id, response)
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
def add_credential_exception_report(cred_exch_id, exc):
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error("add exception report for cred %s", cred_exch_id)
Identical blocks of code found in 2 locations. Consider refactoring.
response = {"success": False, "result": cred_exch_id + "::" + str(exc)}
Identical blocks of code found in 2 locations. Consider refactoring.
add_credential_response(cred_exch_id, response)
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
def get_credential_response(cred_exch_id):
Identical blocks of code found in 2 locations. Consider refactoring.
start_time = time.perf_counter()
Identical blocks of code found in 2 locations. Consider refactoring.
if USE_LOCK:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock.acquire()
Identical blocks of code found in 2 locations. Consider refactoring.
try:
Identical blocks of code found in 2 locations. Consider refactoring.
if cred_exch_id in credential_responses:
Identical blocks of code found in 2 locations. Consider refactoring.
thread_id = None
Identical blocks of code found in 2 locations. Consider refactoring.
response = credential_responses[cred_exch_id]
Identical blocks of code found in 2 locations. Consider refactoring.
del credential_responses[cred_exch_id]
Identical blocks of code found in 2 locations. Consider refactoring.
if cred_exch_id in credential_threads:
Identical blocks of code found in 2 locations. Consider refactoring.
thread_id = credential_threads[cred_exch_id]
Identical blocks of code found in 2 locations. Consider refactoring.
del credential_threads[cred_exch_id]
Identical blocks of code found in 2 locations. Consider refactoring.
del credential_threads[thread_id]
Identical blocks of code found in 2 locations. Consider refactoring.
# override returned id with thread_id, if we have it (unless we have received a problem report)
Identical blocks of code found in 2 locations. Consider refactoring.
if not "::" in response["result"]:
Identical blocks of code found in 2 locations. Consider refactoring.
response["result"] = thread_id
Identical blocks of code found in 2 locations. Consider refactoring.
return response
Identical blocks of code found in 2 locations. Consider refactoring.
else:
Identical blocks of code found in 2 locations. Consider refactoring.
return None
Identical blocks of code found in 2 locations. Consider refactoring.
finally:
Identical blocks of code found in 2 locations. Consider refactoring.
if USE_LOCK:
Identical blocks of code found in 2 locations. Consider refactoring.
credential_lock.release()
Identical blocks of code found in 2 locations. Consider refactoring.
processing_time = time.perf_counter() - start_time
Identical blocks of code found in 2 locations. Consider refactoring.
if processing_time > 0.001:
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.warning(">>> lock time = %s", str(processing_time))
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
class SendCredentialThread(threading.Thread):
Identical blocks of code found in 2 locations. Consider refactoring.
def __init__(self, credential_definition_id, cred_offer, url, headers):
Identical blocks of code found in 2 locations. Consider refactoring.
threading.Thread.__init__(self)
Identical blocks of code found in 2 locations. Consider refactoring.
self.credential_definition_id = credential_definition_id
Identical blocks of code found in 2 locations. Consider refactoring.
self.cred_offer = cred_offer
Identical blocks of code found in 2 locations. Consider refactoring.
self.url = url
Identical blocks of code found in 2 locations. Consider refactoring.
self.headers = headers
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
def run(self):
Identical blocks of code found in 2 locations. Consider refactoring.
start_time = time.perf_counter()
Identical blocks of code found in 2 locations. Consider refactoring.
method = "submit_credential.credential"
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
logging.log_timing_event("issue_credential", {}, start_time, None, False)
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.info("Sending credential offer: %s", json.dumps(self.cred_offer))
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
cred_data = None
Identical blocks of code found in 2 locations. Consider refactoring.
try:
Identical blocks of code found in 2 locations. Consider refactoring.
response = requests.post(
Identical blocks of code found in 2 locations. Consider refactoring.
self.url, json.dumps(self.cred_offer), headers=self.headers
Identical blocks of code found in 2 locations. Consider refactoring.
)
Identical blocks of code found in 2 locations. Consider refactoring.
response.raise_for_status()
Identical blocks of code found in 2 locations. Consider refactoring.
cred_data = response.json()
Identical blocks of code found in 2 locations. Consider refactoring.
if "credential_exchange_id" in cred_data:
Identical blocks of code found in 2 locations. Consider refactoring.
result_available = add_credential_request(
Identical blocks of code found in 2 locations. Consider refactoring.
cred_data["credential_exchange_id"]
Identical blocks of code found in 2 locations. Consider refactoring.
)
Identical blocks of code found in 2 locations. Consider refactoring.
else:
Identical blocks of code found in 2 locations. Consider refactoring.
raise Exception(json.dumps(cred_data))
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
# wait for confirmation from the agent, which will include the credential exchange id
Identical blocks of code found in 2 locations. Consider refactoring.
if result_available and not result_available.wait(
Identical blocks of code found in 2 locations. Consider refactoring.
MAX_CRED_RESPONSE_TIMEOUT
Identical blocks of code found in 2 locations. Consider refactoring.
):
Identical blocks of code found in 2 locations. Consider refactoring.
add_credential_timeout_report(cred_data["credential_exchange_id"], cred_data["thread_id"])
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error(
Identical blocks of code found in 2 locations. Consider refactoring.
"Got credential TIMEOUT: %s %s %s",
Identical blocks of code found in 2 locations. Consider refactoring.
cred_data["thread_id"],
Identical blocks of code found in 2 locations. Consider refactoring.
cred_data["credential_exchange_id"],
Identical blocks of code found in 2 locations. Consider refactoring.
cred_data["connection_id"],
Identical blocks of code found in 2 locations. Consider refactoring.
)
Identical blocks of code found in 2 locations. Consider refactoring.
end_time = time.perf_counter()
Identical blocks of code found in 2 locations. Consider refactoring.
logging.log_timing_method(method, start_time, end_time, False,
Identical blocks of code found in 2 locations. Consider refactoring.
data={
Identical blocks of code found in 2 locations. Consider refactoring.
'thread_id':cred_data["thread_id"],
Identical blocks of code found in 2 locations. Consider refactoring.
'credential_exchange_id':cred_data["credential_exchange_id"],
Identical blocks of code found in 2 locations. Consider refactoring.
'Error': 'Timeout',
Identical blocks of code found in 2 locations. Consider refactoring.
'elapsed_time': (end_time-start_time)
Identical blocks of code found in 2 locations. Consider refactoring.
}
Identical blocks of code found in 2 locations. Consider refactoring.
)
Identical blocks of code found in 2 locations. Consider refactoring.
success = False
Identical blocks of code found in 2 locations. Consider refactoring.
outcome = "timeout"
Identical blocks of code found in 2 locations. Consider refactoring.
else:
Identical blocks of code found in 2 locations. Consider refactoring.
# response was received for this cred exchange via a web hook
Identical blocks of code found in 2 locations. Consider refactoring.
end_time = time.perf_counter()
Identical blocks of code found in 2 locations. Consider refactoring.
logging.log_timing_method(method, start_time, end_time, True)
Identical blocks of code found in 2 locations. Consider refactoring.
success = True
Identical blocks of code found in 2 locations. Consider refactoring.
outcome = "success"
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
# there should be some form of response available
Identical blocks of code found in 2 locations. Consider refactoring.
self.cred_response = get_credential_response(
Identical blocks of code found in 2 locations. Consider refactoring.
cred_data["credential_exchange_id"]
Identical blocks of code found in 2 locations. Consider refactoring.
)
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
except Exception as exc:
Identical blocks of code found in 2 locations. Consider refactoring.
logging.LOGGER.error("got credential exception: %s", str(exc))
Identical blocks of code found in 2 locations. Consider refactoring.
# if cred_data is not set we don't have a credential to set status for
Identical blocks of code found in 2 locations. Consider refactoring.
end_time = time.perf_counter()
Identical blocks of code found in 2 locations. Consider refactoring.
success = False
Identical blocks of code found in 2 locations. Consider refactoring.
outcome = str(exc)
Identical blocks of code found in 2 locations. Consider refactoring.
if cred_data:
Identical blocks of code found in 2 locations. Consider refactoring.
add_credential_exception_report(
Identical blocks of code found in 2 locations. Consider refactoring.
cred_data["credential_exchange_id"], exc
Identical blocks of code found in 2 locations. Consider refactoring.
)
Identical blocks of code found in 2 locations. Consider refactoring.
data = {
Identical blocks of code found in 2 locations. Consider refactoring.
"thread_id": cred_data["thread_id"],
Identical blocks of code found in 2 locations. Consider refactoring.
"credential_exchange_id": cred_data["credential_exchange_id"],
Identical blocks of code found in 2 locations. Consider refactoring.
"Error": str(exc),
Identical blocks of code found in 2 locations. Consider refactoring.
"elapsed_time": (end_time - start_time),
Identical blocks of code found in 2 locations. Consider refactoring.
}
Identical blocks of code found in 2 locations. Consider refactoring.
else:
Identical blocks of code found in 2 locations. Consider refactoring.
data = {
Identical blocks of code found in 2 locations. Consider refactoring.
"Error": str(exc),
Identical blocks of code found in 2 locations. Consider refactoring.
"elapsed_time": (end_time - start_time)
Identical blocks of code found in 2 locations. Consider refactoring.
}
Identical blocks of code found in 2 locations. Consider refactoring.
logging.log_timing_method(method, start_time, end_time, False,
Identical blocks of code found in 2 locations. Consider refactoring.
data=data
Identical blocks of code found in 2 locations. Consider refactoring.
)
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
# don't re-raise; we want to log the exception as the credential error response
Identical blocks of code found in 2 locations. Consider refactoring.
self.cred_response = {"success": False, "result": str(exc)}
Identical blocks of code found in 2 locations. Consider refactoring.
 
Identical blocks of code found in 2 locations. Consider refactoring.
processing_time = end_time - start_time
Identical blocks of code found in 2 locations. Consider refactoring.
message = {"thread_id": self.cred_response["result"]}
Identical blocks of code found in 2 locations. Consider refactoring.
logging.log_timing_event("issue_credential", message, start_time, end_time, success, outcome=outcome)