The forge module needs a place for its token that only its own processes read: own-put keeps it as module/<module>/<name>, named by the caller's account, its newest generation only. Builds: design 47 H2 §822, §808; H-A-345, H-A-349 DQ-7, H-A-360
238 lines
10 KiB
Go
238 lines
10 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"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")
|
|
}
|
|
t.Run("own-put keeps a module's own secret, handed to that module alone", ownPutKeepsAModulesOwn)
|
|
t.Run("secret_holders lists every record by name and fingerprint, never the value", secretHoldersByNameAndFingerprint)
|
|
}
|
|
|
|
// ownPutKeepsAModulesOwn: design 47 H2 §822, H-A-360. A module puts a secret in two steps and reads it back through get
|
|
// by the full name own-put answered; another module's account and a non-module account are refused it; a second put
|
|
// keeps only the newer generation; the journal says each write, never the value.
|
|
func ownPutKeepsAModulesOwn(t *testing.T) {
|
|
v := newVault(t, nil)
|
|
var journal strings.Builder
|
|
v.Log = func(format string, args ...any) { fmt.Fprintf(&journal, format+"\n", args...) }
|
|
put := func(caller, name, value string) (Changed, error) {
|
|
t.Helper()
|
|
o, err := v.OwnPutOpen(caller, name)
|
|
if err != nil {
|
|
return Changed{}, err
|
|
}
|
|
if o.Secret != "module/gitea/admin-token" {
|
|
t.Fatalf("own-put names %q, want module/gitea/admin-token", o.Secret)
|
|
}
|
|
return v.OwnPutTake(caller, o.Call, sealToCall(t, o.SealTo, value))
|
|
}
|
|
first, err := put("novox.gitea", "admin-token", "tok-1")
|
|
if err != nil || first.Name != "module/gitea/admin-token" {
|
|
t.Fatalf("%+v %v", first, err)
|
|
}
|
|
if got := handedTo(t, v, "ace.gitea", "module/gitea/admin-token"); got != "tok-1" {
|
|
t.Fatalf("handed %q to another process of the module, want tok-1", got)
|
|
}
|
|
second, err := put("ace.gitea", "admin-token", "tok-2")
|
|
if err != nil || second.Generation <= first.Generation {
|
|
t.Fatalf("second put %+v (%v) after generation %d", second, err, first.Generation)
|
|
}
|
|
rec, _, _ := v.Store.Read("module/gitea/admin-token")
|
|
if rec.Previous != nil || handedTo(t, v, "novox.gitea", "module/gitea/admin-token") != "tok-2" {
|
|
t.Fatalf("the newer generation alone is kept: %+v", rec)
|
|
}
|
|
key, _ := seal.GenerateKeyPair()
|
|
for _, other := range []string{"novox.mesh-config", "person.jo", "novox.node-tools", "provisioner.novox"} {
|
|
if _, err := v.Get(other, "module/gitea/admin-token", key.PublicKey); err == nil {
|
|
t.Errorf("%s was handed gitea's own secret", other)
|
|
}
|
|
}
|
|
for _, c := range []struct{ caller, name string }{{"person.jo", "x"}, {"novox.node-tools", "x"}, {"node.ace", "x"}, {"novox.gitea", ""}, {"novox.gitea", "A B"}} {
|
|
if _, err := v.OwnPutOpen(c.caller, c.name); err == nil {
|
|
t.Errorf("own-put opened for %s naming %q", c.caller, c.name)
|
|
}
|
|
}
|
|
o, _ := v.OwnPutOpen("novox.gitea", "admin-token")
|
|
if _, err := v.OwnPutTake("ace.gitea", o.Call, sealToCall(t, o.SealTo, "tok-3")); err == nil {
|
|
t.Error("a call opened by one caller was taken by another")
|
|
}
|
|
if all := journal.String() + everything(t, v); strings.Contains(all, "tok-") || !strings.Contains(all, "own-put: module/gitea/admin-token kept at generation") {
|
|
t.Errorf("the journal says each write without the value: %s", all)
|
|
}
|
|
}
|
|
|
|
// 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 secretHoldersByNameAndFingerprint(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"])
|
|
}
|
|
}
|