def get_db_statistics(): # Get table names # db_entry_object = Account() account_table_name = db_entry_object.table_name # db_entry_object = ServiceLinkRecord() slr_table_name = db_entry_object.table_name # db_entry_object = ServiceLinkStatusRecord() ssr_table_name = db_entry_object.table_name # db_entry_object = ConsentRecord() cr_table_name = db_entry_object.table_name # db_entry_object = ConsentStatusRecord() csr_table_name = db_entry_object.table_name # Get DB cursor try: logger.debug("Getting DB cursor") cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) return False account_query = "SELECT COUNT(*) FROM " + account_table_name + ";" slr_query = "SELECT COUNT(*) FROM " + slr_table_name + ";" ssr_query = "SELECT COUNT(*) FROM " + ssr_table_name + ";" cr_query = "SELECT COUNT(*) FROM " + cr_table_name + ";" csr_query = "SELECT COUNT(*) FROM " + csr_table_name + ";" try: cursor, account_count = execute_sql_count(cursor=cursor, sql_query=account_query) cursor, slr_count = execute_sql_count(cursor=cursor, sql_query=slr_query) cursor, ssr_count = execute_sql_count(cursor=cursor, sql_query=ssr_query) cursor, cr_count = execute_sql_count(cursor=cursor, sql_query=cr_query) cursor, csr_count = execute_sql_count(cursor=cursor, sql_query=csr_query) except Exception as exp: logger.debug('sql_query: ' + repr(exp)) raise else: count_dict = { 'account': account_count, 'service_link': slr_count, 'service_link_status': ssr_count, 'consent': cr_count, 'consent_status': csr_count } return count_dict
def post(self, account_id, source_slr_id, sink_slr_id): try: endpoint = str( api.url_for(self, account_id=account_id, source_slr_id=source_slr_id, sink_slr_id=sink_slr_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: account_id = str(account_id) except Exception as exp: raise ApiError(code=400, title="Unsupported account_id", detail=repr(exp), source=endpoint) try: source_slr_id = str(source_slr_id) except Exception as exp: raise ApiError(code=400, title="Unsupported source_slr_id", detail=repr(exp), source=endpoint) try: sink_slr_id = str(sink_slr_id) except Exception as exp: raise ApiError(code=400, title="Unsupported sink_slr_id", detail=repr(exp), source=endpoint) # load JSON json_data = request.get_json() if not json_data: error_detail = { '0': 'Set application/json as Content-Type', '1': 'Provide json payload' } raise ApiError(code=400, title="No input data provided", detail=error_detail, source=endpoint) else: logger.debug("json_data: " + json.dumps(json_data)) # Validate payload content schema = NewConsent() schema_validation_result = schema.load(json_data) # Check validation errors if schema_validation_result.errors: logger.error("Invalid payload") raise ApiError(code=400, title="Invalid payload", detail=dict(schema_validation_result.errors), source=endpoint) else: logger.debug("JSON validation -> OK") ###### # Source ###### # Consent Record try: source_cr_payload = json_data['data']['source'][ 'consentRecordPayload']['attributes'] except Exception as exp: raise ApiError(code=400, title="Could not fetch source_cr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got source_cr_payload: " + json.dumps(source_cr_payload)) # Consent Status Record try: source_csr_payload = json_data['data']['source'][ 'consentStatusRecordPayload']['attributes'] except Exception as exp: raise ApiError( code=400, title="Could not fetch source_csr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got source_csr_payload: " + json.dumps(source_csr_payload)) ###### # Sink ###### # Consent Record try: sink_cr_payload = json_data['data']['sink'][ 'consentRecordPayload']['attributes'] except Exception as exp: raise ApiError(code=400, title="Could not fetch sink_cr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got sink_cr_payload: " + json.dumps(sink_cr_payload)) # Consent Status Record try: sink_csr_payload = json_data['data']['sink'][ 'consentStatusRecordPayload']['attributes'] except Exception as exp: raise ApiError(code=400, title="Could not fetch sink_csr_payload from json", detail=repr(exp), source=endpoint) else: logger.debug("Got sink_csr_payload: " + json.dumps(sink_csr_payload)) ##### # IDs from CR and CSR payloads ##### try: # Source CR try: source_cr_cr_id = source_cr_payload['common_part']['cr_id'] source_cr_rs_id = source_cr_payload['common_part'][ 'rs_description']['resource_set']['rs_id'] source_cr_slr_id = source_cr_payload['common_part']['slr_id'] source_cr_subject_id = source_cr_payload['common_part'][ 'subject_id'] source_cr_surrogate_id = source_cr_payload['common_part'][ 'surrogate_id'] source_cr_role = source_cr_payload['common_part']['role'] except Exception as exp: error_title = "Could not fetch IDs from Source CR payload" raise # Source CSR try: source_csr_surrogate_id = source_csr_payload['surrogate_id'] source_csr_cr_id = source_csr_payload['cr_id'] source_csr_prev_record_id = source_csr_payload[ 'prev_record_id'] source_csr_record_id = source_csr_payload['record_id'] source_csr_consent_status = source_csr_payload[ 'consent_status'] source_csr_issued = source_csr_payload['iat'] except Exception as exp: error_title = "Could not fetch IDs from Source CSR payload" raise # Sink CR try: sink_cr_cr_id = sink_cr_payload['common_part']['cr_id'] sink_cr_rs_id = sink_cr_payload['common_part'][ 'rs_description']['resource_set']['rs_id'] sink_cr_slr_id = sink_cr_payload['common_part']['slr_id'] sink_cr_subject_id = sink_cr_payload['common_part'][ 'subject_id'] sink_cr_surrogate_id = sink_cr_payload['common_part'][ 'surrogate_id'] sink_cr_role = sink_cr_payload['common_part']['role'] except Exception as exp: error_title = "Could not fetch IDs from Sink CR payload" raise # Sink CSR try: sink_csr_surrogate_id = sink_csr_payload['surrogate_id'] sink_csr_cr_id = sink_csr_payload['cr_id'] sink_csr_prev_record_id = sink_csr_payload['prev_record_id'] sink_csr_record_id = sink_csr_payload['record_id'] sink_csr_consent_status = sink_csr_payload['consent_status'] sink_csr_issued = sink_csr_payload['iat'] except Exception as exp: error_title = "Could not fetch IDs from Sink CSR payload" raise except Exception as exp: logger.error(error_title) raise ApiError(code=400, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("IDs fetched from CR and CSR payloads") ###### # Sign #### # Sign Source CR try: source_cr_signed = sign_cr(account_id=account_id, payload=source_cr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Source's CR: " + repr(exp)) raise else: logger.info("Source CR signed") logger.debug("source_cr_signed: " + json.dumps(source_cr_signed)) # Sign Source CSR try: source_csr_signed = sign_csr(account_id=account_id, payload=source_csr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Source's CSR: " + repr(exp)) raise else: logger.info("Source CSR signed") logger.debug("source_csr_signed: " + json.dumps(source_csr_signed)) # Sign Sink CR try: sink_cr_signed = sign_cr(account_id=account_id, payload=sink_cr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Source's CR: " + repr(exp)) raise else: logger.info("Sink's CR signed") logger.debug("sink_cr_signed: " + json.dumps(sink_cr_signed)) # Sign Sink CSR try: sink_csr_signed = sign_csr(account_id=account_id, payload=sink_csr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Sink's CSR: " + repr(exp)) raise else: logger.info("Sink's CSR signed") logger.debug("sink_csr_signed: " + json.dumps(sink_csr_signed)) ######### # Store # ######### logger.info("Creating objects to store") # Source SLR try: logger.info("Creating Source SLR") source_slr_entry = ServiceLinkRecord( surrogate_id=source_cr_surrogate_id, account_id=account_id, service_link_record_id=source_cr_slr_id) except Exception as exp: error_title = "Failed to create Source's Service Link Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("source_slr_entry created") logger.info("source_slr_entry: " + source_slr_entry.log_entry) # Sink SLR try: logger.info("Creating Sink SLR") sink_slr_entry = ServiceLinkRecord( surrogate_id=sink_cr_surrogate_id, account_id=account_id, service_link_record_id=sink_cr_slr_id) except Exception as exp: error_title = "Failed to create Sink's Service Link Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("sink_slr_entry created") logger.info("sink_slr_entry: " + sink_slr_entry.log_entry) # Source CR try: logger.info("Creating Source CR") source_cr_entry = ConsentRecord( consent_record=source_cr_signed, consent_id=source_cr_cr_id, surrogate_id=source_cr_surrogate_id, resource_set_id=source_cr_rs_id, service_link_record_id=source_cr_slr_id, subject_id=source_cr_subject_id, role=source_cr_role) except Exception as exp: error_title = "Failed to create Source's Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("source_cr_entry created") logger.info("source_cr_entry: " + source_cr_entry.log_entry) # Sink CR try: logger.info("Creating Sink CR") sink_cr_entry = ConsentRecord( consent_record=sink_cr_signed, consent_id=sink_cr_cr_id, surrogate_id=sink_cr_surrogate_id, resource_set_id=sink_cr_rs_id, service_link_record_id=sink_cr_slr_id, subject_id=sink_cr_subject_id, role=sink_cr_role) except Exception as exp: error_title = "Failed to create Sink's Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("sink_cr_entry created") logger.info("sink_cr_entry: " + sink_cr_entry.log_entry) # Source CSR try: logger.info("Creating Source CSR") source_csr_entry = ConsentStatusRecord( consent_status_record_id=source_csr_record_id, status=source_csr_consent_status, consent_status_record=source_csr_signed, consent_record_id=source_csr_cr_id, issued_at=source_csr_issued, prev_record_id=source_csr_prev_record_id) except Exception as exp: error_title = "Failed to create Source's Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("source_csr_entry created") logger.info("source_csr_entry: " + source_csr_entry.log_entry) # Sink CSR try: logger.info("Creating Sink CSR") sink_csr_entry = ConsentStatusRecord( consent_status_record_id=sink_csr_record_id, status=sink_csr_consent_status, consent_status_record=sink_csr_signed, consent_record_id=sink_csr_cr_id, issued_at=sink_csr_issued, prev_record_id=sink_csr_prev_record_id) except Exception as exp: error_title = "Failed to create Sink's Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("sink_csr_entry created") logger.info("sink_csr_entry: " + sink_csr_entry.log_entry) # Store CRs and CSRs try: logger.info( "About to store Consent Records and Consent Status Records") stored_source_cr_entry, stored_source_csr_entry, stored_sink_cr_entry, stored_sink_csr_entry = store_cr_and_csr( source_slr_entry=source_slr_entry, sink_slr_entry=sink_slr_entry, source_cr_entry=source_cr_entry, source_csr_entry=source_csr_entry, sink_cr_entry=sink_cr_entry, sink_csr_entry=sink_csr_entry, endpoint=endpoint) except Exception as exp: error_title = "Could not store Consent Record and Consent Status Record" logger.error(error_title + ": " + repr(exp)) raise else: logger.info("Stored Consent Record and Consent Status Record") logger.info("Source CR: " + stored_source_cr_entry.log_entry) logger.info("Source CSR: " + stored_source_csr_entry.log_entry) logger.info("Sink CR: " + stored_sink_cr_entry.log_entry) logger.info("Sink CSR: " + stored_sink_csr_entry.log_entry) # Response data container try: response_data = {} response_data['data'] = {} response_data['data']['source'] = {} response_data['data']['source']['consentRecord'] = {} response_data['data']['source']['consentRecord'][ 'type'] = "ConsentRecord" response_data['data']['source']['consentRecord']['attributes'] = {} response_data['data']['source']['consentRecord']['attributes'][ 'cr'] = stored_source_cr_entry.to_record_dict response_data['data']['source']['consentStatusRecord'] = {} response_data['data']['source']['consentStatusRecord'][ 'type'] = "ConsentStatusRecord" response_data['data']['source']['consentStatusRecord'][ 'attributes'] = {} response_data['data']['source']['consentStatusRecord'][ 'attributes']['csr'] = stored_source_csr_entry.to_record_dict response_data['data']['sink'] = {} response_data['data']['sink']['consentRecord'] = {} response_data['data']['sink']['consentRecord'][ 'type'] = "ConsentRecord" response_data['data']['sink']['consentRecord']['attributes'] = {} response_data['data']['sink']['consentRecord']['attributes'][ 'cr'] = stored_sink_cr_entry.to_record_dict response_data['data']['sink']['consentStatusRecord'] = {} response_data['data']['sink']['consentStatusRecord'][ 'type'] = "ConsentStatusRecord" response_data['data']['sink']['consentStatusRecord'][ 'attributes'] = {} response_data['data']['sink']['consentStatusRecord']['attributes'][ 'csr'] = stored_sink_csr_entry.to_record_dict except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + json.dumps(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + json.dumps(response_data_dict)) return make_json_response(data=response_data_dict, status_code=201)
def get(self, sink_cr_id): try: endpoint = str(api.url_for(self, sink_cr_id=sink_cr_id)) except Exception as exp: endpoint = str(__name__) try: api_key = request.headers.get('Api-Key') except Exception as exp: logger.error("No ApiKey in headers") logger.debug("No ApiKey in headers: " + repr(repr(exp))) return provideApiKey(endpoint=endpoint) try: sink_cr_id = str(sink_cr_id) except Exception as exp: raise ApiError(code=400, title="Unsupported sink_cr_id", detail=repr(exp), source=endpoint) else: logger.debug("sink_cr_id: " + repr(sink_cr_id)) # Init Sink's Consent Record Object try: sink_cr_entry = ConsentRecord(consent_id=sink_cr_id, role="Sink") except Exception as exp: error_title = "Failed to create Sink's Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("sink_cr_entry: " + sink_cr_entry.log_entry) try: source_cr, sink_slr = get_auth_token_data( sink_cr_object=sink_cr_entry) except Exception as exp: error_title = "Failed to get Authorization token data" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("source_cr: " + source_cr.log_entry) logger.debug("sink_slr: " + sink_slr.log_entry) # Response data container try: response_data = {} response_data['data'] = {} response_data['data']['source'] = {} response_data['data']['source']['consentRecord'] = {} response_data['data']['source']['consentRecord'][ 'type'] = "ConsentRecord" response_data['data']['source']['consentRecord']['attributes'] = {} response_data['data']['source']['consentRecord']['attributes'][ 'cr'] = source_cr.to_record_dict response_data['data']['sink'] = {} response_data['data']['sink']['serviceLinkRecord'] = {} response_data['data']['sink']['serviceLinkRecord'][ 'type'] = "ServiceLinkRecord" response_data['data']['sink']['serviceLinkRecord'][ 'attributes'] = {} response_data['data']['sink']['serviceLinkRecord']['attributes'][ 'slr'] = sink_slr.to_record_dict except Exception as exp: logger.error('Could not prepare response data: ' + repr(exp)) raise ApiError(code=500, title="Could not prepare response data", detail=repr(exp), source=endpoint) else: logger.info('Response data ready') logger.debug('response_data: ' + json.dumps(response_data)) response_data_dict = dict(response_data) logger.debug('response_data_dict: ' + json.dumps(response_data_dict)) return make_json_response(data=response_data_dict, status_code=200)
def get_crs(surrogate_id="", slr_id="", subject_id="", consent_pair_id="", account_id="", status_id="", consent_pairs=False): """ Get Consent Records :param surrogate_id: :param slr_id: :param subject_id: :param consent_pair_id: :param account_id: :return: """ try: surrogate_id = str(surrogate_id) except Exception: raise TypeError("surrogate_id MUST be str, not " + str(type(surrogate_id))) try: slr_id = str(slr_id) except Exception: raise TypeError("slr_id MUST be str, not " + str(type(slr_id))) try: subject_id = str(subject_id) except Exception: raise TypeError("subject_id MUST be str, not " + str(type(subject_id))) try: consent_pair_id = str(consent_pair_id) except Exception: raise TypeError("consent_pair_id MUST be str, not " + str(type(consent_pair_id))) try: account_id = str(account_id) except Exception: raise TypeError("account_id MUST be str, not " + str(type(account_id))) try: status_id = str(status_id) except Exception: raise TypeError("status_id MUST be str, not " + str(type(status_id))) try: consent_pairs = bool(consent_pairs) except Exception: raise TypeError("consent_pairs MUST be bool, not " + str(type(consent_pairs))) logger.info("surrogate_id: " + surrogate_id) logger.info("slr_id: " + slr_id) logger.info("subject_id: " + subject_id) logger.info("consent_pair_id: " + consent_pair_id) logger.info("account_id: " + account_id) logger.info("status_id: " + status_id) if consent_pairs: logger.info("consent_pairs: True") else: logger.info("consent_pairs: False") # Get table name logger.info("Create ConsentRecord object") db_entry_object = ConsentRecord() logger.info(db_entry_object.log_entry) logger.info("Get table name") table_name = db_entry_object.table_name logger.info("Got table name: " + str(table_name)) # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise # Get primary keys for slr try: cursor, id_list = get_consent_ids(cursor=cursor, surrogate_id=surrogate_id, slr_id=slr_id, subject_id=subject_id, consent_pair_id=consent_pair_id, account_id=account_id, table_name=table_name) except Exception as exp: logger.error('Could not get primary key list: ' + repr(exp)) raise # Get ConsentRecords from database logger.info("Get ConsentRecords from database") cr_list = [] account_id_list = [] logger.info("Getting ConsentRecords") for entry_id in id_list: logger.info("Getting ConsentRecord with cr_id: " + str(entry_id)) db_entry_dict, account_id_from_db = get_cr(cr_id=entry_id, account_id=account_id) cr_list.append(db_entry_dict) account_id_list.append(account_id_from_db) logger.info("ConsentRecord object added to list: " + json.dumps(db_entry_dict)) logger.info("account_id added to list: " + str(account_id_from_db)) if consent_pairs: logger.info("Getting Consent Record pairs") for entry_id in id_list: logger.info("Getting ConsentRecord with consent_pair_id: " + str(entry_id)) db_entry_dict, account_id_from_db = get_cr( consent_pair_id=entry_id, account_id=account_id) cr_list.append(db_entry_dict) account_id_list.append(account_id_from_db) logger.info("ConsentRecord object added to list: " + json.dumps(db_entry_dict)) logger.info("account_id added to list: " + str(account_id_from_db)) logger.info("ConsentRecords fetched: " + json.dumps(cr_list)) try: account_id_list = list(set(account_id_list)) except Exception as exp: logger.error('Could not remove duplicates from account id listing: ' + repr(exp)) raise return cr_list, account_id_list
def get_cr(cr_id="", surrogate_id="", slr_id="", subject_id="", consent_pair_id="", account_id="", cursor=None): """ Get Consent Record entry :param account_id: :param slr_id: :return: dict """ try: cr_id = str(cr_id) except Exception: raise TypeError("cr_id MUST be str, not " + str(type(cr_id))) try: surrogate_id = str(surrogate_id) except Exception: raise TypeError("surrogate_id MUST be str, not " + str(type(surrogate_id))) try: slr_id = str(slr_id) except Exception: raise TypeError("slr_id MUST be str, not " + str(type(slr_id))) try: subject_id = str(subject_id) except Exception: raise TypeError("subject_id MUST be str, not " + str(type(subject_id))) try: consent_pair_id = str(consent_pair_id) except Exception: raise TypeError("consent_pair_id MUST be str, not " + str(type(consent_pair_id))) try: account_id = str(account_id) except Exception: raise TypeError("account_id MUST be str, not " + str(type(account_id))) if cursor is None: # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise try: db_entry_object = ConsentRecord(consent_id=cr_id, surrogate_id=surrogate_id, service_link_record_id=slr_id, subject_id=subject_id, consent_pair_id=consent_pair_id, accounts_id=account_id) except Exception as exp: error_title = "Failed to create ConsentRecord object" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("ConsentRecord object created: " + db_entry_object.log_entry) # Get slr from DB try: cursor = db_entry_object.from_db(cursor=cursor) except Exception as exp: error_title = "Failed to fetch ConsentRecord from DB" logger.error(error_title + ": " + repr(exp)) raise else: logger.info("ConsentRecord fetched") logger.debug("ConsentRecord fetched from db: " + db_entry_object.log_entry) return db_entry_object.to_api_dict, str(db_entry_object.accounts_id)
def store_csr(account_id=None, record_id=None, cr_id=None, surrogate_id=None, consent_status=None, iat=None, prev_record_id=None, csr_signed=None, endpoint="store_csr()"): # Parameter Check if account_id is None: raise AttributeError("Provide account_id as parameter") if record_id is None: raise AttributeError("Provide record_id as parameter") if cr_id is None: raise AttributeError("Provide cr_id as parameter") if surrogate_id is None: raise AttributeError("Provide surrogate_id as parameter") if consent_status is None: raise AttributeError("Provide consent_status as parameter") if iat is None: raise AttributeError("Provide iat as parameter") if prev_record_id is None: raise AttributeError("Provide prev_record_id as parameter") if csr_signed is None: raise AttributeError("Provide csr_signed as parameter") # Parameter type check try: account_id = str(account_id) except Exception: raise TypeError("account_id MUST be str, not " + str(type(account_id))) try: record_id = str(record_id) except Exception: raise TypeError("record_id MUST be str, not " + str(type(record_id))) try: cr_id = str(cr_id) except Exception: raise TypeError("cr_id MUST be str, not " + str(type(cr_id))) try: surrogate_id = str(surrogate_id) except Exception: raise TypeError("surrogate_id MUST be str, not " + str(type(surrogate_id))) try: consent_status = str(consent_status) except Exception: raise TypeError("consent_status MUST be str, not " + str(type(consent_status))) try: iat = int(iat) except Exception: raise TypeError("iat MUST be int, not " + str(type(iat))) try: prev_record_id = str(prev_record_id) except Exception: raise TypeError("prev_record_id MUST be str, not " + str(type(prev_record_id))) if not isinstance(csr_signed, dict): raise TypeError("csr_signed MUST be dict, not " + str(type(csr_signed))) ###### # Base information #### # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) ########### # Entries # ########### # Existing Consent Record ### # Init Consent Record Object try: logger.info("Create ConsentRecord object") cr_entry = ConsentRecord(consent_id=cr_id, accounts_id=account_id, surrogate_id=surrogate_id) except Exception as exp: error_title = "Failed to create Consent Record object" error_detail = str(exp.message) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("cr_entry: " + cr_entry.log_entry) # Get Consent Record from DB try: logger.info("Get Consent Record from DB") cursor = cr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "Consent Record not found" error_detail = str(exp.message) logger.error(error_title + ": " + repr(exp)) raise IndexError(error_title + " - " + error_detail) except Exception as exp: error_title = "Failed to fetch Consent Record" error_detail = str(exp.message) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("cr_entry: " + cr_entry.log_entry) # Get primary key of Consent Record database entry try: logger.info("Get primary key of Consent Record database entry") cr_entry_primary_key = cr_entry.id except Exception as exp: error_title = "Failed to fetch Consent Record primary key" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise KeyError(error_title + " - " + error_detail) else: logger.debug("cr_entry_primary_key: " + str(cr_entry_primary_key)) # CSR try: logger.info("Create ConsentStatusRecord object") csr_entry = ConsentStatusRecord( consent_status_record_id=record_id, status=consent_status, consent_status_record=csr_signed, consent_record_id=cr_id, issued_at=iat, prev_record_id=prev_record_id, consent_records_id=int(cr_entry_primary_key), accounts_id=int(account_id)) except Exception as exp: error_title = "Failed to create Consent Status Record object" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.info("csr_entry: " + csr_entry.log_entry) ########### # Store # ########### # CSR # Get database table name for Consent Status Record try: logger.info("Get Consent Status Record table name") csr_table_name = csr_entry.table_name except Exception as exp: error_title = "Could not get table name of Consent Status Record database table" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.info("Got Consent Status Record table name: " + str(csr_table_name)) # Store CSR try: logger.info("Store ConsentStatusRecord") try: cursor = csr_entry.to_db(cursor=cursor) except Exception as exp: error_title = "Failed to store Consent Status Record of Sink Service" error_detail = repr(exp) logger.error(error_title + ": " + repr(exp)) raise RuntimeError(error_title + " - " + error_detail) else: logger.debug("csr_entry: " + csr_entry.log_entry) # Commit db.connection.commit() except Exception as exp: logger.error('Consent Status Record Commit failed: ' + repr(exp)) db.connection.rollback() logger.error('--> rollback') error_title = "Could not write to database" error_detail = repr(exp) logger.error(error_title + ": " + error_detail) raise else: logger.info("Consent Status Record commited") return csr_entry
def get_last_cr_status(consent_id=None, account_id="", endpoint="get_last_cr_status()"): if consent_id is None: raise AttributeError("Provide consent_id as parameter") if account_id is None: raise AttributeError("Provide account_id as parameter") # Get DB cursor try: logger.info("Getting database cursor") cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise # Init Consent Record Object try: logger.info("Create ConsentRecord object") cr_entry = ConsentRecord(consent_id=consent_id) logger.info(cr_entry.log_entry) except Exception as exp: error_title = "Failed to create Consent Record object" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("ConsentRecord object: " + cr_entry.log_entry) # Get Consent Record from DB try: logger.info("Getting Consent Record from DB") cursor = cr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "Consent Record not found from DB with given ID" logger.error(error_title + ": " + repr(exp)) raise except Exception as exp: error_title = "Failed to fetch Consent Record from DB" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("cr_entry: " + cr_entry.log_entry) # Create Consent Status Record object try: logger.info("Creating Consent Status Record object") csr_entry = ConsentStatusRecord() except Exception as exp: error_title = "Failed to create Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("Consent Status Record object: " + csr_entry.log_entry) # Get Consent Status Record ID try: logger.info("Getting ID of last Consent Status Record") cursor, csr_id = get_last_csr_id(cursor=cursor, consent_id=consent_id, account_id=account_id, table_name=csr_entry.table_name) except IndexError as exp: error_title = "Consent Status Record not found from DB with given Consent Record ID" logger.error(error_title + ": " + repr(exp)) raise except Exception as exp: error_title = "Failed to get last Consent Status Record ID from database" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("Consent Status Record ID: " + str(csr_id)) # Append IDs to Consent Status Record Object try: logger.info("Appending IDs to Consent Status Record object") csr_entry.consent_status_record_id = csr_id csr_entry.accounts_id = account_id except Exception as exp: error_title = "Failed to append IDs to Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise else: logger.info("Appended IDs to Consent Status Record object: " + csr_entry.log_entry) # Get Consent Status Record from DB try: logger.info("Getting Consent Status Record from DB") cursor = csr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "Consent Status Record not found from DB with given ID" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "Failed to fetch Consent Status Record from DB" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("Consent Status Record object: " + csr_entry.log_entry) return csr_entry.to_api_dict
def get_auth_token_data(sink_cr_object=None, endpoint="get_auth_token_data()"): if sink_cr_object is None: raise AttributeError("Provide sink_cr_object as parameter") # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise # Get Sink's CR from DB try: logger.info("Get Sink's CR from DB") cursor = sink_cr_object.from_db(cursor=cursor) except Exception as exp: error_title = "Failed to fetch Sink's CR from DB" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("sink_cr_object: " + sink_cr_object.log_entry) # Get required id's from Sink's CR try: logger.info("Get required id's from Sink's CR") sink_slr_primary_key = str(sink_cr_object.service_link_records_id) except Exception as exp: error_title = "Failed to get id's from Sink's CR" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("sink_slr_primary_key: " + sink_slr_primary_key) # Init Source's Consent Record Object try: logger.info("Init Source's Consent Record Object") source_cr_entry = ConsentRecord( consent_pair_id=sink_cr_object.consent_id, accounts_id=sink_cr_object.accounts_id, role="Source") except Exception as exp: error_title = "Failed to create Source's Consent Record object" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("source_cr_entry: " + source_cr_entry.log_entry) # Get Source's Consent Record from DB try: logger.info("Get Source's Consent Record from DB") cursor = source_cr_entry.from_db(cursor=cursor) except Exception as exp: error_title = "Failed to fetch Source's CR from DB" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("source_cr_entry: " + source_cr_entry.log_entry) # Init Sink's Service Link Record Object try: logger.info("Init Sink's Service Link Record Object") sink_slr_entry = ServiceLinkRecord( id=sink_slr_primary_key, service_link_record_id=sink_cr_object.service_link_record_id) except Exception as exp: error_title = "Failed to create Source's Service Link Record object" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("sink_slr_entry: " + sink_slr_entry.log_entry) # Get Source's Consent Record from DB try: logger.info("Get Source's Consent Record from DB") cursor = sink_slr_entry.from_db(cursor=cursor) except Exception as exp: error_title = "Failed to fetch Sink's SLR from DB" logger.error(error_title + ": " + repr(exp)) raise else: logger.debug("sink_slr_entry: " + sink_slr_entry.log_entry) return source_cr_entry, sink_slr_entry
def get_account_id_by_cr(cr_id=None, endpoint="get_account_id_by_cr(cr_id, endpoint)"): if cr_id is None: raise AttributeError("Provide cr_id as parameter") logger.info("Executing for: " + str(endpoint)) ## # Account try: logger.info("Create Account object") account_entry = Account() except Exception as exp: error_title = "Failed to create Account object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("account_entry: " + account_entry.log_entry) # Get database table name for Consent Status Record try: logger.info("Get Account table name") account_table_name = account_entry.table_name except Exception as exp: error_title = "Failed to get Account table name" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got Account table name: " + str(account_table_name)) ## # ServiceLinkRecord try: logger.info("Create ServiceLinkRecord object") slr_entry = ServiceLinkRecord() except Exception as exp: error_title = "Failed to create ServiceLinkRecord object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("slr_entry: " + slr_entry.log_entry) # Get database table name for Consent Status Record try: logger.info("Get ServiceLinkRecord table name") slr_table_name = slr_entry.table_name except Exception as exp: error_title = "Failed to get ServiceLinkRecord table name" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got ServiceLinkRecord table name: " + str(slr_table_name)) ## # ConsentRecord try: logger.info("Create ConsentRecord object") cr_entry = ConsentRecord() except Exception as exp: error_title = "Failed to create Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("cr_entry: " + cr_entry.log_entry) # Get database table name for Consent Status Record try: logger.info("Get Consent Record table name") cr_table_name = cr_entry.table_name except Exception as exp: error_title = "Failed to get Consent Record table name" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got Consent Record table name: " + str(cr_table_name)) # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) # Get Account ID try: logger.info("Get Account ID") cursor, account_id = get_account_id_by_csr_id( cursor=cursor, cr_id=cr_id, acc_table_name=account_table_name, slr_table_name=slr_table_name, cr_table_name=cr_table_name) except IndexError as exp: error_title = "Account ID Not Found" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "Failed to get Account ID" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got Account ID: " + str(cr_table_name)) return account_id
def get_auth_token_data(sink_cr_object=None, endpoint="get_auth_token_data()"): if sink_cr_object is None: raise AttributeError("Provide sink_cr_object as parameter") # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) # Get Sink's CR from DB try: cursor = sink_cr_object.from_db(cursor=cursor) except Exception as exp: error_title = "Failed to fetch Sink's CR from DB" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) finally: logger.debug("sink_cr_object: " + sink_cr_object.log_entry) # Get required id's from Sink's CR try: sink_rs_id = str(sink_cr_object.resource_set_id) sink_slr_primary_key = str(sink_cr_object.service_link_records_id) except Exception as exp: error_title = "Failed to get id's from Sink's CR" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) finally: logger.debug("sink_rs_id: " + sink_rs_id) # Init Source's Consent Record Object try: source_cr_entry = ConsentRecord(resource_set_id=sink_rs_id, role="Source") except Exception as exp: error_title = "Failed to create Source's Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) finally: logger.debug("source_cr_entry: " + source_cr_entry.log_entry) # Get Source's Consent Record from DB source_cr = {} try: cursor = source_cr_entry.from_db(cursor=cursor) source_cr = source_cr_entry.consent_record except Exception as exp: error_title = "Failed to fetch Source's CR from DB" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) finally: logger.debug("source_cr_entry: " + source_cr_entry.log_entry) logger.debug("source_cr: " + json.dumps(source_cr)) # Init Sink's Service Link Record Object try: sink_slr_entry = ServiceLinkRecord(id=sink_slr_primary_key) except Exception as exp: error_title = "Failed to create Source's Service Link Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) finally: logger.debug("source_cr_entry: " + source_cr_entry.log_entry) # Get Source's Consent Record from DB sink_slr = {} try: cursor = sink_slr_entry.from_db(cursor=cursor) sink_slr = sink_slr_entry.service_link_record except Exception as exp: error_title = "Failed to fetch Sink's SLR from DB" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) finally: logger.debug("sink_slr_entry: " + sink_slr_entry.log_entry) logger.debug("sink_slr: " + json.dumps(sink_slr)) return source_cr, json.loads(sink_slr)
def add_csr(cr_id=None, csr_payload=None, endpoint="add_csr()"): if cr_id is None: raise AttributeError("Provide cr_id as parameter") if csr_payload is None: raise AttributeError("Provide csr_payload as parameter") ###### # Base information #### # Get DB cursor try: cursor = get_db_cursor() except Exception as exp: logger.error('Could not get database cursor: ' + repr(exp)) raise ApiError(code=500, title="Failed to get database cursor", detail=repr(exp), source=endpoint) # IDs from CSR payload try: logger.info("Fetching IDs from CSR payload") csr_surrogate_id = csr_payload['surrogate_id'] csr_cr_id = csr_payload['cr_id'] csr_prev_record_id = csr_payload['prev_record_id'] csr_record_id = csr_payload['record_id'] csr_consent_status = csr_payload['consent_status'] csr_issued = csr_payload['iat'] except Exception as exp: error_title = "Could not fetch IDs from CSR payload" logger.error(error_title) raise ApiError(code=400, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Fetched IDs from CSR payload") # Verify that cr_id and csr_cr_id are the same if cr_id != csr_cr_id: error_title = "cr_id from URI and cr_id from payload are not identical" logger.error(error_title + " | cr_id from URI: " + str(cr_id) + ", cr_id from payload: " + str(csr_cr_id)) raise ApiError(code=400, title=error_title, source=endpoint) else: logger.info("Identical IDs: cr_id from URI: " + str(cr_id) + ", cr_id from payload: " + str(csr_cr_id)) ###### # Account ID #### try: logger.info("Get Account ID by CSR_ID") account_id = get_account_id_by_cr(cr_id=cr_id, endpoint=endpoint) except Exception as exp: logger.error("Could not Account ID by CSR_ID: " + repr(exp)) raise else: logger.info("account_id: " + str(account_id)) ###### # Sign #### # Sign CSR try: logger.info("Sign CSR") csr_signed = sign_csr(account_id=account_id, payload=csr_payload, endpoint=endpoint) except Exception as exp: logger.error("Could not sign Source's CSR: " + repr(exp)) raise else: logger.info("Source CR signed") ########### # Entries # ########### # Existing Consent Record ### # Init Consent Record Object try: logger.info("Create ConsentRecord object") cr_entry = ConsentRecord(consent_id=cr_id) except Exception as exp: error_title = "Failed to create Consent Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("sink_cr_entry: " + cr_entry.log_entry) # Get Consent Record from DB try: logger.info("Get Consent Record from DB") cursor = cr_entry.from_db(cursor=cursor) except IndexError as exp: error_title = "Consent Record not found from DB with given ID" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=404, title=error_title, detail=repr(exp), source=endpoint) except Exception as exp: error_title = "Failed to fetch Consent Record from DB" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("cr_entry: " + cr_entry.log_entry) # Get primary key of Consent Record database entry try: logger.info("Get primary key of Consent Record database entry") cr_entry_primary_key = cr_entry.id except Exception as exp: error_title = "Failed to get primary key of Consent Record database entry" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("cr_entry_primary_key: " + str(cr_entry_primary_key)) # CSR try: logger.info("Create ConsentStatusRecord object") csr_entry = ConsentStatusRecord( consent_status_record_id=csr_record_id, status=csr_consent_status, consent_status_record=csr_signed, consent_record_id=cr_id, issued_at=int(csr_issued), prev_record_id=csr_prev_record_id, consent_records_id=int(cr_entry_primary_key) ) except Exception as exp: error_title = "Failed to create Source's Consent Status Record object" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("csr_entry: " + csr_entry.log_entry) ########### # Store # ########### # CSR # Get database table name for Consent Status Record try: logger.info("Get Consent Status Record table name") csr_table_name = csr_entry.table_name except Exception as exp: error_title = "Failed to get Consent Status Record table name" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.info("Got Consent Status Record table name: " + str(csr_table_name)) # Store CSR try: logger.info("Store ConsentStatusRecord") try: cursor = csr_entry.to_db(cursor=cursor) except Exception as exp: error_title = "Failed to store Consent Status Record" logger.error(error_title + ": " + repr(exp)) raise ApiError(code=500, title=error_title, detail=repr(exp), source=endpoint) else: logger.debug("csr_entry: " + csr_entry.log_entry) # Commit db.connection.commit() except Exception as exp: logger.error('Consent Status Record Commit failed: ' + repr(exp)) db.connection.rollback() logger.error('--> rollback') raise else: logger.info("Consent Status Record commited") return csr_entry