JuliusBrussee/caveman · critical

is not set; cannot encrypt/decrypt secrets

Error message

%s is not set; cannot encrypt/decrypt secrets

What it means

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.

Solutions

  1. Export CAVE_LOCAL_ENCRYPTION_KEY to a base64-encoded 32-byte key before starting the process.
  2. Generate a key with: openssl rand -base64 32 and inject it via your secret manager.
  3. Check os.Getenv("CAVE_LOCAL_ENCRYPTION_KEY") != "" as a startup precondition and fail with a clear setup message.

Example fix

// before
err := secretbox.Encrypt(plaintext) // fails: env not set
// after
if os.Getenv("CAVE_LOCAL_ENCRYPTION_KEY") == "" {
    os.Setenv("CAVE_LOCAL_ENCRYPTION_KEY", base64.StdEncoding.EncodeToString(key32))
}
err := secretbox.Encrypt(plaintext)
Defensive patterns

Strategy: try-catch

Validate before calling

if os.Getenv("CAVE_LOCAL_ENCRYPTION_KEY") == "" {
    return errors.New("CAVE_LOCAL_ENCRYPTION_KEY is not set; generate one with: openssl rand -base64 32")
}

Try / catch

if err := secretbox.Encrypt(pt); err != nil && strings.Contains(err.Error(), "is not set") {
    // abort with setup instructions; do not store plaintext fallback
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/a5b0cd5aa56ab7ea. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/secretbox/secretbox.go:39

	"encoding/base64"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/JuliusBrussee/caveman/shared/platform/kms"
	"github.com/JuliusBrussee/caveman/shared/platform/runtimeenv"
)

// envKey is the name of the environment variable holding the base64-encoded
// 32-byte master key.
const envKey = "CAVE_LOCAL_ENCRYPTION_KEY"

// loadKey reads and validates the 32-byte AES key from the environment.
func loadKey() ([]byte, error) {
	keyB64 := os.Getenv(envKey)
	if keyB64 == "" {
		return nil, fmt.Errorf("%s is not set; cannot encrypt/decrypt secrets", envKey)
	}
	keyBytes, err := base64.StdEncoding.DecodeString(keyB64)
	if err != nil {
		return nil, fmt.Errorf("%s is not valid base64: %w", envKey, err)
	}
	if len(keyBytes) != 32 {
		return nil, fmt.Errorf("%s must decode to exactly 32 bytes, got %d", envKey, len(keyBytes))
	}
	return keyBytes, nil
}

// Encrypt seals plaintext with AES-256-GCM and a fresh random nonce, returning
// nonce(12) || ciphertext+tag as raw bytes.
func Encrypt(plaintext []byte) ([]byte, error) {
	if useKMS() {
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		defer cancel()
		wrapped, err := kms.Encrypt(ctx, plaintext)

View on GitHub (pinned to 3ee70a1026)