Files
mesh-catalog/modules/gitea/internal/forgehttp/vault.go
T
jschoubben 280adcce68 C4: the vault keeps the forge token with the scopes it was minted with
The file beside the token that kept its scopes went with C4, and with it
the re-mint of a token carrying a scope beyond the tools' (write:admin).
The value written through own-put now holds the token and its scopes; a
token minted with other scopes, or a value naming none, is minted again
unasked and the one it replaces deleted at the forge. The Go bundles read
the token out of that value. The lost test is back as a row of the scope
test in test/token.test.ts.

Builds: design 47 H2 §808; H-A-439
2026-11-22 01:52:24 +01:00

174 lines
5.5 KiB
Go

package forgehttp
// The forge module's tokens as its Go bundles read them (design 47 H2 §808; H-A-422 (1), H-A-426): kept in the vault as
// the module's own, `module/gitea/<account>`, written by the TypeScript bundle's minter through own-put, and read here
// through the seat `mesh-vault`'s `get`, sealed to this process's own key (the Go SDK's seal opens it), at start and at
// each `secret-changed` that names it. Never a file: the bundle holds the token in memory only, and says nothing of it.
import (
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/seal"
)
// VaultGet is the vault seat's verb a token is read by, and SecretChanged the vault's event that a secret changed.
const (
VaultGet = "seat:mesh-vault.get"
SecretChanged = "mesh-vault.secret-changed"
)
// VaultName is the vault's name of an account's token (H-A-426).
func VaultName(account string) string { return "module/gitea/" + account }
// Ask asks one seat verb and answers its reply (stdio.Ask).
type Ask func(address string, args any) (json.RawMessage, error)
// VaultToken is one account's token as this process holds it, read from the vault.
type VaultToken struct {
Account string
ask Ask
keys seal.KeyPair
mu sync.Mutex
token string
generation int
}
// NewVaultToken is an account's token read through ask, sealed to a key made for this process.
func NewVaultToken(account string, ask Ask) (*VaultToken, error) {
keys, err := seal.GenerateKeyPair()
if err != nil {
return nil, fmt.Errorf("no key to read %s's token with: %w", account, err)
}
return &VaultToken{Account: account, ask: ask, keys: keys}, nil
}
// Load reads the account's token from the vault again.
func (t *VaultToken) Load() error {
raw, err := t.ask(VaultGet, map[string]any{"name": VaultName(t.Account), "public_key": t.keys.PublicKey})
if err != nil {
return fmt.Errorf("the vault did not hand %s: %w", VaultName(t.Account), err)
}
var got struct {
Generation int `json:"generation"`
Sealed *seal.Box `json:"sealed"`
}
if err := json.Unmarshal(raw, &got); err != nil || got.Sealed == nil {
return fmt.Errorf("the vault answered no sealed %s", VaultName(t.Account))
}
opened, err := seal.Open(*got.Sealed, t.keys.PrivateKey)
if err != nil {
return fmt.Errorf("%s does not open with this process's key: %w", VaultName(t.Account), err)
}
// The value holds the token and the scopes it was minted with (H-A-439); the minter judges the scopes, this reads the token.
var value struct {
Token string `json:"token"`
}
if err := json.Unmarshal([]byte(opened), &value); err != nil {
return fmt.Errorf("the vault's %s is not a token with its scopes: %w", VaultName(t.Account), err)
}
token := strings.TrimSpace(value.Token)
if token == "" {
return fmt.Errorf("the vault's %s is empty", VaultName(t.Account))
}
t.mu.Lock()
t.token, t.generation = token, got.Generation
t.mu.Unlock()
return nil
}
// Token is the token held; read from the vault when none is yet (the module mints it first).
func (t *VaultToken) Token() (string, error) {
t.mu.Lock()
token := t.token
t.mu.Unlock()
if token != "" {
return token, nil
}
if err := t.Load(); err != nil {
return "", fmt.Errorf("the token for %s is not kept yet (the module mints it): %w", t.Account, err)
}
t.mu.Lock()
defer t.mu.Unlock()
return t.token, nil
}
// Generation is the vault's generation of the token held, 0 when none is held.
func (t *VaultToken) Generation() int {
t.mu.Lock()
defer t.mu.Unlock()
return t.generation
}
// Changed takes one `secret-changed`: the token is read again when it names this account's; answers whether it did.
func (t *VaultToken) Changed(body []byte) (bool, error) {
var e struct {
Name string `json:"name"`
}
if err := json.Unmarshal(body, &e); err != nil {
return false, fmt.Errorf("%s is not a secret's change: %w", SecretChanged, err)
}
if e.Name != VaultName(t.Account) {
return false, nil
}
return true, t.Load()
}
var (
processTokens = map[string]*VaultToken{}
processTokensMu sync.Mutex
)
// TokenOf is this process's one token of account, read through the runtime's seat calls.
func TokenOf(account string) (*VaultToken, error) {
processTokensMu.Lock()
defer processTokensMu.Unlock()
if t, ok := processTokens[account]; ok {
return t, nil
}
t, err := NewVaultToken(account, stdio.Ask)
if err != nil {
return nil, err
}
processTokens[account] = t
return t, nil
}
// Follow reads each account's token once the runtime serves, and again at each `secret-changed` naming it, saying
// through logf what it read (never the token). It returns when the subscription ends.
func Follow(logf func(format string, args ...any), accounts ...string) error {
var held []*VaultToken
for _, a := range accounts {
t, err := TokenOf(a)
if err != nil {
return err
}
held = append(held, t)
}
<-stdio.Served()
for _, t := range held {
if err := t.Load(); err != nil {
logf("%s is not read at the start (the module mints it): %v", VaultName(t.Account), err)
} else {
logf("read %s at generation %d", VaultName(t.Account), t.Generation())
}
}
return stdio.Subscribe(SecretChanged, func(e stdio.Envelope) error {
var errs []error
for _, t := range held {
if read, err := t.Changed(e.Body); err != nil {
errs = append(errs, err)
logf("%s changed and could not be read again: %v", VaultName(t.Account), err)
} else if read {
logf("read %s again at generation %d", VaultName(t.Account), t.Generation())
}
}
return errors.Join(errs...)
})
}