JuliusBrussee/caveman · info

kms: encode envelope

Error message

kms: encode envelope: %w

What it means

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).

Solutions

  1. Treat it as an internal invariant failure; retry the Encrypt call.
  2. If it reproduces, check for custom json package replacements/vendoring issues in the build.
  3. Report the wrapped inner error upstream since Marshal of string-only structs should never fail.
Defensive patterns

Strategy: try-catch

Try / catch

enc, err := client.Encrypt(ctx, plaintext)
if err != nil {
    if strings.Contains(err.Error(), "encode envelope") {
        return fmt.Errorf("internal: envelope marshal failed: %w", err)
    }
    return err
}

Prevention

When it happens

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

Common situations: Only conceivable in exotic environments where the json package behavior is altered or memory allocation fails; otherwise never seen in production.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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

Appendix: source

Thrown at shared/platform/kms/kms.go:196

	}
	if len(plaintext) > maxPlaintextBytes {
		return nil, fmt.Errorf("kms: plaintext exceeds %d bytes", maxPlaintextBytes)
	}
	var response struct {
		KeyID      string `json:"key_id"`
		Ciphertext string `json:"ciphertext"`
	}
	if err := c.call(ctx, c.region, c.keyID, "encrypt", map[string]string{
		"plaintext": base64.StdEncoding.EncodeToString(plaintext),
	}, &response); err != nil {
		return nil, err
	}
	if response.KeyID != c.keyID || strings.TrimSpace(response.Ciphertext) == "" {
		return nil, errors.New("kms: invalid encrypt response")
	}
	envelope, err := json.Marshal(Envelope{Provider: c.provider, Region: c.region, KeyID: response.KeyID, Ciphertext: response.Ciphertext})
	if err != nil {
		return nil, fmt.Errorf("kms: encode envelope: %w", err)
	}
	return append([]byte(prefix), envelope...), nil
}

// Decrypt delegates to configured environment client.
func Decrypt(ctx context.Context, blob []byte) ([]byte, error) {
	client, err := FromEnvironment()
	if err != nil {
		return nil, err
	}
	return client.Decrypt(ctx, blob)
}

// DecryptPayload unwraps an artifact data key with the dedicated payload KEK,
// while allowing the explicitly configured legacy secrets key during cutover.
func DecryptPayload(ctx context.Context, blob []byte) ([]byte, error) {
	client, err := FromPayloadEnvironment()
	if err != nil {

View on GitHub (pinned to 3ee70a1026)