Source code for cadrumo.adapters.persistence.storage.certificate_secret_backend
"""Concrete adapter for the application certificate-secret capability.This module owns the encrypted secret-store record and its persistence errors.The application-facing contract receives only ``SecretStr`` values, absence,and non-secret mutation witnesses; storage records never cross that boundary.Core types::class:`~cadrumo.core.classification.policies.SensitivityClass`."""from__future__importannotationsfromdatetimeimportdatetime,timedeltafromtypingimportTYPE_CHECKINGfrompydanticimportSecretStr,TypeAdapterfrom....application.auth.certificate_secret_backendimportCertificateSecretBackendfrom....application.auth.modelsimportCertificateSourceNamefrom....core.classification.policiesimportSensitivityClassfrom....core.external_constantsimportUTF_8_ENCODINGfrom....core.time.clockimportnowfrom.blob_store.materialisationimportget_secret_storefrom.errorsimportSecretNotFoundErrorfrom.secret_store.storeimportSecretRecord,SecretStoreifTYPE_CHECKING:from....core.configimportSettings_CERTIFICATE_SOURCE_NAME:TypeAdapter[str]=TypeAdapter(CertificateSourceName)_SECRET_ROTATION_HORIZON=timedelta(days=365*10)"""Effectively unbounded expiry for a rotation-driven operator secret."""_MUTATION_OPERATION_ID_METADATA_KEY="certificate_secret_mutation_operation_id"def_secret_store_key(*,bucket_id:str,name:str)->str:"""Return the bucket-scoped natural key for one certificate source."""canonical=_CERTIFICATE_SOURCE_NAME.validate_python(name)returnf"aeat:certificate-secret:{bucket_id}:{canonical}"
[docs]classSecureStorageCertificateSecretBackend:"""Persist named certificate passphrases through encrypted secure storage."""def__init__(self,*,bucket_id:str,store:SecretStore)->None:"""Bind this adapter to one profile bucket and one explicit store."""self._bucket_id=bucket_idself._store=store
[docs]defget(self,name:str)->SecretStr|None:"""Return the persisted passphrase for ``name``, or ``None`` when absent."""key=_secret_store_key(bucket_id=self._bucket_id,name=name)try:record=self._store.get(key)exceptSecretNotFoundError:returnNonereturnSecretStr(record.value.decode(UTF_8_ENCODING))
[docs]defset(self,name:str,secret:SecretStr,*,operation_id:str|None=None,occurred_at:datetime|None=None,)->None:"""Persist (or rotate) the passphrase for ``name``."""key=_secret_store_key(bucket_id=self._bucket_id,name=name)written_at=occurred_atornow()metadata={_MUTATION_OPERATION_ID_METADATA_KEY:operation_id}ifoperation_idisnotNoneelse{}record=SecretRecord(key=key,value=secret.get_secret_value().encode(UTF_8_ENCODING),classification=SensitivityClass.SECRET,metadata=metadata,created_at=written_at,expires_at=written_at+_SECRET_ROTATION_HORIZON,)self._store.put(record,overwrite=True)
[docs]defremove(self,name:str)->bool:"""Remove the persisted passphrase for ``name``; absent is a no-op."""key=_secret_store_key(bucket_id=self._bucket_id,name=name)try:self._store.delete(key)exceptSecretNotFoundError:returnFalsereturnTrue
[docs]defmutation_operation_id(self,name:str)->str|None:"""Return the non-secret operation id stamped on ``name``, if present."""key=_secret_store_key(bucket_id=self._bucket_id,name=name)try:record=self._store.get(key)exceptSecretNotFoundError:returnNonereturnrecord.metadata.get(_MUTATION_OPERATION_ID_METADATA_KEY)
[docs]defrequest_witness(self,name:str,secret:SecretStr)->str:"""Return a master-keyed witness for matching a retried set request."""key=_secret_store_key(bucket_id=self._bucket_id,name=name)returnself._store.value_witness(key=key,value=secret.get_secret_value().encode(UTF_8_ENCODING),)
[docs]defbuild_certificate_secret_backend(*,bucket_id:str,settings:Settings,)->CertificateSecretBackend:"""Build one bucket-scoped adapter from the explicit routed store."""returnSecureStorageCertificateSecretBackend(bucket_id=bucket_id,store=get_secret_store(settings=settings),)