Refactors cli to use the new config approach. Adds template.conf

This commit is contained in:
Sergi Delgado Segura
2020-03-21 22:00:49 +01:00
parent 00a48ce6f5
commit 3d389a92ac
3 changed files with 42 additions and 39 deletions

View File

@@ -1,28 +1,16 @@
import os import os
import cli.conf as conf
from common.tools import extend_paths, check_conf_fields, setup_logging, setup_data_folder
DATA_DIR = os.path.expanduser("~/.teos_cli/")
CONF_FILE_NAME = "teos_cli.conf"
LOG_PREFIX = "cli" LOG_PREFIX = "cli"
# Load config fields # Load config fields
conf_fields = { DEFAULT_CONF = {
"DEFAULT_TEOS_API_SERVER": {"value": conf.DEFAULT_TEOS_API_SERVER, "type": str}, "TEOS_SERVER": {"value": "http://localhost", "type": str},
"DEFAULT_TEOS_API_PORT": {"value": conf.DEFAULT_TEOS_API_PORT, "type": int}, "TEOS_PORT": {"value": 9814, "type": int},
"DATA_FOLDER": {"value": conf.DATA_FOLDER, "type": str}, "LOG_FILE": {"value": "teos_cli.log", "type": str, "path": True},
"CLIENT_LOG_FILE": {"value": conf.CLIENT_LOG_FILE, "type": str, "path": True}, "APPOINTMENTS_FOLDER_NAME": {"value": "appointment_receipts", "type": str, "path": True},
"APPOINTMENTS_FOLDER_NAME": {"value": conf.APPOINTMENTS_FOLDER_NAME, "type": str, "path": True}, "CLI_PUBLIC_KEY": {"value": "cli_pk.der", "type": str, "path": True},
"CLI_PUBLIC_KEY": {"value": conf.CLI_PUBLIC_KEY, "type": str, "path": True}, "CLI_PRIVATE_KEY": {"value": "cli_sk.der", "type": str, "path": True},
"CLI_PRIVATE_KEY": {"value": conf.CLI_PRIVATE_KEY, "type": str, "path": True}, "TEOS_PUBLIC_KEY": {"value": "teos_pk.der", "type": str, "path": True},
"TEOS_PUBLIC_KEY": {"value": conf.TEOS_PUBLIC_KEY, "type": str, "path": True},
} }
# Expand user (~) if found and check fields are correct
conf_fields["DATA_FOLDER"]["value"] = os.path.expanduser(conf_fields["DATA_FOLDER"]["value"])
# Extend relative paths
conf_fields = extend_paths(conf_fields["DATA_FOLDER"]["value"], conf_fields)
# Sanity check fields and build config dictionary
config = check_conf_fields(conf_fields)
setup_data_folder(config.get("DATA_FOLDER"))
setup_logging(config.get("CLIENT_LOG_FILE"), LOG_PREFIX)

4
cli/template.conf Normal file
View File

@@ -0,0 +1,4 @@
[teos]
TEOS_SERVER = "http://localhost"
TEOS_PORT = 9814

View File

