Verify (manually) phone index add works with cic-meta-server

This commit is contained in:
nolash 2021-04-07 15:34:45 +02:00
parent 805492ab61
commit 7fad15b66d
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 64 additions and 16 deletions

View File

@ -114,6 +114,7 @@ async function processRequest(req, res) {
return; return;
} }
if (!['PUT', 'GET', 'POST'].includes(req.method)) { if (!['PUT', 'GET', 'POST'].includes(req.method)) {
res.writeHead(405, {"Content-Type": "text/plain"}); res.writeHead(405, {"Content-Type": "text/plain"});
res.end(); res.end();
@ -123,6 +124,7 @@ async function processRequest(req, res) {
try { try {
digest = parseDigest(req.url); digest = parseDigest(req.url);
} catch(e) { } catch(e) {
console.error('digest error: ' + e)
res.writeHead(400, {"Content-Type": "text/plain"}); res.writeHead(400, {"Content-Type": "text/plain"});
res.end(); res.end();
return; return;

View File

@ -0,0 +1,7 @@
class Metadata:
"""
:cvar base_url:
:type base_url:
"""
base_url = None

View File

@ -1,21 +1,22 @@
# standard imports # standard imports
import json
import logging import logging
import os import os
# external imports # external imports
import requests
from cic_types.models.person import generate_metadata_pointer from cic_types.models.person import generate_metadata_pointer
from cic_ussd.metadata import make_request from cic_ussd.metadata import make_request
from cic_ussd.metadata.signer import Signer from cic_ussd.metadata.signer import Signer
# local imports # local imports
from cic_ussd.error import MetadataStoreError from cic_ussd.error import MetadataStoreError
from .base import Metadata
logg = logging.getLogger().getChild(__name__) logg = logging.getLogger().getChild(__name__)
class PhonePointerMetadata: class PhonePointerMetadata(Metadata):
base_url = None
def __init__(self, identifier: bytes, engine: str): def __init__(self, identifier: bytes, engine: str):
""" """
@ -34,14 +35,49 @@ class PhonePointerMetadata:
) )
if self.base_url: if self.base_url:
self.url = os.path.join(self.base_url, self.metadata_pointer) self.url = os.path.join(self.base_url, self.metadata_pointer)
self.engine = engine self.engine = engine
def create(self, data: str): def create(self, data: str):
try: try:
data = json.dumps(data).encode('utf-8')
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 = result.content metadata = result.content
logg.debug('data {} meta {} resp {} stats {}'.format(data, metadata, result.reason, result.status_code))
self.edit(data=metadata, engine=self.engine) self.edit(data=metadata, engine=self.engine)
result.raise_for_status() result.raise_for_status()
except requests.exceptions.HTTPError as error: except requests.exceptions.HTTPError as error:
raise MetadataStoreError(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)

View File

@ -13,16 +13,13 @@ from cic_ussd.metadata import make_request
from cic_ussd.metadata.signer import Signer from cic_ussd.metadata.signer import Signer
from cic_ussd.redis import cache_data from cic_ussd.redis import cache_data
from cic_ussd.error import MetadataStoreError from cic_ussd.error import MetadataStoreError
from .base import Metadata
logg = logging.getLogger() logg = logging.getLogger()
class UserMetadata: class UserMetadata(Metadata):
"""
:cvar base_url:
:type base_url:
"""
base_url = None
def __init__(self, identifier: bytes): def __init__(self, identifier: bytes):
""" """

View File

@ -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.files.local_files import create_local_file_data_stores, json_file_parser
from cic_ussd.menu.ussd_menu import UssdMenu from cic_ussd.menu.ussd_menu import UssdMenu
from cic_ussd.metadata.signer import Signer 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, from cic_ussd.operations import (define_response_with_content,
process_menu_interaction_requests, process_menu_interaction_requests,
define_multilingual_responses) define_multilingual_responses)
@ -100,7 +100,7 @@ InMemoryStore.cache = redis.StrictRedis(host=config.get('REDIS_HOSTNAME'),
InMemoryUssdSession.redis_cache = InMemoryStore.cache InMemoryUssdSession.redis_cache = InMemoryStore.cache
# define metadata URL # define metadata URL
UserMetadata.base_url = config.get('CIC_META_URL') Metadata.base_url = config.get('CIC_META_URL')
# define signer values # define signer values
export_dir = config.get('PGP_EXPORT_DIR') export_dir = config.get('PGP_EXPORT_DIR')

View File

@ -13,7 +13,7 @@ from confini import Config
from cic_ussd.db import dsn_from_config from cic_ussd.db import dsn_from_config
from cic_ussd.db.models.base import SessionBase from cic_ussd.db.models.base import SessionBase
from cic_ussd.metadata.signer import Signer 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.redis import InMemoryStore
from cic_ussd.session.ussd_session import UssdSession as InMemoryUssdSession from cic_ussd.session.ussd_session import UssdSession as InMemoryUssdSession
from cic_ussd.validator import validate_presence 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 InMemoryUssdSession.redis_cache = InMemoryStore.cache
# define metadata URL # define metadata URL
UserMetadata.base_url = config.get('CIC_META_URL') Metadata.base_url = config.get('CIC_META_URL')
# define signer values # define signer values
export_dir = config.get('PGP_EXPORT_DIR') export_dir = config.get('PGP_EXPORT_DIR')

View File

@ -2,8 +2,9 @@
import json import json
import logging import logging
# third-party imports # external imports
import celery import celery
from hexathon import strip_0x
# local imports # local imports
from cic_ussd.metadata import blockchain_address_to_metadata_pointer 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) @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) 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) phone_metadata_client.create(data=stripped_address)

View File

@ -80,6 +80,8 @@ phone_idx = []
user_dir = args.dir user_dir = args.dir
user_count = args.user_count user_count = args.user_count
random.seed()
def genPhoneIndex(phone): def genPhoneIndex(phone):
h = hashlib.new('sha256') h = hashlib.new('sha256')
h.update(phone.encode('utf-8')) h.update(phone.encode('utf-8'))

View File

@ -446,6 +446,7 @@ services:
cic-meta-server: cic-meta-server:
hostname: meta
build: build:
context: apps/ context: apps/
dockerfile: cic-meta/docker/Dockerfile dockerfile: cic-meta/docker/Dockerfile
@ -491,6 +492,7 @@ services:
DATABASE_DRIVER: psycopg2 DATABASE_DRIVER: psycopg2
PGP_PASSPHRASE: merman PGP_PASSPHRASE: merman
SERVER_PORT: 9000 SERVER_PORT: 9000
CIC_META_URL: ${CIC_META_URL:-http://meta:8000}
ports: ports:
- ${HTTP_PORT_CIC_USSD:-63315}:9000 - ${HTTP_PORT_CIC_USSD:-63315}:9000
depends_on: depends_on:
@ -519,6 +521,7 @@ services:
CELERY_BROKER_URL: ${CELERY_BROKER_URL:-redis://redis} CELERY_BROKER_URL: ${CELERY_BROKER_URL:-redis://redis}
CELERY_RESULT_URL: ${CELERY_BROKER_URL:-redis://redis} CELERY_RESULT_URL: ${CELERY_BROKER_URL:-redis://redis}
PGP_PASSPHRASE: merman PGP_PASSPHRASE: merman
CIC_META_URL: ${CIC_META_URL:-http://meta:8000}
depends_on: depends_on:
- postgres - postgres
- redis - redis