The controller is not placed (ADR 0590), yet the vault still answered a caller named controller alone (accept), took its relay of who asked (secret-ask, rotate's by) and sealed it the core's credentials. Those branches and accept go with their tests; a node's account alone is never minted or rotated here. The sensitive-verb judge now needs the act's verb to be the vault's own, mesh-vault.<verb>, not any verb ending so (issue 1126 review 1). Claude-Session: https://claude.ai/code/session_01NcbgfJ9Gum47zkFKWRaovL
250 lines
9.7 KiB
Go
250 lines
9.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// fakeAccounts is the vault's state bus-accounts: every key written, and a refusal when told.
|
|
type fakeAccounts struct {
|
|
mu sync.Mutex
|
|
keys map[string]AccountHash
|
|
puts int
|
|
refuse bool
|
|
}
|
|
|
|
func (f *fakeAccounts) Get(user string) (*AccountHash, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
a, ok := f.keys[user]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
return &a, nil
|
|
}
|
|
|
|
func (f *fakeAccounts) Put(a AccountHash) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if f.refuse {
|
|
return errors.New("Permissions Violation for Publish to $KV.mesh-vault_bus-accounts")
|
|
}
|
|
if f.keys == nil {
|
|
f.keys = map[string]AccountHash{}
|
|
}
|
|
// What the runtime does to every state value (credential_field_test.go): the vault's own record is checked as written.
|
|
raw, _ := json.Marshal(a)
|
|
if field := credentialField(raw); field != "" {
|
|
return fmt.Errorf("%s carries a field %q, which names a credential: no secret is kept in state", a.User, field)
|
|
}
|
|
f.keys[a.User] = a
|
|
f.puts++
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeAccounts) Delete(user string) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
delete(f.keys, user)
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeAccounts) Users() ([]string, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
var out []string
|
|
for u := range f.keys {
|
|
out = append(out, u)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// hashOf says whether the key of user is the hash of password at generation, of the vault's own value.
|
|
func (f *fakeAccounts) hashOf(user, password string, generation int) bool {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
a, ok := f.keys[user]
|
|
return ok && !a.Adopted && a.Generation == generation && bcrypt.CompareHashAndPassword([]byte(a.Hash), []byte(password)) == nil
|
|
}
|
|
|
|
func eventsNamed(v testVault, from int, name string) int {
|
|
n := 0
|
|
for _, e := range v.bus.events[from:] {
|
|
if e["event"] == name {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
// A bus account the controller minted for a core or moved module is handed to the vault with origin issued (novox/hq
|
|
// ADR 0374, issue 787): kept as that module's own secret on that machine, served to the controller's core get and to
|
|
// the moved module's client, replaced in place on a re-issue with `secret-changed`, and never made anew by the vault.
|
|
|
|
// handIssued keeps a covered account as the controller's accept kept one with origin issued (accept went with the
|
|
// controller, ADR 0590): what the vault holds of it is served, hashed and never made anew.
|
|
func handIssued(t *testing.T, v testVault, caller, secret, value string) (Changed, error) {
|
|
t.Helper()
|
|
return v.keepWith(secret, value, "module issue", caller, OriginIssued, keepOptions{refuse: v.vaultMinted})
|
|
}
|
|
|
|
func TestAHandedBusAccountIsKeptServedReplacedAndNeverMadeByTheVault(t *testing.T) {
|
|
const moved = "own/g14/moved-bot/broker"
|
|
v := newVault(t, AllowList{"provisioner.g14": {moved}})
|
|
|
|
first, err := handIssued(t, v, ControllerCaller, moved, `{"password":"first-pw"}`)
|
|
if err != nil || first.Generation != 1 {
|
|
t.Fatalf("the handed account: %+v %v", first, err)
|
|
}
|
|
rec, held, _ := v.Store.Read(moved)
|
|
if !held || rec.Origin != OriginIssued || rec.ChangedVia != ControllerCaller {
|
|
t.Fatalf("kept as origin %q via %q (held %v), want issued via the controller", rec.Origin, rec.ChangedVia, held)
|
|
}
|
|
if got := handedTo(t, v, "provisioner.g14", moved); got != `{"password":"first-pw"}` {
|
|
t.Errorf("the moved module's client is handed %q", got)
|
|
}
|
|
|
|
// A re-issue replaces it in place, a new generation, and the vault says it changed.
|
|
before := len(v.bus.events)
|
|
second, err := handIssued(t, v, ControllerCaller, moved, `{"password":"second-pw"}`)
|
|
if err != nil || second.Generation != 2 || second.Unchanged {
|
|
t.Fatalf("the re-issue: %+v %v", second, err)
|
|
}
|
|
if got := handedTo(t, v, "provisioner.g14", moved); got != `{"password":"second-pw"}` {
|
|
t.Errorf("after the re-issue the client is handed %q: a stale value stayed", got)
|
|
}
|
|
changedEvents := 0
|
|
for _, e := range v.bus.events[before:] {
|
|
if e["event"] == ChangedEvent {
|
|
changedEvents++
|
|
}
|
|
}
|
|
if changedEvents != 1 {
|
|
t.Errorf("the re-issue raised %d secret-changed events, want 1", changedEvents)
|
|
}
|
|
|
|
// The vault makes no value for it: not a rotation, and not an accept without origin over it either way round.
|
|
if _, err := v.Rotate(ControllerCaller, moved, "jochen", "leaked"); err == nil || !strings.Contains(err.Error(), "ADR 0374") {
|
|
t.Errorf("the vault rotated a covered account: %v", err)
|
|
}
|
|
if _, err := v.Rotate(ControllerCaller, "own/g14/moved-bot/provisioner-broker", "jochen", "leaked"); err == nil {
|
|
t.Error("the vault made a first value for a covered account by rotating it")
|
|
}
|
|
if got := handedTo(t, v, "provisioner.g14", moved); got != `{"password":"second-pw"}` {
|
|
t.Errorf("a refused rotation changed the value to %q", got)
|
|
}
|
|
|
|
if strings.Contains(everything(t, v), "first-pw") || strings.Contains(everything(t, v), "second-pw") {
|
|
t.Fatal("a handed account is in something the vault said or wrote in the clear")
|
|
}
|
|
}
|
|
|
|
// The vault hashes what it is handed (ADR 0547 clause 9): once per generation into bus-accounts.<user>, saying
|
|
// account-hashed; a refused write is owed and written at the next pass; at start every held account without a key of its
|
|
// generation; an adopted key is replaced by the vault's own; never the value, and never the hash in what it says.
|
|
func TestAHandedBusAccountIsHashedOncePerGeneration(t *testing.T) {
|
|
const secret, user = "own/ace/mesh-build-go/broker", "ace.mesh-build-go"
|
|
account := func(pw string) string {
|
|
return `{"url":"nats://x","node":"ace","module":"mesh-build-go","user":"` + user + `","password":"` + pw + `"}`
|
|
}
|
|
v := newVault(t, nil)
|
|
acc := &fakeAccounts{}
|
|
v.Accounts = acc
|
|
for _, step := range []struct {
|
|
name string
|
|
do func() error
|
|
password string
|
|
generation int
|
|
puts int
|
|
said int
|
|
}{
|
|
{"handed: hashed once, said", func() error { _, err := handIssued(t, v, ControllerCaller, secret, account("pw-1")); return err }, "pw-1", 1, 1, 1},
|
|
{"the same value again: nothing", func() error { _, err := handIssued(t, v, ControllerCaller, secret, account("pw-1")); return err }, "pw-1", 1, 1, 0},
|
|
{"start with a key of this generation: nothing", func() error { _, f := v.HashHeld(); return mapErr(f) }, "pw-1", 1, 1, 0},
|
|
{"a re-issue: the new generation hashed", func() error { _, err := handIssued(t, v, ControllerCaller, secret, account("pw-2")); return err }, "pw-2", 2, 2, 1},
|
|
{"a refused write is owed, not lost", func() error {
|
|
acc.refuse = true
|
|
_, err := handIssued(t, v, ControllerCaller, secret, account("pw-3"))
|
|
if _, owed := v.hashes.list()[secret]; !owed {
|
|
return errors.New("the refused hash is not owed")
|
|
}
|
|
return err
|
|
}, "pw-2", 2, 2, 0},
|
|
{"the next pass writes what is owed", func() error { acc.refuse = false; v.HashOwed(); return nil }, "pw-3", 3, 3, 1},
|
|
{"an adopted key is replaced at start", func() error {
|
|
acc.keys[user] = AccountHash{User: user, Hash: "$2a$10$adopted", Adopted: true}
|
|
_, f := v.HashHeld()
|
|
return mapErr(f)
|
|
}, "pw-3", 3, 4, 1},
|
|
{"by hand, again", func() error { _, err := v.HashUser(user, "person.operator"); return err }, "pw-3", 3, 5, 1},
|
|
{"a value with no user is read by its secret's name", func() error {
|
|
_, err := handIssued(t, v, ControllerCaller, "own/g14/dunst/provisioner-broker", `{"password":"pw-p"}`)
|
|
if !acc.hashOf("provisioner.g14", "pw-p", 1) {
|
|
return errors.New("provisioner.g14 is not hashed")
|
|
}
|
|
return err
|
|
}, "pw-3", 3, 6, 1},
|
|
{"a process's own account (ADR 0436 §2) is covered and hashed", func() error {
|
|
_, err := handIssued(t, v, ControllerCaller, "own/ace/mesh-build-go/broker-worker",
|
|
`{"node":"ace","module":"mesh-build-go","user":"ace.mesh-build-go.worker","password":"pw-w"}`)
|
|
if !acc.hashOf("ace.mesh-build-go.worker", "pw-w", 1) {
|
|
return errors.New("ace.mesh-build-go.worker is not hashed")
|
|
}
|
|
if _, rerr := v.Rotate(ControllerCaller, "own/ace/mesh-build-go/broker-worker", "jochen", "x"); rerr == nil {
|
|
return errors.New("the vault rotated a process's covered account")
|
|
}
|
|
if Covered("own/ace/mesh-build-go/broker-") || Covered("own/ace/mesh-build-go/brokers") {
|
|
return errors.New("a name that is no process account is covered")
|
|
}
|
|
return err
|
|
}, "pw-3", 3, 7, 1},
|
|
} {
|
|
before := len(v.bus.events)
|
|
if err := step.do(); err != nil {
|
|
t.Fatalf("%s: %v", step.name, err)
|
|
}
|
|
if !acc.hashOf(user, step.password, step.generation) || acc.puts != step.puts || eventsNamed(v, before, AccountHashedEvent) != step.said {
|
|
t.Errorf("%s: key %+v, %d writes (want %d), %d said (want %d)", step.name, acc.keys[user], acc.puts, step.puts,
|
|
eventsNamed(v, before, AccountHashedEvent), step.said)
|
|
}
|
|
}
|
|
if _, err := v.HashUser("nobody.here", "person.operator"); err == nil {
|
|
t.Error("an account the vault holds no value for was hashed by hand")
|
|
}
|
|
said := everything(t, v)
|
|
for _, never := range []string{"pw-1", "pw-2", "pw-3", acc.keys[user].Hash} {
|
|
if strings.Contains(said, never) {
|
|
t.Fatalf("%q is in something the vault said or wrote", never)
|
|
}
|
|
}
|
|
}
|
|
|
|
func mapErr(failed map[string]string) error {
|
|
for k, v := range failed {
|
|
return errors.New(k + ": " + v)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// A core module's handed account is the one the controller's core get is answered, sealed to its machine's key.
|
|
func TestAHandedCoreAccountIsKeptAsTheModulesOwnSecret(t *testing.T) {
|
|
const natsBroker = "own/anchor/nats/broker"
|
|
v := newVault(t, nil)
|
|
if _, err := handIssued(t, v, ControllerCaller, natsBroker, `{"password":"core-pw"}`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rec, held, _ := v.Store.Read(natsBroker)
|
|
if !held || rec.Origin != OriginIssued {
|
|
t.Fatalf("the core's account is held %v as %q", held, rec.Origin)
|
|
}
|
|
if !Covered(natsBroker) || Covered("own/anchor/nats/token") || Covered("pair/a/b/c/broker") {
|
|
t.Error("the covered names are not the module's broker and provisioner-broker own secrets")
|
|
}
|
|
}
|