Skip to content

Commit

Permalink
Strip secrets files content (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
makukha committed Sep 3, 2024
1 parent 60e79d0 commit e5fe409
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.bumpversion]
current_version = "0.2.0"
current_version = "0.2.1"
allow_dirty = true
files = [
{filename = "src/pydantic_file_secrets/__version__.py"},
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# pydantic-file-secrets 🔑

> Use file secrets in nested [Pydantic Settings](https://docs.pydantic.dev/latest/concepts/pydantic_settings/) models, drop-in replacement of `SecretsSettingsSource`.
> Use file secrets in nested [Pydantic Settings](https://docs.pydantic.dev/latest/concepts/pydantic_settings/) models, drop-in replacement for `SecretsSettingsSource`.
![GitHub License](https://img.shields.io/github/license/makukha/pydantic-file-secrets)
[![Tests](https://raw.githubusercontent.com/makukha/pydantic-file-secrets/0.2.0/docs/badge/tests.svg)](https://github.com/makukha/pydantic-file-secrets)
[![Coverage](https://raw.githubusercontent.com/makukha/pydantic-file-secrets/0.2.0/docs/badge/coverage.svg)](https://github.com/makukha/pydantic-file-secrets)
[![Tests](https://raw.githubusercontent.com/makukha/pydantic-file-secrets/0.2.1/docs/badge/tests.svg)](https://github.com/makukha/pydantic-file-secrets)
[![Coverage](https://raw.githubusercontent.com/makukha/pydantic-file-secrets/0.2.1/docs/badge/coverage.svg)](https://github.com/makukha/pydantic-file-secrets)
[![linting - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v1.json)](https://github.com/astral-sh/ruff)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) \
[![pypi](https://img.shields.io/pypi/v/pydantic-file-secrets.svg#0.2.0)](https://pypi.python.org/pypi/pydantic-file-secrets)
[![pypi](https://img.shields.io/pypi/v/pydantic-file-secrets.svg#0.2.1)](https://pypi.python.org/pypi/pydantic-file-secrets)
[![versions](https://img.shields.io/pypi/pyversions/pydantic-file-secrets.svg)](https://pypi.org/project/pydantic-file-secrets)
[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)

Expand Down
8 changes: 4 additions & 4 deletions docs/badge/tests.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ build-backend = "pdm.backend"
[project]
name = "pydantic-file-secrets"
dynamic = ["version"]
description = "Use file secrets in nested models of Pydantic Settings."
description = """Use file secrets in nested Pydantic Settings models, \
drop-in replacement for SecretsSettingsSource.
"""
authors = [
{name = "Michael Makukha", email = "m.makukha@gmail.com"},
]
Expand Down
2 changes: 1 addition & 1 deletion src/pydantic_file_secrets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def validate_secrets_path(self, path: Path) -> None:
@staticmethod
def load_secrets(path: Path) -> dict[str, str]:
return {
str(p.relative_to(path)): p.read_text()
str(p.relative_to(path)): p.read_text().strip()
for p in path.glob('**/*')
if p.is_file()
}
Expand Down
2 changes: 1 addition & 1 deletion src/pydantic_file_secrets/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2.0'
__version__ = '0.2.1'
34 changes: 34 additions & 0 deletions tests/test_pydantic_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,37 @@ class Settings(BaseSettings):
assert conf.key_empty == '' # should be None if working
assert conf.key_none == 'null' # should be Null if working
assert isinstance(conf.key_enum, TestEnum) # should be True if working


def test_str_strip_whitespace_not_specified(secrets_dir):
class Settings(BaseSettings):
key: str

model_config = SettingsConfigDict(
secrets_dir=secrets_dir,
# str_strip_whitespace not specified
)

secrets_dir.add_files(
('key', ' value '),
)

conf = Settings()
assert conf.key == 'value' # spaces are stripped by SecretsSettingsSource


def test_str_strip_whitespace_not_respected(secrets_dir):
class Settings(BaseSettings):
key: str

model_config = SettingsConfigDict(
secrets_dir=secrets_dir,
str_strip_whitespace=False,
)

secrets_dir.add_files(
('key', ' value '),
)

conf = Settings()
assert conf.key == 'value' # str_strip_whitespace not respected
16 changes: 16 additions & 0 deletions tests/test_strip_whitespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def test_strip_whitespace(settings_model, monkeypatch, secrets_dir):
monkeypatch.setenv('DB__USER', 'user')
secrets_dir.add_files(
('app_key', ' secret1 '),
('db___password', '\tsecret2\n'), # file name with delimiter
)
Settings = settings_model(
model_config=dict(
env_nested_delimiter='__',
secrets_dir=secrets_dir,
secrets_nested_delimiter='___',
),
)
conf = Settings()
assert conf.app_key == 'secret1'
assert conf.db.password == 'secret2' # noqa: S105

0 comments on commit e5fe409

Please sign in to comment.