Files
mesh-catalog/modules/mesh-vault/cmd/mesh-vault/licences_test.go
T
jschoubben 45b0779b68
mesh/delivery merged: source.main moved to novox/mesh-catalog@792a79720b30
mesh/check checking 45b0779b6801 on 792a79720b30
mesh-vault: a store file that is not a record is listed by name with its error (issue 1409, V10)
Store.ListReadable lists every record and, beside them, each file it cannot read
by name with its error; RecordsHeld lists those as rows with an error and goes
on. recordsError stays for a store that cannot be listed at all. Store.List and
ListPrefix keep their behaviour for their other callers.

Builds: design 39 bus-and-switch §20; design 39 parts §12. Lead answers (issue 1409): V9 (keep recordsCount and recordsError), V10.
2026-11-12 17:41:39 +01:00

182 lines
7.7 KiB
Go

package main
import (
"encoding/json"
"os"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/seal"
)
// putLicence hands one part of a licence to the vault in licence-put's two steps.
func putLicence(t *testing.T, v testVault, caller, key, part string, generation int, value string) (Changed, error) {
t.Helper()
o, err := v.LicencePutOpen(caller, key, part, generation)
if err != nil {
return Changed{}, err
}
return v.LicencePutTake(caller, o.Call, sealToCall(t, o.SealTo, value))
}
// design 39 bus-and-switch §15, B3, B5: the caller's generation is the record's; an older one is refused; the
// event names the secret and its generation, never the value.
func TestLicencePutKeepsTheNewestGenerationAndRefusesAnOlder(t *testing.T) {
v := newVault(t, nil)
const manager = "claude-licence-manager"
if _, err := putLicence(t, v, manager, "jane_at_example.org", LicenceGrant, 3, `{"refreshToken":"rt-3"}`); err != nil {
t.Fatal(err)
}
rec, held, err := v.Store.Read("licence.jane_at_example.org.grant")
if err != nil || !held || rec.Generation != 3 {
t.Fatalf("held %v at generation %d (%v), want generation 3", held, rec.Generation, err)
}
if _, err := putLicence(t, v, manager, "jane_at_example.org", LicenceGrant, 2, `{"refreshToken":"rt-2"}`); err == nil ||
!strings.Contains(err.Error(), "not newer") {
t.Fatalf("an older generation was taken: %v", err)
}
if c, err := putLicence(t, v, manager, "jane_at_example.org", LicenceGrant, 4, `{"refreshToken":"rt-4"}`); err != nil ||
c.Generation != 4 {
t.Fatalf("generation 4: %+v %v", c, err)
}
said := 0
for _, raw := range v.bus.raw {
if strings.Contains(raw, "rt-") {
t.Fatalf("an event carries the value: %s", raw)
}
if strings.Contains(raw, `"secret-changed"`) && strings.Contains(raw, "licence.jane_at_example.org.grant") {
said++
}
}
if said != 2 {
t.Fatalf("secret-changed said %d times, want 2: %v", said, v.bus.raw)
}
for _, bad := range []struct{ key, part string }{{"Jane@x", LicenceGrant}, {"jane", "refresh"}} {
if _, err := v.LicencePutOpen(manager, bad.key, bad.part, 1); err == nil {
t.Errorf("licence-put took %s/%s", bad.key, bad.part)
}
}
if _, err := v.LicencePutOpen("", "jane", LicenceAccess, 1); err == nil {
t.Error("licence-put took an unproven caller")
}
}
// design 39 bus-and-switch §17/AC1, §16, B3, B8, B25: a licence's access token is refused to a machine no binding names
// and for a licence its binding does not name, and given after a binding.changed naming it, to any consumer of that
// machine; the grant goes to the manager's tool runner, with no machine guard.
func TestATokenIsGivenOnlyToAMachineABindingNames(t *testing.T) {
v := newVault(t, nil)
lb, _ := memLicenceBindings()
v.LicenceBindings = &lb
const manager = "novox.node-tools"
for _, key := range []string{"jane_at_example.org", "api"} {
for _, part := range []string{LicenceGrant, LicenceAccess} {
if _, err := putLicence(t, v, manager, key, part, 1, key+"-"+part); err != nil {
t.Fatal(err)
}
}
}
access := "licence.jane_at_example.org.access"
// B31: until the first view of the bindings is answered, an access token is refused, saying why.
answered := false
lb.Answered = func() bool { return answered }
if err := lb.Heard([]byte(`{"consumer":"g14","licence":"jane","key":"jane_at_example.org","generation":1}`)); err != nil {
t.Fatal(err)
}
if _, _, err := asks(v, "g14.node-tools", access); err == nil || !strings.Contains(err.Error(), "first view of the licence bindings has not been answered") {
t.Fatalf("a token was handed before the first view was answered: %v", err)
}
answered = true
if _, _, err := asks(v, "g14.node-tools", access); err != nil {
t.Fatalf("once the first view is answered the bound machine is refused: %v", err)
}
if err := lb.Heard([]byte(`{"consumer":"g14","licence":"","key":"","generation":2}`)); err != nil {
t.Fatal(err)
}
if _, _, err := asks(v, "g14.node-tools", access); err == nil || !strings.Contains(err.Error(), "no consumer on g14") {
t.Fatalf("a machine no binding names was handed the token: %v", err)
}
if err := lb.Heard([]byte(`{"consumer":"g14","licence":"api","key":"api","generation":4}`)); err != nil {
t.Fatal(err)
}
if _, _, err := asks(v, "g14.node-tools", access); err == nil {
t.Fatal("a licence its binding does not name was handed to g14")
}
if got, _, err := asks(v, "g14.node-tools", "licence.api.access"); err != nil || got != "api-access" {
t.Fatalf("g14's own licence: %q %v", got, err)
}
if err := lb.Heard([]byte(`{"consumer":"g14.worker-1","licence":"jane@example.org","key":"jane_at_example.org","generation":2}`)); err != nil {
t.Fatal(err)
}
if got, _, err := asks(v, "g14.node-tools", access); err != nil || got != "jane_at_example.org-access" {
t.Fatalf("after a binding.changed naming it: %q %v", got, err)
}
if _, _, err := asks(v, "ace.node-tools", access); err == nil {
t.Fatal("another machine was handed g14's token")
}
if got, _, err := asks(v, manager, "licence.jane_at_example.org.grant"); err != nil || got != "jane_at_example.org-grant" {
t.Fatalf("the manager's grant: %q %v", got, err)
}
}
// issue 1409, design 39 bus-and-switch §20, parts §12: secret_holders lists every record of the store by name and
// fingerprint, a licence's two secrets with their key and part, a record it cannot open with an error, and no value.
func TestSecretHoldersListsEveryStoreRecordByNameAndFingerprintNeverTheValue(t *testing.T) {
v := newVault(t, nil)
const manager = "claude-licence-manager"
grant, access := `{"refreshToken":"rt-SECRET-grant"}`, `{"accessToken":"at-SECRET-access"}`
for part, value := range map[string]string{LicenceGrant: grant, LicenceAccess: access} {
if _, err := putLicence(t, v, manager, "jane_at_example.org", part, 3, value); err != nil {
t.Fatal(err)
}
}
if _, err := v.Put("pair/postgres/gitea", "other-SECRET-value", "the test"); err != nil {
t.Fatal(err)
}
broken, _, _ := v.Store.Read("pair/postgres/gitea")
broken.Name, broken.AtRest = "pair/broken/one", seal.Box{}
if err := v.Store.Write(broken); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(v.Store.pathOf("pair/notjson/one"), []byte("{not json"), 0o600); err != nil {
t.Fatal(err)
}
got, err := toolNamed(t, definitions(&Env{Vault: v.Vault}), "secret_holders").Run(map[string]any{})
if err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(got)
if strings.Contains(string(raw), "SECRET") {
t.Fatalf("the answer holds a value: %s", raw)
}
m := got.(map[string]any)
rows := map[string]RecordHeld{}
for _, r := range m["records"].([]RecordHeld) {
rows[r.Name] = r
}
if m["recordsCount"] != 5 || len(rows) != 5 {
t.Fatalf("records %v, want 5 (two licence secrets, a pair, a sealed-broken one, a file that is not JSON)", rows)
}
for part, value := range map[string]string{LicenceGrant: grant, LicenceAccess: access} {
r := rows["licence.jane_at_example.org."+part]
if r.Key != "jane_at_example.org" || r.Part != part || r.Generation != 3 || r.ChangedBy != manager ||
r.Origin != OriginAccepted || r.Length != len(value) || r.Fingerprint != Fingerprint(value) || r.Error != "" {
t.Errorf("the %s secret's row is %+v", part, r)
}
}
if r := rows["pair/postgres/gitea"]; r.Key != "" || r.Fingerprint != Fingerprint("other-SECRET-value") {
t.Errorf("the pair's row is %+v", r)
}
if r := rows["pair/broken/one"]; r.Error == "" || r.Fingerprint != "" {
t.Errorf("a record the vault cannot open must stay listed with an error: %+v", r)
}
if r := rows["pair/notjson/one"]; r.Error == "" || r.Fingerprint != "" {
t.Errorf("a file that is not JSON must be listed by name with its error, the listing going on (V10): %+v", r)
}
if _, bad := m["recordsError"]; bad {
t.Errorf("recordsError is for a store that cannot be listed at all (V10): %v", m["recordsError"])
}
}