Files
mesh-controller/internal/secrets/seal.go
T
jschoubben e140ed5d0b An operator key, a second seal on every own secret, and the vault keeps the export
novox/hq ADR 0085, amended: the mesh's root secrets — the store's superuser,
the broker's administrator, every secret a module holds for itself — were
sealed to a node key and nothing else, so a lost node took them with it.
Now the mesh records an operator's public sealing key and seals every own
secret to it as well, minted or accepted. The private half is written once
by `operator key new` to a file the operator keeps off the mesh; the mesh
holds one more blob per secret that it cannot open.

`secret recover` opens a secret with that key, to a 0600 file, from the
store or from an export; `secret export` writes every operator-sealed copy
as ciphertext. A module that `keeps` (the vault) is handed that export as a
declared file on its own disk, so recovery survives the store.

Secrets made before the key exists have no operator copy and are said so —
the plaintext was discarded — until each is issued again.
2026-09-20 23:56:49 +02:00

201 lines
8.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package secrets
import (
"crypto/ecdh"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"golang.org/x/crypto/nacl/box"
)
// Secrets the mesh delivers and cannot read.
//
// **What this is not.** The obvious arrangement is a credentials column, encrypted at rest. It
// has been built, in another mesh, and that mesh's own tooling records what it bought: a query
// against the encrypted column returns zero rows and proves nothing, so auditing moved to the
// decrypted copies on the nodes; and the tool for finding a secret has to search **by value**
// rather than by name, because the same password sits in the provisions table, in the environment
// table, in each node's environment file in plain text, and inside every connection string
// composed from it — copies its own documentation calls "often the only copies actually in use".
//
// Two faults there, and encryption at rest addresses neither. **The control plane can read what
// it stores**, so a copy of its database is a copy of every credential in the mesh. And **one
// secret has many homes with nothing tracking them.**
//
// So here the value is sealed to the node that will use it before it is stored, with a key that
// node generated and whose private half the mesh has never seen. What gets written is unusable by
// whoever holds it, the mesh included. And nothing is composed centrally — a connection string is
// assembled on the machine that needs one, so the mesh never mints a second copy in a shape
// nothing tracks.
// Sealed is one value, closed to both ends of a provision.
//
// Two blobs of the same secret rather than one shared key: a key both ends hold is a key the mesh
// would have to distribute, which is this problem again one level down.
type Sealed struct {
ForConsumer string
ForProvider string
// Which key each was sealed to, kept so a node that regenerated its key can be told what it
// can no longer open rather than discovering it as a service that will not start.
ConsumerKey string
ProviderKey string
}
// Make generates a secret and seals it to both ends, keeping no readable copy.
//
// The plaintext exists for the length of this call. Rotation is therefore generating a new one
// rather than reading the old one back — the only version of rotation that is honest about what
// the mesh knows.
func Make(consumerKey, providerKey string) (Sealed, error) {
sealed, _, err := MakeAlso(consumerKey, providerKey)
return sealed, err
}
// MakeAlso is Make with further recipients: the same fresh value, sealed once more to each key in
// `also`, returned in that order.
//
// **For the operator key, and nothing else so far** (novox/hq ADR 0085, amended). A secret a module
// holds for itself is sealed to its node and, when the mesh has an operator key, to that as well —
// so a person holding the key can recover it when the node cannot. The plaintext still exists only
// inside this call; a third blob is one more thing the mesh cannot open, not one more copy it can.
func MakeAlso(consumerKey, providerKey string, also ...string) (Sealed, []string, error) {
if consumerKey == "" || providerKey == "" {
// Sealing to an empty key would produce a blob nobody can open, stored as though it were
// a working credential. The caller knows which node is which and says so.
return Sealed{}, nil, fmt.Errorf("both ends need a sealing key before a secret can be made")
}
// 30 bytes, not 32: base64url of 30 is exactly 40 characters, and 40 is the longest secret an
// S3 access key accepts (8–40), the tightest of the backends a minted password reaches — the same
// "fit the tightest backend" rule ADR 0049 sets for the login, on the secret. 240 bits is ample.
value := make([]byte, 30)
if _, err := rand.Read(value); err != nil {
return Sealed{}, nil, err
}
// Base64 without padding, because it lands in a configuration file something else parses and
// a password containing a newline or a quote is a support call.
password := base64.RawURLEncoding.EncodeToString(value)
forConsumer, err := Seal(consumerKey, []byte(password))
if err != nil {
return Sealed{}, nil, err
}
forProvider, err := Seal(providerKey, []byte(password))
if err != nil {
return Sealed{}, nil, err
}
more := make([]string, 0, len(also))
for _, key := range also {
blob, err := Seal(key, []byte(password))
if err != nil {
return Sealed{}, nil, err
}
more = append(more, blob)
}
return Sealed{
ForConsumer: forConsumer, ForProvider: forProvider,
ConsumerKey: consumerKey, ProviderKey: providerKey,
}, more, nil
}
// Accept seals a value somebody supplied, rather than one the mesh made.
//
// **The mesh generates most of what it hands out and discards the plaintext.** An API key for a
// hosted service does not work that way: it comes from a person, and the mesh's job is to carry it
// to the machines that need it without being able to read it afterwards
// (novox/hq ADR 0024).
//
// So the value is sealed on the way in and **the plaintext is not kept**. That is the whole of the
// difference from the arrangement this replaces, where an operator-supplied key sits in a column
// the control plane can read — which makes a copy of the database a copy of every account the mesh
// touches.
//
// The consequence is deliberate and worth stating: **the mesh cannot show it back.** Somebody who
// loses the key gets a new one from wherever it came from; there is no "reveal" and there cannot
// be one, because a mesh that can reveal a secret is a mesh that holds it.
func Accept(value string, consumerKey, providerKey string) (Sealed, error) {
if strings.TrimSpace(value) == "" {
return Sealed{}, fmt.Errorf("there is nothing to seal")
}
if consumerKey == "" || providerKey == "" {
return Sealed{}, fmt.Errorf("both ends need a sealing key before a secret can be kept")
}
forConsumer, err := Seal(consumerKey, []byte(value))
if err != nil {
return Sealed{}, err
}
forProvider, err := Seal(providerKey, []byte(value))
if err != nil {
return Sealed{}, err
}
return Sealed{
ForConsumer: forConsumer, ForProvider: forProvider,
ConsumerKey: consumerKey, ProviderKey: providerKey,
}, nil
}
// Open is the other half of Seal, for the one holder of a private key this program ever acts for:
// the operator, recovering a secret with the key that never entered the mesh (novox/hq ADR 0085,
// amended). A node opens its own blobs in the host; the controller opens nothing of a node's, and
// cannot — it has no node's private key, which is the whole point of sealing.
func Open(privateKey string, sealed string) ([]byte, error) {
private, err := base64.StdEncoding.DecodeString(strings.TrimSpace(privateKey))
if err != nil || len(private) != 32 {
return nil, fmt.Errorf("that is not a sealing key")
}
key, err := ecdh.X25519().NewPrivateKey(private)
if err != nil {
return nil, fmt.Errorf("that is not a usable sealing key: %w", err)
}
blob, err := base64.StdEncoding.DecodeString(sealed)
if err != nil {
return nil, fmt.Errorf("this is not a sealed value: %w", err)
}
var pub, priv [32]byte
copy(pub[:], key.PublicKey().Bytes())
copy(priv[:], private)
out, ok := box.OpenAnonymous(nil, blob, &pub, &priv)
if !ok {
return nil, fmt.Errorf("this was not sealed to that key")
}
return out, nil
}
// Keypair makes a sealing keypair for a holder outside the mesh — the operator. The private half is
// returned to be written where the caller says and nowhere else; the public half is what the mesh
// records and seals to.
func Keypair() (public, private string, err error) {
key, err := ecdh.X25519().GenerateKey(rand.Reader)
if err != nil {
return "", "", err
}
return base64.StdEncoding.EncodeToString(key.PublicKey().Bytes()),
base64.StdEncoding.EncodeToString(key.Bytes()), nil
}
// Fingerprint names a public key without being one: the first bytes of its hash, so two people can
// agree which key they mean out loud.
func Fingerprint(publicKey string) string {
sum := sha256.Sum256([]byte(strings.TrimSpace(publicKey)))
return "sha256:" + hex.EncodeToString(sum[:8])
}
// Seal closes a value to a node's public sealing key.
func Seal(publicKey string, value []byte) (string, error) {
public, err := base64.StdEncoding.DecodeString(publicKey)
if err != nil || len(public) != 32 {
return "", fmt.Errorf("%q is not a sealing key", publicKey)
}
var pub [32]byte
copy(pub[:], public)
sealed, err := box.SealAnonymous(nil, value, &pub, rand.Reader)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(sealed), nil
}