285 lines
11 KiB
Go
285 lines
11 KiB
Go
package main
|
|
|
|
// Issue 749's proof on a real bus: a NATS server whose users are the accounts of the mesh's parts (the vault's own,
|
|
// the provider's machine's runtime, each machine's provisioner client), each allowed only its own subjects, so the
|
|
// caller a tool sees is the one the bus proved — as the mesh's runtime does (subjects ending in the caller's token,
|
|
// which only that caller may publish; mesh-controller internal/broker). A listener on every subject keeps every byte
|
|
// that crosses.
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/seal"
|
|
"github.com/nats-io/nats-server/v2/server"
|
|
"github.com/nats-io/nats.go"
|
|
)
|
|
|
|
// callerToken is a caller's name as the last token of a subject.
|
|
func callerToken(user string) string { return strings.ReplaceAll(user, ".", "_") }
|
|
|
|
func untoken(token string) string { return strings.ReplaceAll(token, "_", ".") }
|
|
|
|
const busConf = `listen: 127.0.0.1:-1
|
|
authorization {
|
|
users = [
|
|
{ user: "novox.mesh-vault", password: "pw", permissions: {
|
|
publish: { allow: ["mesh.mod.gitea.call.gitea_agent_token.novox.novox_mesh-vault"] }
|
|
subscribe: { allow: ["mesh.seat.mesh-vault.call.get.*", "_INBOX.novox.mesh-vault.>"] }
|
|
allow_responses: true } }
|
|
{ user: "novox.node-tools", password: "pw", permissions: {
|
|
publish: { deny: [">"] }
|
|
subscribe: { allow: ["mesh.mod.gitea.call.gitea_agent_token.novox.*"] }
|
|
allow_responses: true } }
|
|
{ user: "provisioner.anchor", password: "pw", permissions: {
|
|
publish: { allow: ["mesh.seat.mesh-vault.call.get.provisioner_anchor"] }
|
|
subscribe: { allow: ["_INBOX.provisioner.anchor.>"] } } }
|
|
{ user: "provisioner.laptop", password: "pw", permissions: {
|
|
publish: { allow: ["mesh.seat.mesh-vault.call.get.provisioner_laptop"] }
|
|
subscribe: { allow: ["_INBOX.provisioner.laptop.>"] } } }
|
|
{ user: "tap", password: "pw", permissions: { publish: { deny: [">"] }, subscribe: { allow: [">"] } } }
|
|
]
|
|
}
|
|
`
|
|
|
|
type busAnswer struct {
|
|
Result json.RawMessage `json:"result,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
func connectAs(t *testing.T, url, user string) *nats.Conn {
|
|
t.Helper()
|
|
conn, err := nats.Connect(url, nats.UserInfo(user, "pw"), nats.CustomInboxPrefix("_INBOX."+user),
|
|
nats.ErrorHandler(func(*nats.Conn, *nats.Subscription, error) {}))
|
|
if err != nil {
|
|
t.Fatalf("%s cannot connect: %v", user, err)
|
|
}
|
|
t.Cleanup(conn.Close)
|
|
return conn
|
|
}
|
|
|
|
// forgeOnABus is the forge's read tokens issued into a vault keeping rule, on a real bus: what a machine's client is
|
|
// answered when it asks over the bus with a key of its own (opened), and every byte that crossed.
|
|
func forgeOnABus(t *testing.T, rule Askers) (ask func(user, name string) (string, error), heard func() string) {
|
|
t.Helper()
|
|
conf := filepath.Join(t.TempDir(), "bus.conf")
|
|
if err := os.WriteFile(conf, []byte(busConf), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
opts, err := server.ProcessConfigFile(conf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
opts.NoLog, opts.NoSigs = true, true
|
|
s, err := server.NewServer(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
go s.Start()
|
|
if !s.ReadyForConnections(10 * time.Second) {
|
|
t.Fatal("the bus did not come up")
|
|
}
|
|
t.Cleanup(s.Shutdown)
|
|
url := s.ClientURL()
|
|
|
|
// Every byte that crosses.
|
|
var mu sync.Mutex
|
|
var crossed []string
|
|
tap := connectAs(t, url, "tap")
|
|
if _, err := tap.Subscribe(">", func(m *nats.Msg) {
|
|
mu.Lock()
|
|
crossed = append(crossed, m.Subject+" "+string(m.Data))
|
|
mu.Unlock()
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = tap.Flush()
|
|
|
|
// The forge's machine's runtime serves the stand-in issuing tool, naming the caller the subject proves.
|
|
tool := perHolderProvider(agentTokens, nil)
|
|
runtime := connectAs(t, url, "novox.node-tools")
|
|
if _, err := runtime.Subscribe("mesh.mod.gitea.call.gitea_agent_token.novox.*", func(m *nats.Msg) {
|
|
parts := strings.Split(m.Subject, ".")
|
|
var args map[string]any
|
|
_ = json.Unmarshal(m.Data, &args)
|
|
out, err := tool(untoken(parts[len(parts)-1]), args)
|
|
answer := busAnswer{}
|
|
if err != nil {
|
|
answer.Error = err.Error()
|
|
} else {
|
|
answer.Result, _ = json.Marshal(out)
|
|
}
|
|
raw, _ := json.Marshal(answer)
|
|
_ = m.Respond(raw)
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = runtime.Flush()
|
|
|
|
// The vault, on its own account: it asks the tool over the bus in its own name, and answers `get` to the caller
|
|
// the subject proves. Its rule for who may ask would let both clients ask for both values: its rule for a
|
|
// holder's value is what keeps each to its own.
|
|
v := newVault(t, rule)
|
|
vaultConn := connectAs(t, url, "novox.mesh-vault")
|
|
v.Ask = func(key string, body any) (json.RawMessage, error) {
|
|
call, node, _ := strings.Cut(key, "@")
|
|
module, tl, _ := strings.Cut(call, ".")
|
|
raw, _ := json.Marshal(body)
|
|
msg, err := vaultConn.Request(fmt.Sprintf("mesh.mod.%s.call.%s.%s.%s", module, tl, node,
|
|
callerToken("novox.mesh-vault")), raw, 5*time.Second)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var a busAnswer
|
|
if err := json.Unmarshal(msg.Data, &a); err != nil {
|
|
return nil, err
|
|
}
|
|
if a.Error != "" {
|
|
return nil, fmt.Errorf("%s", a.Error)
|
|
}
|
|
return a.Result, nil
|
|
}
|
|
if _, err := vaultConn.Subscribe("mesh.seat.mesh-vault.call.get.*", func(m *nats.Msg) {
|
|
parts := strings.Split(m.Subject, ".")
|
|
var args struct {
|
|
Name string `json:"name"`
|
|
PublicKey string `json:"public_key"`
|
|
}
|
|
_ = json.Unmarshal(m.Data, &args)
|
|
answer := busAnswer{}
|
|
got, err := v.Get(untoken(parts[len(parts)-1]), args.Name, args.PublicKey)
|
|
if err != nil {
|
|
answer.Error = err.Error()
|
|
} else {
|
|
answer.Result, _ = json.Marshal(got)
|
|
}
|
|
raw, _ := json.Marshal(answer)
|
|
_ = m.Respond(raw)
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = vaultConn.Flush()
|
|
|
|
// The controller's `secret issued` for the forge's read tokens, naming both holders.
|
|
issued, err := v.TakeIssuedFor(ControllerCaller, "gitea-read", "novox", "gitea", "gitea_agent_token", twoAgents)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(issued.Holders) != 2 {
|
|
t.Fatalf("kept %+v", issued)
|
|
}
|
|
|
|
// Each machine's client asks over the bus, with a key of its own.
|
|
ask = func(user, name string) (string, error) {
|
|
conn := connectAs(t, url, user)
|
|
key, err := seal.GenerateKeyPair()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, _ := json.Marshal(map[string]string{"name": name, "public_key": key.PublicKey})
|
|
msg, err := conn.Request("mesh.seat.mesh-vault.call.get."+callerToken(user), raw, 3*time.Second)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var a busAnswer
|
|
if err := json.Unmarshal(msg.Data, &a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.Error != "" {
|
|
return "", fmt.Errorf("%s", a.Error)
|
|
}
|
|
var got Answer
|
|
if err := json.Unmarshal(a.Result, &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return seal.Open(got.Sealed, key.PrivateKey)
|
|
}
|
|
// No client can ask in another's name: the bus refuses laptop's client anchor's subject.
|
|
laptop := connectAs(t, url, "provisioner.laptop")
|
|
key, _ := seal.GenerateKeyPair()
|
|
raw, _ := json.Marshal(map[string]string{"name": anchorsToken, "public_key": key.PublicKey})
|
|
if _, err := laptop.Request("mesh.seat.mesh-vault.call.get."+callerToken("provisioner.anchor"), raw,
|
|
time.Second); err == nil {
|
|
t.Error("laptop's client asked in anchor's name and was answered")
|
|
}
|
|
heard = func() string {
|
|
_ = tap.Flush()
|
|
time.Sleep(100 * time.Millisecond)
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return strings.Join(crossed, "\n")
|
|
}
|
|
return ask, heard
|
|
}
|
|
|
|
// noneInTheClear fails when an issued value, or its base64, crossed the bus.
|
|
func noneInTheClear(t *testing.T, all string) {
|
|
t.Helper()
|
|
if !strings.Contains(all, "mesh.mod.gitea.call.gitea_agent_token.novox.novox_mesh-vault") ||
|
|
!strings.Contains(all, "mesh.seat.mesh-vault.call.get.provisioner_anchor") {
|
|
t.Fatalf("the listener heard nothing of the calls:\n%s", all)
|
|
}
|
|
for _, token := range agentTokens {
|
|
for _, form := range []string{token, base64.StdEncoding.EncodeToString([]byte(token)),
|
|
base64.RawURLEncoding.EncodeToString([]byte(token))} {
|
|
if strings.Contains(all, form) {
|
|
t.Fatalf("an issued value crossed the bus in the clear")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOnARealBusTwoHoldersOnTwoMachinesEachGetOnlyTheirOwnIssuedValueAndNoneCrossesInTheClear(t *testing.T) {
|
|
ask, heard := forgeOnABus(t, bothMayAsk)
|
|
if got, err := ask("provisioner.anchor", anchorsToken); err != nil || got != agentTokens["anchor/claude-code"] {
|
|
t.Errorf("anchor's client got %q, %v", got, err)
|
|
}
|
|
if got, err := ask("provisioner.laptop", laptopsToken); err != nil || got != agentTokens["laptop/claude-code"] {
|
|
t.Errorf("laptop's client got %q, %v", got, err)
|
|
}
|
|
if got, err := ask("provisioner.anchor", laptopsToken); err == nil || !strings.Contains(err.Error(), "alone") {
|
|
t.Errorf("anchor's client was handed laptop's value %q: %v", got, err)
|
|
}
|
|
if got, err := ask("provisioner.laptop", anchorsToken); err == nil || !strings.Contains(err.Error(), "alone") {
|
|
t.Errorf("laptop's client was handed anchor's value %q: %v", got, err)
|
|
}
|
|
noneInTheClear(t, heard())
|
|
}
|
|
|
|
// Issue 757's proof on a real bus, with the rule the running vault keeps: a machine's client is handed the value the
|
|
// forge issued for its claude-code because the module's plan there names gitea-read from the forge's machine, and
|
|
// is refused another machine's and another module's, sealed to its key and never in the clear.
|
|
func TestOnARealBusTheRunningRuleHandsAClientTheIssuedValueItsModulesPlanNamesAndNoOther(t *testing.T) {
|
|
plan := `{"templates":{"source":"x","digest":"y"},"files":[],"provisions":[{"name":"gitea-read",` +
|
|
`"provision":"gitea-read","provider":"novox","path":"/var/lib/claude-code/gitea-read.secret"}]}`
|
|
mirror := assignments{
|
|
"anchor.claude-code": `{"node":"anchor","module":"claude-code","provisioning":` + plan + `}`,
|
|
"laptop.claude-code": `{"node":"laptop","module":"claude-code","provisioning":` + plan + `}`,
|
|
"anchor.zsh": `{"node":"anchor","module":"zsh","provisioning":{"secrets":[{"name":"token"}]}}`,
|
|
}
|
|
ask, heard := forgeOnABus(t, Declared{Read: mirror.read})
|
|
if got, err := ask("provisioner.anchor", anchorsToken); err != nil || got != agentTokens["anchor/claude-code"] {
|
|
t.Errorf("anchor's client got %q, %v", got, err)
|
|
}
|
|
if got, err := ask("provisioner.laptop", laptopsToken); err != nil || got != agentTokens["laptop/claude-code"] {
|
|
t.Errorf("laptop's client got %q, %v", got, err)
|
|
}
|
|
for _, r := range []struct{ user, name, why string }{
|
|
{"provisioner.anchor", laptopsToken, "another machine's holder value"},
|
|
{"provisioner.laptop", anchorsToken, "another machine's holder value"},
|
|
{"provisioner.anchor", "provision/novox/gitea-read/anchor/zsh", "another module's, its plan naming it not"},
|
|
} {
|
|
if got, err := ask(r.user, r.name); err == nil {
|
|
t.Errorf("%s was handed %s, %s: %q", r.user, r.name, r.why, got)
|
|
}
|
|
}
|
|
noneInTheClear(t, heard())
|
|
}
|