From ea336283dcee52997c3d0e41b0b1c57ed40abcb6 Mon Sep 17 00:00:00 2001 From: Philip Wafula Date: Fri, 5 Mar 2021 16:28:07 +0000 Subject: [PATCH] Philip/dry run prep --- apps/cic-ussd/.config/database.ini | 2 +- apps/cic-ussd/.config/pgp.ini | 9 +++++---- apps/cic-ussd/.config/test/pgp.ini | 9 +++++---- apps/cic-ussd/cic_ussd/processor.py | 15 +++++++-------- apps/cic-ussd/cic_ussd/runnable/server.py | 6 +++--- apps/cic-ussd/cic_ussd/runnable/tasker.py | 7 +++---- apps/cic-ussd/cic_ussd/tasks/callback_handler.py | 6 ++---- apps/cic-ussd/docker/Dockerfile | 3 +++ apps/cic-ussd/setup.cfg | 1 + apps/cic-ussd/tests/fixtures/config.py | 4 ++-- docker-compose.yml | 6 ++++++ 11 files changed, 38 insertions(+), 30 deletions(-) diff --git a/apps/cic-ussd/.config/database.ini b/apps/cic-ussd/.config/database.ini index 3d28342..7e2c3e4 100644 --- a/apps/cic-ussd/.config/database.ini +++ b/apps/cic-ussd/.config/database.ini @@ -1,7 +1,7 @@ [database] NAME=cic_ussd USER=postgres -PASSWORD=password +PASSWORD= HOST=localhost PORT=5432 ENGINE=postgresql diff --git a/apps/cic-ussd/.config/pgp.ini b/apps/cic-ussd/.config/pgp.ini index 955d7d3..c06f283 100644 --- a/apps/cic-ussd/.config/pgp.ini +++ b/apps/cic-ussd/.config/pgp.ini @@ -1,4 +1,5 @@ -[keys] -public = ../cic-internal-integration/apps/contract-migration/testdata/pgp/publickeys_meta.asc -private = ../cic-internal-integration/apps/contract-migration/testdata/pgp/privatekeys_meta.asc -passphrase = merman +[pgp] +export_dir = /usr/src/pgp/keys/ +keys_path = /usr/src/secrets/ +private_keys = privatekeys_meta.asc +passphrase = diff --git a/apps/cic-ussd/.config/test/pgp.ini b/apps/cic-ussd/.config/test/pgp.ini index 955d7d3..c06f283 100644 --- a/apps/cic-ussd/.config/test/pgp.ini +++ b/apps/cic-ussd/.config/test/pgp.ini @@ -1,4 +1,5 @@ -[keys] -public = ../cic-internal-integration/apps/contract-migration/testdata/pgp/publickeys_meta.asc -private = ../cic-internal-integration/apps/contract-migration/testdata/pgp/privatekeys_meta.asc -passphrase = merman +[pgp] +export_dir = /usr/src/pgp/keys/ +keys_path = /usr/src/secrets/ +private_keys = privatekeys_meta.asc +passphrase = diff --git a/apps/cic-ussd/cic_ussd/processor.py b/apps/cic-ussd/cic_ussd/processor.py index d294910..5087fb1 100644 --- a/apps/cic-ussd/cic_ussd/processor.py +++ b/apps/cic-ussd/cic_ussd/processor.py @@ -102,9 +102,9 @@ def process_exit_successful_transaction(display_key: str, user: User, ussd_sessi transaction_amount = to_wei(int(ussd_session.get('session_data').get('transaction_amount'))) token_symbol = 'SRF' recipient_phone_number = ussd_session.get('session_data').get('recipient_phone_number') - sender_phone_number = user.phone_number - tx_recipient_information = recipient_phone_number - tx_sender_information = sender_phone_number + recipient = get_user_by_phone_number(phone_number=recipient_phone_number) + tx_recipient_information = define_account_tx_metadata(user=recipient) + tx_sender_information = define_account_tx_metadata(user=user) return translation_for( key=display_key, @@ -184,10 +184,10 @@ def format_transactions(transactions: list, preferred_language: str): for transaction in transactions: recipient_phone_number = transaction.get('recipient_phone_number') sender_phone_number = transaction.get('sender_phone_number') - value = transaction.get('destination_value') + value = transaction.get('to_value') timestamp = transaction.get('timestamp') action_tag = transaction.get('action_tag') - token_symbol = transaction.get('destination_token_symbol') + token_symbol = 'SRF' if action_tag == 'SENT' or action_tag == 'ULITUMA': formatted_transactions += f'{action_tag} {value} {token_symbol} {recipient_phone_number} {timestamp}.\n' @@ -222,6 +222,8 @@ def process_account_statement(user: User, display_key: str, ussd_session: dict): middle_transaction_set = [] last_transaction_set = [] + transactions = json.loads(transactions) + if len(transactions) > 6: last_transaction_set += transactions[6:] middle_transaction_set += transactions[3:][:3] @@ -233,10 +235,7 @@ def process_account_statement(user: User, display_key: str, ussd_session: dict): else: first_transaction_set += transactions[:3] - logg.debug(f'TRANSACTIONS: {transactions}') - if display_key == 'ussd.kenya.first_transaction_set': - logg.debug(f'FIRST TRANSACTION SET: {first_transaction_set}') return translation_for( key=display_key, preferred_language=user.preferred_language, diff --git a/apps/cic-ussd/cic_ussd/runnable/server.py b/apps/cic-ussd/cic_ussd/runnable/server.py index 36d764c..a7bdf93 100644 --- a/apps/cic-ussd/cic_ussd/runnable/server.py +++ b/apps/cic-ussd/cic_ussd/runnable/server.py @@ -101,9 +101,9 @@ InMemoryUssdSession.redis_cache = InMemoryStore.cache UserMetadata.base_url = config.get('CIC_META_URL') # define signer values -Signer.gpg_path = '/tmp/.gpg' -Signer.gpg_passphrase = config.get('KEYS_PASSPHRASE') -Signer.key_file_path = config.get('KEYS_PRIVATE') +Signer.gpg_path = config.get('PGP_EXPORT_DIR') +Signer.gpg_passphrase = config.get('PGP_PASSPHRASE') +Signer.key_file_path = f"{config.get('PGP_KEYS_PATH')}{config.get('PGP_PRIVATE_KEYS')}" # initialize celery app celery.Celery(backend=config.get('CELERY_RESULT_URL'), broker=config.get('CELERY_BROKER_URL')) diff --git a/apps/cic-ussd/cic_ussd/runnable/tasker.py b/apps/cic-ussd/cic_ussd/runnable/tasker.py index 52c84aa..1cdbe47 100644 --- a/apps/cic-ussd/cic_ussd/runnable/tasker.py +++ b/apps/cic-ussd/cic_ussd/runnable/tasker.py @@ -39,7 +39,6 @@ config.censor('PASSWORD', 'DATABASE') # define log levels if args.vv: logging.getLogger().setLevel(logging.DEBUG) - logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG) elif args.v: logging.getLogger().setLevel(logging.INFO) @@ -66,9 +65,9 @@ InMemoryUssdSession.redis_cache = InMemoryStore.cache UserMetadata.base_url = config.get('CIC_META_URL') # define signer values -Signer.gpg_path = '/tmp/.gpg' -Signer.gpg_passphrase = config.get('KEYS_PASSPHRASE') -Signer.key_file_path = config.get('KEYS_PRIVATE') +Signer.gpg_path = config.get('PGP_EXPORT_DIR') +Signer.gpg_passphrase = config.get('PGP_PASSPHRASE') +Signer.key_file_path = f"{config.get('PGP_KEYS_PATH')}{config.get('PGP_PRIVATE_KEYS')}" # set up celery current_app = celery.Celery(__name__) diff --git a/apps/cic-ussd/cic_ussd/tasks/callback_handler.py b/apps/cic-ussd/cic_ussd/tasks/callback_handler.py index 575a571..2d4fca7 100644 --- a/apps/cic-ussd/cic_ussd/tasks/callback_handler.py +++ b/apps/cic-ussd/cic_ussd/tasks/callback_handler.py @@ -165,7 +165,6 @@ def process_statement_callback(result, param: str, status_code: int): sender_blockchain_address = transaction.get('sender') recipient_address = transaction.get('recipient') source_token = transaction.get('source_token') - destination_token_symbol = transaction.get('destination_token_symbol') # filter out any transactions that are "gassy" if '0x0000000000000000000000000000000000000000' in source_token: @@ -198,9 +197,8 @@ def process_statement_callback(result, param: str, status_code: int): logg.warning(f'Tx with recipient not found in cic-ussd') # add transaction values - processed_transaction['destination_value'] = from_wei(value=transaction.get('destination_value')) - processed_transaction['destination_token_symbol'] = destination_token_symbol - processed_transaction['source_value'] = from_wei(value=transaction.get('source_value')) + processed_transaction['to_value'] = from_wei(value=transaction.get('to_value')) + processed_transaction['from_value'] = from_wei(value=transaction.get('from_value')) raw_timestamp = transaction.get('timestamp') timestamp = datetime.utcfromtimestamp(raw_timestamp).strftime('%d/%m/%y, %H:%M') diff --git a/apps/cic-ussd/docker/Dockerfile b/apps/cic-ussd/docker/Dockerfile index 5ffa702..9f9e12a 100644 --- a/apps/cic-ussd/docker/Dockerfile +++ b/apps/cic-ussd/docker/Dockerfile @@ -11,6 +11,9 @@ RUN apk update && \ apk add git linux-headers postgresql-dev gnupg bash RUN apk add --update musl-dev gcc libffi-dev +# create secrets directory +RUN mkdir -vp pgp/keys + # create application directory RUN mkdir -vp cic-ussd diff --git a/apps/cic-ussd/setup.cfg b/apps/cic-ussd/setup.cfg index 138e5b1..620483a 100644 --- a/apps/cic-ussd/setup.cfg +++ b/apps/cic-ussd/setup.cfg @@ -33,6 +33,7 @@ packages = cic_ussd.db.models cic_ussd.files cic_ussd.menu + cic_ussd.metadata cic_ussd.runnable cic_ussd.session cic_ussd.state_machine diff --git a/apps/cic-ussd/tests/fixtures/config.py b/apps/cic-ussd/tests/fixtures/config.py index e43fb09..ba03f4d 100644 --- a/apps/cic-ussd/tests/fixtures/config.py +++ b/apps/cic-ussd/tests/fixtures/config.py @@ -114,8 +114,8 @@ def setup_metadata_signer(load_config): temp_dir = tempfile.mkdtemp(dir='/tmp') logg.debug(f'Created temp dir: {temp_dir}') Signer.gpg_path = temp_dir - Signer.key_file_path = load_config.get('KEYS_PRIVATE') - Signer.gpg_passphrase = load_config.get('KEYS_PASSPHRASE') + Signer.gpg_passphrase = load_config.get('PGP_PASSPHRASE') + Signer.key_file_path = f"{load_config.get('PGP_KEYS_PATH')}{load_config.get('PGP_PRIVATE_KEYS')}" @pytest.fixture(scope='function') diff --git a/docker-compose.yml b/docker-compose.yml index 28762eb..33381e7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -466,12 +466,15 @@ services: DATABASE_NAME: cic_ussd DATABASE_ENGINE: postgresql DATABASE_DRIVER: psycopg2 + PGP_PASSPHRASE: merman SERVER_PORT: 8000 ports: - ${HTTP_PORT_CIC_USSD:-63315}:8000 depends_on: - postgres - redis + volumes: + - ./apps/contract-migration/testdata/pgp/:/usr/src/secrets/ deploy: restart_policy: condition: on-failure @@ -492,9 +495,12 @@ services: DATABASE_DRIVER: psycopg2 CELERY_BROKER_URL: ${CELERY_BROKER_URL:-redis://redis} CELERY_RESULT_URL: ${CELERY_BROKER_URL:-redis://redis} + PGP_PASSPHRASE: merman depends_on: - postgres - redis + volumes: + - ./apps/contract-migration/testdata/pgp/:/usr/src/secrets/ deploy: restart_policy: condition: on-failure