From 8a334112c0b9d687af605fca767c2b7cecfd8ff9 Mon Sep 17 00:00:00 2001 From: Ian Costanzo Date: Thu, 8 Oct 2020 09:04:07 -0700 Subject: [PATCH 1/3] Convert config files to yaml and force yaml formal for config files Signed-off-by: Ian Costanzo --- aries_cloudagent/commands/provision.py | 4 +- aries_cloudagent/commands/start.py | 2 +- aries_cloudagent/config/argparse.py | 7 +++- .../config/tests/test-general-args.cfg | 7 ---- .../config/tests/test-general-args.yaml | 4 ++ .../config/tests/test-transport-args.yaml | 5 +++ .../config/tests/test_argparse.py | 33 ++++++++++++--- demo/{demo-args.cfg => demo-args.yaml} | 25 +++++------- ...cal-indy-args.cfg => local-indy-args.yaml} | 40 ++++++++----------- requirements.txt | 3 +- 10 files changed, 73 insertions(+), 57 deletions(-) delete mode 100644 aries_cloudagent/config/tests/test-general-args.cfg create mode 100644 aries_cloudagent/config/tests/test-general-args.yaml create mode 100644 aries_cloudagent/config/tests/test-transport-args.yaml rename demo/{demo-args.cfg => demo-args.yaml} (54%) rename demo/{local-indy-args.cfg => local-indy-args.yaml} (66%) diff --git a/aries_cloudagent/commands/provision.py b/aries_cloudagent/commands/provision.py index 3ac543d6b4..74a165b509 100644 --- a/aries_cloudagent/commands/provision.py +++ b/aries_cloudagent/commands/provision.py @@ -5,8 +5,8 @@ from typing import Sequence from ..config import argparse as arg -from ..config.base import BaseError from ..config.default_context import DefaultContextBuilder +from ..config.base import BaseError from ..config.ledger import ledger_config from ..config.util import common_config from ..config.wallet import wallet_config @@ -41,7 +41,7 @@ async def provision(settings: dict): def execute(argv: Sequence[str] = None): """Entrypoint.""" - parser = ArgumentParser() + parser = arg.create_argument_parser() parser.prog += " provision" get_settings = init_argument_parser(parser) args = parser.parse_args(argv) diff --git a/aries_cloudagent/commands/start.py b/aries_cloudagent/commands/start.py index a13b4fb21c..7506527023 100644 --- a/aries_cloudagent/commands/start.py +++ b/aries_cloudagent/commands/start.py @@ -40,7 +40,7 @@ def init_argument_parser(parser: ArgumentParser): def execute(argv: Sequence[str] = None): """Entrypoint.""" - parser = ArgumentParser() + parser = arg.create_argument_parser() parser.prog += " start" get_settings = init_argument_parser(parser) args = parser.parse_args(argv) diff --git a/aries_cloudagent/config/argparse.py b/aries_cloudagent/config/argparse.py index cf67126cf4..d1a213e26f 100644 --- a/aries_cloudagent/config/argparse.py +++ b/aries_cloudagent/config/argparse.py @@ -3,7 +3,7 @@ import abc from os import environ -from configargparse import ArgumentParser, Namespace +from configargparse import ArgumentParser, Namespace, YAMLConfigFileParser from typing import Type from .error import ArgsParseError @@ -53,6 +53,11 @@ def get_registered(cls, category: str = None): ) +def create_argument_parser(): + """Create am instance of an arg parser, force yaml format for external config.""" + return ArgumentParser(config_file_parser_class=YAMLConfigFileParser) + + def load_argument_groups(parser: ArgumentParser, *groups: Type[ArgumentGroup]): """Log a set of argument groups into a parser. diff --git a/aries_cloudagent/config/tests/test-general-args.cfg b/aries_cloudagent/config/tests/test-general-args.cfg deleted file mode 100644 index 12d1f32de1..0000000000 --- a/aries_cloudagent/config/tests/test-general-args.cfg +++ /dev/null @@ -1,7 +0,0 @@ -# see: https://pypi.org/project/ConfigArgParse/ -# this is a comment -plugin = foo # ... also a comment - -storage-type = bar - -endpoint = test_endpoint diff --git a/aries_cloudagent/config/tests/test-general-args.yaml b/aries_cloudagent/config/tests/test-general-args.yaml new file mode 100644 index 0000000000..3159bb952d --- /dev/null +++ b/aries_cloudagent/config/tests/test-general-args.yaml @@ -0,0 +1,4 @@ +# see: https://pypi.org/project/ConfigArgParse/ +plugin: foo # ... also a comment +storage-type: bar +endpoint: test_endpoint diff --git a/aries_cloudagent/config/tests/test-transport-args.yaml b/aries_cloudagent/config/tests/test-transport-args.yaml new file mode 100644 index 0000000000..b396529cf1 --- /dev/null +++ b/aries_cloudagent/config/tests/test-transport-args.yaml @@ -0,0 +1,5 @@ +# see: https://pypi.org/project/ConfigArgParse/ +inbound-transport: + - [http, 0.0.0.0, 8030] + - [ws, 0.0.0.0, 8040] +outbound-transport: http diff --git a/aries_cloudagent/config/tests/test_argparse.py b/aries_cloudagent/config/tests/test_argparse.py index 7c3c20174a..926d6fca83 100644 --- a/aries_cloudagent/config/tests/test_argparse.py +++ b/aries_cloudagent/config/tests/test_argparse.py @@ -1,5 +1,5 @@ import itertools -from configargparse import ArgumentParser, ArgumentTypeError +from configargparse import ArgumentParser, ArgumentTypeError, YAMLConfigFileParser from asynctest import TestCase as AsyncTestCase, mock as async_mock @@ -10,7 +10,7 @@ class TestArgParse(AsyncTestCase): async def test_groups(self): """Test optional argument parsing.""" - parser = ArgumentParser() + parser = argparse.create_argument_parser() groups = ( g @@ -24,7 +24,7 @@ async def test_groups(self): async def test_transport_settings(self): """Test required argument parsing.""" - parser = ArgumentParser() + parser = argparse.create_argument_parser() group = argparse.TransportGroup() group.add_arguments(parser) @@ -54,10 +54,10 @@ async def test_transport_settings(self): assert settings.get("transport.outbound_configs") == ["http"] assert result.max_outbound_retry == 5 - async def test_transport_settings_file(self): + async def test_general_settings_file(self): """Test file argument parsing.""" - parser = ArgumentParser() + parser = argparse.create_argument_parser() group = argparse.GeneralGroup() group.add_arguments(parser) @@ -68,7 +68,7 @@ async def test_transport_settings_file(self): result = parser.parse_args( [ "--arg-file", - "./aries_cloudagent/config/tests/test-general-args.cfg", + "./aries_cloudagent/config/tests/test-general-args.yaml", ] ) @@ -80,6 +80,27 @@ async def test_transport_settings_file(self): assert settings.get("external_plugins") == ["foo"] assert settings.get("storage_type") == "bar" + async def test_transport_settings_file(self): + """Test file argument parsing.""" + + parser = argparse.create_argument_parser() + group = argparse.GeneralGroup() + group.add_arguments(parser) + group = argparse.TransportGroup() + group.add_arguments(parser) + + with async_mock.patch.object(parser, "exit") as exit_parser: + parser.parse_args(["-h"]) + exit_parser.assert_called_once() + + result = parser.parse_args( + [ + "--arg-file", + "./aries_cloudagent/config/tests/test-transport-args.yaml", + ] + ) + # no asserts, just testing that the parser doesn't fail + def test_bytesize(self): bs = ByteSize() with self.assertRaises(ArgumentTypeError): diff --git a/demo/demo-args.cfg b/demo/demo-args.yaml similarity index 54% rename from demo/demo-args.cfg rename to demo/demo-args.yaml index 73691fb6b4..f1ecd75d7b 100644 --- a/demo/demo-args.cfg +++ b/demo/demo-args.yaml @@ -1,21 +1,14 @@ # see: https://pypi.org/project/ConfigArgParse/ for file format overview - # this runs aca-py with a minumum set of parameters - # the following are quivalent to: # ./bin/aca-py start -it http 0.0.0.0 8020 -ot http --endpoint http://localhost:8020 --admin-insecure-mode --admin 0.0.0.0 8021 --no-ledger - # run as: -# ./bin/aca-py start --arg-file ./demo/demo-args.cfg - -inbound-transport = [[http, 0.0.0.0, 8030], [ws, 0.0.0.0, 8040]] - -outbound-transport = http - -endpoint = http://192.168.0.48:8030 - -admin-insecure-mode = true - -admin = [0.0.0.0, 8031] - -no-ledger = true +# ./bin/aca-py start --arg-file ./demo/demo-args.yaml +inbound-transport: + - [http, 0.0.0.0, 8030] + - [ws, 0.0.0.0, 8040] +outbound-transport: http +endpoint: http://192.168.0.48:8030 +admin-insecure-mode: true +admin: [0.0.0.0, 8031] +no-ledger: true diff --git a/demo/local-indy-args.cfg b/demo/local-indy-args.yaml similarity index 66% rename from demo/local-indy-args.cfg rename to demo/local-indy-args.yaml index 2a787d165e..f0ab20fb3b 100644 --- a/demo/local-indy-args.cfg +++ b/demo/local-indy-args.yaml @@ -1,43 +1,37 @@ # see: https://pypi.org/project/ConfigArgParse/ for file format overview - # before running aca-py, run the following (the commands are embedded below, next to the related parameters): # - run a local postgres database # - run a local instance of von-network # - register your did (seed) on the network - # run aca-py as: -# ACAPY_WALLET_SEED=my_seed_000000000000000000000000 ACAPY_WALLET_KEY=key ./bin/aca-py start --arg-file ./demo/local-indy-args.cfg - -admin-insecure-mode = true -admin = [0.0.0.0, 8031] -label = My Indy Agent - +# ACAPY_WALLET_SEED=my_seed_000000000000000000000000 ACAPY_WALLET_KEY=key ./bin/aca-py start --arg-file ./demo/local-indy-args.yaml +admin-insecure-mode: true +admin: [0.0.0.0, 8031] +label: My Indy Agent # the following is the callback url for your controller -webhook-url = http://localhost:7000/agentcb - +webhook-url: http://localhost:7000/agentcb # assumes you are running a local von-network, like: # cd von-network # ./manage start -genesis-url = http://localhost:9000/genesis - -inbound-transport = [[http, 0.0.0.0, 8030], [ws, 0.0.0.0, 8040]] -outbound-transport = http +genesis-url: http://localhost:9000/genesis +inbound-transport: + - [http, 0.0.0.0, 8030] + - [ws, 0.0.0.0, 8040] +outbound-transport: http # the following is the public endpoint advertised by the agent -endpoint = http://192.168.0.48:8030 -auto-ping-connection = true - +endpoint: http://192.168.0.48:8030 +auto-ping-connection: true # register your did using (this example is for von-network): # curl -d '{"seed":"my_seed_000000000000000000000000", "role":"TRUST_ANCHOR", "alias":"My Agent"}' -X POST http://localhost:9000/register # note that the env var name is configured in argparse.py # seed = comes from ACAPY_WALLET_SEED -wallet-type = indy -wallet-name = testwallet +wallet-type: indy +wallet-name: testwallet # wallet-key = comes from ACAPY_WALLET_KEY - # run a local postgres (docker) like: # docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres:10 -wallet-storage-type = postgres_storage +wallet-storage-type: postgres_storage # could be sent using env var ACAPY_WALLET_STORAGE_CONFIG -wallet-storage-config = {"url":"localhost:5432","max_connections":5} +wallet-storage-config: "{\"url\":\"localhost:5432\",\"max_connections\":5}" # could be sent using env var ACAPY_WALLET_STORAGE_CREDS -wallet-storage-creds = {"account":"postgres","password":"mysecretpassword","admin_account":"postgres","admin_password":"mysecretpassword"} +wallet-storage-creds: "{\"account\":\"postgres\",\"password\":\"mysecretpassword\",\"admin_account\":\"postgres\",\"admin_password\":\"mysecretpassword\"}" diff --git a/requirements.txt b/requirements.txt index fa5276e461..724905b2ba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,5 @@ pynacl~=1.3.0 requests~=2.23.0 pyld==2.0.1 py_multicodec==0.2.1 -git+https://github.com/ianco/ConfigArgParse.git#egg=ConfigArgParse +pyyaml~=5.3.1 +ConfigArgParse~=1.2.3 From 21e94bb5cb157050129ef0b74aa63919c6ab8f26 Mon Sep 17 00:00:00 2001 From: Ian Costanzo Date: Thu, 8 Oct 2020 09:25:19 -0700 Subject: [PATCH 2/3] Update help for arg-file Signed-off-by: Ian Costanzo --- aries_cloudagent/config/argparse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aries_cloudagent/config/argparse.py b/aries_cloudagent/config/argparse.py index d1a213e26f..09db031842 100644 --- a/aries_cloudagent/config/argparse.py +++ b/aries_cloudagent/config/argparse.py @@ -399,7 +399,8 @@ def add_arguments(self, parser: ArgumentParser): parser.add_argument( "--arg-file", is_config_file=True, - help="Load aca-py arguments from the specified file.", + help="Load aca-py arguments from the specified file. Note that\ + this file *must* be in YAML format.", ) parser.add_argument( "--plugin", From b3fb5cee39a9f80ba01b1a485308b0f4460bb8a5 Mon Sep 17 00:00:00 2001 From: Ian Costanzo Date: Thu, 8 Oct 2020 13:03:47 -0700 Subject: [PATCH 3/3] Convert to single quotes Signed-off-by: Ian Costanzo --- demo/local-indy-args.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/local-indy-args.yaml b/demo/local-indy-args.yaml index f0ab20fb3b..204b04e3ea 100644 --- a/demo/local-indy-args.yaml +++ b/demo/local-indy-args.yaml @@ -32,6 +32,6 @@ wallet-name: testwallet # docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres:10 wallet-storage-type: postgres_storage # could be sent using env var ACAPY_WALLET_STORAGE_CONFIG -wallet-storage-config: "{\"url\":\"localhost:5432\",\"max_connections\":5}" +wallet-storage-config: '{"url":"localhost:5432","max_connections":5}' # could be sent using env var ACAPY_WALLET_STORAGE_CREDS -wallet-storage-creds: "{\"account\":\"postgres\",\"password\":\"mysecretpassword\",\"admin_account\":\"postgres\",\"admin_password\":\"mysecretpassword\"}" +wallet-storage-creds: '{"account":"postgres","password":"mysecretpassword","admin_account":"postgres","admin_password":"mysecretpassword"}'