@@ -10,15 +10,17 @@ from coincurve import PublicKey
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from requests import ConnectTimeout, ConnectionError from requests import ConnectTimeout, ConnectionError
from cli import config, LOG_PREFIX
from cli.help import help_add_appointment, help_get_appointment from cli.help import help_add_appointment, help_get_appointment
from common.blob import Blob from cli import DEFAULT_CONF, DATA_DIR, CONF_FILE_NAME, LOG_PREFIX
import common.cryptographer import common.cryptographer
from common.blob import Blob
from common import constants from common import constants
from common.logger import Logger from common.logger import Logger
from common.appointment import Appointment from common.appointment import Appointment
from common.config_loader import ConfigLoader
from common.cryptographer import Cryptographer from common.cryptographer import Cryptographer
from common.tools import setup_logging, setup_data_folder
from common.tools import check_sha256_hex_format, check_locator_format, compute_locator from common.tools import check_sha256_hex_format, check_locator_format, compute_locator
logger = Logger(actor="Client", log_name_prefix=LOG_PREFIX) logger = Logger(actor="Client", log_name_prefix=LOG_PREFIX)
@@ -77,7 +79,7 @@ def load_keys(teos_pk_path, cli_sk_path, cli_pk_path):
return teos_pk, cli_sk, cli_pk_der return teos_pk, cli_sk, cli_pk_der
def add_appointment(args): def add_appointment(args, config):
""" """
Manages the add_appointment command, from argument parsing, trough sending the appointment to the tower, until Manages the add_appointment command, from argument parsing, trough sending the appointment to the tower, until
saving the appointment receipt. saving the appointment receipt.
@@ -151,7 +153,7 @@ def add_appointment(args):
data = {"appointment": appointment.to_dict(), "signature": signature, "public_key": hex_pk_der.decode("utf-8")} data = {"appointment": appointment.to_dict(), "signature": signature, "public_key": hex_pk_der.decode("utf-8")}
# Send appointment to the server. # Send appointment to the server.
server_response = post_appointment(data) server_response = post_appointment(data, config)
if server_response is None: if server_response is None:
return False return False
@@ -174,7 +176,7 @@ def add_appointment(args):
logger.info("Appointment accepted and signed by the Eye of Satoshi") logger.info("Appointment accepted and signed by the Eye of Satoshi")
# All good, store appointment and signature # All good, store appointment and signature
return save_appointment_receipt(appointment.to_dict(), signature) return save_appointment_receipt(appointment.to_dict(), signature, config)
def parse_add_appointment_args(args): def parse_add_appointment_args(args):
@@ -225,7 +227,7 @@ def parse_add_appointment_args(args):
return appointment_data return appointment_data
def post_appointment(data): def post_appointment(data, config):
""" """
Sends appointment data to add_appointment endpoint to be processed by the tower. Sends appointment data to add_appointment endpoint to be processed by the tower.
@@ -241,7 +243,7 @@ def post_appointment(data):
logger.info("Sending appointment to the Eye of Satoshi") logger.info("Sending appointment to the Eye of Satoshi")
try: try:
add_appointment_endpoint = "{}:{}".format(teos_api_server, teos_api_port) add_appointment_endpoint = "{}:{}".format(config.get("TEOS_SERVER"), config.get("TEOS_PORT"))
return requests.post(url=add_appointment_endpoint, json=json.dumps(data), timeout=5) return requests.post(url=add_appointment_endpoint, json=json.dumps(data), timeout=5)
except ConnectTimeout: except ConnectTimeout:
@@ -297,7 +299,7 @@ def process_post_appointment_response(response):
return response_json return response_json
def save_appointment_receipt(appointment, signature): def save_appointment_receipt(appointment, signature, config):
""" """
Saves an appointment receipt to disk. A receipt consists in an appointment and a signature from the tower. Saves an appointment receipt to disk. A receipt consists in an appointment and a signature from the tower.
@@ -333,7 +335,7 @@ def save_appointment_receipt(appointment, signature):
return False return False
def get_appointment(locator): def get_appointment(locator, config):
""" """
Gets information about an appointment from the tower. Gets information about an appointment from the tower.
@@ -351,7 +353,7 @@ def get_appointment(locator):
logger.error("The provided locator is not valid", locator=locator) logger.error("The provided locator is not valid", locator=locator)
return None return None
get_appointment_endpoint = "{}:{}/get_appointment".format(teos_api_server, teos_api_port) get_appointment_endpoint = "{}:{}/get_appointment".format(config.get("TEOS_SERVER"), config.get("TEOS_PORT"))
parameters = "?locator={}".format(locator) parameters = "?locator={}".format(locator)
try: try:
@@ -391,31 +393,40 @@ def show_usage():
if __name__ == "__main__": if __name__ == "__main__":
teos_api_server = config.get("DEFAULT_TEOS_API_SERVER") command_line_conf = {}
teos_api_port = config.get("DEFAULT_TEOS_API_PORT")
commands = ["add_appointment", "get_appointment", "help"] commands = ["add_appointment", "get_appointment", "help"]
try: try:
opts, args = getopt(argv[1:], "s:p:h", ["server", "port", "help"]) opts, args = getopt(argv[1:], "s:p:h", ["server", "port", "help"])
for opt, arg in opts: for opt, arg in opts:
if opt in ["-s", "server"]: if opt in ["-s", "--server"]:
if arg: if arg:
teos_api_server = arg command_line_conf["TEOS_SERVER"] = arg
if opt in ["-p", "--port"]: if opt in ["-p", "--port"]:
if arg: if arg:
teos_api_port = int(arg) try:
command_line_conf["TEOS_PORT"] = int(arg)
except ValueError:
exit("port must be an integer")
if opt in ["-h", "--help"]: if opt in ["-h", "--help"]:
sys.exit(show_usage()) sys.exit(show_usage())
# Loads config and sets up the data folder and log file
config_loader = ConfigLoader(DATA_DIR, CONF_FILE_NAME, DEFAULT_CONF, command_line_conf)
config = config_loader.build_config()
setup_data_folder(DATA_DIR)
setup_logging(config.get("LOG_FILE"), LOG_PREFIX)
if args: if args:
command = args.pop(0) command = args.pop(0)
if command in commands: if command in commands:
if command == "add_appointment": if command == "add_appointment":
add_appointment(args) add_appointment(args, config)
elif command == "get_appointment": elif command == "get_appointment":
if not args: if not args:
@@ -427,7 +438,7 @@ if __name__ == "__main__":
if arg_opt in ["-h", "--help"]: if arg_opt in ["-h", "--help"]:
sys.exit(help_get_appointment()) sys.exit(help_get_appointment())
appointment_data = get_appointment(arg_opt) appointment_data = get_appointment(arg_opt, config)
if appointment_data: if appointment_data:
print(appointment_data) print(appointment_data)