{"record":{"id":"89462ca9c00208bd","repo":"JuliusBrussee/caveman","slug":"secretbox-payload-kms-decrypt-w","errorCode":null,"errorMessage":"secretbox: payload KMS decrypt: %w","messagePattern":"secretbox: payload KMS decrypt: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":151,"sourceCode":"\t}\n\tnonce, ct := envelope[:ns], envelope[ns:]\n\tplain, err := gcm.Open(nil, nonce, ct, nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"aes-gcm open: %w\", err)\n\t}\n\treturn plain, nil\n}\n\n// DecryptPayloadKey unwraps an artifact data-encryption key. KMS envelopes are\n// restricted to the configured payload key plus the explicit legacy secrets\n// key used before key separation.\nfunc DecryptPayloadKey(envelope []byte) ([]byte, error) {\n\tif kms.IsEnvelope(envelope) {\n\t\tctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n\t\tdefer cancel()\n\t\tplaintext, err := kms.DecryptPayload(ctx, envelope)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"secretbox: payload KMS decrypt: %w\", err)\n\t\t}\n\t\treturn plaintext, nil\n\t}\n\treturn Decrypt(envelope)\n}\n\nfunc useKMS() bool {\n\treturn strings.EqualFold(strings.TrimSpace(os.Getenv(\"CAVE_KMS_PROVIDER\")), kms.ProviderScaleway)\n}\n\n// ResolveEnvironmentSecret loads a boot-time secret. In production plaintext\n// environment variables are rejected: operators must provide a base64-encoded\n// secretbox/KMS envelope in ciphertextEnv. Local development may continue using\n// plaintextEnv. An entirely absent optional secret returns an empty string.\nfunc ResolveEnvironmentSecret(plaintextEnv, ciphertextEnv string) (string, error) {\n\tplain := strings.TrimSpace(os.Getenv(plaintextEnv))\n\tencoded := strings.TrimSpace(os.Getenv(ciphertextEnv))\n\tproduction := runtimeenv.IsProduction()","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L133-L169","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Grant the runtime identity kms:Decrypt on the key referenced by the envelope and confirm the key is enabled.","Check that KMS endpoint connectivity exists (egress/VPC endpoint) so the 10s timeout is not hit.","Verify the envelope was produced in the same KMS key/region configuration you are decrypting with; re-wrap if not.","Run locally with valid cloud credentials (or use the legacy plaintext path only in dev) before processing KMS artifacts."],"exampleFix":"// before (CI role without KMS access)\nkey, err := secretbox.DecryptPayloadKey(envelope) // secretbox: payload KMS decrypt: AccessDeniedException\n\n// after (pre-flight IAM check)\nif err := kms.CanDecrypt(ctx, keyARN); err != nil {\n    return fmt.Errorf(\"runtime identity cannot decrypt payload keys: %w\", err)\n}\nkey, err := secretbox.DecryptPayloadKey(envelope)","handlingStrategy":"try-catch","validationCode":"// pre-flight: confirm the runtime identity can use the KMS key\nif err := kms.VerifyDecryptPermission(ctx, keyARN); err != nil {\n    return fmt.Errorf(\"KMS pre-flight failed: %w\", err)\n}","typeGuard":"func isKMSEnvelope(b []byte) bool { return kms.IsEnvelope(b) }","tryCatchPattern":"key, err := secretbox.DecryptPayloadKey(envelope)\nif err != nil {\n    if strings.Contains(err.Error(), \"payload KMS decrypt\") {\n        return fmt.Errorf(\"KMS rejected the envelope — check IAM kms:Decrypt, key state, and region: %w\", err)\n    }\n    return err\n}","preventionTips":["Grant kms:Decrypt to the workload role before deploying KMS-wrapped artifacts.","Ensure network paths to the KMS endpoint (VPC endpoint/egress) exist.","Set cloud credentials explicitly in CI; never assume ambient auth.","Monitor the 10s timeout budget and alert on KMS latency."],"tags":["go","kms","cloud","envelope-encryption"],"backgroundTag":"kms-decrypt-failed","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"}