Refactors how meta handler interacts with cic-meta to fix bugs.

This commit is contained in:
PhilipWafula 2021-05-27 19:01:48 +03:00
parent 5b89c151f9
commit 9e4ad4c650
Signed by untrusted user: mango-habanero
GPG Key ID: B00CE9034DA19FB7
2 changed files with 29 additions and 18 deletions

View File

@ -78,28 +78,27 @@ class MetadataRequestsHandler(Metadata):
:param data: The data to be stored in the metadata server. :param data: The data to be stored in the metadata server.
:type data: dict|str :type data: dict|str
""" """
data = json.dumps(data).encode('utf-8') data = json.dumps(data)
result = make_request(method='POST', url=self.url, data=data, headers=self.headers) result = make_request(method='POST', url=self.url, data=data, headers=self.headers)
metadata_http_error_handler(result=result) metadata_http_error_handler(result=result)
metadata = result.content metadata = result.json()
self.edit(data=metadata) self.edit(data=metadata)
def edit(self, data: bytes): def edit(self, data: Union[Dict, str]):
""" This function is responsible for editing data in the metadata server corresponding to a unique pointer. """ This function is responsible for editing data in the metadata server corresponding to a unique pointer.
:param data: The data to be edited in the metadata server. :param data: The data to be edited in the metadata server.
:type data: bytes :type data: dict
""" """
cic_meta_signer = Signer() cic_meta_signer = Signer()
signature = cic_meta_signer.sign_digest(data=data) signature = cic_meta_signer.sign_digest(data=data)
algorithm = cic_meta_signer.get_operational_key().get('algo') algorithm = cic_meta_signer.get_operational_key().get('algo')
decoded_data = data.decode('utf-8')
formatted_data = { formatted_data = {
'm': data.decode('utf-8'), 'm': json.dumps(data),
's': { 's': {
'engine': self.engine, 'engine': self.engine,
'algo': algorithm, 'algo': algorithm,
'data': signature, 'data': signature,
'digest': json.loads(data).get('digest'), 'digest': data.get('digest'),
} }
} }
formatted_data = json.dumps(formatted_data) formatted_data = json.dumps(formatted_data)
@ -110,19 +109,32 @@ class MetadataRequestsHandler(Metadata):
decoded_identifier = self.identifier.decode("utf-8") decoded_identifier = self.identifier.decode("utf-8")
except UnicodeDecodeError: except UnicodeDecodeError:
decoded_identifier = self.identifier.hex() decoded_identifier = self.identifier.hex()
logg.info(f'identifier: {decoded_identifier}. metadata pointer: {self.metadata_pointer} set to: {decoded_data}.') logg.info(f'identifier: {decoded_identifier}. metadata pointer: {self.metadata_pointer} set to: {data}.')
def query(self): def query(self):
"""This function is responsible for querying the metadata server for data corresponding to a unique pointer.""" """
:return:
:rtype:
"""
# retrieve the metadata
result = make_request(method='GET', url=self.url) result = make_request(method='GET', url=self.url)
metadata_http_error_handler(result=result) metadata_http_error_handler(result=result)
response_data = result.json()
data = json.loads(response_data) # json serialize retrieved data
if not isinstance(data, dict): result_data = result.json()
raise ValueError(f'Invalid data object: {data}.')
# validate result data format
if not isinstance(result_data, dict):
raise ValueError(f'Invalid result data object: {result_data}.')
if result.status_code == 200 and self.cic_type == ':cic.person': if result.status_code == 200 and self.cic_type == ':cic.person':
# validate person metadata
person = Person() person = Person()
deserialized_person = person.deserialize(person_data=data) person_data = person.deserialize(person_data=result_data)
data = json.dumps(deserialized_person.serialize())
cache_data(self.metadata_pointer, data=data) # format new person data for caching
data = json.dumps(person_data.serialize())
# cache metadata
cache_data(key=self.metadata_pointer, data=data)
logg.debug(f'caching: {data} with key: {self.metadata_pointer}') logg.debug(f'caching: {data} with key: {self.metadata_pointer}')

View File

@ -47,14 +47,13 @@ class Signer:
logg.debug(f'using signing key: {key_id}, algorithm: {key_algorithm}') logg.debug(f'using signing key: {key_id}, algorithm: {key_algorithm}')
return gpg_keys[0] return gpg_keys[0]
def sign_digest(self, data: bytes): def sign_digest(self, data: dict):
""" """
:param data: :param data:
:type data: :type data:
:return: :return:
:rtype: :rtype:
""" """
data = json.loads(data)
digest = data['digest'] digest = data['digest']
key_id = self.get_operational_key().get('keyid') key_id = self.get_operational_key().get('keyid')
signature = self.gpg.sign(digest, passphrase=self.gpg_passphrase, keyid=key_id) signature = self.gpg.sign(digest, passphrase=self.gpg_passphrase, keyid=key_id)