Secrets encryption #4
					 5 changed files with 18 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,8 @@
 | 
			
		|||
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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue