diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index 3c6255c..b10424b 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -13,4 +13,4 @@ jobs: key: bazel - uses: actions/checkout@v2 - name: Run Bazel tests - run: bazel test ...:all --test_output=all + run: bazel test ...:all --test_output=all --action_env=WGKEX_CONFIG_FILE=`pwd`/wgkex.yaml.example diff --git a/wgkex.yaml.example b/wgkex.yaml.example index 126d245..e85786e 100644 --- a/wgkex.yaml.example +++ b/wgkex.yaml.example @@ -18,4 +18,5 @@ mqtt: password: SECRET keepalive: 5 tls: False -domain_prefix: myprefix- \ No newline at end of file +domain_prefix: myprefix- +log_level: DEBUG \ No newline at end of file diff --git a/wgkex/broker/app.py b/wgkex/broker/app.py index 95265b1..ae41886 100644 --- a/wgkex/broker/app.py +++ b/wgkex/broker/app.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import re import dataclasses +import logging from typing import Tuple, Any from flask import Flask @@ -14,6 +15,11 @@ from wgkex.config import config +logging.basicConfig( + format="%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", + datefmt="%Y-%m-%d:%H:%M:%S", + level=config.load_config().get("log_level"), +) WG_PUBKEY_PATTERN = re.compile(r"^[A-Za-z0-9+/]{42}[AEIMQUYcgkosw480]=$") @@ -88,7 +94,7 @@ def wg_key_exchange() -> Tuple[str, int]: domain = data.domain # in case we want to decide here later we want to publish it only to dedicated gateways gateway = "all" - print(f"wg_key_exchange: Domain: {domain}, Key:{key}") + logging.info(f"wg_key_exchange: Domain: {domain}, Key:{key}") mqtt.publish(f"wireguard/{domain}/{gateway}", key) return jsonify({"Message": "OK"}), 200 @@ -100,7 +106,7 @@ def handle_mqtt_connect( ) -> None: """Prints status of connect message.""" # TODO(ruairi): Clarify current usage of this function. - print( + logging.debug( "MQTT connected to {}:{}".format( app.config["MQTT_BROKER_URL"], app.config["MQTT_BROKER_PORT"] ) @@ -114,7 +120,9 @@ def handle_mqtt_message( ) -> None: """Prints message contents.""" # TODO(ruairi): Clarify current usage of this function. - print(f"MQTT message received on {message.topic}: {message.payload.decode()}") + logging.debug( + f"MQTT message received on {message.topic}: {message.payload.decode()}" + ) def is_valid_wg_pubkey(pubkey: str) -> str: diff --git a/wgkex/config/config.py b/wgkex/config/config.py index 4756e65..81d35af 100644 --- a/wgkex/config/config.py +++ b/wgkex/config/config.py @@ -72,6 +72,7 @@ class Config: domains: List[str] mqtt: MQTT domain_prefix: str + log_level: str @classmethod def from_dict(cls, cfg: Dict[str, str]) -> "Config": @@ -83,7 +84,10 @@ def from_dict(cls, cfg: Dict[str, str]) -> "Config": """ mqtt_cfg = MQTT.from_dict(cfg["mqtt"]) return cls( - domains=cfg["domains"], mqtt=mqtt_cfg, domain_prefix=cfg["domain_prefix"] + domains=cfg["domains"], + mqtt=mqtt_cfg, + domain_prefix=cfg["domain_prefix"], + log_level=cfg["log_level"], ) diff --git a/wgkex/config/config_test.py b/wgkex/config/config_test.py index ff30dd2..58427c8 100644 --- a/wgkex/config/config_test.py +++ b/wgkex/config/config_test.py @@ -3,7 +3,7 @@ import config import yaml -_VALID_CFG = "domain_prefix: ffmuc_\ndomains:\n- a\n- b\nmqtt:\n broker_port: 1883\n broker_url: mqtt://broker\n keepalive: 5\n password: pass\n tls: true\n username: user\n" +_VALID_CFG = "domain_prefix: ffmuc_\nlog_level: DEBUG\ndomains:\n- a\n- b\nmqtt:\n broker_port: 1883\n broker_url: mqtt://broker\n keepalive: 5\n password: pass\n tls: true\n username: user\n" _INVALID_LINT = "domain_prefix: ffmuc_\nBAD_KEY_FOR_DOMAIN:\n- a\n- b\nmqtt:\n broker_port: 1883\n broker_url: mqtt://broker\n keepalive: 5\n password: pass\n tls: true\n username: user\n" _INVALID_CFG = "asdasdasdasd" diff --git a/wgkex/worker/app.py b/wgkex/worker/app.py index cf020e0..fcd98c1 100644 --- a/wgkex/worker/app.py +++ b/wgkex/worker/app.py @@ -12,7 +12,7 @@ logging.basicConfig( format="%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", datefmt="%Y-%m-%d:%H:%M:%S", - level=logging.DEBUG, + level=config.load_config().get("log_level"), ) _CLEANUP_TIME = 300 diff --git a/wgkex/worker/mqtt.py b/wgkex/worker/mqtt.py index 488491d..695f0f8 100644 --- a/wgkex/worker/mqtt.py +++ b/wgkex/worker/mqtt.py @@ -15,7 +15,7 @@ logging.basicConfig( format="%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", datefmt="%Y-%m-%d:%H:%M:%S", - level=logging.DEBUG, + level=load_config().get("log_level"), )