diff --git a/pssecret_server/fernet.py b/pssecret_server/fernet.py index c90d010..aa63600 100644 --- a/pssecret_server/fernet.py +++ b/pssecret_server/fernet.py @@ -1,8 +1,10 @@ -from cryptography.fernet import Fernet -from pssecret_server.settings import Settings, get_settings from typing import Annotated + +from cryptography.fernet import Fernet from fastapi import Depends +from pssecret_server.settings import Settings, get_settings + def get_fernet(settings: Annotated[Settings, Depends(get_settings)]) -> Fernet: return Fernet(settings.secrets_encryption_key) diff --git a/pssecret_server/main.py b/pssecret_server/main.py index 46c39d8..6c90499 100644 --- a/pssecret_server/main.py +++ b/pssecret_server/main.py @@ -26,7 +26,9 @@ FernetDep = Annotated[Fernet, Depends(get_fernet)] ), response_model=SecretSaveResult, ) -async def set_secret(data: Secret, redis: RedisDep, fernet: FernetDep) -> dict[str, str]: +async def set_secret( + data: Secret, redis: RedisDep, fernet: FernetDep +) -> dict[str, str]: data = encrypt_secret(data, fernet) return { "key": await save_secret(data, redis), @@ -44,7 +46,9 @@ async def set_secret(data: Secret, redis: RedisDep, fernet: FernetDep) -> dict[s response_model=Secret, responses={404: {"description": "The item was not found"}}, ) -async def get_secret(secret_key: str, redis: RedisDep, fernet: FernetDep) -> dict[str, bytes]: +async def get_secret( + secret_key: str, redis: RedisDep, fernet: FernetDep +) -> dict[str, bytes]: data: bytes | None = await redis.getdel(secret_key) if data is None: diff --git a/pssecret_server/utils.py b/pssecret_server/utils.py index 239ff37..e57b710 100644 --- a/pssecret_server/utils.py +++ b/pssecret_server/utils.py @@ -1,7 +1,7 @@ from uuid import uuid4 -from redis.asyncio import Redis from cryptography.fernet import Fernet +from redis.asyncio import Redis from pssecret_server.models import Secret diff --git a/tests/conftest.py b/tests/conftest.py index b7e212f..88c8915 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ from collections.abc import AsyncGenerator -from cryptography.fernet import Fernet import pytest +from cryptography.fernet import Fernet from fastapi.testclient import TestClient from pydantic_settings import SettingsConfigDict from redis import asyncio as aioredis @@ -31,7 +31,7 @@ def get_test_settings() -> Settings: return TestSettings() -@pytest.fixture +@pytest.fixture def fernet(settings: Settings) -> Fernet: return get_fernet(settings) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 35cc82f..de2984b 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,13 +1,15 @@ -from cryptography.fernet import Fernet, InvalidToken import pytest -from pssecret_server.utils import encrypt_secret, decrypt_secret +from cryptography.fernet import Fernet, InvalidToken + +from pssecret_server.utils import decrypt_secret, encrypt_secret + from ..factories import SecretFactory def test_encrypte_secret_ok(fernet: Fernet): secret = SecretFactory().build() encrypted_secret = encrypt_secret(secret, fernet) - + assert secret.data != encrypted_secret.data