secrets: add support for the data folder

The secrets module will now look for "moonraker.secrets"
in the data folder.  If the file does not exist the deprecated
"secrets_path" option will be used as a fallback.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-08-07 07:38:50 -04:00 committed by Eric Callahan
parent 524552eb84
commit 1327602ec3
1 changed files with 13 additions and 8 deletions

View File

@ -21,16 +21,17 @@ class Secrets:
def __init__(self, config: ConfigHelper) -> None:
server = config.get_server()
self.secrets_file: Optional[pathlib.Path] = None
path: Optional[str] = config.get('secrets_path', None)
path: Optional[str] = config.get("secrets_path", None, deprecate=True)
app_args = server.get_app_args()
alias = app_args["alias"]
data_path = app_args["data_path"]
fpath = pathlib.Path(data_path).joinpath(f"{alias}.secrets")
if not fpath.is_file() and path is not None:
fpath = pathlib.Path(path).expanduser().resolve()
self.type = "invalid"
self.values: Dict[str, Any] = {}
if path is not None:
self.secrets_file = pathlib.Path(path).expanduser().resolve()
if not self.secrets_file.is_file():
server.add_warning(
"[secrets]: option 'secrets_path', file does not exist: "
f"'{self.secrets_file}'")
return
if fpath.is_file():
self.secrets_file = fpath
data = self.secrets_file.read_text()
vals = self._parse_json(data)
if vals is not None:
@ -52,6 +53,10 @@ class Secrets:
self.type = "ini"
logging.debug(f"[secrets]: Loaded {self.type} file: "
f"{self.secrets_file}")
elif path is not None:
server.add_warning(
"[secrets]: option 'secrets_path', file does not exist: "
f"'{self.secrets_file}'")
else:
logging.debug(
"[secrets]: Option `secrets_path` not supplied")