{"record":{"id":"a77897a4f771e3be","repo":"JuliusBrussee/caveman","slug":"kms-encode-envelope-w","errorCode":null,"errorMessage":"kms: encode envelope: %w","messagePattern":"kms: encode envelope: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"shared/platform/kms/kms.go","lineNumber":196,"sourceCode":"\t}\n\tif len(plaintext) > maxPlaintextBytes {\n\t\treturn nil, fmt.Errorf(\"kms: plaintext exceeds %d bytes\", maxPlaintextBytes)\n\t}\n\tvar response struct {\n\t\tKeyID      string `json:\"key_id\"`\n\t\tCiphertext string `json:\"ciphertext\"`\n\t}\n\tif err := c.call(ctx, c.region, c.keyID, \"encrypt\", map[string]string{\n\t\t\"plaintext\": base64.StdEncoding.EncodeToString(plaintext),\n\t}, &response); err != nil {\n\t\treturn nil, err\n\t}\n\tif response.KeyID != c.keyID || strings.TrimSpace(response.Ciphertext) == \"\" {\n\t\treturn nil, errors.New(\"kms: invalid encrypt response\")\n\t}\n\tenvelope, err := json.Marshal(Envelope{Provider: c.provider, Region: c.region, KeyID: response.KeyID, Ciphertext: response.Ciphertext})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"kms: encode envelope: %w\", err)\n\t}\n\treturn append([]byte(prefix), envelope...), nil\n}\n\n// Decrypt delegates to configured environment client.\nfunc Decrypt(ctx context.Context, blob []byte) ([]byte, error) {\n\tclient, err := FromEnvironment()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn client.Decrypt(ctx, blob)\n}\n\n// DecryptPayload unwraps an artifact data key with the dedicated payload KEK,\n// while allowing the explicitly configured legacy secrets key during cutover.\nfunc DecryptPayload(ctx context.Context, blob []byte) ([]byte, error) {\n\tclient, err := FromPayloadEnvironment()\n\tif err != nil {","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/kms/kms.go#L178-L214","documentation":"Encrypt marshals the Envelope struct (provider, region, keyID, ciphertext) to JSON after a successful API call; a marshal failure is wrapped as \"kms: encode envelope: %w\". Since Envelope contains only plain string fields, this is a defensive error that is practically unreachable unless json.Marshal itself is broken (e.g. unsupported options).","triggerScenarios":"Practically never: json.Marshal of a struct with four string fields cannot fail. It exists as a defensive branch after the encrypt API response is validated.","commonSituations":"Only conceivable in exotic environments where the json package behavior is altered or memory allocation fails; otherwise never seen in production.","solutions":["Treat it as an internal invariant failure; retry the Encrypt call.","If it reproduces, check for custom json package replacements/vendoring issues in the build.","Report the wrapped inner error upstream since Marshal of string-only structs should never fail."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"enc, err := client.Encrypt(ctx, plaintext)\nif err != nil {\n    if strings.Contains(err.Error(), \"encode envelope\") {\n        return fmt.Errorf(\"internal: envelope marshal failed: %w\", err)\n    }\n    return err\n}","preventionTips":["Treat occurrences as an internal invariant failure and alert.","Avoid replacing/forking the json package in builds.","Keep Envelope fields JSON-marshalable types."],"tags":["go","kms","json","envelope"],"backgroundTag":"json-marshal-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"}