From 7fad15b66db09981e71d00bb9eec7a29f5c35aac Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 7 Apr 2021 15:34:45 +0200 Subject: [PATCH] Verify (manually) phone index add works with cic-meta-server --- apps/cic-meta/scripts/server/server.ts | 2 + apps/cic-ussd/cic_ussd/metadata/base.py | 7 ++++ apps/cic-ussd/cic_ussd/metadata/phone.py | 42 +++++++++++++++++-- apps/cic-ussd/cic_ussd/metadata/user.py | 9 ++-- apps/cic-ussd/cic_ussd/runnable/server.py | 4 +- apps/cic-ussd/cic_ussd/runnable/tasker.py | 4 +- apps/cic-ussd/cic_ussd/tasks/metadata.py | 7 ++-- .../scripts/create_import_users.py | 2 + docker-compose.yml | 3 ++ 9 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 apps/cic-ussd/cic_ussd/metadata/base.py diff --git a/apps/cic-meta/scripts/server/server.ts b/apps/cic-meta/scripts/server/server.ts index ca9c2dfb..9bef8bda 100755 --- a/apps/cic-meta/scripts/server/server.ts +++ b/apps/cic-meta/scripts/server/server.ts @@ -114,6 +114,7 @@ async function processRequest(req, res) { return; } + if (!['PUT', 'GET', 'POST'].includes(req.method)) { res.writeHead(405, {"Content-Type": "text/plain"}); res.end(); @@ -123,6 +124,7 @@ async function processRequest(req, res) { try { digest = parseDigest(req.url); } catch(e) { + console.error('digest error: ' + e) res.writeHead(400, {"Content-Type": "text/plain"}); res.end(); return; diff --git a/apps/cic-ussd/cic_ussd/metadata/base.py b/apps/cic-ussd/cic_ussd/metadata/base.py new file mode 100644 index 00000000..8f163d78 --- /dev/null +++ b/apps/cic-ussd/cic_ussd/metadata/base.py @@ -0,0 +1,7 @@ +class Metadata: + """ + :cvar base_url: + :type base_url: + """ + + base_url = None diff --git a/apps/cic-ussd/cic_ussd/metadata/phone.py b/apps/cic-ussd/cic_ussd/metadata/phone.py index 5ce1bdc6..f50ef0a3 100644 --- a/apps/cic-ussd/cic_ussd/metadata/phone.py +++ b/apps/cic-ussd/cic_ussd/metadata/phone.py @@ -1,21 +1,22 @@ # standard imports +import json import logging import os # external imports +import requests from cic_types.models.person import generate_metadata_pointer from cic_ussd.metadata import make_request from cic_ussd.metadata.signer import Signer # local imports from cic_ussd.error import MetadataStoreError +from .base import Metadata logg = logging.getLogger().getChild(__name__) -class PhonePointerMetadata: - - base_url = None +class PhonePointerMetadata(Metadata): def __init__(self, identifier: bytes, engine: str): """ @@ -34,14 +35,49 @@ class PhonePointerMetadata: ) if self.base_url: self.url = os.path.join(self.base_url, self.metadata_pointer) + self.engine = engine def create(self, data: str): try: + data = json.dumps(data).encode('utf-8') result = make_request(method='POST', url=self.url, data=data, headers=self.headers) metadata = result.content + logg.debug('data {} meta {} resp {} stats {}'.format(data, metadata, result.reason, result.status_code)) self.edit(data=metadata, engine=self.engine) result.raise_for_status() except requests.exceptions.HTTPError as error: raise MetadataStoreError(error) + + + def edit(self, data: bytes, engine: str): + """ + :param data: + :type data: + :param engine: + :type engine: + :return: + :rtype: + """ + cic_meta_signer = Signer() + signature = cic_meta_signer.sign_digest(data=data) + algorithm = cic_meta_signer.get_operational_key().get('algo') + formatted_data = { + 'm': data.decode('utf-8'), + 's': { + 'engine': engine, + 'algo': algorithm, + 'data': signature, + 'digest': json.loads(data).get('digest'), + } + } + formatted_data = json.dumps(formatted_data).encode('utf-8') + + try: + result = make_request(method='PUT', url=self.url, data=formatted_data, headers=self.headers) + logg.info(f'Signed content submission status: {result.status_code}.') + result.raise_for_status() + except requests.exceptions.HTTPError as error: + raise MetadataStoreError(error) + diff --git a/apps/cic-ussd/cic_ussd/metadata/user.py b/apps/cic-ussd/cic_ussd/metadata/user.py index 2485c1fd..2f8b3286 100644 --- a/apps/cic-ussd/cic_ussd/metadata/user.py +++ b/apps/cic-ussd/cic_ussd/metadata/user.py @@ -13,16 +13,13 @@ from cic_ussd.metadata import make_request from cic_ussd.metadata.signer import Signer from cic_ussd.redis import cache_data from cic_ussd.error import MetadataStoreError +from .base import Metadata logg = logging.getLogger() -class UserMetadata: - """ - :cvar base_url: - :type base_url: - """ - base_url = None +class UserMetadata(Metadata): + def __init__(self, identifier: bytes): """ diff --git a/apps/cic-ussd/cic_ussd/runnable/server.py b/apps/cic-ussd/cic_ussd/runnable/server.py index ad08dc80..ecb8481e 100644 --- a/apps/cic-ussd/cic_ussd/runnable/server.py +++ b/apps/cic-ussd/cic_ussd/runnable/server.py @@ -23,7 +23,7 @@ from cic_ussd.encoder import PasswordEncoder from cic_ussd.files.local_files import create_local_file_data_stores, json_file_parser from cic_ussd.menu.ussd_menu import UssdMenu from cic_ussd.metadata.signer import Signer -from cic_ussd.metadata.user import UserMetadata +from cic_ussd.metadata.base import Metadata from cic_ussd.operations import (define_response_with_content, process_menu_interaction_requests, define_multilingual_responses) @@ -100,7 +100,7 @@ InMemoryStore.cache = redis.StrictRedis(host=config.get('REDIS_HOSTNAME'), InMemoryUssdSession.redis_cache = InMemoryStore.cache # define metadata URL -UserMetadata.base_url = config.get('CIC_META_URL') +Metadata.base_url = config.get('CIC_META_URL') # define signer values export_dir = config.get('PGP_EXPORT_DIR') diff --git a/apps/cic-ussd/cic_ussd/runnable/tasker.py b/apps/cic-ussd/cic_ussd/runnable/tasker.py index 0c341e61..6d787f9d 100644 --- a/apps/cic-ussd/cic_ussd/runnable/tasker.py +++ b/apps/cic-ussd/cic_ussd/runnable/tasker.py @@ -13,7 +13,7 @@ from confini import Config from cic_ussd.db import dsn_from_config from cic_ussd.db.models.base import SessionBase from cic_ussd.metadata.signer import Signer -from cic_ussd.metadata.user import UserMetadata +from cic_ussd.metadata.base import Metadata from cic_ussd.redis import InMemoryStore from cic_ussd.session.ussd_session import UssdSession as InMemoryUssdSession from cic_ussd.validator import validate_presence @@ -63,7 +63,7 @@ InMemoryStore.cache = redis.StrictRedis(host=config.get('REDIS_HOSTNAME'), InMemoryUssdSession.redis_cache = InMemoryStore.cache # define metadata URL -UserMetadata.base_url = config.get('CIC_META_URL') +Metadata.base_url = config.get('CIC_META_URL') # define signer values export_dir = config.get('PGP_EXPORT_DIR') diff --git a/apps/cic-ussd/cic_ussd/tasks/metadata.py b/apps/cic-ussd/cic_ussd/tasks/metadata.py index 3f7085f5..a9d6948e 100644 --- a/apps/cic-ussd/cic_ussd/tasks/metadata.py +++ b/apps/cic-ussd/cic_ussd/tasks/metadata.py @@ -2,8 +2,9 @@ import json import logging -# third-party imports +# external imports import celery +from hexathon import strip_0x # local imports from cic_ussd.metadata import blockchain_address_to_metadata_pointer @@ -51,7 +52,7 @@ def edit_user_metadata(blockchain_address: str, data: bytes, engine: str): @celery_app.task(bind=True, base=CriticalMetadataTask) -def add_phone_pointer(blockchain_address: str, phone: str, engine: str): +def add_phone_pointer(self, blockchain_address: str, phone: str, engine: str): stripped_address = strip_0x(blockchain_address) - phone_metadata_client = PhonePointerMetadata(identifier=phone, engine=engine) + phone_metadata_client = PhonePointerMetadata(identifier=phone.encode('utf-8'), engine=engine) phone_metadata_client.create(data=stripped_address) diff --git a/apps/contract-migration/scripts/create_import_users.py b/apps/contract-migration/scripts/create_import_users.py index b49c3202..112a1f76 100644 --- a/apps/contract-migration/scripts/create_import_users.py +++ b/apps/contract-migration/scripts/create_import_users.py @@ -80,6 +80,8 @@ phone_idx = [] user_dir = args.dir user_count = args.user_count +random.seed() + def genPhoneIndex(phone): h = hashlib.new('sha256') h.update(phone.encode('utf-8')) diff --git a/docker-compose.yml b/docker-compose.yml index 65fb4109..6f20e1fd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -446,6 +446,7 @@ services: cic-meta-server: + hostname: meta build: context: apps/ dockerfile: cic-meta/docker/Dockerfile @@ -491,6 +492,7 @@ services: DATABASE_DRIVER: psycopg2 PGP_PASSPHRASE: merman SERVER_PORT: 9000 + CIC_META_URL: ${CIC_META_URL:-http://meta:8000} ports: - ${HTTP_PORT_CIC_USSD:-63315}:9000 depends_on: @@ -519,6 +521,7 @@ services: CELERY_BROKER_URL: ${CELERY_BROKER_URL:-redis://redis} CELERY_RESULT_URL: ${CELERY_BROKER_URL:-redis://redis} PGP_PASSPHRASE: merman + CIC_META_URL: ${CIC_META_URL:-http://meta:8000} depends_on: - postgres - redis