{"record":{"id":"6510855f70d9a82f","repo":"JuliusBrussee/caveman","slug":"envelope-wrap-data-key-w","errorCode":null,"errorMessage":"envelope: wrap data key: %w","messagePattern":"envelope: wrap data key: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/envelope/envelope.go","lineNumber":90,"sourceCode":"\t\treturn nil, nil, fmt.Errorf(\"envelope: data key entropy: %w\", err)\n\t}\n\tblock, err := aes.NewCipher(dataKey)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: aes: %w\", err)\n\t}\n\tgcm, err := cipher.NewGCM(block)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: gcm: %w\", err)\n\t}\n\tnonce := make([]byte, gcm.NonceSize())\n\tif _, err := rand.Read(nonce); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: nonce entropy: %w\", err)\n\t}\n\tciphertext = gcm.Seal(nonce, nonce, plaintext, aad)\n\n\twrapped, err := secretbox.EncryptPayloadKey(dataKey)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: wrap data key: %w\", err)\n\t}\n\tmeta := Metadata{Scheme: scheme, WrappedDataKey: base64.StdEncoding.EncodeToString(wrapped), ScopeHash: scopeHash}\n\tmetaJSON, err = json.Marshal(meta)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: marshal metadata: %w\", err)\n\t}\n\treturn ciphertext, metaJSON, nil\n}\n\n// Open reverses Seal: it unwraps the data key from metadata and decrypts the\n// ciphertext. An unknown scheme fails closed.\nfunc Open(ciphertext []byte, metaJSON []byte) ([]byte, error) {\n\tvar meta Metadata\n\tif err := json.Unmarshal(metaJSON, &meta); err != nil {\n\t\treturn nil, fmt.Errorf(\"envelope: parse metadata: %w\", err)\n\t}\n\tif meta.Scheme == schemeV2 {\n\t\treturn nil, fmt.Errorf(\"envelope: tenant scope required for scheme %q\", meta.Scheme)","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/envelope/envelope.go#L72-L108","documentation":"After sealing, seal wraps the plaintext data key via secretbox.EncryptPayloadKey and wraps failure as 'envelope: wrap data key: %w'. Envelope encryption requires the data key to be encrypted under the master/keyring key; if the wrapping step fails, no usable ciphertext metadata can be produced, so sealing aborts.","triggerScenarios":"Seal/SealForScope while secretbox.EncryptPayloadKey fails — typically the master wrapping key/secretbox context is uninitialized, or the underlying crypto primitives fail.","commonSituations":"Service started before KMS/master key material was loaded; rotated or missing master key; misconfigured secretbox init in a custom bootstrap path.","solutions":["Inspect the wrapped cause for the underlying secretbox failure.","Ensure the master/wrapping key is loaded and initialized before calling Seal (complete bootstrap/KMS setup first).","Verify secretbox configuration matches the deployed key material (rotation can leave stale keys).","Retry after re-initializing the crypto context; if persistent, check library version compatibility of key formats."],"exampleFix":"// before\nciphertext, meta, err := envelope.Seal(plaintext, aad) // master key not loaded yet\n// error: envelope: wrap data key: ...\n\n// after\nif err := kms.Init(ctx); err != nil { return err } // load master key first\nciphertext, meta, err := envelope.Seal(plaintext, aad)","handlingStrategy":"validation","validationCode":"// before sealing, ensure the master wrapping key is initialized\nif !secretbox.Ready() { // or equivalent readiness check for your bootstrap\n    return errors.New(\"master wrapping key not loaded; complete KMS/bootstrap first\")\n}","typeGuard":null,"tryCatchPattern":"ciphertext, meta, err := envelope.Seal(plaintext, aad)\nif err != nil {\n    var wrapErr error\n    if errors.As(err, &wrapErr) && strings.Contains(err.Error(), \"wrap data key\") {\n        // re-init key material, then retry once\n        if initErr := kms.Init(ctx); initErr == nil {\n            ciphertext, meta, err = envelope.Seal(plaintext, aad)\n        }\n    }\n    return err\n}","preventionTips":["Initialize KMS/master key material during service startup, before any Seal calls.","Handle key rotation so the wrapping key context stays current.","Verify key format compatibility when upgrading library versions."],"tags":["crypto","envelope-encryption","key-wrapping"],"backgroundTag":"missing-credentials","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"}