From c339c505add68f33f5d3622c0e4d1883390cc4c0 Mon Sep 17 00:00:00 2001 From: Jean-Marie Mineau Date: Wed, 6 Oct 2021 17:27:37 +0200 Subject: [PATCH] add ssl entries to Config --- src/kassandra/config.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/kassandra/config.py b/src/kassandra/config.py index d695be9..c0e5d78 100644 --- a/src/kassandra/config.py +++ b/src/kassandra/config.py @@ -5,14 +5,37 @@ Config related tools. import dataclasses import yaml +from typing import Optional + @dataclasses.dataclass class Config: username: str homeserver: str password: str - port: int - host: str alert_rooms: list[str] + port: int = 8000 + host: str = "127.0.0.1" + tls: bool = False + tls_auth: bool = False + tls_crt: Optional[str] = None + tls_key: Optional[str] = None + ca_crt: Optional[str] = None + + def check_integrity(self)->bool: + """ Check the integrity of the config. + Raise an error if the config is invalid, + should always return True (or raise an error). + """ + if self.tls_auth and not self.tls: + raise ValueError("tls_auth is enable, but not tls.") + if self.tls and tls_crt is None: + raise ValueError("tls is enable but tls_crt was not provided") + if self.tls and tls_key is None: + raise ValueError("tls is enable but tls_key was not provided") + if self.tls_auth and ca_cert is None: + raise ValueError("tls_auth is enable, but ca_crt was not provided") + return True + def load_config(file:str)->Config: """ @@ -20,5 +43,7 @@ def load_config(file:str)->Config: """ with open(file, 'r') as f: data = yaml.load(f, Loader=yaml.loader.SafeLoader) - return Config(**data) + config = Config(**data) + config.check_integrity() + return config