Karta.Storage (Karta v1.0.0)

Message bodies at rest: encrypted (Karta.Storage.Cipher, keys from Karta.Vault) and kept in a blob store (Karta.Storage.BlobStore).

Configuration:

config :karta, Karta.Storage,
  blob_store: {Karta.Storage.BlobStore.S3, endpoint: ..., bucket: ..., ...}

Every function also takes blob_store: {module, config} in its options, which overrides the configuration (tests use this to inject failures).

Keys are lower-case letters, digits, / and -, at most 512 characters, e.g. messages/0193.... Errors are returned, never raised, so a caller can answer an SMTP client with a temporary failure.

Summary

Functions

Deletes the body stored under key. Deleting a missing key succeeds.

Reads and decrypts the body stored under key.

The bytes first to last (both included, 0 <= first <= last < size) of the body stored under key, whose size is known (messages.size), as a stream of binaries. Only the encrypted chunks that hold the range are read, 16 at a time (1 MiB of plaintext): the body is never loaded whole when the blob store can read a range (Karta.Storage.BlobStore). The first part is read and checked before this returns, so a missing or undecryptable body is an error here; a failure later raises while the stream is read.

Stores data (a message Karta made itself), encrypted, under key. It may be iodata: it is encrypted in slices of the parts as they are, never joined into one more copy of the whole.

Stores prefix (iodata, e.g. a Received: header) followed by the contents of the file at path, encrypted, under key. The file is read in 64 KiB pieces; memory use does not depend on its size. With offset: n, the file's first n bytes are left out (the header block of a message whose header Karta rewrote into prefix).

Runs folder over the plaintext of the body stored under key, chunk by chunk, in order: {:ok, acc} to go on, {:error, reason} to stop. Each chunk is authenticated before it is folded in (Karta.Storage.Cipher), so a damaged body is {:error, :undecryptable} and never a shorter message; a body cut short is {:error, :truncated}.

Functions

delete(key, opts \\ [])

@spec delete(String.t(), keyword()) :: :ok | {:error, term()}

Deletes the body stored under key. Deleting a missing key succeeds.

get(key, opts \\ [])

@spec get(String.t(), keyword()) ::
  {:ok, binary()} | {:error, :not_found | :undecryptable | term()}

Reads and decrypts the body stored under key.

get_range(key, size, first, last, opts \\ [])

@spec get_range(
  String.t(),
  non_neg_integer(),
  non_neg_integer(),
  non_neg_integer(),
  keyword()
) ::
  {:ok, Enumerable.t()} | {:error, :not_found | :undecryptable | term()}

The bytes first to last (both included, 0 <= first <= last < size) of the body stored under key, whose size is known (messages.size), as a stream of binaries. Only the encrypted chunks that hold the range are read, 16 at a time (1 MiB of plaintext): the body is never loaded whole when the blob store can read a range (Karta.Storage.BlobStore). The first part is read and checked before this returns, so a missing or undecryptable body is an error here; a failure later raises while the stream is read.

put(key, data, opts \\ [])

@spec put(String.t(), iodata(), keyword()) :: :ok | {:error, term()}

Stores data (a message Karta made itself), encrypted, under key. It may be iodata: it is encrypted in slices of the parts as they are, never joined into one more copy of the whole.

put_file(key, prefix, path, opts \\ [])

@spec put_file(String.t(), iodata(), Path.t(), keyword()) :: :ok | {:error, term()}

Stores prefix (iodata, e.g. a Received: header) followed by the contents of the file at path, encrypted, under key. The file is read in 64 KiB pieces; memory use does not depend on its size. With offset: n, the file's first n bytes are left out (the header block of a message whose header Karta rewrote into prefix).

stream(key, acc, folder, opts \\ [])

@spec stream(
  String.t(),
  acc,
  (binary(), acc -> {:ok, acc} | {:error, term()}),
  keyword()
) :: {:ok, acc} | {:error, term()}
when acc: var

Runs folder over the plaintext of the body stored under key, chunk by chunk, in order: {:ok, acc} to go on, {:error, reason} to stop. Each chunk is authenticated before it is folded in (Karta.Storage.Cipher), so a damaged body is {:error, :undecryptable} and never a shorter message; a body cut short is {:error, :truncated}.

Memory use does not depend on the size of the body: the store is asked for the next piece when the folder has taken the last one, and reading stops as soon as the folder returns an error. The store's reader is closed however the read ends.