JuliusBrussee/caveman · error

secretbox: payload KMS decrypt

Error message

secretbox: payload KMS decrypt: %w

What it means

DecryptPayloadKey detected a KMS envelope (via kms.IsEnvelope) and delegated to kms.DecryptPayload, which failed (network, IAM permissions, disabled key, timeout within the 10s context). secretbox wraps that failure with this prefix and returns no local fallback — KMS envelopes are never decrypted locally.

Solutions

  1. Grant the runtime identity kms:Decrypt on the key referenced by the envelope and confirm the key is enabled.
  2. Check that KMS endpoint connectivity exists (egress/VPC endpoint) so the 10s timeout is not hit.
  3. Verify the envelope was produced in the same KMS key/region configuration you are decrypting with; re-wrap if not.
  4. Run locally with valid cloud credentials (or use the legacy plaintext path only in dev) before processing KMS artifacts.

Example fix

// before (CI role without KMS access)
key, err := secretbox.DecryptPayloadKey(envelope) // secretbox: payload KMS decrypt: AccessDeniedException

// after (pre-flight IAM check)
if err := kms.CanDecrypt(ctx, keyARN); err != nil {
    return fmt.Errorf("runtime identity cannot decrypt payload keys: %w", err)
}
key, err := secretbox.DecryptPayloadKey(envelope)
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-flight: confirm the runtime identity can use the KMS key
if err := kms.VerifyDecryptPermission(ctx, keyARN); err != nil {
    return fmt.Errorf("KMS pre-flight failed: %w", err)
}

Type guard

func isKMSEnvelope(b []byte) bool { return kms.IsEnvelope(b) }

Try / catch

key, err := secretbox.DecryptPayloadKey(envelope)
if err != nil {
    if strings.Contains(err.Error(), "payload KMS decrypt") {
        return fmt.Errorf("KMS rejected the envelope — check IAM kms:Decrypt, key state, and region: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling DecryptPayloadKey (typically from the artifact 'open' path) on an envelope whose header marks it as KMS-wrapped, while the KMS Decrypt call fails: missing kms:Decrypt IAM permission, key disabled/deleted, wrong region, network outage, or the 10-second context deadline expiring.

Common situations: Running the service with a role lacking KMS permissions; KMS key rotated/disabled in the cloud console; VPC without a KMS endpoint route causing timeouts; CI environments without cloud credentials attempting to open production artifacts.

Related errors


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

Appendix: source

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

	}
	nonce, ct := envelope[:ns], envelope[ns:]
	plain, err := gcm.Open(nil, nonce, ct, nil)
	if err != nil {
		return nil, fmt.Errorf("aes-gcm open: %w", err)
	}
	return plain, nil
}

// DecryptPayloadKey unwraps an artifact data-encryption key. KMS envelopes are
// restricted to the configured payload key plus the explicit legacy secrets
// key used before key separation.
func DecryptPayloadKey(envelope []byte) ([]byte, error) {
	if kms.IsEnvelope(envelope) {
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		defer cancel()
		plaintext, err := kms.DecryptPayload(ctx, envelope)
		if err != nil {
			return nil, fmt.Errorf("secretbox: payload KMS decrypt: %w", err)
		}
		return plaintext, nil
	}
	return Decrypt(envelope)
}

func useKMS() bool {
	return strings.EqualFold(strings.TrimSpace(os.Getenv("CAVE_KMS_PROVIDER")), kms.ProviderScaleway)
}

// ResolveEnvironmentSecret loads a boot-time secret. In production plaintext
// environment variables are rejected: operators must provide a base64-encoded
// secretbox/KMS envelope in ciphertextEnv. Local development may continue using
// plaintextEnv. An entirely absent optional secret returns an empty string.
func ResolveEnvironmentSecret(plaintextEnv, ciphertextEnv string) (string, error) {
	plain := strings.TrimSpace(os.Getenv(plaintextEnv))
	encoded := strings.TrimSpace(os.Getenv(ciphertextEnv))
	production := runtimeenv.IsProduction()

View on GitHub (pinned to 3ee70a1026)