{"record":{"id":"a5b0cd5aa56ab7ea","repo":"JuliusBrussee/caveman","slug":"s-is-not-set-cannot-encrypt-decrypt-secrets","errorCode":null,"errorMessage":"%s is not set; cannot encrypt/decrypt secrets","messagePattern":"(.+?) is not set; cannot encrypt/decrypt secrets","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":39,"sourceCode":"\t\"encoding/base64\"\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/JuliusBrussee/caveman/shared/platform/kms\"\n\t\"github.com/JuliusBrussee/caveman/shared/platform/runtimeenv\"\n)\n\n// envKey is the name of the environment variable holding the base64-encoded\n// 32-byte master key.\nconst envKey = \"CAVE_LOCAL_ENCRYPTION_KEY\"\n\n// loadKey reads and validates the 32-byte AES key from the environment.\nfunc loadKey() ([]byte, error) {\n\tkeyB64 := os.Getenv(envKey)\n\tif keyB64 == \"\" {\n\t\treturn nil, fmt.Errorf(\"%s is not set; cannot encrypt/decrypt secrets\", envKey)\n\t}\n\tkeyBytes, err := base64.StdEncoding.DecodeString(keyB64)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"%s is not valid base64: %w\", envKey, err)\n\t}\n\tif len(keyBytes) != 32 {\n\t\treturn nil, fmt.Errorf(\"%s must decode to exactly 32 bytes, got %d\", envKey, len(keyBytes))\n\t}\n\treturn keyBytes, nil\n}\n\n// Encrypt seals plaintext with AES-256-GCM and a fresh random nonce, returning\n// nonce(12) || ciphertext+tag as raw bytes.\nfunc Encrypt(plaintext []byte) ([]byte, error) {\n\tif useKMS() {\n\t\tctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n\t\tdefer cancel()\n\t\twrapped, err := kms.Encrypt(ctx, plaintext)","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L21-L57","documentation":"secretbox.encrypt/decrypt require the AES key from the CAVE_LOCAL_ENCRYPTION_KEY environment variable; loadKey returns this error when the variable is unset or empty. Encryption cannot proceed without a key, so the call fails fast.","triggerScenarios":"Calling secretbox.Encrypt or secretbox.Decrypt on a machine/container where CAVE_LOCAL_ENCRYPTION_KEY is not exported, was dropped by the process manager, or was set only in a shell profile not sourced by the service.","commonSituations":"Fresh developer environments or CI runners missing the secret; Docker/Kubernetes manifests omitting the env var from the deployment spec; switching shells where the var was exported only locally.","solutions":["Export CAVE_LOCAL_ENCRYPTION_KEY to a base64-encoded 32-byte key before starting the process.","Generate a key with: openssl rand -base64 32 and inject it via your secret manager.","Check os.Getenv(\"CAVE_LOCAL_ENCRYPTION_KEY\") != \"\" as a startup precondition and fail with a clear setup message."],"exampleFix":"// before\nerr := secretbox.Encrypt(plaintext) // fails: env not set\n// after\nif os.Getenv(\"CAVE_LOCAL_ENCRYPTION_KEY\") == \"\" {\n    os.Setenv(\"CAVE_LOCAL_ENCRYPTION_KEY\", base64.StdEncoding.EncodeToString(key32))\n}\nerr := secretbox.Encrypt(plaintext)","handlingStrategy":"try-catch","validationCode":"if os.Getenv(\"CAVE_LOCAL_ENCRYPTION_KEY\") == \"\" {\n    return errors.New(\"CAVE_LOCAL_ENCRYPTION_KEY is not set; generate one with: openssl rand -base64 32\")\n}","typeGuard":null,"tryCatchPattern":"if err := secretbox.Encrypt(pt); err != nil && strings.Contains(err.Error(), \"is not set\") {\n    // abort with setup instructions; do not store plaintext fallback\n}","preventionTips":["Export the key in every environment (dev, CI, prod) via your secret manager","Add a startup precondition check for the env var","Document key setup in onboarding docs and Dockerfile/compose env"],"tags":["encryption","environment","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}