Archived
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc5a8208a2 | ||
|
|
a5a355aeec | ||
|
|
4d30b5de13 | ||
|
|
c97942d591 |
@@ -151,6 +151,13 @@ func busCommand(ctx context.Context, args []string) error {
|
|||||||
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
||||||
sub, args = args[0], args[1:]
|
sub, args = args[0], args[1:]
|
||||||
}
|
}
|
||||||
|
// The view's credential, a terminal line like a person's (bus_view.go).
|
||||||
|
switch sub {
|
||||||
|
case "view-credential":
|
||||||
|
return busViewCredential(ctx, args)
|
||||||
|
case "view-revoke":
|
||||||
|
return busViewRevoke(ctx, args)
|
||||||
|
}
|
||||||
set := flag.NewFlagSet("bus", flag.ContinueOnError)
|
set := flag.NewFlagSet("bus", flag.ContinueOnError)
|
||||||
snapshot := set.String("snapshot-taken", "", "where the streams' snapshot a person took is, while the mesh takes none itself")
|
snapshot := set.String("snapshot-taken", "", "where the streams' snapshot a person took is, while the mesh takes none itself")
|
||||||
reversible := set.Bool("reversible", false, "the new version can be undone by putting the old one back")
|
reversible := set.Bool("reversible", false, "the new version can be undone by putting the old one back")
|
||||||
@@ -160,14 +167,14 @@ func busCommand(ctx context.Context, args []string) error {
|
|||||||
if rest, err := parseAround(set, args); err != nil {
|
if rest, err := parseAround(set, args); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(rest) > 0 {
|
} else if len(rest) > 0 {
|
||||||
return errors.New("bus [upgrade --why … --reversible|--irreversible [--snapshot-taken <where>]]")
|
return errors.New(busUsage)
|
||||||
}
|
}
|
||||||
switch sub {
|
switch sub {
|
||||||
case "":
|
case "":
|
||||||
return busStatus(ctx)
|
return busStatus(ctx)
|
||||||
case "upgrade":
|
case "upgrade":
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("bus says what a bus upgrade would do, or `bus upgrade` — not %q", sub)
|
return fmt.Errorf("bus says what a bus upgrade would do, or `bus upgrade`, `bus view-credential`, `bus view-revoke` — not %q", sub)
|
||||||
}
|
}
|
||||||
// Everything refused before anything is done.
|
// Everything refused before anything is done.
|
||||||
if err := why.require("bus upgrade"); err != nil {
|
if err := why.require("bus upgrade"); err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/novox/mesh-controller/internal/broker"
|
||||||
|
"github.com/novox/mesh-controller/internal/inventory"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The view's credential: the one read-only user a page in a browser connects to the bus as, over the
|
||||||
|
// bus module's WebSocket listener (novox/hq research 036, gap G1; broker.KindView).
|
||||||
|
//
|
||||||
|
// **A terminal line, like a person's credential** (operator.go): printed once, never stored — the mesh
|
||||||
|
// keeps a hash — and revoked by forgetting the row, which the next composition of the user list makes
|
||||||
|
// real. There is one view; issuing it again rotates its password.
|
||||||
|
const busUsage = "bus [upgrade --why … --reversible|--irreversible [--snapshot-taken <where>] | view-credential | view-revoke]"
|
||||||
|
|
||||||
|
// busWebSocketPort is the port the bus module's WebSocket listener is published on, mirrored from the
|
||||||
|
// nats module's manifest (its `bus-websocket` opening), because the credential names where to connect
|
||||||
|
// and the controller does not read the module's configuration. Reached across the overlay only: the
|
||||||
|
// opening is from the mesh, and the mesh's filter admits nothing else.
|
||||||
|
const busWebSocketPort = 4223
|
||||||
|
|
||||||
|
func busViewCredential(ctx context.Context, args []string) error {
|
||||||
|
if len(args) != 0 {
|
||||||
|
return errors.New("bus view-credential takes nothing: there is one view, and this prints its credential once")
|
||||||
|
}
|
||||||
|
open, err := openStores(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer open.Close()
|
||||||
|
inv := open.inventory
|
||||||
|
|
||||||
|
// Refused here rather than at the next composition, where it would stop the whole file.
|
||||||
|
if _, err := broker.PermissionsFor(broker.Principal{Kind: broker.KindView, PasswordHash: "x"}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
password, err := inv.MintBusPassword(ctx, inventory.BusUser{Username: broker.ViewUser, Kind: inventory.BusView})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
where, err := broker.FromEnvironment()
|
||||||
|
if err != nil && !errors.Is(err, broker.ErrNotConfigured) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
host := where.Address
|
||||||
|
if h, _, err := net.SplitHostPort(where.Address); err == nil {
|
||||||
|
host = h
|
||||||
|
}
|
||||||
|
websocket := ""
|
||||||
|
if host != "" {
|
||||||
|
websocket = "ws://" + net.JoinHostPort(host, strconv.Itoa(busWebSocketPort))
|
||||||
|
}
|
||||||
|
held, err := json.Marshal(struct {
|
||||||
|
WebSocket string `json:"websocket,omitempty"`
|
||||||
|
URL string `json:"url,omitempty"`
|
||||||
|
Fingerprint string `json:"fingerprint,omitempty"`
|
||||||
|
User string `json:"user"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
InboxPrefix string `json:"inbox_prefix"`
|
||||||
|
Hears []string `json:"hears"`
|
||||||
|
Reads string `json:"reads"`
|
||||||
|
HowToRead string `json:"how_to_read"`
|
||||||
|
}{
|
||||||
|
WebSocket: websocket, URL: "nats://" + where.Address, Fingerprint: where.Fingerprint,
|
||||||
|
User: broker.ViewUser, Password: password,
|
||||||
|
// The client must make its inboxes under the view's own prefix: its subscribe grant is
|
||||||
|
// `_INBOX.view.>` and no wider (design 25 §4), and a client's default inbox is not under it.
|
||||||
|
InboxPrefix: "_INBOX." + broker.ViewUser,
|
||||||
|
Hears: broker.ViewHears, Reads: broker.ViewBucket,
|
||||||
|
HowToRead: "direct reads only, no watch (a consumer is refused): list with a request to $JS.API.DIRECT.GET.KV_" +
|
||||||
|
broker.ViewBucket + ` carrying {"multi_last":["$KV.` + broker.ViewBucket + `.>"]}, answered until a 204 status; ` +
|
||||||
|
"read one key with $JS.API.DIRECT.GET.KV_" + broker.ViewBucket + ".$KV." + broker.ViewBucket + ".<number> " +
|
||||||
|
"(nats.js: kvm.open(bucket, {allow_direct: true}), never create); re-read the key an event's number names",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("issued the view, which hears %s and reads the bucket %s, and nothing else\n",
|
||||||
|
strings.Join(broker.ViewHears, ", "), broker.ViewBucket)
|
||||||
|
fmt.Println(" this is the only time the credential is printed; the mesh keeps a hash")
|
||||||
|
fmt.Println(" it works once the bus has been told, which is the next push to the machine holding mesh-broker —")
|
||||||
|
fmt.Println(" and while a new build of the bus module waits for that machine, the next `bus upgrade` a person starts,")
|
||||||
|
fmt.Println(" which is also what brings the WebSocket listener it connects through")
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println(string(held))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func busViewRevoke(ctx context.Context, args []string) error {
|
||||||
|
if len(args) != 0 {
|
||||||
|
return errors.New("bus view-revoke takes nothing: there is one view")
|
||||||
|
}
|
||||||
|
open, err := openStores(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer open.Close()
|
||||||
|
|
||||||
|
if err := open.inventory.ForgetBusUser(ctx, broker.ViewUser); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// **Revoked at the next composition, not now** — as a person is (operator revoke): the bus's users
|
||||||
|
// are a file, and the credential stops working when the file no longer names it.
|
||||||
|
fmt.Println("the view is forgotten, and its credential stops working at the next composition — " +
|
||||||
|
"push the machine holding mesh-broker to make it so")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -15,16 +15,23 @@ import (
|
|||||||
"github.com/novox/mesh-controller/internal/secrets"
|
"github.com/novox/mesh-controller/internal/secrets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A module's own secret given at the operator's desk (novox/hq ADR 0259 §10).
|
// A module's own secret given at the operator's desk (novox/hq ADR 0259 §10, ADR 0277).
|
||||||
|
//
|
||||||
|
// mesh-controller secret ask <node> <module> <name> [--at <desk>]
|
||||||
//
|
//
|
||||||
// **The value never passes through whoever asked for it.** An agent, or the operator at the mesh MCP
|
// **The value never passes through whoever asked for it.** An agent, or the operator at the mesh MCP
|
||||||
// server, calls `give` with the machine, the module, the secret's name and the desk — never a value. The
|
// server, calls `secret-ask` (or `give`) with the machine, the module, the secret's name and the desk — never
|
||||||
// controller makes a sealing keypair for this one call, asks the desk's `node-launcher.secret` to prompt the
|
// a value. The controller makes a sealing keypair for this one call, asks the desk's `node-launcher.secret` to
|
||||||
// operator without showing what is typed, and is answered with what was typed **sealed to that key**: no
|
// prompt the operator without showing what is typed, and is answered with what was typed **sealed to that
|
||||||
// plaintext on the bus, in a runtime's log or in any call's record. It opens it here, seals it to the
|
// key**: no plaintext on the bus, in a runtime's log or in any call's record. It opens it here, seals it to the
|
||||||
// module's machine exactly as `secret accept` does, and forgets it. What it answers says only that the
|
// module's machine exactly as `secret accept` does, and forgets it. What it answers says only that the
|
||||||
// value was taken, or why not.
|
// value was taken, or why not.
|
||||||
//
|
//
|
||||||
|
// **Bounded, and the prompt says who asked** (ADR 0277): one open prompt per secret and few an hour, read from
|
||||||
|
// the store before the prompt opens (inventory.OpenSecretAsk), so an agent cannot keep a prompt in front of the
|
||||||
|
// operator until they type. The prompt names the module, the secret, the machine and who asked — the caller as
|
||||||
|
// the bus named it, never a word the caller chose — written by the desk's launcher from those names alone.
|
||||||
|
//
|
||||||
// **What remains** (ADR 0234's accepted residual risk): on an X11 desk any program of the operator's
|
// **What remains** (ADR 0234's accepted residual risk): on an X11 desk any program of the operator's
|
||||||
// account can read the keys as they are typed. And a program that calls the desk's prompt itself, with a
|
// account can read the keys as they are typed. And a program that calls the desk's prompt itself, with a
|
||||||
// key of its own, is answered with what the operator typed into a prompt they did not ask for — as it could
|
// key of its own, is answered with what the operator typed into a prompt they did not ask for — as it could
|
||||||
@@ -54,6 +61,37 @@ type deskGive struct {
|
|||||||
// announce raises the condition that says a module's own secret was given (secretGivenObservation), on
|
// announce raises the condition that says a module's own secret was given (secretGivenObservation), on
|
||||||
// every channel; nil announces nothing (a test that does not look).
|
// every channel; nil announces nothing (a test that does not look).
|
||||||
announce func(node, module, name, how string) error
|
announce func(node, module, name, how string) error
|
||||||
|
// askedBy is who asked, as the bus named the caller: said in the prompt and recorded.
|
||||||
|
askedBy string
|
||||||
|
// open records the ask and holds the bounds (one open per secret, few an hour), answering the record's id;
|
||||||
|
// nil keeps no record (a test that does not look). end closes it with how it ended.
|
||||||
|
open func(node, module, name, desk string) (int64, error)
|
||||||
|
end func(id int64, outcome string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// askedByName is the caller as the prompt names it: the first clause of what the bus said, in the characters
|
||||||
|
// a name has, at most 80 of them. The desk's launcher refuses anything else, so no words of the caller's own
|
||||||
|
// reach the prompt.
|
||||||
|
func askedByName(caller string) string {
|
||||||
|
first, _, _ := strings.Cut(caller, ",")
|
||||||
|
var b strings.Builder
|
||||||
|
for _, r := range strings.TrimSpace(first) {
|
||||||
|
switch {
|
||||||
|
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '.', r == '_', r == '/', r == '@',
|
||||||
|
r == '-', r == ' ':
|
||||||
|
b.WriteRune(r)
|
||||||
|
default:
|
||||||
|
b.WriteRune('-')
|
||||||
|
}
|
||||||
|
if b.Len() >= 80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
name := strings.TrimSpace(b.String())
|
||||||
|
if name == "" || strings.HasPrefix(name, "-") {
|
||||||
|
return "an unnamed caller"
|
||||||
|
}
|
||||||
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
// errNothingGiven is a prompt dismissed, or not answered in time: nothing changes.
|
// errNothingGiven is a prompt dismissed, or not answered in time: nothing changes.
|
||||||
@@ -95,20 +133,37 @@ func (d deskGive) give(node, module, name, desk string) (string, error) {
|
|||||||
"bus, where an agent may answer first (novox/hq ADR 0259 §10)", module, name, node, module, name)
|
"bus, where an agent may answer first (novox/hq ADR 0259 §10)", module, name, node, module, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// The bounds, read and kept before anybody is asked to type (novox/hq ADR 0277): one open prompt per secret,
|
||||||
|
// few an hour. How the ask ends is recorded whatever happens below.
|
||||||
|
outcome := "failed"
|
||||||
|
if d.open != nil {
|
||||||
|
id, err := d.open(node, module, name, desk)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("nobody was asked to type anything: %w", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if d.end != nil {
|
||||||
|
_ = d.end(id, outcome)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
public, private, err := secrets.Keypair()
|
public, private, err := secrets.Keypair()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("no key could be made to take the value: %w", err)
|
return "", fmt.Errorf("no key could be made to take the value: %w", err)
|
||||||
}
|
}
|
||||||
// By name, never by words: the holder writes the prompt from these, and says the controller asks, which
|
// By name, never by words: the holder writes the prompt from these, and says the controller asks, which
|
||||||
// the bus alone makes true (broker.ControllerOnly).
|
// the bus alone makes true (broker.ControllerOnly). Who asked is the bus's word on the caller, cut to a
|
||||||
|
// name's characters — never an argument of the call.
|
||||||
raw, err := d.ask(desk, map[string]any{
|
raw, err := d.ask(desk, map[string]any{
|
||||||
"module": module,
|
"module": module,
|
||||||
"secret": name,
|
"secret": name,
|
||||||
"node": node,
|
"node": node,
|
||||||
|
"asked_by": askedByName(d.askedBy),
|
||||||
"seal_to": public,
|
"seal_to": public,
|
||||||
"timeout_seconds": deskPromptWithin,
|
"timeout_seconds": deskPromptWithin,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
outcome = "refused"
|
||||||
return "", fmt.Errorf("the desk on %s could not be asked: %w", desk, err)
|
return "", fmt.Errorf("the desk on %s could not be asked: %w", desk, err)
|
||||||
}
|
}
|
||||||
var answer struct {
|
var answer struct {
|
||||||
@@ -121,8 +176,10 @@ func (d deskGive) give(node, module, name, desk string) (string, error) {
|
|||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case answer.TimedOut:
|
case answer.TimedOut:
|
||||||
|
outcome = "timed-out"
|
||||||
return "", fmt.Errorf("%w: the prompt on %s was not answered within %d seconds", errNothingGiven, desk, deskPromptWithin)
|
return "", fmt.Errorf("%w: the prompt on %s was not answered within %d seconds", errNothingGiven, desk, deskPromptWithin)
|
||||||
case answer.Cancelled:
|
case answer.Cancelled:
|
||||||
|
outcome = "dismissed"
|
||||||
return "", fmt.Errorf("%w: the prompt on %s was dismissed", errNothingGiven, desk)
|
return "", fmt.Errorf("%w: the prompt on %s was dismissed", errNothingGiven, desk)
|
||||||
case answer.Sealed == "":
|
case answer.Sealed == "":
|
||||||
return "", fmt.Errorf("the desk on %s answered no sealed value", desk)
|
return "", fmt.Errorf("the desk on %s answered no sealed value", desk)
|
||||||
@@ -137,6 +194,7 @@ func (d deskGive) give(node, module, name, desk string) (string, error) {
|
|||||||
opened[i] = 0
|
opened[i] = 0
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(value) == "" {
|
if strings.TrimSpace(value) == "" {
|
||||||
|
outcome = "empty"
|
||||||
return "", fmt.Errorf("%w: the prompt on %s was answered empty", errNothingGiven, desk)
|
return "", fmt.Errorf("%w: the prompt on %s was answered empty", errNothingGiven, desk)
|
||||||
}
|
}
|
||||||
untilStart, err := d.accept(value)
|
untilStart, err := d.accept(value)
|
||||||
@@ -144,8 +202,10 @@ func (d deskGive) give(node, module, name, desk string) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
outcome = "given"
|
||||||
act := link.HandAct{Verb: "secret accept", Args: []string{node, module, name, "--at-desk", desk},
|
act := link.HandAct{Verb: "secret accept", Args: []string{node, module, name, "--at-desk", desk},
|
||||||
Why: fmt.Sprintf("the operator gave %s for %s on %s at the desk on %s", name, module, node, desk),
|
Why: fmt.Sprintf("the operator gave %s for %s on %s at the desk on %s, asked by %s", name, module, node, desk,
|
||||||
|
askedByName(d.askedBy)),
|
||||||
Cause: "given-at-the-desk"}
|
Cause: "given-at-the-desk"}
|
||||||
recorded := ""
|
recorded := ""
|
||||||
if err := d.record(act); err != nil {
|
if err := d.record(act); err != nil {
|
||||||
@@ -165,15 +225,23 @@ func (d deskGive) give(node, module, name, desk string) (string, error) {
|
|||||||
return words + recorded, nil
|
return words + recorded, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// giveAtDesk is `secret accept <node> <module> <name> --at-desk <machine>`: the desk path, on this
|
// askAtDesk is `secret ask <node> <module> <name> [--at <machine>]`, and the terminal's `secret accept … --at-desk
|
||||||
// controller's stores and bus.
|
// <machine>`: the desk path, on this controller's stores and bus. The desk is the module's machine unless named.
|
||||||
func giveAtDesk(ctx context.Context, node, module, name, desk string) error {
|
func askAtDesk(ctx context.Context, node, module, name, desk string) error {
|
||||||
|
if desk == "" {
|
||||||
|
desk = node
|
||||||
|
}
|
||||||
open, err := openStores(ctx)
|
open, err := openStores(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer open.Close()
|
defer open.Close()
|
||||||
d := deskGive{
|
d := deskGive{
|
||||||
|
askedBy: link.Caller(),
|
||||||
|
open: func(node, module, name, desk string) (int64, error) {
|
||||||
|
return open.inventory.OpenSecretAsk(ctx, node, module, name, askedByName(link.Caller()), desk)
|
||||||
|
},
|
||||||
|
end: func(id int64, outcome string) error { return open.inventory.EndSecretAsk(ctx, id, outcome) },
|
||||||
declares: func(module, name string) error { return open.inventory.DeclaresOwnSecret(ctx, module, name) },
|
declares: func(module, name string) error { return open.inventory.DeclaresOwnSecret(ctx, module, name) },
|
||||||
known: func(machine string) error {
|
known: func(machine string) error {
|
||||||
_, err := open.inventory.NodeByName(ctx, machine)
|
_, err := open.inventory.NodeByName(ctx, machine)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -124,12 +125,21 @@ func TestADismissedEmptyLateOrForeignAnswerTakesNothing(t *testing.T) {
|
|||||||
|
|
||||||
func TestTheGiveVerbRunsTheDeskPathAndTheControllerMayAskTheDesk(t *testing.T) {
|
func TestTheGiveVerbRunsTheDeskPathAndTheControllerMayAskTheDesk(t *testing.T) {
|
||||||
argv, err := argvFor("give", map[string]any{"node": "anchor", "module": "telegram", "secret": "telegram-token", "at": "laptop"})
|
argv, err := argvFor("give", map[string]any{"node": "anchor", "module": "telegram", "secret": "telegram-token", "at": "laptop"})
|
||||||
if err != nil || strings.Join(argv, " ") != "secret accept anchor telegram telegram-token --at-desk laptop" {
|
if err != nil || strings.Join(argv, " ") != "secret ask anchor telegram telegram-token --at laptop" {
|
||||||
t.Fatalf("%v %v", argv, err)
|
t.Fatalf("%v %v", argv, err)
|
||||||
}
|
}
|
||||||
if _, err := argvFor("give", map[string]any{"node": "anchor", "module": "telegram", "secret": "telegram-token"}); err == nil {
|
if _, err := argvFor("give", map[string]any{"node": "anchor", "module": "telegram", "secret": "telegram-token"}); err == nil {
|
||||||
t.Error("give without a desk was taken")
|
t.Error("give without a desk was taken")
|
||||||
}
|
}
|
||||||
|
// secret-ask is the same line, with the desk the module's machine unless named (novox/hq ADR 0277).
|
||||||
|
argv, err = argvFor("secret-ask", map[string]any{"node": "anchor", "module": "telegram", "secret": "telegram-token"})
|
||||||
|
if err != nil || strings.Join(argv, " ") != "secret ask anchor telegram telegram-token" {
|
||||||
|
t.Fatalf("%v %v", argv, err)
|
||||||
|
}
|
||||||
|
argv, err = argvFor("secret-ask", map[string]any{"node": "anchor", "module": "telegram", "secret": "telegram-token", "at": "laptop"})
|
||||||
|
if err != nil || strings.Join(argv, " ") != "secret ask anchor telegram telegram-token --at laptop" {
|
||||||
|
t.Fatalf("%v %v", argv, err)
|
||||||
|
}
|
||||||
perms, err := broker.PermissionsFor(broker.Principal{Kind: broker.KindController})
|
perms, err := broker.PermissionsFor(broker.Principal{Kind: broker.KindController})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -266,13 +276,22 @@ func TestASecretValueIsNeverAcceptedThroughAVerb(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The `give` verb's own line passes the terminal-only rule of ADR 0266, and no other `secret accept` does: a
|
// The `give` and `secret-ask` verbs' own line passes the terminal-only rule of ADR 0266, and no `secret accept`
|
||||||
// value, a file, a provider or an extra word is still the terminal's alone.
|
// does: a value, a file, a provider or an extra word is still the terminal's alone (novox/hq ADR 0277).
|
||||||
func TestOnlyTheGiveLinePassesTheTerminalRuleForSecrets(t *testing.T) {
|
func TestOnlyTheGiveLinePassesTheTerminalRuleForSecrets(t *testing.T) {
|
||||||
if err := terminalOnly([]string{"secret", "accept", "anchor", "telegram", "telegram-token", "--at-desk", "laptop"}); err != nil {
|
for _, argv := range [][]string{
|
||||||
t.Errorf("give's line refused: %v", err)
|
{"secret", "ask", "anchor", "telegram", "telegram-token", "--at", "laptop"},
|
||||||
|
{"secret", "ask", "anchor", "telegram", "telegram-token"},
|
||||||
|
} {
|
||||||
|
if err := terminalOnly(argv); err != nil {
|
||||||
|
t.Errorf("%v refused: %v", argv, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for _, argv := range [][]string{
|
for _, argv := range [][]string{
|
||||||
|
{"secret", "accept", "anchor", "telegram", "telegram-token", "--at-desk", "laptop"},
|
||||||
|
{"secret", "ask", "anchor", "telegram", "telegram-token", "--from", "/tmp/x"},
|
||||||
|
{"secret", "ask", "anchor", "telegram", "telegram-token", "--at", "laptop", "--local"},
|
||||||
|
{"secret", "ask", "anchor", "telegram", "--at", "laptop"},
|
||||||
{"secret", "accept", "anchor", "telegram", "telegram-token"},
|
{"secret", "accept", "anchor", "telegram", "telegram-token"},
|
||||||
{"secret", "accept", "anchor", "telegram", "telegram-token", "--from", "/tmp/x"},
|
{"secret", "accept", "anchor", "telegram", "telegram-token", "--from", "/tmp/x"},
|
||||||
{"secret", "accept", "anchor", "telegram", "telegram-token", "--at-desk", "laptop", "--local"},
|
{"secret", "accept", "anchor", "telegram", "telegram-token", "--at-desk", "laptop", "--local"},
|
||||||
@@ -398,3 +417,96 @@ func TestAGiveNamingAMachineTheMeshDoesNotKnowAsksNobody(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// novox/hq ADR 0277: the prompt names who asked — the controller's word on the bus's caller, cut to a name's
|
||||||
|
// characters — and never a word the caller chose: there is no argument for it.
|
||||||
|
func TestThePromptNamesWhoAskedFromTheBussWordAlone(t *testing.T) {
|
||||||
|
d, _, acts, asked := aDesk(t, sealedTo(t, typed))
|
||||||
|
d.askedBy = "g14/claude-code, through the mesh-controller seat"
|
||||||
|
if _, err := d.give("anchor", "telegram", "telegram-token", "laptop"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got := (*asked)[0]["asked_by"]; got != "g14/claude-code" {
|
||||||
|
t.Errorf("asked_by %q", got)
|
||||||
|
}
|
||||||
|
if len(*acts) != 1 || !strings.Contains((*acts)[0].Why, "asked by g14/claude-code") {
|
||||||
|
t.Errorf("the record: %+v", *acts)
|
||||||
|
}
|
||||||
|
for in, want := range map[string]string{
|
||||||
|
"jochen at a shell on novox": "jochen at a shell on novox",
|
||||||
|
"laptop/agent": "laptop/agent",
|
||||||
|
"Your bank asks\nType your PIN, now": "Your bank asks-Type your PIN",
|
||||||
|
"": "an unnamed caller",
|
||||||
|
"<b>x</b>": "an unnamed caller",
|
||||||
|
strings.Repeat("a", 100): strings.Repeat("a", 80),
|
||||||
|
"--prompt, something else": "an unnamed caller",
|
||||||
|
} {
|
||||||
|
if got := askedByName(in); got != want {
|
||||||
|
t.Errorf("askedByName(%q) = %q, want %q", in, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The verb's schema has no argument that reaches the prompt's words.
|
||||||
|
for _, verb := range []string{"give", "secret-ask"} {
|
||||||
|
for _, free := range []string{"asked_by", "prompt", "message", "value", "from"} {
|
||||||
|
if _, err := argvFor(verb, map[string]any{"node": "anchor", "module": "telegram", "secret": "telegram-token",
|
||||||
|
"at": "laptop", free: "x"}); err == nil {
|
||||||
|
t.Errorf("%s takes %s", verb, free)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// An ask for a secret is bounded before anybody is asked to type (ADR 0277): the record refuses it, nothing is
|
||||||
|
// asked; and how every ask ends is recorded.
|
||||||
|
func TestASecretAskIsBoundedAndItsEndRecorded(t *testing.T) {
|
||||||
|
d, accepted, _, asked := aDesk(t, sealedTo(t, typed))
|
||||||
|
var ended []string
|
||||||
|
d.open = func(node, module, name, desk string) (int64, error) { return 7, nil }
|
||||||
|
d.end = func(id int64, outcome string) error {
|
||||||
|
ended = append(ended, fmt.Sprintf("%d %s", id, outcome))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, err := d.give("anchor", "telegram", "telegram-token", "laptop"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
d.open = func(node, module, name, desk string) (int64, error) {
|
||||||
|
return 0, errors.New("an ask for telegram-token of telegram on anchor is still open")
|
||||||
|
}
|
||||||
|
_, err := d.give("anchor", "telegram", "telegram-token", "laptop")
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "nobody was asked to type anything") || !strings.Contains(err.Error(), "still open") {
|
||||||
|
t.Errorf("a second ask: %v", err)
|
||||||
|
}
|
||||||
|
if len(*asked) != 1 || len(*accepted) != 1 {
|
||||||
|
t.Errorf("asked %d, accepted %d", len(*asked), len(*accepted))
|
||||||
|
}
|
||||||
|
d.open = func(node, module, name, desk string) (int64, error) { return 8, nil }
|
||||||
|
d.ask = func(string, map[string]any) (json.RawMessage, error) {
|
||||||
|
return json.RawMessage(`{"cancelled":true}`), nil
|
||||||
|
}
|
||||||
|
if _, err := d.give("anchor", "telegram", "telegram-token", "laptop"); !errors.Is(err, errNothingGiven) {
|
||||||
|
t.Errorf("a dismissed prompt: %v", err)
|
||||||
|
}
|
||||||
|
if strings.Join(ended, "; ") != "7 given; 8 dismissed" {
|
||||||
|
t.Errorf("ended: %v", ended)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A secret ask cannot be turned into a secret read: the line carries no value, the answer carries none, and every
|
||||||
|
// other `secret` line is the terminal's.
|
||||||
|
func TestASecretAskIsNeverASecretRead(t *testing.T) {
|
||||||
|
t.Setenv(verbVar, "mesh-controller.secret-ask")
|
||||||
|
for _, args := range [][]string{
|
||||||
|
{"ask", "anchor", "telegram", "telegram-token", "the-value"},
|
||||||
|
{"ask", "anchor", "telegram"},
|
||||||
|
{"ask", "anchor", "telegram", "telegram-token", "--from", "/dev/null"},
|
||||||
|
} {
|
||||||
|
if err := secretCommand(context.Background(), args); err == nil {
|
||||||
|
t.Errorf("secret %v was taken", args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, args := range [][]string{{"recover", "anchor", "telegram", "telegram-token"}, {"export"}} {
|
||||||
|
if err := terminalOnly(append([]string{"secret"}, args...)); err == nil {
|
||||||
|
t.Errorf("secret %v passed the terminal rule", args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -768,10 +768,22 @@ func (a *verbArguments) commandLine() ([]string, error) {
|
|||||||
}
|
}
|
||||||
return append(argv, "--json"), nil
|
return append(argv, "--json"), nil
|
||||||
case "give":
|
case "give":
|
||||||
|
// The same line as secret-ask with the desk named (novox/hq ADR 0277): one path, one set of bounds.
|
||||||
if err := need("node", "module", "secret", "at"); err != nil {
|
if err := need("node", "module", "secret", "at"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return []string{"secret", "accept", str("node"), str("module"), str("secret"), "--at-desk", str("at")}, nil
|
return []string{"secret", "ask", str("node"), str("module"), str("secret"), "--at", str("at")}, nil
|
||||||
|
case "secret-ask":
|
||||||
|
// A module's own secret asked for, typed by the operator at the desk (novox/hq ADR 0277): never a value
|
||||||
|
// in the arguments. The desk is the module's machine unless at names another.
|
||||||
|
if err := need("node", "module", "secret"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
argv := []string{"secret", "ask", str("node"), str("module"), str("secret")}
|
||||||
|
if at := str("at"); at != "" {
|
||||||
|
argv = append(argv, "--at", at)
|
||||||
|
}
|
||||||
|
return argv, nil
|
||||||
case "rotate":
|
case "rotate":
|
||||||
if p := str("provision"); p != "" {
|
if p := str("provision"); p != "" {
|
||||||
argv := []string{"rotate", p}
|
argv := []string{"rotate", p}
|
||||||
@@ -1538,10 +1550,11 @@ var terminalOnlyCommands = map[string]string{
|
|||||||
"licence": "the licences' secrets",
|
"licence": "the licences' secrets",
|
||||||
}
|
}
|
||||||
|
|
||||||
// givenAtTheDesk is exactly the line the `give` verb composes, and nothing beside it: `secret accept <node>
|
// givenAtTheDesk is exactly the line the `give` and `secret-ask` verbs compose, and nothing beside it: `secret ask
|
||||||
// <module> <secret> --at-desk <machine>`, with no other word — no value, no file, no provider.
|
// <node> <module> <secret>`, with `--at <machine>` or no other word — no value, no file, no provider (novox/hq
|
||||||
|
// ADR 0277). The terminal's own `secret accept … --at-desk` is the terminal's.
|
||||||
func givenAtTheDesk(argv []string) bool {
|
func givenAtTheDesk(argv []string) bool {
|
||||||
if len(argv) != 7 || argv[0] != "secret" || argv[1] != "accept" || argv[5] != "--at-desk" {
|
if (len(argv) != 5 && len(argv) != 7) || argv[0] != "secret" || argv[1] != "ask" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, w := range argv[2:5] {
|
for _, w := range argv[2:5] {
|
||||||
@@ -1549,7 +1562,10 @@ func givenAtTheDesk(argv []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return argv[6] != "" && !strings.HasPrefix(argv[6], "-")
|
if len(argv) == 5 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return argv[5] == "--at" && argv[6] != "" && !strings.HasPrefix(argv[6], "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
// terminalOnly refuses, through any verb, a command that is the operator's at the controller's terminal
|
// terminalOnly refuses, through any verb, a command that is the operator's at the controller's terminal
|
||||||
|
|||||||
@@ -276,12 +276,6 @@ var accountedFlags = map[string]map[string]string{
|
|||||||
"all": "withheld: every measurement of a fortnight is more than a call should carry; `command` reaches it",
|
"all": "withheld: every measurement of a fortnight is more than a call should carry; `command` reaches it",
|
||||||
},
|
},
|
||||||
// The desk path of `secret accept` (novox/hq ADR 0259 §10): a value is never an argument of a call.
|
// The desk path of `secret accept` (novox/hq ADR 0259 §10): a value is never an argument of a call.
|
||||||
"secret accept": {
|
|
||||||
"at-desk": "=at",
|
|
||||||
"from": "withheld: a file of the control node's is read at a shell, never named by a call",
|
|
||||||
"provider": "withheld: a pair credential's value is given at a shell; give takes a module's own secret",
|
|
||||||
"local": "withheld: it goes with --provider",
|
|
||||||
},
|
|
||||||
"hand-acts": {"json": "set by the verb: the answer is data"},
|
"hand-acts": {"json": "set by the verb: the answer is data"},
|
||||||
"conditions": {"json": "set by the verb: the answer is data"},
|
"conditions": {"json": "set by the verb: the answer is data"},
|
||||||
"retire": {"json": "set by the verb: the answer is data"},
|
"retire": {"json": "set by the verb: the answer is data"},
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ func secretCommand(ctx context.Context, args []string) error {
|
|||||||
}
|
}
|
||||||
switch args[0] {
|
switch args[0] {
|
||||||
case "accept":
|
case "accept":
|
||||||
|
case "ask":
|
||||||
|
return secretAsk(ctx, args[1:])
|
||||||
case "rotate":
|
case "rotate":
|
||||||
return secretRotate(ctx, args[1:])
|
return secretRotate(ctx, args[1:])
|
||||||
case "recover":
|
case "recover":
|
||||||
@@ -78,7 +80,7 @@ func secretCommand(ctx context.Context, args []string) error {
|
|||||||
if *from != "" || *provider != "" {
|
if *from != "" || *provider != "" {
|
||||||
return errors.New("--at-desk gives a module's own secret, and takes neither --from nor --provider")
|
return errors.New("--at-desk gives a module's own secret, and takes neither --from nor --provider")
|
||||||
}
|
}
|
||||||
return giveAtDesk(ctx, node, module, name, *desk)
|
return askAtDesk(ctx, node, module, name, *desk)
|
||||||
}
|
}
|
||||||
|
|
||||||
value, err := valueFor(node, module, name, *from)
|
value, err := valueFor(node, module, name, *from)
|
||||||
@@ -148,7 +150,24 @@ func secretCommand(ctx context.Context, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// secretAsk is `secret ask <node> <module> <name> [--at <machine>]` (novox/hq ADR 0277): the operator is asked
|
||||||
|
// for a module's own secret in a prompt at the desk, which only they answer. The one `secret` line a verb may
|
||||||
|
// run beside rotate (givenAtTheDesk): it carries no value and answers none.
|
||||||
|
func secretAsk(ctx context.Context, args []string) error {
|
||||||
|
rest, flags := split(args)
|
||||||
|
set := flag.NewFlagSet("secret ask", flag.ContinueOnError)
|
||||||
|
at := set.String("at", "", "the machine the operator sits at, where the prompt opens; the module's machine when absent")
|
||||||
|
if err := set.Parse(flags); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(rest) != 3 {
|
||||||
|
return errors.New("secret ask <node> <module> <name> [--at <machine>]")
|
||||||
|
}
|
||||||
|
return askAtDesk(ctx, rest[0], rest[1], rest[2], *at)
|
||||||
|
}
|
||||||
|
|
||||||
const secretUsage = "secret rotate <node> <module> <name> [--why <text> [--cause <word>]]\n" +
|
const secretUsage = "secret rotate <node> <module> <name> [--why <text> [--cause <word>]]\n" +
|
||||||
|
"secret ask <node> <module> <name> [--at <machine>]\n" +
|
||||||
"secret accept <node> <module> <name> [--from <file> | --at-desk <machine>] [--provider <node> [--local <name>]]\n" +
|
"secret accept <node> <module> <name> [--from <file> | --at-desk <machine>] [--provider <node> [--local <name>]]\n" +
|
||||||
"secret recover <node> <module> <name> --key <operator-key> [--out <file>] [--from-export <file>] [--provider <node>]\n" +
|
"secret recover <node> <module> <name> --key <operator-key> [--out <file>] [--from-export <file>] [--provider <node>]\n" +
|
||||||
"secret export [--out <file>]"
|
"secret export [--out <file>]"
|
||||||
|
|||||||
+67
-1
@@ -42,8 +42,64 @@ const (
|
|||||||
// Its authority is the union of what the modules it carries would each have had for their
|
// Its authority is the union of what the modules it carries would each have had for their
|
||||||
// tools — and nothing of what they consume, because tools are what it runs, not reactions.
|
// tools — and nothing of what they consume, because tools are what it runs, not reactions.
|
||||||
KindNodeTools Kind = "node-tools"
|
KindNodeTools Kind = "node-tools"
|
||||||
|
// KindView is the one read-only principal a view onto the bus connects as (novox/hq research 036,
|
||||||
|
// gap G1): a page in a browser, over the bus module's WebSocket listener, watching the issue tracker.
|
||||||
|
// Fixed, and derived from no declaration: what it hears is ViewHears, what it reads is ViewBucket,
|
||||||
|
// and it publishes nothing but direct reads of that one bucket (ViewReads), each answered in its own
|
||||||
|
// inbox. Composed like every other user, into the same
|
||||||
|
// file, once its credential is minted (`bus view-credential`); forgotten like every other user
|
||||||
|
// (`bus view-revoke`), at the next composition.
|
||||||
|
KindView Kind = "view"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ViewUser is the view's one username: there is one view, and it is nobody's machine or module.
|
||||||
|
const ViewUser = "view"
|
||||||
|
|
||||||
|
// ViewBucket is the state the view reads: the issue tracker's issues, as the bus names the bucket
|
||||||
|
// (mesh-issues's state `issues`, novox/hq ADR 0201).
|
||||||
|
var ViewBucket = BucketName("mesh-issues", "issues")
|
||||||
|
|
||||||
|
// ViewHears are the events the view subscribes, each named: the issue tracker's own, the controller's
|
||||||
|
// walks and conditions, and the delivery owner's — what a page about issues shows beside them. Subscribe
|
||||||
|
// only, and no stream or consumer of its own: a page hears what happens while it is open, and reads the
|
||||||
|
// bucket for everything before.
|
||||||
|
var ViewHears = []string{
|
||||||
|
moduleEventSubject("mesh-issues", "opened"),
|
||||||
|
moduleEventSubject("mesh-issues", "moved"),
|
||||||
|
moduleEventSubject("mesh-issues", "noted"),
|
||||||
|
moduleEventSubject("mesh-issues", "linked"),
|
||||||
|
seatEventSubject(ControllerSeat, "plan-moved"),
|
||||||
|
seatEventSubject(ControllerSeat, "condition-raised"),
|
||||||
|
seatEventSubject(ControllerSeat, "condition-changed"),
|
||||||
|
seatEventSubject(ControllerSeat, "condition-cleared"),
|
||||||
|
moduleEventSubject("mesh-delivery", "transition"),
|
||||||
|
moduleEventSubject("mesh-delivery", "group"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// ViewReads are the JetStream API requests the view makes, on ViewBucket's stream and no other: binding
|
||||||
|
// (STREAM.INFO), and direct reads — one key by its subject (`DIRECT.GET.<stream>.$KV.<bucket>.<key>`), and
|
||||||
|
// the batch form on the bare subject, which answers the newest value of every key (`multi_last`) into the
|
||||||
|
// asker's inbox. The page lists the bucket with the batch, and re-reads one key when the tracker's event
|
||||||
|
// names it (every event carries the issue's `number`).
|
||||||
|
//
|
||||||
|
// **No consumer, deliberately, and so no watch.** A KV watch is a push consumer, and a push consumer's
|
||||||
|
// deliver subject is the creator's choice, delivered by the server's own client — which the server does
|
||||||
|
// not hold to the creator's permissions. Measured on 2.11.17 (2026-10-10): the view, granted
|
||||||
|
// CONSUMER.CREATE on this stream, made a consumer delivering to `mesh.mod.mesh-issues.event.opened`, and
|
||||||
|
// a module subscribed there received the bucket's entry as the tracker's event. A grant of CONSUMER.CREATE
|
||||||
|
// is a publish to any subject in the account; the view publishes nothing, so it has none (nor
|
||||||
|
// CONSUMER.DELETE, which would let it delete a module's consumer). Nothing here is a write either: no
|
||||||
|
// `$KV.<bucket>.>`, which is what a put or a delete publishes to, and no STREAM.* that defines, purges or
|
||||||
|
// deletes.
|
||||||
|
func ViewReads() []string {
|
||||||
|
stream := "KV_" + ViewBucket
|
||||||
|
return []string{
|
||||||
|
"$JS.API.STREAM.INFO." + stream,
|
||||||
|
"$JS.API.DIRECT.GET." + stream,
|
||||||
|
"$JS.API.DIRECT.GET." + stream + ".>",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// RuntimeModule is the module that IS the node's tool runtime (novox/hq ADR 0175). Where it is
|
// RuntimeModule is the module that IS the node's tool runtime (novox/hq ADR 0175). Where it is
|
||||||
// assigned, the mesh composes one runtime principal for the machine in place of that module's own,
|
// assigned, the mesh composes one runtime principal for the machine in place of that module's own,
|
||||||
// and the per-module containers that served tools until then stop being the way tools reach a node.
|
// and the per-module containers that served tools until then stop being the way tools reach a node.
|
||||||
@@ -261,6 +317,8 @@ func (p Principal) Username() string {
|
|||||||
switch p.Kind {
|
switch p.Kind {
|
||||||
case KindPerson:
|
case KindPerson:
|
||||||
return "person." + p.Module
|
return "person." + p.Module
|
||||||
|
case KindView:
|
||||||
|
return ViewUser
|
||||||
case KindModule, KindNodeTools:
|
case KindModule, KindNodeTools:
|
||||||
// The runtime is named exactly as the module it stands for would have been: the mesh
|
// The runtime is named exactly as the module it stands for would have been: the mesh
|
||||||
// issues its credential through the same path a module's takes (`module issue`), and
|
// issues its credential through the same path a module's takes (`module issue`), and
|
||||||
@@ -533,6 +591,14 @@ func PermissionsFor(p Principal) (Permissions, error) {
|
|||||||
// itself, its replies to the asker's own inbox.
|
// itself, its replies to the asker's own inbox.
|
||||||
pub = append(pub, discovering()...)
|
pub = append(pub, discovering()...)
|
||||||
|
|
||||||
|
case KindView:
|
||||||
|
// Hears what it is for and reads one bucket, and nothing else (ViewHears, ViewReads): no tool,
|
||||||
|
// no event of its own, no stream, no bucket written. Its requests are answered in its own
|
||||||
|
// inbox, granted below with the person's; a reply to anything is never permitted, because
|
||||||
|
// nothing is ever asked of it.
|
||||||
|
sub = append(sub, ViewHears...)
|
||||||
|
pub = append(pub, ViewReads()...)
|
||||||
|
|
||||||
case KindEnrolment:
|
case KindEnrolment:
|
||||||
// A leaked token is useless for anything but enrolling: it cannot read a declaration, hear
|
// A leaked token is useless for anything but enrolling: it cannot read a declaration, hear
|
||||||
// an event, or subscribe any inbox but the one its own token derives (design 25 §6).
|
// an event, or subscribe any inbox but the one its own token derives (design 25 §6).
|
||||||
@@ -834,7 +900,7 @@ func PermissionsFor(p Principal) (Permissions, error) {
|
|||||||
pub = unique(pub)
|
pub = unique(pub)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Kind == KindPerson {
|
if p.Kind == KindPerson || p.Kind == KindView {
|
||||||
// An inbox to hear answers in, and nothing else. No ack subject: a person has no durable
|
// An inbox to hear answers in, and nothing else. No ack subject: a person has no durable
|
||||||
// consumer, because nothing is delivered to a person — they ask and are answered.
|
// consumer, because nothing is delivered to a person — they ask and are answered.
|
||||||
sub = append(sub, p.inbox())
|
sub = append(sub, p.inbox())
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ func TestTheComposedConfigMatchesTheGolden(t *testing.T) {
|
|||||||
// The bus's own module: the snapshot API and its inbox, nothing else (novox/hq ADR 0235).
|
// The bus's own module: the snapshot API and its inbox, nothing else (novox/hq ADR 0235).
|
||||||
{Kind: KindModule, Node: "one", Module: "nats", SnapshotsTheBus: true,
|
{Kind: KindModule, Node: "one", Module: "nats", SnapshotsTheBus: true,
|
||||||
Serves: []string{"nats_streams"}, PasswordHash: "$2a$11$bbbbbbbbbbbbbbbbbbbbbb"},
|
Serves: []string{"nats_streams"}, PasswordHash: "$2a$11$bbbbbbbbbbbbbbbbbbbbbb"},
|
||||||
|
// The view: hears the issue tracker and reads its bucket, writes nothing (research 036).
|
||||||
|
{Kind: KindView, PasswordHash: "$2a$11$vvvvvvvvvvvvvvvvvvvvvv"},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
+4
@@ -56,6 +56,10 @@ accounts {
|
|||||||
subscribe: { allow: ["$SRV.INFO", "$SRV.INFO.shop", "$SRV.INFO.shop.>", "$SRV.PING", "$SRV.PING.shop", "$SRV.PING.shop.>", "$SRV.STATS", "$SRV.STATS.shop", "$SRV.STATS.shop.>", "_INBOX.two.shop.>", "mesh.assignment.two.shop", "mesh.mod.shop.tool.>"] }
|
subscribe: { allow: ["$SRV.INFO", "$SRV.INFO.shop", "$SRV.INFO.shop.>", "$SRV.PING", "$SRV.PING.shop", "$SRV.PING.shop.>", "$SRV.STATS", "$SRV.STATS.shop", "$SRV.STATS.shop.>", "_INBOX.two.shop.>", "mesh.assignment.two.shop", "mesh.mod.shop.tool.>"] }
|
||||||
allow_responses: { max: 1, ttl: "1m" }
|
allow_responses: { max: 1, ttl: "1m" }
|
||||||
} }
|
} }
|
||||||
|
{ user: "view", password: "$2a$11$vvvvvvvvvvvvvvvvvvvvvv", permissions: {
|
||||||
|
publish: { allow: ["$JS.API.DIRECT.GET.KV_mesh-issues_issues", "$JS.API.DIRECT.GET.KV_mesh-issues_issues.>", "$JS.API.STREAM.INFO.KV_mesh-issues_issues"] }
|
||||||
|
subscribe: { allow: ["_INBOX.view.>", "mesh.mod.mesh-delivery.event.group", "mesh.mod.mesh-delivery.event.transition", "mesh.mod.mesh-issues.event.linked", "mesh.mod.mesh-issues.event.moved", "mesh.mod.mesh-issues.event.noted", "mesh.mod.mesh-issues.event.opened", "mesh.seat.mesh-controller.event.condition-changed", "mesh.seat.mesh-controller.event.condition-cleared", "mesh.seat.mesh-controller.event.condition-raised", "mesh.seat.mesh-controller.event.plan-moved"] }
|
||||||
|
} }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,9 @@ type Records struct {
|
|||||||
Enrolling []string
|
Enrolling []string
|
||||||
// People is each person's name against the tools they may invoke, `*` for an administrator.
|
// People is each person's name against the tools they may invoke, `*` for an administrator.
|
||||||
People map[string][]string
|
People map[string][]string
|
||||||
|
// View says the mesh minted the view's credential (`bus view-credential`), so the one read-only
|
||||||
|
// view principal is composed (KindView); forgotten, it is left out, like a person.
|
||||||
|
View bool
|
||||||
// Interchangeable is each module whose definition says its instances are the same anywhere
|
// Interchangeable is each module whose definition says its instances are the same anywhere
|
||||||
// (ADR 0160), which decides whether the module's plain subject is issued to every instance.
|
// (ADR 0160), which decides whether the module's plain subject is issued to every instance.
|
||||||
Interchangeable map[string]bool
|
Interchangeable map[string]bool
|
||||||
@@ -142,6 +145,9 @@ func Users(r Records) ([]Principal, error) {
|
|||||||
for _, person := range sortedNames(r.People) {
|
for _, person := range sortedNames(r.People) {
|
||||||
out = append(out, Principal{Kind: KindPerson, Module: person, Invokes: r.People[person]})
|
out = append(out, Principal{Kind: KindPerson, Module: person, Invokes: r.People[person]})
|
||||||
}
|
}
|
||||||
|
if r.View {
|
||||||
|
out = append(out, Principal{Kind: KindView})
|
||||||
|
}
|
||||||
|
|
||||||
// Refused here rather than discovered by the server. Two users with one name is a file the
|
// Refused here rather than discovered by the server. Two users with one name is a file the
|
||||||
// server reads as one of them, and which one depends on the order — so a module assigned to a
|
// server reads as one of them, and which one depends on the order — so a module assigned to a
|
||||||
|
|||||||
@@ -0,0 +1,230 @@
|
|||||||
|
package broker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/nats-io/nats-server/v2/server"
|
||||||
|
"github.com/nats-io/nats.go"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The view against a real server, over WebSocket (novox/hq research 036): the composed user list is
|
||||||
|
// what the server reads, the listener is the shape the bus module declares (no TLS, compression on,
|
||||||
|
// reached across the overlay only), and the view with its credential binds the issue tracker's bucket,
|
||||||
|
// lists it with one batch read, follows a tracker event to re-read the key it names — and is refused
|
||||||
|
// every write: a put, a delete, an event, and a consumer delivering onto the tracker's event subject.
|
||||||
|
//
|
||||||
|
// go test ./internal/broker/ -run TestTheView
|
||||||
|
func TestTheViewReadsTheIssuesOverWebSocketAndWritesNothing(t *testing.T) {
|
||||||
|
hash := func(password string) string {
|
||||||
|
h, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return string(h)
|
||||||
|
}
|
||||||
|
const node = "anchor"
|
||||||
|
tracker := Principal{Kind: KindModule, Node: node, Module: "mesh-issues", State: []string{"issues"},
|
||||||
|
Emits: []string{"opened", "moved"}, PasswordHash: hash("tracker")}
|
||||||
|
accounts, err := ComposeAccounts([]Principal{
|
||||||
|
{Kind: KindController, PasswordHash: hash("controller")},
|
||||||
|
tracker,
|
||||||
|
{Kind: KindView, PasswordHash: hash("view")},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
conf := filepath.Join(t.TempDir(), "accounts.conf")
|
||||||
|
if err := os.WriteFile(conf, []byte(accounts), 0o600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// The server reads the composed file as the bus does — through its own parser — and listens as the
|
||||||
|
// bus module's configuration says: a WebSocket listener without TLS and with compression, beside
|
||||||
|
// the client port. Ports chosen by the system, so this runs beside a live bus.
|
||||||
|
opts, err := server.ProcessConfigFile(conf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("the server refused the composed user list: %v", err)
|
||||||
|
}
|
||||||
|
opts.Host, opts.Port = "127.0.0.1", server.RANDOM_PORT
|
||||||
|
opts.JetStream, opts.StoreDir = true, t.TempDir()
|
||||||
|
opts.NoLog, opts.NoSigs = true, true
|
||||||
|
opts.Websocket = server.WebsocketOpts{Host: "127.0.0.1", Port: server.RANDOM_PORT, NoTLS: true, Compression: true}
|
||||||
|
s, err := server.NewServer(opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
go s.Start()
|
||||||
|
if !s.ReadyForConnections(30 * time.Second) {
|
||||||
|
s.Shutdown()
|
||||||
|
t.Fatal("the server did not come up")
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { s.Shutdown(); s.WaitForShutdown() })
|
||||||
|
|
||||||
|
dial := func(url, user, password string, refused chan<- string) *nats.Conn {
|
||||||
|
t.Helper()
|
||||||
|
nc, err := nats.Connect(url, nats.UserInfo(user, password), nats.CustomInboxPrefix("_INBOX."+user),
|
||||||
|
nats.Compression(true), nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
|
||||||
|
if refused != nil && errors.Is(err, nats.ErrPermissionViolation) {
|
||||||
|
refused <- err.Error()
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s could not connect to %s: %v", user, url, err)
|
||||||
|
}
|
||||||
|
t.Cleanup(nc.Close)
|
||||||
|
return nc
|
||||||
|
}
|
||||||
|
|
||||||
|
// The controller defines the bucket, as it does for every module's state; the tracker writes it.
|
||||||
|
controller := dial(s.ClientURL(), "controller", "controller", nil)
|
||||||
|
cjs, _ := controller.JetStream()
|
||||||
|
if _, err := cjs.CreateKeyValue(&nats.KeyValueConfig{Bucket: ViewBucket, History: 8}); err != nil {
|
||||||
|
t.Fatalf("the controller could not define %s: %v", ViewBucket, err)
|
||||||
|
}
|
||||||
|
trackerConn := dial(s.ClientURL(), tracker.Username(), "tracker", nil)
|
||||||
|
tjs, _ := trackerConn.JetStream()
|
||||||
|
tkv, err := tjs.KeyValue(ViewBucket)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := tkv.Put("365", []byte(`{"number":365,"status":"open"}`)); err != nil {
|
||||||
|
t.Fatalf("the tracker could not write its own bucket: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The view, over WebSocket with its credential.
|
||||||
|
refused := make(chan string, 8)
|
||||||
|
view := dial(s.WebsocketURL(), ViewUser, "view", refused)
|
||||||
|
if !strings.HasPrefix(view.ConnectedUrl(), "ws://") {
|
||||||
|
t.Fatalf("the view is connected to %s, not over WebSocket", view.ConnectedUrl())
|
||||||
|
}
|
||||||
|
vjs, _ := view.JetStream(nats.MaxWait(3 * time.Second))
|
||||||
|
vkv, err := vjs.KeyValue(ViewBucket)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("the view could not bind %s: %v", ViewBucket, err)
|
||||||
|
}
|
||||||
|
if got, err := vkv.Get("365"); err != nil {
|
||||||
|
t.Fatalf("the view could not read a key: %v", err)
|
||||||
|
} else if !strings.Contains(string(got.Value()), `"number":365`) {
|
||||||
|
t.Fatalf("the view read %q", got.Value())
|
||||||
|
}
|
||||||
|
if _, err := tkv.Put("366", []byte(`{"number":366,"status":"open"}`)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The list: one batch read, the newest value of every key, into the view's own inbox, ended by the
|
||||||
|
// server's end-of-batch status (204).
|
||||||
|
listed := map[string]string{}
|
||||||
|
inbox := view.NewRespInbox()
|
||||||
|
batch, err := view.SubscribeSync(inbox)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := view.PublishRequest("$JS.API.DIRECT.GET.KV_"+ViewBucket, inbox,
|
||||||
|
[]byte(`{"multi_last":["$KV.`+ViewBucket+`.>"]}`)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
m, err := batch.NextMsg(5 * time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("the view's batch read ended without its end-of-batch (%d keys so far): %v", len(listed), err)
|
||||||
|
}
|
||||||
|
if m.Header.Get("Status") == "204" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if status := m.Header.Get("Status"); status != "" {
|
||||||
|
t.Fatalf("the batch read answered %s %s", status, m.Header.Get("Description"))
|
||||||
|
}
|
||||||
|
listed[m.Header.Get("Nats-Subject")] = string(m.Data)
|
||||||
|
}
|
||||||
|
_ = batch.Unsubscribe()
|
||||||
|
for _, key := range []string{"365", "366"} {
|
||||||
|
if !strings.Contains(listed["$KV."+ViewBucket+"."+key], `"number":`+key) {
|
||||||
|
t.Errorf("the batch read did not list %s: %v", key, listed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A change followed: the tracker moves 365 and says so; the view hears the event and reads the key
|
||||||
|
// it names.
|
||||||
|
moved, err := view.SubscribeSync("mesh.mod.mesh-issues.event.moved")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_ = view.Flush()
|
||||||
|
if _, err := tkv.Put("365", []byte(`{"number":365,"status":"located"}`)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := trackerConn.Publish("mesh.mod.mesh-issues.event.moved", []byte(`{"number":365,"to":"located"}`)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := moved.NextMsg(5 * time.Second); err != nil {
|
||||||
|
t.Fatalf("the view did not hear the tracker's event: %v", err)
|
||||||
|
}
|
||||||
|
if got, err := vkv.Get("365"); err != nil || !strings.Contains(string(got.Value()), "located") {
|
||||||
|
t.Fatalf("after the event the view read %v (%v)", got, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// **The hole a watch would open, shut** (ViewReads): a consumer delivering onto the tracker's event
|
||||||
|
// subject would have the server republish the bucket there, as the tracker. Refused, and nothing
|
||||||
|
// reaches a module listening on that subject.
|
||||||
|
listener, err := trackerConn.SubscribeSync("mesh.mod.mesh-issues.event.opened")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_ = trackerConn.Flush()
|
||||||
|
if _, err := vjs.AddConsumer("KV_"+ViewBucket, &nats.ConsumerConfig{Name: "w", DeliverSubject: "mesh.mod.mesh-issues.event.opened",
|
||||||
|
AckPolicy: nats.AckNonePolicy, FilterSubject: "$KV." + ViewBucket + ".>"}); err == nil {
|
||||||
|
t.Error("the view made a consumer")
|
||||||
|
}
|
||||||
|
if _, err := vkv.WatchAll(); err == nil {
|
||||||
|
t.Error("the view made a watch, which is a consumer")
|
||||||
|
}
|
||||||
|
if m, err := listener.NextMsg(2 * time.Second); err == nil {
|
||||||
|
t.Errorf("a message reached the tracker's event subject from the view: %q", m.Data)
|
||||||
|
}
|
||||||
|
// The refusals of the consumer create land as permission violations too; drained before the writes.
|
||||||
|
drain := time.After(500 * time.Millisecond)
|
||||||
|
for draining := true; draining; {
|
||||||
|
select {
|
||||||
|
case <-refused:
|
||||||
|
case <-drain:
|
||||||
|
draining = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// And every write is refused: the server says so, and the bucket is unchanged.
|
||||||
|
if _, err := vkv.Put("367", []byte(`{"number":367}`)); err == nil {
|
||||||
|
t.Error("the view put a key")
|
||||||
|
}
|
||||||
|
if err := vkv.Delete("365"); err == nil {
|
||||||
|
t.Error("the view deleted a key")
|
||||||
|
}
|
||||||
|
if err := view.Publish("mesh.mod.mesh-issues.event.opened", []byte(`{"number":367}`)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_ = view.Flush()
|
||||||
|
violations := map[string]bool{}
|
||||||
|
deadline := time.After(10 * time.Second)
|
||||||
|
for len(violations) < 3 {
|
||||||
|
select {
|
||||||
|
case v := <-refused:
|
||||||
|
for _, subject := range []string{"$KV." + ViewBucket + ".367", "$KV." + ViewBucket + ".365", "mesh.mod.mesh-issues.event.opened"} {
|
||||||
|
if strings.Contains(v, subject) {
|
||||||
|
violations[subject] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-deadline:
|
||||||
|
t.Fatalf("the server refused %d of the view's 3 writes as permission violations", len(violations))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := tkv.Get("367"); !errors.Is(err, nats.ErrKeyNotFound) {
|
||||||
|
t.Errorf("after the view's put, 367 is %v", err)
|
||||||
|
}
|
||||||
|
if _, err := tkv.Get("365"); err != nil {
|
||||||
|
t.Errorf("after the view's delete, 365 is gone: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
package broker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The view (novox/hq research 036): one read-only user, composed like every other, whose whole
|
||||||
|
// authority is a list here — so a grant that is not on the list fails a test, not a review.
|
||||||
|
|
||||||
|
// Exactly what it hears, exactly what it asks, and nothing it could write or answer. A mutation that
|
||||||
|
// adds a publish grant — `$KV.<bucket>.>`, an event, a tool — fails here.
|
||||||
|
func TestTheViewHearsAndReadsAndCanPublishNothingElse(t *testing.T) {
|
||||||
|
perms, err := PermissionsFor(Principal{Kind: KindView})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
wantSub := append(append([]string(nil), ViewHears...), "_INBOX.view.>")
|
||||||
|
sort.Strings(wantSub)
|
||||||
|
if !reflect.DeepEqual(perms.Subscribe, wantSub) {
|
||||||
|
t.Errorf("the view subscribes\n %v\nand should subscribe exactly\n %v", perms.Subscribe, wantSub)
|
||||||
|
}
|
||||||
|
wantPub := ViewReads()
|
||||||
|
sort.Strings(wantPub)
|
||||||
|
if !reflect.DeepEqual(perms.Publish, wantPub) {
|
||||||
|
t.Errorf("the view publishes\n %v\nand should publish exactly\n %v", perms.Publish, wantPub)
|
||||||
|
}
|
||||||
|
if len(perms.PublishDeny) != 0 {
|
||||||
|
t.Errorf("the view needs no deny, because nothing it may publish reaches the controller's own: %v", perms.PublishDeny)
|
||||||
|
}
|
||||||
|
if perms.AllowResponses {
|
||||||
|
t.Error("the view may answer, and nothing is ever asked of it")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every publish grant is binding the one bucket's stream or reading it directly. **The mutation this
|
||||||
|
// holds against**: a write grant of any shape, and a consumer of any shape — a consumer's deliver
|
||||||
|
// subject is the creator's choice, so creating one is publishing anywhere (ViewReads).
|
||||||
|
stream := "KV_" + ViewBucket
|
||||||
|
for _, p := range perms.Publish {
|
||||||
|
readOnly := p == "$JS.API.STREAM.INFO."+stream ||
|
||||||
|
p == "$JS.API.DIRECT.GET."+stream ||
|
||||||
|
p == "$JS.API.DIRECT.GET."+stream+".>"
|
||||||
|
if !readOnly {
|
||||||
|
t.Errorf("the view is granted a publish on %q, which is not a read of %s", p, ViewBucket)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, refused := range []string{
|
||||||
|
"$KV." + ViewBucket + ".365", // a put or a delete
|
||||||
|
"$KV.mesh-controller_conditions.x", // another bucket
|
||||||
|
"$JS.API.STREAM.CREATE." + stream, // defining the stream
|
||||||
|
"$JS.API.STREAM.PURGE." + stream, // emptying it
|
||||||
|
"$JS.API.STREAM.DELETE." + stream, // deleting it
|
||||||
|
"$JS.API.STREAM.MSG.DELETE." + stream, // deleting a message
|
||||||
|
"$JS.API.CONSUMER.CREATE.KV_mesh-controller_conditions.x", // reading another bucket
|
||||||
|
"$JS.API.CONSUMER.CREATE." + stream + ".w.$KV." + ViewBucket + ".>", // a watch: delivers anywhere
|
||||||
|
"$JS.API.CONSUMER.CREATE." + stream, // an unnamed consumer
|
||||||
|
"$JS.API.CONSUMER.DELETE." + stream + ".a_mesh-issues", // a module's consumer
|
||||||
|
"$JS.FC." + stream + ".x",
|
||||||
|
"$JS.API.DIRECT.GET.KV_mesh-controller_conditions", // another bucket, directly
|
||||||
|
"$JS.API.STREAM.INFO.EVENTS", // the events stream
|
||||||
|
"$JS.API.INFO", // the account
|
||||||
|
"mesh.mod.mesh-issues.event.opened", // claiming the tracker said something
|
||||||
|
"mesh.mod.mesh-issues.tool.open", // opening an issue
|
||||||
|
"mesh.seat.issue-tracker.tool.open", // through the seat
|
||||||
|
"mesh.seat.issue-tracker.tool.open.novox", // on one machine
|
||||||
|
"mesh.seat.mesh-controller.tool.status", // the controller's verbs
|
||||||
|
"mesh.seat.mesh-controller.event.plan-moved",
|
||||||
|
"$SRV.PING",
|
||||||
|
"_INBOX.controller.x",
|
||||||
|
} {
|
||||||
|
if MayPublish(perms, refused) {
|
||||||
|
t.Errorf("the view may publish %q", refused)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, refused := range []string{
|
||||||
|
"mesh.mod.mesh-issues.tool.open", // a tool asked of the tracker
|
||||||
|
"mesh.mod.telegram.event.received", // another module's events
|
||||||
|
"mesh.seat.mesh-controller.event.applied",
|
||||||
|
"mesh.control.novox.report",
|
||||||
|
"_INBOX.controller.x",
|
||||||
|
"_INBOX.person.jochen.x",
|
||||||
|
"_DELIVER.controller.EVENTS",
|
||||||
|
} {
|
||||||
|
if MaySubscribe(perms, refused) {
|
||||||
|
t.Errorf("the view may subscribe %q", refused)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, heard := range []string{
|
||||||
|
"mesh.mod.mesh-issues.event.opened",
|
||||||
|
"mesh.mod.mesh-issues.event.moved",
|
||||||
|
"mesh.mod.mesh-issues.event.noted",
|
||||||
|
"mesh.mod.mesh-issues.event.linked",
|
||||||
|
"mesh.seat.mesh-controller.event.plan-moved",
|
||||||
|
"mesh.seat.mesh-controller.event.condition-raised",
|
||||||
|
"mesh.seat.mesh-controller.event.condition-changed",
|
||||||
|
"mesh.seat.mesh-controller.event.condition-cleared",
|
||||||
|
"mesh.mod.mesh-delivery.event.transition",
|
||||||
|
"mesh.mod.mesh-delivery.event.group",
|
||||||
|
"_INBOX.view.abc",
|
||||||
|
} {
|
||||||
|
if !MaySubscribe(perms, heard) {
|
||||||
|
t.Errorf("the view cannot subscribe %q", heard)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Composed once the mesh minted its credential, and not before: its row is the whole record of it.
|
||||||
|
func TestTheViewIsComposedOnlyOnceItsCredentialIsMinted(t *testing.T) {
|
||||||
|
without, err := Users(Records{Nodes: []string{"anchor"}})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, p := range without {
|
||||||
|
if p.Kind == KindView {
|
||||||
|
t.Fatal("the view is composed before its credential was minted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
with, err := Users(Records{Nodes: []string{"anchor"}, View: true})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
views := 0
|
||||||
|
for _, p := range with {
|
||||||
|
if p.Kind == KindView {
|
||||||
|
views++
|
||||||
|
if p.Username() != ViewUser {
|
||||||
|
t.Errorf("the view is called %q, and its row is %q", p.Username(), ViewUser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if views != 1 {
|
||||||
|
t.Fatalf("%d view users composed; there is one view", views)
|
||||||
|
}
|
||||||
|
// And without its hash it is named as missing, like any user — never written as a user anybody is.
|
||||||
|
_, missing := WithPasswords(with, map[string]string{})
|
||||||
|
found := false
|
||||||
|
for _, m := range missing {
|
||||||
|
found = found || m == ViewUser
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Error("a view with no password was not named as missing one")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -231,17 +231,32 @@ var ControllerVerbs = []Verb{
|
|||||||
"cause": "with why: the cause in a word, the word a second rotation for the same reason uses (optional)",
|
"cause": "with why: the cause in a word, the word a second rotation for the same reason uses (optional)",
|
||||||
}, nil)},
|
}, nil)},
|
||||||
{Name: "give", Description: "Take a module's own secret from the operator at their desk (novox/hq ADR 0259 " +
|
{Name: "give", Description: "Take a module's own secret from the operator at their desk (novox/hq ADR 0259 " +
|
||||||
"§10): a prompt that does not show what is typed opens on the machine named by at, its answer comes " +
|
"§10): the same as secret-ask with the desk named. A prompt that does not show what is typed opens on " +
|
||||||
"back sealed to this call alone, and is sealed to the module's machine as `secret accept` seals it. " +
|
"the machine named by at, its answer comes back sealed to this call alone, and is sealed to the " +
|
||||||
"The value is never an argument and never in the answer: the answer says it was taken, or why not. " +
|
"module's machine as `secret accept` seals it. The value is never an argument and never in the answer: " +
|
||||||
"Recorded in the hand-act log as a value given at the desk. The prompt waits 25 seconds; dismissed " +
|
"the answer says it was taken, or why not. Recorded in the hand-act log as a value given at the desk. " +
|
||||||
"or unanswered, nothing changes. Then push the machine.",
|
"The prompt waits 25 seconds; dismissed or unanswered, nothing changes. Then push the machine.",
|
||||||
Input: schema(map[string]string{
|
Input: schema(map[string]string{
|
||||||
"node": "the machine the module runs on, which the secret is sealed to",
|
"node": "the machine the module runs on, which the secret is sealed to",
|
||||||
"module": "the module's name",
|
"module": "the module's name",
|
||||||
"secret": "the own secret's name in the module's definition",
|
"secret": "the own secret's name in the module's definition",
|
||||||
"at": "the machine the operator sits at, where the prompt opens",
|
"at": "the machine the operator sits at, where the prompt opens",
|
||||||
}, []string{"node", "module", "secret", "at"})},
|
}, []string{"node", "module", "secret", "at"})},
|
||||||
|
{Name: "secret-ask", Description: "Ask the operator for a module's own secret (novox/hq ADR 0277): a prompt " +
|
||||||
|
"that shows nothing of what is typed opens at the desk — the module's machine, or the one at names — " +
|
||||||
|
"naming the module, the secret and who asked; the operator types the value there, never on a channel " +
|
||||||
|
"and never in a verb's argument; the answer comes back sealed to this call alone and is sealed to the " +
|
||||||
|
"module's machine as `secret accept` seals it. The answer says the value was taken, or why not. " +
|
||||||
|
"Refused for a secret the mesh issues itself (the bus account, one the mesh may make) and for a module " +
|
||||||
|
"that runs as an account of its own, whose value is typed at the controller's terminal. Bounded: one " +
|
||||||
|
"open prompt per secret, three asks an hour. Recorded in the hand-act log, with who asked, and " +
|
||||||
|
"announced on every channel. Then push the machine.",
|
||||||
|
Input: schema(map[string]string{
|
||||||
|
"node": "the machine the module runs on, which the secret is sealed to",
|
||||||
|
"module": "the module's name",
|
||||||
|
"secret": "the own secret's name in the module's definition",
|
||||||
|
"at": "the machine the operator sits at, where the prompt opens; the module's machine when absent",
|
||||||
|
}, []string{"node", "module", "secret"})},
|
||||||
{Name: "issue", Description: "Give a module on a machine its account on the bus: minted, and sealed to the " +
|
{Name: "issue", Description: "Give a module on a machine its account on the bus: minted, and sealed to the " +
|
||||||
"machine as the module's own secret named broker, read at the next push of that machine. For a module " +
|
"machine as the module's own secret named broker, read at the next push of that machine. For a module " +
|
||||||
"whose definition declares that secret; refused with the reason otherwise. Issued again, it replaces the account.",
|
"whose definition declares that secret; refused with the reason otherwise. Issued again, it replaces the account.",
|
||||||
|
|||||||
@@ -108,6 +108,15 @@ func (i *Inventory) BusRecords(ctx context.Context) (broker.Records, error) {
|
|||||||
for _, p := range people {
|
for _, p := range people {
|
||||||
out.People[p.Name] = p.Invokes
|
out.People[p.Name] = p.Invokes
|
||||||
}
|
}
|
||||||
|
// The view is composed once its credential is minted and until it is forgotten: its row is the
|
||||||
|
// record of it, nothing else being declared about it (broker.KindView).
|
||||||
|
kept, err := i.BusUsers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return broker.Records{}, err
|
||||||
|
}
|
||||||
|
if u, minted := kept[broker.ViewUser]; minted && u.Kind == BusView {
|
||||||
|
out.View = true
|
||||||
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,9 @@ const (
|
|||||||
// BusNodeTools is a machine's tool runtime (novox/hq ADR 0175): named like the module it
|
// BusNodeTools is a machine's tool runtime (novox/hq ADR 0175): named like the module it
|
||||||
// stands for, recorded as what it is.
|
// stands for, recorded as what it is.
|
||||||
BusNodeTools = "node-tools"
|
BusNodeTools = "node-tools"
|
||||||
|
// BusView is the one read-only view onto the bus (broker.KindView): its row is the whole record of
|
||||||
|
// it, minted by `bus view-credential` and forgotten by `bus view-revoke`.
|
||||||
|
BusView = "view"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MintBusPassword makes a bus password and records its hash under a username, replacing whatever was
|
// MintBusPassword makes a bus password and records its hash under a username, replacing whatever was
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
-- A module's own secret asked for at a desk (novox/hq ADR 0277).
|
||||||
|
--
|
||||||
|
-- Every ask for a module's own secret at a desk: who asked, for which secret of which module on which
|
||||||
|
-- machine, at which desk, and how it ended. Read before a prompt opens, so that one open ask per secret and
|
||||||
|
-- few per hour hold: an agent that keeps a prompt in front of the operator until they type is refused.
|
||||||
|
create table secret_ask (
|
||||||
|
id bigserial primary key,
|
||||||
|
node uuid not null references node(id) on delete cascade,
|
||||||
|
module text not null,
|
||||||
|
name text not null,
|
||||||
|
asked_by text not null,
|
||||||
|
desk text not null,
|
||||||
|
opened_at timestamptz not null default now(),
|
||||||
|
ended_at timestamptz,
|
||||||
|
-- given · dismissed · timed-out · empty · refused · failed
|
||||||
|
outcome text
|
||||||
|
);
|
||||||
|
create index secret_ask_by_secret on secret_ask (node, module, name, opened_at desc);
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package inventory
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// An ask for a module's own secret at a desk (novox/hq ADR 0277, migration 0091): every one is recorded
|
||||||
|
// before the prompt opens, and the bounds are read from the record. A verb may ask the operator to type a
|
||||||
|
// secret; it may not keep a prompt in front of them. **One open ask per secret, and few per hour.**
|
||||||
|
|
||||||
|
// The bounds of asking for one secret.
|
||||||
|
const (
|
||||||
|
// SecretAskOpenFor is how long an ask that has not ended counts as open: longer than any prompt waits,
|
||||||
|
// so a process that died with its prompt does not hold the secret for ever.
|
||||||
|
SecretAskOpenFor = 2 * time.Minute
|
||||||
|
// SecretAsksPerHour is how many asks for one secret an hour takes.
|
||||||
|
SecretAsksPerHour = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenSecretAsk records that an ask for a module's own secret on a machine opens at a desk, or refuses it in
|
||||||
|
// words when one is still open for that secret or the hour's asks are spent. It answers the record's id, which
|
||||||
|
// EndSecretAsk closes.
|
||||||
|
func (i *Inventory) OpenSecretAsk(ctx context.Context, node, module, name, askedBy, desk string) (int64, error) {
|
||||||
|
record, err := i.NodeByName(ctx, node)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
tx, err := i.store.Pool().Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }()
|
||||||
|
// Serialised per secret, so two asks at once do not both pass the count.
|
||||||
|
if _, err := tx.Exec(ctx, `select pg_advisory_xact_lock(hashtext($1))`, node+"/"+module+"/"+name); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
var open int
|
||||||
|
var openBy string
|
||||||
|
if err := tx.QueryRow(ctx,
|
||||||
|
`select count(*), coalesce(min(asked_by), '') from secret_ask
|
||||||
|
where node = $1 and module = $2 and name = $3 and ended_at is null and opened_at > now() - $4::interval`,
|
||||||
|
record.ID, module, name, SecretAskOpenFor.String()).Scan(&open, &openBy); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if open > 0 {
|
||||||
|
return 0, fmt.Errorf("an ask for %s of %s on %s is still open (asked by %s): one prompt at a time for a secret, "+
|
||||||
|
"and this one ends within %s", name, module, node, openBy, SecretAskOpenFor)
|
||||||
|
}
|
||||||
|
var lastHour int
|
||||||
|
if err := tx.QueryRow(ctx,
|
||||||
|
`select count(*) from secret_ask
|
||||||
|
where node = $1 and module = $2 and name = $3 and opened_at > now() - interval '1 hour'`,
|
||||||
|
record.ID, module, name).Scan(&lastHour); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if lastHour >= SecretAsksPerHour {
|
||||||
|
return 0, fmt.Errorf("%s of %s on %s was asked for %d times in the last hour, and an hour takes %d asks for one "+
|
||||||
|
"secret: the operator is not kept at a prompt", name, module, node, lastHour, SecretAsksPerHour)
|
||||||
|
}
|
||||||
|
var id int64
|
||||||
|
if err := tx.QueryRow(ctx,
|
||||||
|
`insert into secret_ask (node, module, name, asked_by, desk) values ($1, $2, $3, $4, $5) returning id`,
|
||||||
|
record.ID, module, name, askedBy, desk).Scan(&id); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return id, tx.Commit(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndSecretAsk closes an ask with how it ended: given, dismissed, timed-out, empty, refused or failed.
|
||||||
|
func (i *Inventory) EndSecretAsk(ctx context.Context, id int64, outcome string) error {
|
||||||
|
_, err := i.store.Pool().Exec(ctx,
|
||||||
|
`update secret_ask set ended_at = now(), outcome = $2 where id = $1 and ended_at is null`, id, outcome)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SecretAsk is one recorded ask for a module's own secret.
|
||||||
|
type SecretAsk struct {
|
||||||
|
ID int64
|
||||||
|
Node string
|
||||||
|
Module string
|
||||||
|
Name string
|
||||||
|
AskedBy string
|
||||||
|
Desk string
|
||||||
|
OpenedAt time.Time
|
||||||
|
EndedAt *time.Time
|
||||||
|
Outcome string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SecretAsks is every ask for secrets since a moment, newest first.
|
||||||
|
func (i *Inventory) SecretAsks(ctx context.Context, since time.Time) ([]SecretAsk, error) {
|
||||||
|
rows, err := i.store.Pool().Query(ctx,
|
||||||
|
`select a.id, n.name, a.module, a.name, a.asked_by, a.desk, a.opened_at, a.ended_at, coalesce(a.outcome, '')
|
||||||
|
from secret_ask a join node n on n.id = a.node
|
||||||
|
where a.opened_at >= $1 order by a.opened_at desc`, since)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []SecretAsk
|
||||||
|
for rows.Next() {
|
||||||
|
var a SecretAsk
|
||||||
|
if err := rows.Scan(&a.ID, &a.Node, &a.Module, &a.Name, &a.AskedBy, &a.Desk, &a.OpenedAt, &a.EndedAt, &a.Outcome); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, a)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package inventory
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// An ask for a module's own secret at a desk is bounded by the record (novox/hq ADR 0277): one open per secret,
|
||||||
|
// three an hour, and every one says who asked and how it ended.
|
||||||
|
func TestASecretAskIsOneAtATimeAndFewAnHour(t *testing.T) {
|
||||||
|
inv := ForTest(t)
|
||||||
|
ctx := t.Context()
|
||||||
|
if _, err := inv.AddNode(ctx, "shanks"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
first, err := inv.OpenSecretAsk(ctx, "shanks", "mounts", "smb-credentials", "g14/claude-code", "shanks")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := inv.OpenSecretAsk(ctx, "shanks", "mounts", "smb-credentials", "laptop/agent", "shanks"); err == nil ||
|
||||||
|
!strings.Contains(err.Error(), "still open") || !strings.Contains(err.Error(), "g14/claude-code") {
|
||||||
|
t.Errorf("a second ask while one is open: %v", err)
|
||||||
|
}
|
||||||
|
// Another secret is its own.
|
||||||
|
other, err := inv.OpenSecretAsk(ctx, "shanks", "mounts", "other", "laptop/agent", "shanks")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := inv.EndSecretAsk(ctx, other, "dismissed"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := inv.EndSecretAsk(ctx, first, "given"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for i := 0; i < SecretAsksPerHour-1; i++ {
|
||||||
|
id, err := inv.OpenSecretAsk(ctx, "shanks", "mounts", "smb-credentials", "g14/claude-code", "shanks")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ask %d: %v", i+2, err)
|
||||||
|
}
|
||||||
|
if err := inv.EndSecretAsk(ctx, id, "timed-out"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := inv.OpenSecretAsk(ctx, "shanks", "mounts", "smb-credentials", "g14/claude-code", "shanks"); err == nil ||
|
||||||
|
!strings.Contains(err.Error(), "times in the last hour") {
|
||||||
|
t.Errorf("a fourth ask in an hour: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := inv.OpenSecretAsk(ctx, "nowhere", "mounts", "smb-credentials", "x", "nowhere"); err == nil {
|
||||||
|
t.Error("a machine the mesh does not know was taken")
|
||||||
|
}
|
||||||
|
asks, err := inv.SecretAsks(ctx, time.Now().Add(-time.Hour))
|
||||||
|
if err != nil || len(asks) != SecretAsksPerHour+1 {
|
||||||
|
t.Fatalf("%d asks, %v", len(asks), err)
|
||||||
|
}
|
||||||
|
if a := asks[len(asks)-1]; a.Node != "shanks" || a.Module != "mounts" || a.Name != "smb-credentials" || a.AskedBy != "g14/claude-code" ||
|
||||||
|
a.Outcome != "given" || a.EndedAt == nil {
|
||||||
|
t.Errorf("the first ask: %+v", a)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -76,6 +76,7 @@
|
|||||||
"hand-act",
|
"hand-act",
|
||||||
"drill",
|
"drill",
|
||||||
"warranted",
|
"warranted",
|
||||||
|
"secret-ask",
|
||||||
"hand-acts",
|
"hand-acts",
|
||||||
"durations",
|
"durations",
|
||||||
"conditions",
|
"conditions",
|
||||||
|
|||||||
Reference in New Issue
Block a user