A source now declares how its deliveries prove themselves: token (the default, unchanged), hmac (HMAC-SHA256 over the raw body, per-source header and prefix, secret sealed at rest and write-only) or none. The events module verifies before the connector parses, answers 401 and logs the refusal. The digest comparison is ported from internal/webhookguard/hmac.go and the header-borne signature from internal/infrastructure/captureintake/receiver.go; the old copies stay until their service goes. The seal package moves to internal/platform/seal so events can seal the secret.
89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
// Package seal encrypts credentials at rest with age. Each sealed value is
|
|
// bound to a label naming where it is stored, so a value copied into another
|
|
// row doesn't open there.
|
|
package seal
|
|
|
|
import (
|
|
"bytes"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"filippo.io/age"
|
|
)
|
|
|
|
// ErrWrongLabel means a sealed value was opened under a label other than the
|
|
// one it was sealed for.
|
|
var ErrWrongLabel = errors.New("sealed value belongs to a different record")
|
|
|
|
// Sealer seals to its own identity and to any backup recipients, and opens
|
|
// with its identity.
|
|
type Sealer struct {
|
|
identity *age.X25519Identity
|
|
recipients []age.Recipient
|
|
}
|
|
|
|
// New returns a sealer for an age identity ("AGE-SECRET-KEY-1...") and the
|
|
// backup recipients ("age1...") that can also open what it seals.
|
|
func New(identity string, backups []string) (*Sealer, error) {
|
|
id, err := age.ParseX25519Identity(identity)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("age identity: %w", err)
|
|
}
|
|
s := &Sealer{identity: id, recipients: []age.Recipient{id.Recipient()}}
|
|
for _, b := range backups {
|
|
r, err := age.ParseX25519Recipient(b)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("backup recipient %q: %w", b, err)
|
|
}
|
|
s.recipients = append(s.recipients, r)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
type envelope struct {
|
|
Label string `json:"label"`
|
|
Data []byte `json:"data"`
|
|
}
|
|
|
|
// Seal encrypts data for label.
|
|
func (s *Sealer) Seal(label string, data []byte) ([]byte, error) {
|
|
plain, err := json.Marshal(envelope{Label: label, Data: data})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out bytes.Buffer
|
|
w, err := age.Encrypt(&out, s.recipients...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("seal: %w", err)
|
|
}
|
|
if _, err := w.Write(plain); err != nil {
|
|
return nil, fmt.Errorf("seal: %w", err)
|
|
}
|
|
if err := w.Close(); err != nil {
|
|
return nil, fmt.Errorf("seal: %w", err)
|
|
}
|
|
return out.Bytes(), nil
|
|
}
|
|
|
|
// Open decrypts a value sealed for label.
|
|
func (s *Sealer) Open(label string, sealed []byte) ([]byte, error) {
|
|
r, err := age.Decrypt(bytes.NewReader(sealed), s.identity)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open: %w", err)
|
|
}
|
|
plain, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open: %w", err)
|
|
}
|
|
var env envelope
|
|
if err := json.Unmarshal(plain, &env); err != nil {
|
|
return nil, fmt.Errorf("open: %w", err)
|
|
}
|
|
if env.Label != label {
|
|
return nil, ErrWrongLabel
|
|
}
|
|
return env.Data, nil
|
|
}
|