Skip to content

Commit 63f10a1

Browse files
authored
Merge pull request #63 from homeylab/config_updates
update configuration defaults and checks
2 parents 4d07423 + a196272 commit 63f10a1

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

bookstack_file_exporter/config_helper/config_helper.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,8 @@ def _generate_config(self, config_file: str) -> models.UserInput:
7979

8080
def _generate_credentials(self) -> Tuple[str, str]:
8181
# if user provided credentials in config file, load them
82-
token_id = ""
83-
token_secret = ""
84-
if self.user_inputs.credentials:
85-
token_id = self.user_inputs.credentials.token_id
86-
token_secret = self.user_inputs.credentials.token_secret
82+
token_id = self.user_inputs.credentials.token_id
83+
token_secret = self.user_inputs.credentials.token_secret
8784

8885
# check to see if env var is specified, if so, it takes precedence
8986
token_id = self._check_var(_BOOKSTACK_TOKEN_FIELD, token_id)
@@ -98,8 +95,13 @@ def _generate_remote_config(self) -> Dict[str, StorageProviderConfig]:
9895
self.user_inputs.minio.access_key)
9996
minio_secret_key = self._check_var(_MINIO_SECRET_KEY_FIELD,
10097
self.user_inputs.minio.secret_key)
98+
10199
object_config["minio"] = StorageProviderConfig(minio_access_key,
102100
minio_secret_key, self.user_inputs.minio)
101+
for platform, config in object_config.items():
102+
if not config.is_valid(platform):
103+
error_str = "provided " + platform + " configuration is invalid"
104+
raise ValueError(error_str)
103105
return object_config
104106

105107
def _generate_headers(self) -> Dict[str, str]:

bookstack_file_exporter/config_helper/models.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
class ObjectStorageConfig(BaseModel):
77
"""YAML schema for minio configuration"""
88
host: str
9-
access_key: Optional[str] = None
10-
secret_key: Optional[str] = None
9+
access_key: Optional[str] = ""
10+
secret_key: Optional[str] = ""
1111
bucket: str
12-
path: Optional[str] = None
12+
path: Optional[str] = ""
1313
region: str
14-
keep_last: Optional[int] = None
14+
keep_last: Optional[int] = 0
1515

1616
# pylint: disable=too-few-public-methods
1717
class BookstackAccess(BaseModel):
1818
"""YAML schema for bookstack access credentials"""
19-
token_id: str
20-
token_secret: str
19+
token_id: Optional[str] = ""
20+
token_secret: Optional[str] = ""
2121

2222
# pylint: disable=too-few-public-methods
2323
class Assets(BaseModel):
@@ -41,11 +41,11 @@ class HttpConfig(BaseModel):
4141
class UserInput(BaseModel):
4242
"""YAML schema for user provided configuration file"""
4343
host: str
44-
credentials: Optional[BookstackAccess] = None
44+
credentials: Optional[BookstackAccess] = BookstackAccess()
4545
formats: List[Literal["markdown", "html", "pdf", "plaintext"]]
46-
output_path: Optional[str] = None
46+
output_path: Optional[str] = ""
4747
assets: Optional[Assets] = Assets()
4848
minio: Optional[ObjectStorageConfig] = None
49-
keep_last: Optional[int] = None
49+
keep_last: Optional[int] = 0
5050
run_interval: Optional[int] = 0
5151
http_config: Optional[HttpConfig] = HttpConfig()

bookstack_file_exporter/config_helper/remote.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import logging
2+
13
from bookstack_file_exporter.config_helper.models import ObjectStorageConfig
24

5+
log = logging.getLogger(__name__)
6+
37
## convenience class
48
## able to work for minio, s3, etc.
59
class StorageProviderConfig:
@@ -21,6 +25,7 @@ def __init__(self, access_key: str, secret_key: str, config: ObjectStorageConfig
2125
self.config = config
2226
self._access_key = access_key
2327
self._secret_key = secret_key
28+
self._valid_checker = {'minio': self._is_minio_valid()}
2429

2530
@property
2631
def access_key(self) -> str:
@@ -31,3 +36,21 @@ def access_key(self) -> str:
3136
def secret_key(self) -> str:
3237
"""return secret key for use"""
3338
return self._secret_key
39+
40+
def is_valid(self, storage_type: str) -> bool:
41+
"""check if object storage config is valid"""
42+
return self._valid_checker[storage_type]
43+
44+
def _is_minio_valid(self) -> bool:
45+
"""check if minio config is valid"""
46+
# required values - keys already checked so skip
47+
checks = {
48+
"bucket": self.config.bucket,
49+
"host": self.config.host
50+
}
51+
52+
for prop, check in checks.items():
53+
if not check:
54+
log.error("%s is missing from minio configuration and is required", prop)
55+
return False
56+
return True

examples/config.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,14 @@
33
# if you put http here, it will try verify=false, not to check certs
44
host: "https://bookstack.mydomain.org"
55
# You could optionally set the bookstack token_id and token_secret here instead of env
6-
# If using env vars instead you can omit/comment out this section
6+
# If using env vars instead you can ignore this section
77
credentials:
88
# set here or as env variable, BOOKSTACK_TOKEN_ID
99
# env var takes precedence over below
1010
token_id: ""
1111
# set here or as env variable, BOOKSTACK_TOKEN_SECRET
1212
# env var takes precedence over below
1313
token_secret: ""
14-
# optional - additional headers to add, examples below
15-
# if not required, you can omit/comment out section
16-
additional_headers:
17-
test: "test"
18-
test2: "test2"
19-
User-Agent: "test-agent"
2014
# supported formats from bookstack below
2115
# specify one or more
2216
formats:

0 commit comments

Comments
 (0)