Files
mesh-catalog/modules/mesh-config/cmd/mesh-config/delivery.go
T

134 lines
4.7 KiB
Go

package main
// What mesh-config asks of the delivery parts and the vault, through their tools and seat verbs on the bus: never a
// bucket of theirs (novox/hq ADR 0596). Each key is the address a module's `invokes` names (mesh-sdk calladdress).
import (
"encoding/json"
"fmt"
"strings"
engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
)
// The addresses asked.
const (
// HavesAsk is the status view's read of the node-engines' haves, narrowed to one node and module.
HavesAsk = "mesh-delivery-view.node_haves"
// ManifestReadsAsk is the node-engine seat's verb saying what manifest forms a node-engine reads (ADR 0618 §3), asked
// of one node by the address's `@<node>`.
ManifestReadsAsk = "seat:node-engine.manifest-reads"
// ConfigReadersAsk is the indexer's own tool saying who reads a module's config keys (design 32).
ConfigReadersAsk = "mesh-delivery-indexer.indexer_config_readers"
// VaultRetireAsk is the seat mesh-vault's verb retiring one module's own secret on a node (design 32).
VaultRetireAsk = "seat:mesh-vault.retire"
)
// toolDelivery answers Delivery through the bus.
type toolDelivery struct {
ask func(key string, body any) (json.RawMessage, error)
}
// Have asks node_haves for one node and module and reads the fingerprint the node runs.
func (d toolDelivery) Have(node, module string) (string, bool, error) {
raw, err := d.ask(HavesAsk, map[string]any{"node": node, "module": module})
if err != nil {
return "", false, err
}
answer, output, ok := unwrapAnswer(raw)
if !ok {
return "", false, fmt.Errorf("node_haves refused: %s", output)
}
var kept struct {
Keys map[string]json.RawMessage `json:"keys"`
}
if err := json.Unmarshal(answer, &kept); err != nil {
return "", false, fmt.Errorf("node_haves answered what is not its listing: %s", output)
}
key, err := engineclient.HaveKey(node, module)
if err != nil {
return "", false, err
}
have, ok := kept.Keys[key]
if !ok {
return "", false, nil
}
h, err := engineclient.DecodeHave(have)
if err != nil {
return "", false, fmt.Errorf("the have of %s on %s is not readable: %w", module, node, err)
}
return h.Fp, true, nil
}
// Reads asks a node's node-engine what it reads; an answer means it states it.
func (d toolDelivery) Reads(node string) error {
_, err := d.ask(ManifestReadsAsk+"@"+node, map[string]any{})
return err
}
// Readers asks the indexer who reads a module's config keys; an indexer that does not serve the tool yet is "not known".
func (d toolDelivery) Readers(module string) ([]ReaderRow, bool, error) {
raw, err := d.ask(ConfigReadersAsk, map[string]any{"module": module})
if err != nil {
if notServed(err) {
return nil, false, nil
}
return nil, false, err
}
answer, output, ok := unwrapAnswer(raw)
if !ok {
if notServed(fmt.Errorf("%s", output)) {
return nil, false, nil
}
return nil, false, fmt.Errorf("indexer_config_readers refused: %s", output)
}
var a struct {
Readers []ReaderRow `json:"readers"`
}
if err := json.Unmarshal(answer, &a); err != nil {
return nil, false, fmt.Errorf("indexer_config_readers answered what is not its listing: %s", output)
}
return a.Readers, true, nil
}
// notServed is whether an error says the address has no holder serving it.
func notServed(err error) bool {
m := strings.ToLower(err.Error())
for _, w := range []string{"no responders", "not served", "unknown tool", "no such tool", "serves no"} {
if strings.Contains(m, w) {
return true
}
}
return false
}
// toolVault asks the seat mesh-vault to retire a secret: the hand act that warrants it is recorded first, by this
// module, naming the node, module, secret and the ask of the Approve; the vault reads it back by its id (`warrant`).
type toolVault struct {
ask func(key string, body any) (json.RawMessage, error)
recorder meshhandactsclient.Recorder
}
// Retire records the warrant and calls the vault's verb.
func (v toolVault) Retire(r RetireRequest) error {
taken, err := v.recorder.Record(meshhandactsclient.Request{What: r.Node + " " + r.Module + " " + r.Name + " " + r.Ask,
Why: "the operator approved retiring " + r.Module + "'s secret " + r.Name + " on " + r.Node + " (ask " + r.Ask + ")",
Cause: "the operator's Approve of ask " + r.Ask, Verb: "mesh-vault.retire"})
if err != nil {
return err
}
answer, _, _ := unwrapAnswer(taken.Answer)
var rec struct {
Act struct {
ID string `json:"id"`
} `json:"act"`
}
if json.Unmarshal(answer, &rec) != nil || rec.Act.ID == "" {
return fmt.Errorf("mesh-hand-acts recorded the warrant and named no act")
}
_, err = v.ask(VaultRetireAsk, map[string]any{"node": r.Node, "module": r.Module, "name": r.Name, "warrant": rec.Act.ID, "ask": r.Ask})
return err
}