s365b.core.secrets ================== .. py:module:: s365b.core.secrets .. autoapi-nested-parse:: Encryption of secrets stored in the database. Certificate private keys, SMTP passwords and similar values are stored in PostgreSQL encrypted with AES-256-GCM under the **master key** (a file passed to the containers as a Docker secret). A database dump alone therefore does not reveal them. This is separate from backup repository encryption (:mod:`s365b.storage.crypto`), which uses its own passphrase-derived key. Attributes ---------- .. autoapisummary:: s365b.core.secrets.KEY_LEN Exceptions ---------- .. autoapisummary:: s365b.core.secrets.SecretBoxError Classes ------- .. autoapisummary:: s365b.core.secrets.SecretBox Functions --------- .. autoapisummary:: s365b.core.secrets.generate_master_key s365b.core.secrets.load_master_key Module Contents --------------- .. py:data:: KEY_LEN :value: 32 .. py:exception:: SecretBoxError Bases: :py:obj:`Exception` Raised when a secret cannot be decrypted (wrong key or tampered data). .. py:function:: generate_master_key() -> str Create a new random master key. :returns: The key as URL-safe base64 text, suitable for writing to a file. .. py:function:: load_master_key(path: pathlib.Path) -> bytes Read and decode a master key file. :param path: File written by :func:`generate_master_key`. :returns: The 32-byte key. :raises SecretBoxError: If the file does not hold a valid key. .. py:class:: SecretBox(key: bytes) Encrypts and decrypts small secrets with the master key. The ciphertext format is ``version(1) || nonce(12) || ciphertext+tag``, base64-encoded so it fits in a text column. An optional *context* string (e.g. ``"tenant:42:cert"``) is bound as associated data, so a ciphertext copied into another row fails to decrypt. :param key: 32-byte master key. .. py:method:: encrypt(plaintext: bytes, context: str = '') -> str Encrypt a secret. :param plaintext: Secret bytes. :param context: Associated data that must match on decryption. :returns: Base64 text safe to store in the database. .. py:method:: decrypt(token: str, context: str = '') -> bytes Decrypt a value produced by :meth:`encrypt`. :param token: Stored base64 text. :param context: The same associated data used to encrypt. :returns: The plaintext secret. :raises SecretBoxError: If the key, context or data do not match.