VA1: the vault takes a module's own secret through own-put and answers it through get
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
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -59,6 +60,61 @@ func TestLicencePutKeepsTheNewestGenerationAndRefusesAnOlder(t *testing.T) {
|
||||
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
|
||||
@@ -121,7 +177,7 @@ func TestATokenIsGivenOnlyToAMachineABindingNames(t *testing.T) {
|
||||
|
||||
// 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) {
|
||||
func secretHoldersByNameAndFingerprint(t *testing.T) {
|
||||
v := newVault(t, nil)
|
||||
const manager = "claude-licence-manager"
|
||||
grant, access := `{"refreshToken":"rt-SECRET-grant"}`, `{"accessToken":"at-SECRET-access"}`
|
||||
|
||||
@@ -646,4 +646,45 @@ func TestTheVaultWholeMintsOnTheBusWhoeverHoldsTheLease(t *testing.T) {
|
||||
t.Errorf("a waiter on %s heard %s", other, extra.Subject)
|
||||
}
|
||||
}
|
||||
// Design 47 H2 §822: a module puts its own secret in two steps through the seat and reads it back through get; a
|
||||
// second module's get of that name is refused; a second put keeps the newer generation alone.
|
||||
putOwn := func(caller, value string) Changed {
|
||||
t.Helper()
|
||||
raw, err := p.call(Seat+"."+OwnPutVerb, caller, map[string]any{"name": "admin-token"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var o Opened
|
||||
_ = json.Unmarshal(raw, &o)
|
||||
raw, err = p.call(Seat+"."+OwnPutVerb, caller, map[string]any{"call": o.Call, "sealed": sealToCall(t, o.SealTo, value)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var c Changed
|
||||
_ = json.Unmarshal(raw, &c)
|
||||
return c
|
||||
}
|
||||
getOwn := func(caller string) (string, error) {
|
||||
key, _ := seal.GenerateKeyPair()
|
||||
raw, err := p.call(Seat+".get", caller, map[string]any{"name": "module/gitea/admin-token", "public_key": key.PublicKey})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var a Answer
|
||||
_ = json.Unmarshal(raw, &a)
|
||||
return seal.Open(a.Sealed, key.PrivateKey)
|
||||
}
|
||||
first := putOwn("novox.gitea", "tok-1")
|
||||
if got, err := getOwn("ace.gitea"); err != nil || got != "tok-1" || first.Name != "module/gitea/admin-token" {
|
||||
t.Fatalf("own-put %+v, get answered %q (%v)", first, got, err)
|
||||
}
|
||||
if _, err := getOwn("novox.mesh-config"); err == nil {
|
||||
t.Error("another module was handed gitea's own secret")
|
||||
}
|
||||
if second := putOwn("ace.gitea", "tok-2"); second.Generation <= first.Generation {
|
||||
t.Errorf("the second put kept generation %d after %d", second.Generation, first.Generation)
|
||||
}
|
||||
if got, _ := getOwn("novox.gitea"); got != "tok-2" {
|
||||
t.Errorf("get answered %q after the second put, want tok-2", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
// A module's own secret, written by the module itself (design 47 H2 §822, H-A-345, H-A-349 DQ-7, H-A-360): `own-put` in
|
||||
// two steps modelled on `licence-put` — name the secret and get a call and a key made for it, then send the call and the
|
||||
// value sealed to that key. The vault keeps it as `module/<module>/<name>`, the module named by the caller's account as
|
||||
// the bus proved it (`<machine>.<module>`, a bundle's call named so by the runtime), never by an argument, and keeps the
|
||||
// newest generation only. `get` answers that full name to a caller of the same module alone, sealed to its key. Each
|
||||
// write is said in the vault's journal, never the value.
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
// OwnPutVerb is the verb a module writes its own secret with.
|
||||
const OwnPutVerb = "own-put"
|
||||
|
||||
// modulePrefix starts the name of every secret a module wrote itself through own-put.
|
||||
const modulePrefix = "module/"
|
||||
|
||||
// callerModule is the module a proved caller is an account of, `<machine>.<module>`, or why it is none: a person, a
|
||||
// machine's provisioner client or node-engine, and the tool runner's own account answer no module's secret (H-A-360).
|
||||
func callerModule(caller string) (string, error) {
|
||||
machine, module, ok := strings.Cut(caller, ".")
|
||||
switch {
|
||||
case caller == "" || caller == stdio.Unproven:
|
||||
return "", errors.New("a module's own secret answers a caller the runtime proves, and it proved none")
|
||||
case !ok || machine == "" || module == "" || strings.Contains(module, "."),
|
||||
machine == "person", machine == "node", machine+"." == ProvisionerPrefix,
|
||||
module == toolRunnerModule:
|
||||
return "", fmt.Errorf("%s is no module's account (<machine>.<module>), so it has no own secret here", caller)
|
||||
}
|
||||
return stdio.ModuleOf(caller), nil
|
||||
}
|
||||
|
||||
// ModuleSecret is the vault's name of module's own secret name: `module/<module>/<name>`.
|
||||
func ModuleSecret(module, name string) (string, error) {
|
||||
full := modulePrefix + module + "/" + name
|
||||
if strings.TrimSpace(name) == "" {
|
||||
return "", errors.New("own-put names the secret; nothing is taken")
|
||||
}
|
||||
if err := validName(full); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return full, nil
|
||||
}
|
||||
|
||||
// moduleSecretRefusal says whether name is a module's own secret written through own-put and, when it is, why caller
|
||||
// may not have it ("" when caller is an account of that module).
|
||||
func moduleSecretRefusal(caller, name string) (bool, string) {
|
||||
rest, ok := strings.CutPrefix(name, modulePrefix)
|
||||
if !ok {
|
||||
return false, ""
|
||||
}
|
||||
owner, _, _ := strings.Cut(rest, "/")
|
||||
module, err := callerModule(caller)
|
||||
if err != nil {
|
||||
return true, err.Error()
|
||||
}
|
||||
if module != owner {
|
||||
return true, fmt.Sprintf("%s is %s's own secret, handed only to an account of %s", name, owner, owner)
|
||||
}
|
||||
return true, ""
|
||||
}
|
||||
|
||||
// OwnPutOpen opens a call to take a secret of the caller's module.
|
||||
func (v *Vault) OwnPutOpen(caller, name string) (Opened, error) {
|
||||
module, err := callerModule(caller)
|
||||
if err != nil {
|
||||
return Opened{}, err
|
||||
}
|
||||
secret, err := ModuleSecret(module, name)
|
||||
if err != nil {
|
||||
return Opened{}, err
|
||||
}
|
||||
id, key, err := v.openCall(OwnPutVerb, secret, caller, nil)
|
||||
if err != nil {
|
||||
return Opened{}, err
|
||||
}
|
||||
return Opened{Call: id, SealTo: key, Secret: secret}, nil
|
||||
}
|
||||
|
||||
// OwnPutTake opens the value sealed to the call's key and keeps it as the module's own, its newest generation only.
|
||||
func (v *Vault) OwnPutTake(caller, call, sealed string) (Changed, error) {
|
||||
c, err := v.takeCall(OwnPutVerb, call)
|
||||
if err != nil {
|
||||
return Changed{}, err
|
||||
}
|
||||
if c.by != caller {
|
||||
return Changed{}, fmt.Errorf("the own-put call %q was opened by another caller; nothing was taken", call)
|
||||
}
|
||||
value, err := c.open(sealed)
|
||||
if err != nil {
|
||||
return Changed{}, err
|
||||
}
|
||||
defer zero(value)
|
||||
if len(value) == 0 {
|
||||
return Changed{}, errors.New("the value handed in is empty; nothing was taken")
|
||||
}
|
||||
changed, err := v.keepWith(c.secret, string(value), caller, OwnPutVerb, OriginAccepted, keepOptions{})
|
||||
if err != nil {
|
||||
v.log("own-put of %s by %s refused: %v", c.secret, caller, err)
|
||||
return Changed{}, err
|
||||
}
|
||||
v.log("own-put: %s kept at generation %d, written by %s", c.secret, changed.Generation, caller)
|
||||
return changed, nil
|
||||
}
|
||||
@@ -231,6 +231,26 @@ func definitions(env *Env) []toolDef {
|
||||
g, _ := a["generation"].(float64)
|
||||
return v.LicencePutOpen(caller, arg(a, "licence"), arg(a, "part"), int(g))
|
||||
}},
|
||||
{Name: Seat + "." + OwnPutVerb,
|
||||
Description: "Store a secret as the calling module's own (design 47 H2 §822), in two steps: name the secret and " +
|
||||
"get a call and a key made for it; then send the call and the value sealed to that key. Kept as " +
|
||||
"module/<module>/<name>, the module named by the caller's account as the bus proved it, never by an " +
|
||||
"argument, its newest generation only; get answers that name to an account of the same module alone. " +
|
||||
"Answers the call and the key, then the secret's name and generation; never the value.",
|
||||
Input: map[string]any{"type": "object", "properties": map[string]any{
|
||||
"name": str("the first step: the secret's name, among the calling module's own"),
|
||||
"call": str("the second step: the call the first step answered"),
|
||||
"sealed": str("the second step: the value sealed to the call's key (NaCl anonymous box, base64) — never the value")}},
|
||||
run: func(caller string, a map[string]any) (any, error) {
|
||||
v, err := env.ready()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if call := arg(a, "call"); call != "" {
|
||||
return v.OwnPutTake(caller, call, arg(a, "sealed"))
|
||||
}
|
||||
return v.OwnPutOpen(caller, arg(a, "name"))
|
||||
}},
|
||||
{Name: Seat + ".move-in",
|
||||
Description: "Take one value of the controller's store into the vault, as it is (design 57's V3): from the " +
|
||||
"node-engine of a machine it was sealed to, and no other caller, in two steps — name the row (node, " +
|
||||
|
||||
@@ -383,8 +383,16 @@ func (v *Vault) Get(caller, name, publicKey string) (Answer, error) {
|
||||
return Answer{}, fmt.Errorf("%s may not ask for %s: %s", caller, name, why)
|
||||
}
|
||||
isLicence, licenceWhy := v.licenceRefusal(caller, name)
|
||||
isModule, moduleWhy := moduleSecretRefusal(caller, name)
|
||||
if isLogin {
|
||||
// Judged above: the node's own engine, sealed to its own key.
|
||||
} else if isModule {
|
||||
// A module's own secret written through own-put, to an account of that module alone (design 47 H2 §822).
|
||||
if moduleWhy != "" {
|
||||
v.log("refused %s to %s: %s", name, caller, moduleWhy)
|
||||
_ = v.recordHanding(Handing{To: caller, Name: name, Outcome: "refused", Why: moduleWhy})
|
||||
return Answer{}, fmt.Errorf("%s may not ask for %s: %s", caller, name, moduleWhy)
|
||||
}
|
||||
} else if isLicence {
|
||||
// A licence's secret, by the licence rule (design 39 bus-and-switch §16, §17).
|
||||
if licenceWhy != "" {
|
||||
|
||||
@@ -233,7 +233,8 @@
|
||||
"node-account-confirm",
|
||||
"abandon",
|
||||
"retire",
|
||||
"licence-put"
|
||||
"licence-put",
|
||||
"own-put"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user