Documentation
¶
Overview ¶
Package crypto provides AES-256-GCM encryption of secrets stored at rest (webhook secrets, upstream passwords, SSO client secrets, AI API keys, storage backend credentials).
The key comes from the OPENARTIFACT_SECRET_KEY environment variable (any string of at least 16 bytes). Two ciphertext generations exist, both encoded as "<prefix><base64(nonce||sealed)>":
- "enc:v2:" (written by Encrypt): the AES-256 key is derived with HKDF-SHA256 (RFC 5869) from the configured secret, using the fixed salt [hkdfSalt] and the context string [hkdfInfo]. The salt is a constant because the secret is the only per-deployment input; it separates this derivation from any other use of the same string.
- "enc:v1:" (legacy, read-only): the AES-256 key is the plain SHA-256 of the configured secret. Decrypt still opens these transparently; values are re-sealed as v2 the next time they are saved. There is no forced migration.
An older binary that only knows "enc:v1:" cannot read "enc:v2:" values, so downgrading after v2 values were written requires re-entering those secrets.
HKDF is a key-derivation function, not a password hash: it does not slow down guessing, so the secret should be high-entropy (for example `openssl rand -base64 32`).
Values without a known prefix are treated as legacy plaintext by Decrypt. When no key is configured, Encrypt returns the plaintext unchanged (and Enabled() is false), so deployments without a key keep working but store secrets unprotected.
Index ¶
Constants ¶
const EnvKey = "OPENARTIFACT_SECRET_KEY"
EnvKey is the environment variable holding the secret-encryption key.
const MinKeyLen = 16
MinKeyLen is the minimum accepted key length in bytes.
Variables ¶
var ErrEncryptedInput = errors.New("crypto: value must be plaintext, not an encrypted value")
ErrEncryptedInput is returned by CheckPlaintext for values that already carry the ciphertext prefix.
var ErrNoKey = errors.New("crypto: encrypted value present but " + EnvKey + " is not set")
ErrNoKey is returned by Decrypt when ciphertext is present but no key is configured.
Functions ¶
func CheckPlaintext ¶ added in v0.6.0
CheckPlaintext rejects user-supplied secret input that looks like a ciphertext produced by Encrypt. Call it at every API boundary that accepts a secret to be stored encrypted.
func Decrypt ¶
Decrypt opens an "enc:v2:" or legacy "enc:v1:" value produced by Encrypt. Values without a known prefix are legacy plaintext and returned as-is.
func Encrypt ¶
Encrypt seals plaintext as an "enc:v2:" value. Empty input is returned unchanged; without a configured key the plaintext is returned unchanged. Input is always sealed, even when it already carries an "enc:" prefix: passing such input through would let a caller smuggle a ciphertext into a field that is later decrypted and used (a decryption oracle). API boundaries must additionally reject prefixed input with ErrEncryptedInput.
func InitFromEnv ¶
func InitFromEnv() error
InitFromEnv configures the key from OPENARTIFACT_SECRET_KEY.
func IsEncrypted ¶
IsEncrypted reports whether s is an encrypted value produced by Encrypt. Both the enc:v1: and enc:v2: generations are recognised.
Types ¶
This section is empty.