crypto

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package crypto encrypts the credentials the server keeps in its own database: the instance and data source credentials, and the LLM provider API keys.

The data key is a random 32-byte AES-256 key. A ciphertext is the prefix "v1:" followed by the standard base64 of a fresh 12-byte nonce, the encrypted bytes and the 16-byte GCM tag. Because every value gets its own nonce, equal plaintexts never produce equal ciphertexts, and because the tag authenticates the whole value, decrypting with the wrong key or reading an edited value fails instead of returning garbage.

Index

Constants

View Source
const (
	// Prefix versions the ciphertext layout. It is the only version this build
	// writes, and a reader refuses a value without it: a credential written by
	// the retired obfuscation scheme fails as an error rather than decrypting to
	// garbage that the next write would store as the new plaintext.
	Prefix = "v1:"

	// KeySize is the AES-256 key length in bytes.
	KeySize = 32

	// KeyEnvironment names the environment variable that holds the current
	// key-encryption key. Its presence is what keeps a database read or a
	// database backup from being enough to recover the stored credentials: the
	// data key is wrapped with it instead of being kept in the clear.
	KeyEnvironment = "METAXISDATA_ENCRYPTION_KEY"

	// PreviousKeyEnvironment names an optional comma-separated list of retired
	// key-encryption keys. They are only ever used to open a wrapped data key, so
	// a rotation does not require re-encrypting a single stored credential.
	PreviousKeyEnvironment = "METAXISDATA_ENCRYPTION_KEY_PREVIOUS"
)

Variables

This section is empty.

Functions

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey returns a new random data key.

func IsCiphertext

func IsCiphertext(value string) bool

IsCiphertext reports whether a value is stored in the current ciphertext format.

func ParseKey

func ParseKey(value string) ([]byte, error)

ParseKey parses one key from its configured form: base64 (padded or not), hex, or the raw 32 bytes. The key comes from an environment variable an operator pastes by hand, so all the spellings are accepted.

The encodings are tried before the raw reading, and a hex string of the wrong length is an error rather than a raw key: sixteen random bytes printed as hex are 32 characters, and silently taking those characters as a 256-bit key would leave the deployment with half the entropy it believes it has.

func ParseKeys

func ParseKeys(value string) ([][]byte, error)

ParseKeys parses a comma-separated key list. Empty entries are skipped, so an unset or blank variable yields no keys rather than an error.

Types

type Cipher

type Cipher struct {
	// contains filtered or unexported fields
}

Cipher encrypts and decrypts values with one data key.

func NewCipher

func NewCipher(key []byte) (*Cipher, error)

NewCipher builds a cipher from a 32-byte key.

func (*Cipher) Decrypt

func (c *Cipher) Decrypt(value string) (string, error)

Decrypt decrypts a value. An empty ciphertext stays empty. Anything else must be a "v1:" ciphertext that authenticates under this key; a value written by another scheme, encrypted with another key, truncated or edited is an error.

func (*Cipher) Encrypt

func (c *Cipher) Encrypt(plaintext string) (string, error)

Encrypt encrypts a value. An empty plaintext stays empty: no credential and no ciphertext are the same state.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL