From a196272b5e5aa75d855efbbac671975ce679db4d Mon Sep 17 00:00:00 2001 From: pchang388 Date: Sat, 8 Mar 2025 02:31:16 -0500 Subject: [PATCH] update configuration defaults and checks --- .../config_helper/config_helper.py | 12 ++++++---- .../config_helper/models.py | 18 +++++++-------- .../config_helper/remote.py | 23 +++++++++++++++++++ examples/config.yml | 8 +------ 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/bookstack_file_exporter/config_helper/config_helper.py b/bookstack_file_exporter/config_helper/config_helper.py index 3fb512d..a1c3fe0 100644 --- a/bookstack_file_exporter/config_helper/config_helper.py +++ b/bookstack_file_exporter/config_helper/config_helper.py @@ -79,11 +79,8 @@ def _generate_config(self, config_file: str) -> models.UserInput: def _generate_credentials(self) -> Tuple[str, str]: # if user provided credentials in config file, load them - token_id = "" - token_secret = "" - if self.user_inputs.credentials: - token_id = self.user_inputs.credentials.token_id - token_secret = self.user_inputs.credentials.token_secret + token_id = self.user_inputs.credentials.token_id + token_secret = self.user_inputs.credentials.token_secret # check to see if env var is specified, if so, it takes precedence token_id = self._check_var(_BOOKSTACK_TOKEN_FIELD, token_id) @@ -98,8 +95,13 @@ def _generate_remote_config(self) -> Dict[str, StorageProviderConfig]: self.user_inputs.minio.access_key) minio_secret_key = self._check_var(_MINIO_SECRET_KEY_FIELD, self.user_inputs.minio.secret_key) + object_config["minio"] = StorageProviderConfig(minio_access_key, minio_secret_key, self.user_inputs.minio) + for platform, config in object_config.items(): + if not config.is_valid(platform): + error_str = "provided " + platform + " configuration is invalid" + raise ValueError(error_str) return object_config def _generate_headers(self) -> Dict[str, str]: diff --git a/bookstack_file_exporter/config_helper/models.py b/bookstack_file_exporter/config_helper/models.py index 0f24b99..2df2ca1 100644 --- a/bookstack_file_exporter/config_helper/models.py +++ b/bookstack_file_exporter/config_helper/models.py @@ -6,18 +6,18 @@ class ObjectStorageConfig(BaseModel): """YAML schema for minio configuration""" host: str - access_key: Optional[str] = None - secret_key: Optional[str] = None + access_key: Optional[str] = "" + secret_key: Optional[str] = "" bucket: str - path: Optional[str] = None + path: Optional[str] = "" region: str - keep_last: Optional[int] = None + keep_last: Optional[int] = 0 # pylint: disable=too-few-public-methods class BookstackAccess(BaseModel): """YAML schema for bookstack access credentials""" - token_id: str - token_secret: str + token_id: Optional[str] = "" + token_secret: Optional[str] = "" # pylint: disable=too-few-public-methods class Assets(BaseModel): @@ -41,11 +41,11 @@ class HttpConfig(BaseModel): class UserInput(BaseModel): """YAML schema for user provided configuration file""" host: str - credentials: Optional[BookstackAccess] = None + credentials: Optional[BookstackAccess] = BookstackAccess() formats: List[Literal["markdown", "html", "pdf", "plaintext"]] - output_path: Optional[str] = None + output_path: Optional[str] = "" assets: Optional[Assets] = Assets() minio: Optional[ObjectStorageConfig] = None - keep_last: Optional[int] = None + keep_last: Optional[int] = 0 run_interval: Optional[int] = 0 http_config: Optional[HttpConfig] = HttpConfig() diff --git a/bookstack_file_exporter/config_helper/remote.py b/bookstack_file_exporter/config_helper/remote.py index d97dd04..af3e425 100644 --- a/bookstack_file_exporter/config_helper/remote.py +++ b/bookstack_file_exporter/config_helper/remote.py @@ -1,5 +1,9 @@ +import logging + from bookstack_file_exporter.config_helper.models import ObjectStorageConfig +log = logging.getLogger(__name__) + ## convenience class ## able to work for minio, s3, etc. class StorageProviderConfig: @@ -21,6 +25,7 @@ def __init__(self, access_key: str, secret_key: str, config: ObjectStorageConfig self.config = config self._access_key = access_key self._secret_key = secret_key + self._valid_checker = {'minio': self._is_minio_valid()} @property def access_key(self) -> str: @@ -31,3 +36,21 @@ def access_key(self) -> str: def secret_key(self) -> str: """return secret key for use""" return self._secret_key + + def is_valid(self, storage_type: str) -> bool: + """check if object storage config is valid""" + return self._valid_checker[storage_type] + + def _is_minio_valid(self) -> bool: + """check if minio config is valid""" + # required values - keys already checked so skip + checks = { + "bucket": self.config.bucket, + "host": self.config.host + } + + for prop, check in checks.items(): + if not check: + log.error("%s is missing from minio configuration and is required", prop) + return False + return True diff --git a/examples/config.yml b/examples/config.yml index 115d965..71a7c8e 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -3,7 +3,7 @@ # if you put http here, it will try verify=false, not to check certs host: "https://bookstack.mydomain.org" # You could optionally set the bookstack token_id and token_secret here instead of env -# If using env vars instead you can omit/comment out this section +# If using env vars instead you can ignore this section credentials: # set here or as env variable, BOOKSTACK_TOKEN_ID # env var takes precedence over below @@ -11,12 +11,6 @@ credentials: # set here or as env variable, BOOKSTACK_TOKEN_SECRET # env var takes precedence over below token_secret: "" -# optional - additional headers to add, examples below -# if not required, you can omit/comment out section -additional_headers: - test: "test" - test2: "test2" - User-Agent: "test-agent" # supported formats from bookstack below # specify one or more formats: