From edd21096dfea9cdf1daff14788b73bf8967f1619 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Tue, 19 Nov 2024 16:14:15 +0100 Subject: [PATCH] Fix vault usage --- scripts/env.sh | 7 ------- scripts/start_app.sh | 2 -- scripts/start_scheduler.sh | 2 -- scripts/start_worker.sh | 4 ---- src/core/config.py | 30 ++++++++++++++++++++++++++++-- 5 files changed, 28 insertions(+), 17 deletions(-) delete mode 100644 scripts/env.sh diff --git a/scripts/env.sh b/scripts/env.sh deleted file mode 100644 index 2ac60a3..0000000 --- a/scripts/env.sh +++ /dev/null @@ -1,7 +0,0 @@ -#! /usr/bin/env sh - -response=`curl -X 'GET' "https://$VAULT_HOST/v1/$VAULT_SECRET_PATH" -s \ - -H 'accept: application/json' \ - -H "X-Vault-Token: $VAULT_TOKEN"` - -echo "$(echo "$response" | jq -r '.data.data | to_entries | map("\(.key)='\''\(.value)'\''") | .[]')" \ No newline at end of file diff --git a/scripts/start_app.sh b/scripts/start_app.sh index d14a613..da7b753 100644 --- a/scripts/start_app.sh +++ b/scripts/start_app.sh @@ -1,5 +1,3 @@ #! /usr/bin/env sh -export $(/env.sh) - /opt/venv/bin/python main.py $1 diff --git a/scripts/start_scheduler.sh b/scripts/start_scheduler.sh index a6404fe..e36ea70 100644 --- a/scripts/start_scheduler.sh +++ b/scripts/start_scheduler.sh @@ -1,5 +1,3 @@ #! /usr/bin/env sh -export $(/env.sh) - /opt/venv/bin/taskiq scheduler core.broker:scheduler modules.tasks diff --git a/scripts/start_worker.sh b/scripts/start_worker.sh index 078f847..9703263 100644 --- a/scripts/start_worker.sh +++ b/scripts/start_worker.sh @@ -1,7 +1,3 @@ #! /usr/bin/env sh -export $(/env.sh) - -printenv - /opt/venv/bin/taskiq worker core.broker:broker modules.tasks diff --git a/src/core/config.py b/src/core/config.py index fa70da8..82dd24f 100644 --- a/src/core/config.py +++ b/src/core/config.py @@ -1,7 +1,16 @@ +from pydantic import BaseModel from pydantic_settings import BaseSettings +from httpx import Client -class Config(BaseSettings): + +class Settings(BaseSettings): + VAULT_HOST: str + VAULT_SECRET_PATH: str + VAULT_TOKEN: str + + +class Config(BaseModel): DISCORD_BOT_TOKEN: str DISCORD_BOT_ID: str DISCORD_BOT_ACTIVITY: str @@ -21,4 +30,21 @@ class Config(BaseSettings): REDIS_URI: str -config = Config() # type: ignore +def get_config() -> Config: + settings = Settings() # type: ignore + + with Client() as client: + response = client.get( + f"https://{settings.VAULT_HOST}/v1/{settings.VAULT_SECRET_PATH}", + headers={ + "X-Vault-Token": settings.VAULT_TOKEN, + "Content-Type": "application/json", + } + ) + + response.raise_for_status() + + return Config(**response.json()["data"]) + + +config = get_config()