From 64edeb8d40bf1fe7c0f2d6658aeeb06f9c18d2c4 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Fri, 27 Dec 2024 00:34:54 +0100 Subject: [PATCH] Using env vars for configuration instead of toml config --- .gitignore | 2 +- README.md | 13 +++++-------- conf/pssecret.toml.example | 1 - poetry.lock | 22 +++++++++++++++++++++- pssecret/redis_db.py | 2 +- pssecret/settings.py | 25 ++++--------------------- pyproject.toml | 1 + 7 files changed, 33 insertions(+), 33 deletions(-) delete mode 100644 conf/pssecret.toml.example diff --git a/.gitignore b/.gitignore index 7ebc6bd..8ea7c0a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,9 @@ +.env .idea/ .nvim.lua .python-version .venv/ __pycache__/ build/ -conf/pssecret.toml dist/ pssecret.egg-info/ diff --git a/README.md b/README.md index bf2cccf..515e0a9 100644 --- a/README.md +++ b/README.md @@ -45,13 +45,10 @@ $ pssecret ### Configuration -Configuration is done through config file. By default, path is `/etc/pssecret/pssecret.toml`. -You can override this by setting environment variable `PSSECRET_CONF_FILE` value to actual file -location, e.g.: +Configuration is done via environment variables. -```console -$ PSSECRET_CONF_FILE=/home/user/.conf/pssecret.toml pssecret -``` +Environment variables: -You can find all available configuration options in the example file, located -at [conf/pssecret.toml.example](conf/pssecret.toml.example) under Git root. +- `REDIS_URL`: URL for Redis access. Check what values are supported [here](https://redis.readthedocs.io/en/stable/connections.html#redis.Redis.from_url). + +You can also declare these variables in a `.env` file in the working directory. diff --git a/conf/pssecret.toml.example b/conf/pssecret.toml.example deleted file mode 100644 index 1458255..0000000 --- a/conf/pssecret.toml.example +++ /dev/null @@ -1 +0,0 @@ -redis.url = 'redis://localhost' diff --git a/poetry.lock b/poetry.lock index 3707dea..20cb268 100644 --- a/poetry.lock +++ b/poetry.lock @@ -656,6 +656,26 @@ files = [ [package.dependencies] typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0" +[[package]] +name = "pydantic-settings" +version = "2.7.0" +description = "Settings management using Pydantic" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pydantic_settings-2.7.0-py3-none-any.whl", hash = "sha256:e00c05d5fa6cbbb227c84bd7487c5c1065084119b750df7c8c1a554aed236eb5"}, + {file = "pydantic_settings-2.7.0.tar.gz", hash = "sha256:ac4bfd4a36831a48dbf8b2d9325425b549a0a6f18cea118436d728eb4f1c4d66"}, +] + +[package.dependencies] +pydantic = ">=2.7.0" +python-dotenv = ">=0.21.0" + +[package.extras] +azure-key-vault = ["azure-identity (>=1.16.0)", "azure-keyvault-secrets (>=4.8.0)"] +toml = ["tomli (>=2.0.1)"] +yaml = ["pyyaml (>=6.0.1)"] + [[package]] name = "pygments" version = "2.18.0" @@ -1119,4 +1139,4 @@ hiredis = ["hiredis"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "1b9fc2055c3b8b01ce8590e50215ad011b9bcc2770f4c99e42a2523b99baa888" +content-hash = "1f2ca7562492fce7198a033828bce3cd8b9ed4cd38fc9e5c35784113bb816827" diff --git a/pssecret/redis_db.py b/pssecret/redis_db.py index 8ec0494..0776e53 100644 --- a/pssecret/redis_db.py +++ b/pssecret/redis_db.py @@ -3,4 +3,4 @@ from redis import asyncio as aioredis from pssecret.settings import settings -redis = aioredis.from_url(settings.redis.url) +redis = aioredis.from_url(str(settings.redis_url)) diff --git a/pssecret/settings.py b/pssecret/settings.py index 31178a9..8c8265f 100644 --- a/pssecret/settings.py +++ b/pssecret/settings.py @@ -1,26 +1,9 @@ -import os -import tomllib +from pydantic import RedisDsn +from pydantic_settings import BaseSettings -class Settings: - def __init__(self, data: dict = None): - if data: - self._data = data - else: - with open( - os.getenv("PSSECRET_CONF_FILE", "/etc/pssecret/pssecret.toml"), "rb" - ) as f: - self._data = tomllib.load(f) - - def __getattr__(self, item): - try: - value = self._data[item] - except KeyError: - raise AttributeError - if isinstance(value, dict): - return Settings(data=value) - else: - return value +class Settings(BaseSettings): + redis_url: RedisDsn = RedisDsn("redis://localhost") settings = Settings() diff --git a/pyproject.toml b/pyproject.toml index c987242..62eba9b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,6 +23,7 @@ pssecret = 'pssecret.cli:cli' [tool.poetry.dependencies] python = "^3.11" +pydantic-settings = "2.7.0" click = "8.1.8" fastapi = { version = "0.115.6", extras = [ "standard" ] } redis = "5.2.1"