s365b.core.secrets

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 (s365b.storage.crypto), which uses its own passphrase-derived key.

Attributes

Exceptions

SecretBoxError

Raised when a secret cannot be decrypted (wrong key or tampered data).

Classes

SecretBox

Encrypts and decrypts small secrets with the master key.

Functions

generate_master_key() → str

Create a new random master key.

load_master_key(path) → bytes

Read and decode a master key file.

Module Contents

s365b.core.secrets.KEY_LEN = 32[source]
exception s365b.core.secrets.SecretBoxError[source]

Bases: Exception

Raised when a secret cannot be decrypted (wrong key or tampered data).

s365b.core.secrets.generate_master_key() → str[source]

Create a new random master key.

Returns:

The key as URL-safe base64 text, suitable for writing to a file.

s365b.core.secrets.load_master_key(path: pathlib.Path) → bytes[source]

Read and decode a master key file.

Parameters:

path – File written by generate_master_key().

Returns:

The 32-byte key.

Raises:

SecretBoxError – If the file does not hold a valid key.

class s365b.core.secrets.SecretBox(key: bytes)[source]

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.

Parameters:

key – 32-byte master key.

encrypt(plaintext: bytes, context: str = '') → str[source]

Encrypt a secret.

Parameters:
  • plaintext – Secret bytes.

  • context – Associated data that must match on decryption.

Returns:

Base64 text safe to store in the database.

decrypt(token: str, context: str = '') → bytes[source]

Decrypt a value produced by encrypt().

Parameters:
  • token – Stored base64 text.

  • context – The same associated data used to encrypt.

Returns:

The plaintext secret.

Raises:

SecretBoxError – If the key, context or data do not match.