Tree: Use safe loader for YAML

Loaders that read use a safe type while loaders that write use both
round-trip and safe options.

Also don't create module-level parsers where they're not needed.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri
2024-09-18 12:12:49 -04:00
parent 6c7542de9f
commit 24ea85b3c5
4 changed files with 18 additions and 12 deletions

View File

@@ -4,6 +4,7 @@ application, it should be fine.
"""
import aiofiles
import io
import secrets
from ruamel.yaml import YAML
from fastapi import Header, HTTPException, Request
@@ -13,8 +14,6 @@ from typing import Optional
from common.utils import coalesce
yaml = YAML()
class AuthKeys(BaseModel):
"""
@@ -59,6 +58,9 @@ async def load_auth_keys(disable_from_config: bool):
return
# Create a temporary YAML parser
yaml = YAML(typ=["rt", "safe"])
try:
async with aiofiles.open("api_tokens.yml", "r", encoding="utf8") as auth_file:
contents = await auth_file.read()
@@ -71,10 +73,12 @@ async def load_auth_keys(disable_from_config: bool):
AUTH_KEYS = new_auth_keys
async with aiofiles.open("api_tokens.yml", "w", encoding="utf8") as auth_file:
new_auth_yaml = yaml.safe_dump(
AUTH_KEYS.model_dump(), default_flow_style=False
string_stream = io.StringIO()
yaml.dump(
AUTH_KEYS.model_dump(), string_stream
)
await auth_file.write(new_auth_yaml)
await auth_file.write(string_stream.getvalue())
logger.info(
f"Your API key is: {AUTH_KEYS.api_key}\n"