Skip to content

update configuration defaults and checks #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions bookstack_file_exporter/config_helper/config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]:
Expand Down
18 changes: 9 additions & 9 deletions bookstack_file_exporter/config_helper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
23 changes: 23 additions & 0 deletions bookstack_file_exporter/config_helper/remote.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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
8 changes: 1 addition & 7 deletions examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
# 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
token_id: ""
# 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:
Expand Down