C4: the forge module's tokens live in the vault, one process mints at a time

Two processes that minted at once left the forge two tokens by one name, and a token kept in a file in the
runtime state is a plain state. Both accounts' tokens (mesh-tools, mesh-packages) are now the module's own
secrets in the vault, written through the seat mesh-vault's own-put and read through its get, sealed to the
reading process's key. A refused or expired (token-ttl) token is minted again only by the holder of the lease
mint in the state token-lease (mint-lease); each mint is named <account>-<generation>, and the token it
replaces is deleted by id only after own-put landed. The Go bundles read their tokens through get at start
and at secret-changed; the MESH_GITEA_*_TOKEN_FILE words go. forge_token_lease reads the lease and each
token's age, never the token.

Builds: design 47 H2 §808, §822; H-A-407, H-A-422, H-A-426, H-A-430 (1)
This commit is contained in:
2026-11-22 01:41:30 +01:00
parent 9afbd35759
commit 39ed20c5bc
23 changed files with 1078 additions and 539 deletions
+6 -4
View File
@@ -100,7 +100,8 @@ search that reaches the call's deadline stops there and answers what it found, w
It reaches the forge through the same HTTP client as the package-registry bundle (`internal/forgehttp`), as the
tools' own account (novox/hq ADR 0365 §2, issue 469): the token the TypeScript bundle mints for `mesh-tools` and
keeps (`MESH_GITEA_TOKEN_FILE`), sent only when its scopes line names that account, never the admin login. One tool
keeps in the vault as the module's own (`module/gitea/mesh-tools`, design 47 H2 §808), read through the vault seat's
`get` at start and at each `secret-changed` naming it, never the admin login. One tool
call takes at most 50 seconds.
`go test ./cmd/forge` runs it against a fake forge: the files of one commit created, updated and deleted on
a new branch from its base; an existing branch committed on; the default branch, a protected branch and
@@ -247,9 +248,10 @@ version already gone counts as deleted.
Over the forge's HTTP interface on the machine (`MESH_GITEA_URL`), never as the admin account (design 51: each
holder keeps its write side as an own secret beside a read-only tool token). It **reads** as the tools' account,
with the token the TypeScript bundle keeps for it (`MESH_GITEA_TOKEN_FILE`, scopes including `read:package`), and
**writes** (a publish, a remove, retention's deletion) as the packages account `mesh-packages`
(`MESH_GITEA_PACKAGES_USER`), with its own token (`MESH_GITEA_PACKAGES_TOKEN_FILE`, the one scope `write:package`).
with the token the TypeScript bundle keeps for it in the vault (`module/gitea/mesh-tools`, scopes including
`read:package`), and **writes** (a publish, a remove, retention's deletion) as the packages account `mesh-packages`
(`MESH_GITEA_PACKAGES_USER`), with its own token (`module/gitea/mesh-packages`, the one scope `write:package`), both
read through the vault seat's `get` at start and at each `secret-changed` (design 47 H2 §808).
That account is plain, a member of the team `mesh-packages` that writes the organisation's packages and nothing else;
the watcher makes it with the administrator's login and mints and keeps its token at its start (`packages-account.ts`).
No timer looks at it after that: when the forge refuses a write (401) this bundle raises the finding
+38 -22
View File
@@ -1,35 +1,52 @@
package main
// The commit and the reads act as the tools' own account (novox/hq ADR 0365 §2, issue 469): the token the module
// keeps for `mesh-tools`, never the admin login. That account is no repository's administrator, so the forge
// keeps for `mesh-tools` in the vault (design 47 H2 §808), never the admin login. That account is no repository's administrator, so the forge
// refuses it the protection rules (403, as gitea's reqAdmin does); the commit then judges an existing branch by the
// protection the forge says it applies, and leaves a branch not made yet to the forge's own judgement.
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/seal"
"gitea/internal/forgehttp"
)
const toolsToken = "tools-token-never-shown"
// asTools serves the fake forge to a bundle holding the tools' token: only that token is taken, the rules are
// refused as to a non-administrator, and the branches named protected say so as the forge does.
func asTools(t *testing.T, f *fakeForge, protected map[string]string, scopes string) (*Forge, *[]string) {
dir := t.TempDir()
file := filepath.Join(dir, "token")
if err := os.WriteFile(file, []byte(toolsToken+"\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(file+".scopes", []byte(scopes+"\n"), 0o600); err != nil {
// vaultHolding is the vault seat's get answering account's token sealed to the key it is asked with; "" holds none.
func vaultHolding(t *testing.T, account, token string) *forgehttp.VaultToken {
t.Helper()
v, err := forgehttp.NewVaultToken(account, func(address string, args any) (json.RawMessage, error) {
a := args.(map[string]any)
if address != forgehttp.VaultGet || a["name"] != forgehttp.VaultName(account) {
return nil, fmt.Errorf("asked %s for %v", address, a["name"])
}
if token == "" {
return nil, fmt.Errorf("the vault holds no secret named %s", a["name"])
}
box, err := seal.Seal(token, a["public_key"].(string))
if err != nil {
return nil, err
}
return json.Marshal(map[string]any{"name": a["name"], "generation": 1, "sealed": box})
})
if err != nil {
t.Fatal(err)
}
return v
}
// asTools serves the fake forge to a bundle holding the token the vault hands: only the tools' token is taken, the
// rules are refused as to a non-administrator, and the branches named protected say so as the forge does.
func asTools(t *testing.T, f *fakeForge, protected map[string]string, held string) (*Forge, *[]string) {
var seen []string
const repo = "/api/v1/repos/acme/app"
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -52,12 +69,12 @@ func asTools(t *testing.T, f *fakeForge, protected map[string]string, scopes str
f.handle(w, r)
}))
t.Cleanup(srv.Close)
return &Forge{Client: forgehttp.Client{URL: srv.URL, TokenFile: file, Account: "mesh-tools"}}, &seen
return &Forge{Client: forgehttp.Client{URL: srv.URL, Token: vaultHolding(t, "mesh-tools", held), Account: "mesh-tools"}}, &seen
}
func TestTheToolsAccountCommitsOnANewBranchWithItsTokenThoughTheRulesAreNotItsToRead(t *testing.T) {
f := newFake(t)
forge, _ := asTools(t, f, nil, "read:user,write:issue,write:repository for mesh-tools")
forge, _ := asTools(t, f, nil, toolsToken)
got, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": "fix/365",
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
if err != nil {
@@ -75,7 +92,7 @@ func TestTheToolsAccountIsRefusedAProtectedBranchTheForgeSaysItGuardsAndTheDefau
if rule != "" {
protected[branch] = rule
}
forge, _ := asTools(t, f, protected, "read:user,write:issue,write:repository for mesh-tools")
forge, _ := asTools(t, f, protected, toolsToken)
_, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": branch,
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
if err == nil || !strings.Contains(err.Error(), "refused") || !strings.Contains(err.Error(), "ADR 0266") {
@@ -90,13 +107,13 @@ func TestTheToolsAccountIsRefusedAProtectedBranchTheForgeSaysItGuardsAndTheDefau
}
}
func TestATokenKeptForTheAdministratorIsNeverSent(t *testing.T) {
func TestNoTokenIsSentWhileTheVaultHoldsNoneForTheAccount(t *testing.T) {
f := newFake(t)
// What the module kept before ADR 0365: the admin's token, its scopes line naming no account.
forge, seen := asTools(t, f, nil, "read:user,write:issue,write:repository")
// Design 47 H2 §808: the token is read through the vault seat's get; while the module has minted none, nothing goes.
forge, seen := asTools(t, f, nil, "")
_, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": "fix/365",
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
if err == nil || !strings.Contains(err.Error(), "not mesh-tools's") {
if err == nil || !strings.Contains(err.Error(), "not kept yet") {
t.Fatalf("%v", err)
}
if len(*seen) != 0 {
@@ -104,9 +121,8 @@ func TestATokenKeptForTheAdministratorIsNeverSent(t *testing.T) {
}
}
func TestTheBundleHandedATokenFileNeedsNoAdminLogin(t *testing.T) {
func TestTheBundleReadsItsTokenFromTheVaultAndNeedsNoAdminLogin(t *testing.T) {
t.Setenv("MESH_GITEA_URL", "http://127.0.0.1:3000")
t.Setenv("MESH_GITEA_TOKEN_FILE", "/state/token")
t.Setenv("MESH_GITEA_TOOLS_USER", "")
t.Setenv("MESH_GITEA_ADMIN_USER", "")
t.Setenv("MESH_GITEA_ADMIN_PASSWORD_FILE", "")
@@ -114,7 +130,7 @@ func TestTheBundleHandedATokenFileNeedsNoAdminLogin(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if f.TokenFile != "/state/token" || f.Account != "mesh-tools" || f.User != "" || f.PasswordFile != "" {
if f.Token == nil || f.Token.Account != "mesh-tools" || f.Account != "mesh-tools" || f.User != "" || f.PasswordFile != "" {
t.Fatalf("%+v", f.Client)
}
}
+4 -1
View File
@@ -21,7 +21,10 @@ type Forge struct {
// ForgeFromEnv is the forge as the manifest hands it to this bundle.
func ForgeFromEnv() (*Forge, error) {
c, missing := forgehttp.FromEnv()
c, missing, err := forgehttp.ToolsFromEnv()
if err != nil {
return nil, err
}
if len(missing) > 0 {
return nil, fmt.Errorf("the forge cannot be reached: %s not set by the manifest", strings.Join(missing, ", "))
}
+8 -2
View File
@@ -691,8 +691,8 @@ func TestToolsAreTheOnesTheReadmeNamesAndTheManifestBuilds(t *testing.T) {
// The tools' own account, never the admin login (novox/hq ADR 0365 §2); the package registry's bundle also writes
// as the packages account (design 51: the write side an own secret beside a read-only tool token).
wants := map[string][]string{
"cmd/forge": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_TOKEN_FILE"},
"cmd/package-registry": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_USER", "MESH_GITEA_PACKAGES_TOKEN_FILE"},
"cmd/forge": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER"},
"cmd/package-registry": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_PACKAGES_USER"},
}
for _, a := range m.Build.Artifacts {
want, ok := wants[a.From]
@@ -713,6 +713,12 @@ func TestToolsAreTheOnesTheReadmeNamesAndTheManifestBuilds(t *testing.T) {
t.Errorf("%s is given %s: it acts as an account of its own, not the administrator", a.From, k)
}
}
// Design 47 H2 §808, H-A-422 (1): the token is read through the vault seat's get, never from a file.
for _, k := range []string{"MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_TOKEN_FILE"} {
if a.Env[k] != "" {
t.Errorf("%s is given %s: its token is read from the vault", a.From, k)
}
}
}
for from := range wants {
t.Errorf("the manifest builds no %s bundle", from)
+12
View File
@@ -14,6 +14,8 @@ import (
"strings"
stdio "git.novox.be/novox/mesh-sdk/go"
"gitea/internal/forgehttp"
)
// ToolNames are the tools this bundle serves. The module's manifest lists no `tools` (see the package-registry
@@ -21,6 +23,16 @@ import (
var ToolNames = []string{"gitea_commit", "gitea_commits", "gitea_compare", "gitea_pull_reviews", "gitea_list_dir", "gitea_search_code"}
func main() {
// The tools' account's token, read from the vault at start and at each secret-changed naming it (design 47 H2 §808).
go func() {
account := os.Getenv("MESH_GITEA_TOOLS_USER")
if account == "" {
account = forgehttp.ToolsAccount
}
if err := forgehttp.Follow(func(f string, a ...any) { fmt.Fprintf(os.Stderr, f+"\n", a...) }, account); err != nil {
fmt.Fprintln(os.Stderr, "the vault's changes of the forge token are not heard:", err)
}
}()
if err := stdio.Serve("", Tools(ForgeFromEnv)); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
+9 -3
View File
@@ -78,8 +78,14 @@ func (f *Forge) As(typ string) *Forge {
// ForgeFromEnv is the forge as the manifest hands it to this bundle.
func ForgeFromEnv() (*Forge, error) {
c, missing := forgehttp.FromEnv()
w, writeMissing := forgehttp.WriterFromEnv()
c, missing, err := forgehttp.ToolsFromEnv()
if err != nil {
return nil, err
}
w, writeMissing, err := forgehttp.WriterFromEnv()
if err != nil {
return nil, err
}
for _, m := range writeMissing {
if m != "MESH_GITEA_URL" {
missing = append(missing, m)
@@ -168,7 +174,7 @@ func (f *Forge) DistTags(ctx context.Context, name string) (map[string]string, e
// write is the registry's write side, refused when the manifest handed none.
func (f *Forge) write() (*forgehttp.Client, error) {
if f.Writer == nil {
return nil, errors.New("the package registry's write side is not handed to this bundle (MESH_GITEA_PACKAGES_TOKEN_FILE); nothing was written")
return nil, errors.New("the package registry's write side is not handed to this bundle (the packages account's token); nothing was written")
}
return f.Writer, nil
}
@@ -17,6 +17,7 @@ import (
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/views"
"gitea/internal/forgehttp"
"gitea/internal/packageseats"
)
@@ -49,6 +50,19 @@ func main() {
os.Exit(1)
}
}()
// Both accounts' tokens, read from the vault at start and at each secret-changed naming one (design 47 H2 §808).
go func() {
tools, packages := os.Getenv("MESH_GITEA_TOOLS_USER"), os.Getenv("MESH_GITEA_PACKAGES_USER")
if tools == "" {
tools = forgehttp.ToolsAccount
}
if packages == "" {
packages = forgehttp.PackagesAccount
}
if err := forgehttp.Follow(func(f string, a ...any) { fmt.Fprintf(os.Stderr, f+"\n", a...) }, tools, packages); err != nil {
fmt.Fprintln(os.Stderr, "the vault's changes of the forge tokens are not heard:", err)
}
}()
forge := func() (*Forge, error) { return ForgeFromEnv() }
holder := func() (*packageseats.Holder, error) { return busHolder(forge) }
go followIdle(holder)
@@ -496,7 +496,8 @@ func TestToolsAreTheOnesTheReadmeNamesAndSayWhatTheyDo(t *testing.T) {
t.Errorf("transfers has options (a lifetime): %s", req)
}
// The retention asks the builder's versions; the forge's other bundles ask the catalog half and record a re-read.
if strings.Join(m.Invokes, ",") != "seat:mesh-catalog.revision,seat:mesh-catalog.registrations,seat:mesh-hand-acts.hand-act,seat:mesh-builds.reserved" {
// The forge module's tokens through the vault seat: own-put by the minter, get by every process (design 47 H2 §808).
if strings.Join(m.Invokes, ",") != "seat:mesh-catalog.revision,seat:mesh-catalog.registrations,seat:mesh-hand-acts.hand-act,seat:mesh-builds.reserved,seat:mesh-vault.own-put,seat:mesh-vault.get" {
t.Errorf("the manifest invokes %v", m.Invokes)
}
if len(m.Tools) != 0 {
@@ -506,7 +507,7 @@ func TestToolsAreTheOnesTheReadmeNamesAndSayWhatTheyDo(t *testing.T) {
for _, a := range m.Build.Artifacts {
if a.From == "cmd/package-registry" {
found = true
for _, k := range []string{"MESH_GITEA_URL", "MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_TOKEN_FILE", "MESH_NPM_OWNER"} {
for _, k := range []string{"MESH_GITEA_URL", "MESH_NPM_OWNER"} {
if a.Env[k] == "" {
t.Errorf("the bundle is not given %s", k)
}
@@ -9,17 +9,17 @@ import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
keyparts "git.novox.be/novox/mesh-sdk/go/keys"
"git.novox.be/novox/mesh-sdk/go/seal"
"git.novox.be/novox/mesh-sdk/go/views"
"gitea/internal/forgehttp"
@@ -116,15 +116,10 @@ func TestThePackageSeatsStoreIntoTheForge(t *testing.T) {
g := &registry{uploaded: map[string][]byte{}, held: map[string][]byte{}}
srv := httptest.NewServer(http.HandlerFunc(g.handle))
defer srv.Close()
dir := t.TempDir()
kept := func(name, token, line string) string {
file := filepath.Join(dir, name)
_ = os.WriteFile(file, []byte(token+"\n"), 0o600)
_ = os.WriteFile(file+".scopes", []byte(line+"\n"), 0o600)
return file
}
reader := forgehttp.Client{URL: srv.URL, TokenFile: kept("token", "reader", "read:package,read:user for mesh-tools"), Account: "mesh-tools"}
writer := forgehttp.Client{URL: srv.URL, TokenFile: kept("packages-token", "writer", "write:package for mesh-packages"), Account: "mesh-packages"}
vault := map[string]string{"mesh-tools": "reader", "mesh-packages": "writer"}
writerToken := vaultHolding(t, "mesh-packages", vault)
reader := forgehttp.Client{URL: srv.URL, Token: vaultHolding(t, "mesh-tools", vault), Account: "mesh-tools"}
writer := forgehttp.Client{URL: srv.URL, Token: writerToken, Account: "mesh-packages"}
forge := &Forge{Client: reader, Writer: &writer, Owner: "acme"}
if _, err := (seatStore{forge: func() (*Forge, error) { return &Forge{Client: reader, Owner: "acme"}, nil }}).Put(packageseats.GoSeat, packageseats.Publish{}, nil); err == nil ||
!strings.Contains(err.Error(), "write side") {
@@ -138,12 +133,22 @@ func TestThePackageSeatsStoreIntoTheForge(t *testing.T) {
if k := views.FindingKey("seat", keyparts.EscapeName(packageseats.Self)+".write", PackagesTokenRefused); k != PackagesTokenRefusedKey {
t.Errorf("the refusal's key is %s, the watcher watches %s", k, PackagesTokenRefusedKey)
}
_ = os.WriteFile(writer.TokenFile, []byte("stale\n"), 0o600)
vault["mesh-packages"] = "stale"
if read, err := writerToken.Changed([]byte(`{"name":"module/gitea/mesh-packages","generation":2}`)); !read || err != nil {
t.Fatalf("the secret-changed of the packages token was not read again: %v %v", read, err)
}
if _, err := s.Put(packageseats.GoSeat, packageseats.Publish{Version: "0.1.15"}, moduleZip(t, "git.example/acme/sdk@v0.1.15")); err == nil ||
len(refused) != 1 || !refused[0] {
t.Errorf("a write the forge refused with 401 raised no finding: %v %v", err, refused)
}
_ = os.WriteFile(writer.TokenFile, []byte("writer\n"), 0o600)
vault["mesh-packages"] = "writer"
// Design 47 H2 §808: a secret-changed naming another secret reads nothing; the one naming this token reads it again.
if read, _ := writerToken.Changed([]byte(`{"name":"module/gitea/mesh-tools","generation":3}`)); read {
t.Error("a secret-changed of the tools' token read the packages token again")
}
if read, err := writerToken.Changed([]byte(`{"name":"module/gitea/mesh-packages","generation":3}`)); !read || err != nil {
t.Fatalf("the secret-changed of the packages token was not read again: %v %v", read, err)
}
tgz := tarball(t, `{"name":"@acme/contracts","version":"0.1.15-feat-x.2","main":"index.js"}`)
p := packageseats.Publish{Name: "@acme/contracts", Version: "0.1.15-feat-x.2", Tag: "feat-x", Kind: packageseats.KindPackage, Digest: "sha256:" + sha(tgz)}
@@ -197,3 +202,28 @@ func TestThePackageSeatsStoreIntoTheForge(t *testing.T) {
t.Errorf("a write the forge took after the refusal did not clear it: %v", refused)
}
}
// vaultHolding is the vault seat's get answering account's token from held, sealed to the key it is asked with; an
// account held has no entry is a secret the vault holds none of.
func vaultHolding(t *testing.T, account string, held map[string]string) *forgehttp.VaultToken {
t.Helper()
v, err := forgehttp.NewVaultToken(account, func(address string, args any) (json.RawMessage, error) {
a := args.(map[string]any)
if address != forgehttp.VaultGet || a["name"] != forgehttp.VaultName(account) {
return nil, fmt.Errorf("asked %s for %v", address, a["name"])
}
token, ok := held[account]
if !ok {
return nil, fmt.Errorf("the vault holds no secret named %s", a["name"])
}
box, err := seal.Seal(token, a["public_key"].(string))
if err != nil {
return nil, err
}
return json.Marshal(map[string]any{"name": a["name"], "generation": 1, "sealed": box})
})
if err != nil {
t.Fatal(err)
}
return v
}
@@ -40,6 +40,7 @@ import (
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"git.novox.be/novox/mesh-sdk/go/seal"
"git.novox.be/novox/mesh-sdk/go/testbus"
"gitea/internal/packageseats"
@@ -65,6 +66,7 @@ type player struct {
answers map[string]chan wire
puts map[string][]byte // the put sessions open, by call
gets map[string][]byte // the get sessions open, by call
vault map[string]string // the vault's secrets the seat's get hands, by name (design 47 H2 §808)
}
func (p *player) send(m map[string]any) {
@@ -130,6 +132,19 @@ func (p *player) handle(m wire) {
break
}
p.answer(m.ID, map[string]any{"module": "m", "version": q.Version, "artifacts": []map[string]any{{"artifact": "m:a", "digest": d}}}, nil)
case m.Method == "mesh/ask" && a.Key == "seat:mesh-vault.get":
var q struct {
Name string `json:"name"`
PublicKey string `json:"public_key"`
}
_ = json.Unmarshal(a.Body, &q)
token, ok := p.vault[q.Name]
if !ok {
p.answer(m.ID, nil, errors.New("the vault holds no secret named "+q.Name))
break
}
box, err := seal.Seal(token, q.PublicKey)
p.answer(m.ID, map[string]any{"name": q.Name, "generation": 1, "sealed": box}, err)
case m.Method == "mesh/seat.publish":
p.answer(m.ID, map[string]any{}, p.nc.Publish(a.Subject, a.Body))
case m.Method == "mesh/capabilities":
@@ -378,17 +393,9 @@ func TestThePackageSeatsWhole(t *testing.T) {
}
}))
t.Cleanup(srv.Close)
dir := t.TempDir()
kept := func(name, token, line string) string {
file := filepath.Join(dir, name)
_ = os.WriteFile(file, []byte(token+"\n"), 0o600)
_ = os.WriteFile(file+".scopes", []byte(line+"\n"), 0o600)
return file
}
cmd := exec.CommandContext(ctx, bin)
cmd.Env = append(os.Environ(), "MESH_NODE=novox", "MESH_GITEA_URL="+srv.URL, "MESH_NPM_OWNER=acme", "MESH_GITEA_TOOLS_USER=mesh-tools",
"MESH_GITEA_TOKEN_FILE="+kept("token", "reader", "read:package,read:user,write:issue,write:repository for mesh-tools"),
"MESH_GITEA_PACKAGES_TOKEN_FILE="+kept("packages-token", "writer", "write:package for mesh-packages"))
"MESH_GITEA_PACKAGES_USER=mesh-packages")
cmd.Stderr = os.Stderr
in, _ := cmd.StdinPipe()
out, _ := cmd.StdoutPipe()
@@ -400,6 +407,7 @@ func TestThePackageSeatsWhole(t *testing.T) {
zipped := bigZip(t, "git.example/acme/sdk@v0.1.15")
digest := "sha256:" + sha(zipped)
p := &player{ctx: ctx, t: t, nc: nc, js: js, in: in, answers: map[string]chan wire{},
vault: map[string]string{"module/gitea/mesh-tools": "reader", "module/gitea/mesh-packages": "writer"},
numbered: map[string]string{"git.example/acme/sdk@0.1.15": digest}}
go p.serve(out)
p.send(map[string]any{"id": "init", "method": "initialize", "params": map[string]any{}})
-4
View File
@@ -36,10 +36,6 @@ func runSync() int {
missing = append(missing, k)
}
}
if forge.TokenFile != "" {
// The tools' account cannot make or protect a repository, and must not (ADR 0428 §8).
missing = append(missing, "the setup login (MESH_GITEA_TOKEN_FILE is set instead)")
}
if len(missing) > 0 {
fmt.Fprintf(os.Stderr, "repositories: %v not set by the manifest; nothing was changed\n", missing)
return 1
+5 -1
View File
@@ -48,6 +48,8 @@ import { Calls, callRetentionDays, sayUnsaid } from "./calls.js";
import { Once, askTakenBoundMs, takenState } from "./taken.js";
import { Partials } from "./partials.js";
import { callTools, mergedTool } from "./tools/calls.js";
import { tokenLeaseTool } from "./tools/tokenlease.js";
import { processVaultAndLease } from "./token.js";
import { seatPublish } from "@novox/mesh-sdk/runtime";
import { seat as GIT, events as GIT_EVENTS, type BranchMovedEvent, type PullClosedEvent, type PullMergedEvent, type PullUpdatedEvent } from "@novox/mesh-delivery-contracts-git";
import { seat as RUNNER, events as RUNNER_EVENTS, type StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner";
@@ -60,7 +62,7 @@ import { announcementOf } from "./updated.js";
import { ReadAll, readAllTools } from "./readall.js";
import { DeliveredWrites, writeTools } from "./writes.js";
import { TOOLS_ACCOUNT, TOOLS_ORG, meshAccounts } from "./tools-account.js";
import { PACKAGES_TOKEN_REFUSED, keepPackagesToken, packagesToken, renewOnRefusal } from "./packages-account.js";
import { PACKAGES_ACCOUNT, PACKAGES_TOKEN_REFUSED, keepPackagesToken, packagesToken, renewOnRefusal } from "./packages-account.js";
// Without a way to a token — configured, or mintable with the admin account (token.ts) — there is
// nothing to watch; log and stay quiet rather than crash the runtime. With one, the first poll mints
@@ -305,6 +307,8 @@ registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll),
watched: () => registrations.watched, calls, say: seatPublish, partials, once }) : []),
...callTools(calls, partials, seatPublish, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }),
...(gitea ? [mergedTool(gitea, calls)] : []),
tokenLeaseTool(processVaultAndLease().vault, processVaultAndLease().lease,
[process.env.MESH_GITEA_TOOLS_USER || TOOLS_ACCOUNT, process.env.MESH_GITEA_PACKAGES_USER || PACKAGES_ACCOUNT]),
...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]);
/** What the forge's event asks of the announcer at once (issue 729, 1422): see watch.ts. */
+40 -60
View File
@@ -3,13 +3,12 @@
//
// Two ways in, by what the manifest hands the bundle:
//
// - **as the tools' account** (novox/hq ADR 0365 §2), when MESH_GITEA_TOKEN_FILE names the token the module
// keeps for it (token.ts mints it, for `mesh-tools`, and keeps it at 0600 with its scopes line beside it). A
// token whose scopes line does not say it is the tools' account's — one kept from before, the administrator's —
// is never sent: the call fails, saying so, until the module has minted the account's own. This bundle mints
// nothing; a token the forge rejects is renewed by the module's next call, and said here.
// - **as the tools' account or the packages account** (novox/hq ADR 0365 §2; design 47 H2 §808), with the account's
// token the module keeps in the vault as its own (token.ts mints it; vault.go reads it through the vault seat's
// `get` at start and at `secret-changed`). This bundle mints nothing; a token the forge rejects is renewed by the
// module's next call, and said here.
// - **as the admin account**, whose password the vault delivered into a file this module owns (novox/hq ADR
// 0086), by basic authentication, when no token file is named.
// 0086), by basic authentication, when no token is given.
//
// A credential is read when a call is made and never logged, answered or put in an error.
package forgehttp
@@ -35,12 +34,12 @@ type Client struct {
URL string // e.g. http://127.0.0.1:3000
User string
PasswordFile string
// TokenFile, when set, is the tools' account's token (MESH_GITEA_TOKEN_FILE), sent instead of the admin login;
// Account the account its scopes line must name (MESH_GITEA_TOOLS_USER, default mesh-tools).
TokenFile string
Account string
HTTP *http.Client // when nil, a client with Timeout
Timeout time.Duration // one request's limit when HTTP is nil; 20 s when zero
// Token, when set, is the account's token as the vault holds it (vault.go), sent instead of the admin login;
// Account the account it is (MESH_GITEA_TOOLS_USER, default mesh-tools; MESH_GITEA_PACKAGES_USER for the writer).
Token *VaultToken
Account string
HTTP *http.Client // when nil, a client with Timeout
Timeout time.Duration // one request's limit when HTTP is nil; 20 s when zero
}
// ToolsAccount is the account the forge's tools act as when the manifest names none (token.ts, tools-account.ts).
@@ -52,37 +51,35 @@ const ToolsAccount = "mesh-tools"
const PackagesAccount = "mesh-packages"
// WriterFromEnv is the package registry's write side as the manifest hands it to a bundle: the packages account's
// token (MESH_GITEA_PACKAGES_TOKEN_FILE, kept by the module beside the tools' token) and the names of the variables
// it left unset. Never the admin login: a bundle handed no writer's token writes nothing.
func WriterFromEnv() (Client, []string) {
c := Client{URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"), TokenFile: os.Getenv("MESH_GITEA_PACKAGES_TOKEN_FILE"),
Account: os.Getenv("MESH_GITEA_PACKAGES_USER")}
if c.Account == "" {
c.Account = PackagesAccount
}
var missing []string
for _, kv := range [][2]string{{"MESH_GITEA_URL", c.URL}, {"MESH_GITEA_PACKAGES_TOKEN_FILE", c.TokenFile}} {
if kv[1] == "" {
missing = append(missing, kv[0])
}
}
return c, missing
// token, read from the vault (vault.go), and the names of the variables it left unset. Never the admin login.
func WriterFromEnv() (Client, []string, error) {
return accountFromEnv(os.Getenv("MESH_GITEA_PACKAGES_USER"), PackagesAccount)
}
// FromEnv is the forge as the manifest hands it to a bundle, and the names of the variables it left unset. A bundle
// handed MESH_GITEA_TOKEN_FILE acts as the tools' account and needs no admin login.
func FromEnv() (Client, []string) {
if file := os.Getenv("MESH_GITEA_TOKEN_FILE"); file != "" {
c := Client{URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"), TokenFile: file,
Account: os.Getenv("MESH_GITEA_TOOLS_USER")}
if c.Account == "" {
c.Account = ToolsAccount
}
if c.URL == "" {
return c, []string{"MESH_GITEA_URL"}
}
return c, nil
// ToolsFromEnv is the forge as the tools' account, its token read from the vault (vault.go), and the names of the
// variables the manifest left unset; it needs no admin login.
func ToolsFromEnv() (Client, []string, error) {
return accountFromEnv(os.Getenv("MESH_GITEA_TOOLS_USER"), ToolsAccount)
}
func accountFromEnv(account, fallback string) (Client, []string, error) {
if account == "" {
account = fallback
}
token, err := TokenOf(account)
if err != nil {
return Client{}, nil, err
}
c := Client{URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"), Token: token, Account: account}
if c.URL == "" {
return c, []string{"MESH_GITEA_URL"}, nil
}
return c, nil, nil
}
// FromEnv is the forge as the admin account, as the manifest hands it to a bundle, and the names of the variables it
// left unset.
func FromEnv() (Client, []string) {
c := Client{
URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"),
User: os.Getenv("MESH_GITEA_ADMIN_USER"),
@@ -132,27 +129,10 @@ func (c *Client) password() (string, error) {
return strings.TrimSpace(string(raw)), nil
}
// token is the tools' account's token, refused unless its scopes line names that account.
func (c *Client) token() (string, error) {
raw, err := os.ReadFile(c.TokenFile)
if err != nil {
return "", fmt.Errorf("the token for %s is not kept yet (the module mints it): %w", c.Account, err)
}
line, err := os.ReadFile(c.TokenFile + ".scopes")
if err != nil || !strings.HasSuffix(strings.TrimSpace(string(line)), " for "+c.Account) {
return "", fmt.Errorf("the token kept at %s is not %s's (novox/hq ADR 0365): the module mints that account's own at its next call; nothing was sent", c.TokenFile, c.Account)
}
token := strings.TrimSpace(string(raw))
if token == "" {
return "", fmt.Errorf("the token kept at %s is empty; nothing was sent", c.TokenFile)
}
return token, nil
}
// authorize puts the bundle's credential on a request: the tools' token, or the admin login.
func (c *Client) authorize(req *http.Request) error {
if c.TokenFile != "" {
token, err := c.token()
if c.Token != nil {
token, err := c.Token.Token()
if err != nil {
return err
}
@@ -212,7 +192,7 @@ func (c *Client) Send(ctx context.Context, method, path string, raw []byte, cont
return nil, fmt.Errorf("the forge's answer to %s %s is larger than %d MiB and was not read whole", method, path, limit>>20)
}
switch {
case res.StatusCode == http.StatusUnauthorized && c.TokenFile != "":
case res.StatusCode == http.StatusUnauthorized && c.Token != nil:
return nil, fmt.Errorf("the forge rejected %s's token for %s (401): the module mints it afresh; nothing was done: %w",
c.Account, method+" "+path, &StatusError{method, path, res.StatusCode, "unauthorized"})
case res.StatusCode == http.StatusNotFound:
+166
View File
@@ -0,0 +1,166 @@
package forgehttp
// The forge module's tokens as its Go bundles read them (design 47 H2 §808; H-A-422 (1), H-A-426): kept in the vault as
// the module's own, `module/gitea/<account>`, written by the TypeScript bundle's minter through own-put, and read here
// through the seat `mesh-vault`'s `get`, sealed to this process's own key (the Go SDK's seal opens it), at start and at
// each `secret-changed` that names it. Never a file: the bundle holds the token in memory only, and says nothing of it.
import (
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/seal"
)
// VaultGet is the vault seat's verb a token is read by, and SecretChanged the vault's event that a secret changed.
const (
VaultGet = "seat:mesh-vault.get"
SecretChanged = "mesh-vault.secret-changed"
)
// VaultName is the vault's name of an account's token (H-A-426).
func VaultName(account string) string { return "module/gitea/" + account }
// Ask asks one seat verb and answers its reply (stdio.Ask).
type Ask func(address string, args any) (json.RawMessage, error)
// VaultToken is one account's token as this process holds it, read from the vault.
type VaultToken struct {
Account string
ask Ask
keys seal.KeyPair
mu sync.Mutex
token string
generation int
}
// NewVaultToken is an account's token read through ask, sealed to a key made for this process.
func NewVaultToken(account string, ask Ask) (*VaultToken, error) {
keys, err := seal.GenerateKeyPair()
if err != nil {
return nil, fmt.Errorf("no key to read %s's token with: %w", account, err)
}
return &VaultToken{Account: account, ask: ask, keys: keys}, nil
}
// Load reads the account's token from the vault again.
func (t *VaultToken) Load() error {
raw, err := t.ask(VaultGet, map[string]any{"name": VaultName(t.Account), "public_key": t.keys.PublicKey})
if err != nil {
return fmt.Errorf("the vault did not hand %s: %w", VaultName(t.Account), err)
}
var got struct {
Generation int `json:"generation"`
Sealed *seal.Box `json:"sealed"`
}
if err := json.Unmarshal(raw, &got); err != nil || got.Sealed == nil {
return fmt.Errorf("the vault answered no sealed %s", VaultName(t.Account))
}
token, err := seal.Open(*got.Sealed, t.keys.PrivateKey)
if err != nil {
return fmt.Errorf("%s does not open with this process's key: %w", VaultName(t.Account), err)
}
token = strings.TrimSpace(token)
if token == "" {
return fmt.Errorf("the vault's %s is empty", VaultName(t.Account))
}
t.mu.Lock()
t.token, t.generation = token, got.Generation
t.mu.Unlock()
return nil
}
// Token is the token held; read from the vault when none is yet (the module mints it first).
func (t *VaultToken) Token() (string, error) {
t.mu.Lock()
token := t.token
t.mu.Unlock()
if token != "" {
return token, nil
}
if err := t.Load(); err != nil {
return "", fmt.Errorf("the token for %s is not kept yet (the module mints it): %w", t.Account, err)
}
t.mu.Lock()
defer t.mu.Unlock()
return t.token, nil
}
// Generation is the vault's generation of the token held, 0 when none is held.
func (t *VaultToken) Generation() int {
t.mu.Lock()
defer t.mu.Unlock()
return t.generation
}
// Changed takes one `secret-changed`: the token is read again when it names this account's; answers whether it did.
func (t *VaultToken) Changed(body []byte) (bool, error) {
var e struct {
Name string `json:"name"`
}
if err := json.Unmarshal(body, &e); err != nil {
return false, fmt.Errorf("%s is not a secret's change: %w", SecretChanged, err)
}
if e.Name != VaultName(t.Account) {
return false, nil
}
return true, t.Load()
}
var (
processTokens = map[string]*VaultToken{}
processTokensMu sync.Mutex
)
// TokenOf is this process's one token of account, read through the runtime's seat calls.
func TokenOf(account string) (*VaultToken, error) {
processTokensMu.Lock()
defer processTokensMu.Unlock()
if t, ok := processTokens[account]; ok {
return t, nil
}
t, err := NewVaultToken(account, stdio.Ask)
if err != nil {
return nil, err
}
processTokens[account] = t
return t, nil
}
// Follow reads each account's token once the runtime serves, and again at each `secret-changed` naming it, saying
// through logf what it read (never the token). It returns when the subscription ends.
func Follow(logf func(format string, args ...any), accounts ...string) error {
var held []*VaultToken
for _, a := range accounts {
t, err := TokenOf(a)
if err != nil {
return err
}
held = append(held, t)
}
<-stdio.Served()
for _, t := range held {
if err := t.Load(); err != nil {
logf("%s is not read at the start (the module mints it): %v", VaultName(t.Account), err)
} else {
logf("read %s at generation %d", VaultName(t.Account), t.Generation())
}
}
return stdio.Subscribe(SecretChanged, func(e stdio.Envelope) error {
var errs []error
for _, t := range held {
if read, err := t.Changed(e.Body); err != nil {
errs = append(errs, err)
logf("%s changed and could not be read again: %v", VaultName(t.Account), err)
} else if read {
logf("read %s again at generation %d", VaultName(t.Account), t.Generation())
}
}
return errors.Join(errs...)
})
}
+1
View File
@@ -13,6 +13,7 @@ export const KEPT: readonly { readonly name: string; readonly readable?: boolean
{ name: "first-views" },
{ name: "idle" },
{ name: "taken" },
{ name: "token-lease" },
{ name: "transfers" },
];
+24 -5
View File
@@ -245,6 +245,20 @@
"trusted": false,
"runtime": true,
"default": "10m"
},
"token-ttl": {
"type": "string",
"description": "How old the forge module's token of an account may grow, as a duration such as 24h (design 47 H2 \u00a7808): a token the vault has kept longer than this, by its changed-at, is minted again, one process at a time, and the one it replaces is deleted at the forge.",
"trusted": false,
"runtime": true,
"default": "24h"
},
"mint-lease": {
"type": "string",
"description": "How long one process of the forge module may hold the lease to mint a token, as a duration such as 60s (design 47 H2 \u00a7808): the lease key mint in the state token-lease lives this long, so a process that died holding it is replaced after it, and a process that finds it taken waits at most this long.",
"trusted": false,
"runtime": true,
"default": "60s"
}
},
"facts": {
@@ -527,9 +541,7 @@
"env": {
"MESH_GITEA_URL": "http://127.0.0.1:${port:3000}",
"MESH_GITEA_TOOLS_USER": "mesh-tools",
"MESH_GITEA_TOKEN_FILE": "${dir:runtime-state}/token",
"MESH_GITEA_PACKAGES_USER": "mesh-packages",
"MESH_GITEA_PACKAGES_TOKEN_FILE": "${dir:runtime-state}/packages-token",
"MESH_NPM_OWNER": "novox"
}
},
@@ -546,7 +558,6 @@
"env": {
"MESH_GITEA_URL": "http://127.0.0.1:${port:3000}",
"MESH_GITEA_TOOLS_USER": "mesh-tools",
"MESH_GITEA_TOKEN_FILE": "${dir:runtime-state}/token",
"MESH_GITEA_PATCH_DIR": "${dir:forge-patches}"
}
},
@@ -664,6 +675,11 @@
"name": "taken",
"key-ttl": true,
"max-mib": 8
},
{
"name": "token-lease",
"key-ttl": true,
"max-mib": 1
}
],
"reads": [
@@ -678,7 +694,9 @@
"seat:mesh-catalog.revision",
"seat:mesh-catalog.registrations",
"seat:mesh-hand-acts.hand-act",
"seat:mesh-builds.reserved"
"seat:mesh-builds.reserved",
"seat:mesh-vault.own-put",
"seat:mesh-vault.get"
],
"consumes": [
"mesh-catalog.catalog-revision",
@@ -688,7 +706,8 @@
"git.pull-updated",
"git.pull-closed",
"git.pull-merged",
"mesh-conditions.findings-asked"
"mesh-conditions.findings-asked",
"mesh-vault.secret-changed"
],
"streams": [
{
+12 -22
View File
@@ -9,28 +9,25 @@
// makes `mesh-tools`; its password random, sent once and kept nowhere.
// - **the team**: `mesh-packages` in the organisation that owns the registry, writing packages and nothing else:
// no code, no issues, no pull requests, no administration.
// - **the token**: minted for the account by the administrator's login with the one scope `write:package`, kept at
// 0600 in the module's runtime state as `packages-token`, its scopes line beside it saying whose it is
// (MintedToken: one mint at a time across the bundle's processes, re-minted when the kept one is gone or other).
// - **the token**: minted for the account by the administrator's login with the one scope `write:package`, kept in
// the vault as the module's own `module/gitea/mesh-packages` (design 47 H2 §808, H-A-422, H-A-426; MintedToken: one
// mint at a time across the bundle's processes, a name per mint, re-minted when refused or older than token-ttl).
//
// The watcher mints it at its start (keepPackagesToken). No timer looks at it after that: the Go bundle mints nothing,
// and when the forge refuses its write it raises the finding PACKAGES_TOKEN_REFUSED in the module's findings state; the
// watcher, watching that key, looks again (a refused token is minted afresh) and clears the finding (renewOnRefusal).
import { randomBytes } from "node:crypto";
import { join } from "node:path";
import { MintedToken } from "./token.js";
import { adminFromEnv, MintedToken } from "./token.js";
import { TOOLS_ORG, type ToolsForge } from "./tools-account.js";
/** The account the package seats write as (internal/forgehttp PackagesAccount). */
export const PACKAGES_ACCOUNT = "mesh-packages";
/** Its team in the registry's organisation, and the token's name in the account's list. */
/** Its team in the registry's organisation, and the token's fixed name in the account's list before a name per mint. */
export const PACKAGES_TEAM = "mesh-packages";
export const PACKAGES_TOKEN_NAME = "mesh-packages";
/** The one scope the write side needs; reads go with the tools' token. */
export const PACKAGES_TOKEN_SCOPES: readonly string[] = ["write:package"];
/** Where the token is kept in the module's runtime state (the manifest's MESH_GITEA_PACKAGES_TOKEN_FILE). */
export const PACKAGES_TOKEN_FILE = "packages-token";
/** What the team grants: write on the organisation's packages, and no more. */
export const PACKAGES_TEAM_UNITS = {
@@ -47,26 +44,19 @@ export async function setUpPackagesAccount(forge: ToolsForge, account = PACKAGES
}
/** The packages account's token, minted by the administrator's login from what the manifest hands the watcher. */
export function packagesToken(url: string, env: NodeJS.ProcessEnv = process.env, log?: (line: string) => void): MintedToken {
const admin = env.MESH_GITEA_ADMIN_USER;
const passwordFile = env.MESH_GITEA_ADMIN_PASSWORD_FILE;
const stateDir = env.MESH_GITEA_STATE_DIR;
const missing = [
admin ? null : "MESH_GITEA_ADMIN_USER",
passwordFile ? null : "MESH_GITEA_ADMIN_PASSWORD_FILE",
stateDir ? null : "MESH_GITEA_STATE_DIR",
].filter((v): v is string => v !== null);
if (missing.length) throw new Error(`the package seats' write token cannot be minted: ${missing.join(", ")} not set by the manifest`);
export function packagesToken(url: string, env: NodeJS.ProcessEnv = process.env, log?: (line: string) => void,
over: Partial<Pick<ConstructorParameters<typeof MintedToken>[0], "vault" | "lease" | "fetch" | "now">> = {}): MintedToken {
const { admin, passwordFile } = adminFromEnv(env, "the package seats' write token cannot be minted:");
const account = env.MESH_GITEA_PACKAGES_USER || PACKAGES_ACCOUNT;
if (account === admin) throw new Error(`MESH_GITEA_PACKAGES_USER names the admin account "${admin}"; the package seats write as an account of their own`);
const base = over.vault && over.lease ? {} : MintedToken.settingsFromEnv(env);
return new MintedToken({
url, admin: admin!, passwordFile: passwordFile!, file: join(stateDir!, PACKAGES_TOKEN_FILE), account,
name: PACKAGES_TOKEN_NAME, scopes: PACKAGES_TOKEN_SCOPES, log,
...base, ...over, url, admin, passwordFile, account, oldName: PACKAGES_TOKEN_NAME, scopes: PACKAGES_TOKEN_SCOPES, log,
setUp: async (password) => {
const { GiteaAdmin } = await import("./client.js");
await setUpPackagesAccount(new GiteaAdmin(url, admin!, password), account, () => randomBytes(24).toString("base64url"));
await setUpPackagesAccount(new GiteaAdmin(url, admin, password), account, () => randomBytes(24).toString("base64url"));
},
});
} as ConstructorParameters<typeof MintedToken>[0]);
}
/**
+96
View File
@@ -0,0 +1,96 @@
// The bus as the forge module's token needs it, in memory (design 47 H2 §808, §822): the vault seat's `own-put` in its
// two sealed steps and its `get` sealed to the asker's key, and the runtime's state with a create-only key that lives
// its TTL, as the runtime that launched the bundle answers them. `order` records each own-put and each forge delete a
// test also writes into it, so the delete's place after the own-put can be read.
import nacl from "tweetnacl";
import blakejs from "blakejs";
import { seal } from "@novox/mesh-sdk/seal";
import { useBroker, type Broker } from "@novox/mesh-sdk/messaging";
import { useRuntime } from "@novox/mesh-sdk/runtime";
import { useStateChannel, type StateChange } from "@novox/mesh-sdk/state";
export interface Held { value: string; generation: number; changedAt: string }
export function fakeBus(now: () => Date = () => new Date(), order: string[] = []) {
const vault = new Map<string, Held>();
const calls = new Map<string, { name: string; secretKey: Uint8Array }>();
const kv = new Map<string, { value: unknown; until: number | null }>();
const watchers: { state: string; key: string; deliver: (c: StateChange<unknown>) => Promise<void> }[] = [];
let revision = 0;
const keyOf = (state: string, key: string) => `${state}/${key}`;
const live = (k: string) => {
const e = kv.get(k);
if (e && e.until !== null && e.until <= now().getTime()) kv.delete(k);
return kv.get(k);
};
const tell = async (state: string, key: string, op: "put" | "delete", value?: unknown) => {
revision++;
for (const w of watchers) if (w.state === state && (w.key === "" || w.key === key)) await w.deliver({ state, key, op, value, revision, current: false });
};
const broker: Broker = {
async request<Req, Res>(key: string, body: Req): Promise<Res> {
const b = body as Record<string, string>;
if (key === "seat:mesh-vault.own-put" && b.call === undefined) {
const keys = nacl.box.keyPair();
const call = `call-${calls.size + 1}`;
calls.set(call, { name: `module/gitea/${b.name}`, secretKey: keys.secretKey });
return { call, seal_to: Buffer.from(keys.publicKey).toString("base64"), secret: `module/gitea/${b.name}` } as Res;
}
if (key === "seat:mesh-vault.own-put") {
const c = calls.get(b.call);
if (!c) throw new Error(`no own-put call ${b.call}`);
calls.delete(b.call);
const boxed = new Uint8Array(Buffer.from(b.sealed, "base64"));
const eph = boxed.subarray(0, 32);
const pub = nacl.box.keyPair.fromSecretKey(c.secretKey).publicKey;
const nonce = blakejs.blake2b(Buffer.concat([eph, pub]), undefined, 24);
const opened = nacl.box.open(boxed.subarray(32), nonce, eph, c.secretKey);
if (!opened) throw new Error("the sealed value does not open");
const generation = (vault.get(c.name)?.generation ?? 0) + 1;
vault.set(c.name, { value: Buffer.from(opened).toString("utf8"), generation, changedAt: now().toISOString() });
order.push(`own-put ${c.name} ${generation}`);
return { name: c.name, generation } as Res;
}
if (key === "seat:mesh-vault.get") {
const held = vault.get(b.name);
if (!held) throw new Error(`the vault holds no secret named ${b.name}`);
return { name: b.name, generation: held.generation, "changed-at": held.changedAt, sealed: seal(held.value, b.public_key) } as Res;
}
throw new Error(`not played here: ${key}`);
},
handle: async () => () => {},
publish: async () => {},
subscribe: async () => () => {},
close: async () => {},
};
const ask = async (method: string, params: unknown): Promise<unknown> => {
const p = params as { state: string; key: string; value?: unknown; ttl_seconds?: number };
const k = keyOf(p.state, p.key);
switch (method) {
case "mesh/state.create":
if (live(k)) throw Object.assign(new Error("the key has a value"), { code: -32010 });
kv.set(k, { value: p.value, until: p.ttl_seconds ? now().getTime() + p.ttl_seconds * 1000 : null });
await tell(p.state, p.key, "put", p.value);
return { revision };
case "mesh/state.get": {
const e = live(k);
return e ? { key: p.key, value: e.value, revision } : null;
}
case "mesh/state.put":
kv.set(k, { value: p.value, until: null });
await tell(p.state, p.key, "put", p.value);
return { revision };
case "mesh/state.delete":
kv.delete(k);
await tell(p.state, p.key, "delete");
return null;
default:
throw new Error(`not played here: ${method}`);
}
};
useBroker(() => broker);
useRuntime(ask);
useStateChannel({ ask, watch: async (params, deliver) => { const w = params as { state: string; key: string }; watchers.push({ ...w, deliver }); } });
return { vault, kv, order, ask };
}
+2 -2
View File
@@ -16,8 +16,8 @@ test("the states the module announces are the states its manifest declares", asy
test("the announcement has the layout mesh-sdk's go/announce reads: version 1, the module, the node, the states sorted by name", async () => {
const { statesAnnouncement } = await import("../kept.ts");
assert.deepEqual(statesAnnouncement("gitea", "ace"), { v: 1, module: "gitea", node: "ace", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "taken" }, { name: "transfers" }] });
assert.deepEqual(statesAnnouncement("gitea", undefined), { v: 1, module: "gitea", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "taken" }, { name: "transfers" }] });
assert.deepEqual(statesAnnouncement("gitea", "ace"), { v: 1, module: "gitea", node: "ace", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "taken" }, { name: "token-lease" }, { name: "transfers" }] });
assert.deepEqual(statesAnnouncement("gitea", undefined), { v: 1, module: "gitea", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "taken" }, { name: "token-lease" }, { name: "transfers" }] });
});
test("index.ts says the announcement as the event `states` before the tools are served", () => {
+165 -110
View File
@@ -10,15 +10,19 @@
import { test, after } from "node:test";
import assert from "node:assert/strict";
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
import { mkdtemp, stat, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { collectTools } from "@novox/mesh-sdk/tools";
import { AdminRefused, MintedToken, TOKEN_SCOPES } from "../dist/token.js";
import { AdminRefused, MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, vaultOverBus, type MintedTokenOptions } from "../dist/token.js";
import { tokenLeaseTool } from "../dist/tools/tokenlease.js";
import { fakeBus } from "./fakebus.ts";
import { GiteaClient } from "../dist/client.js";
import { getGiteaTools } from "../dist/tools/index.js";
/** What the vault and the forge were asked, in order: each own-put and each token deleted (design 47 H2 §808). */
const ORDER: string[] = [];
const ADMIN = "mesh-admin";
const PASSWORD = "the-vault-minted-this";
@@ -126,6 +130,7 @@ function fakeForge(): Promise<Forge> {
if (matches.length > 1) return json(res, 422, { message: `multiple matches for token name '${key}'` });
const t = matches[0];
forge.deletedBy.push(byId ? "id" : "name");
ORDER.push(`delete ${t.name}`);
const twin = forge.twins.findIndex((w) => w.id === t.id);
if (twin >= 0) forge.twins.splice(twin, 1);
else forge.tokens.delete(t.name);
@@ -253,58 +258,59 @@ function fakeForge(): Promise<Forge> {
// ---- What the runtime's environment gives the module. ----
async function delivered(forge: Forge): Promise<{ env: NodeJS.ProcessEnv; file: string; logs: string[] }> {
async function delivered(forge: Forge, now: () => Date = () => new Date()) {
const dir = await mkdtemp(join(tmpdir(), "gitea-"));
const passwordFile = join(dir, "admin.secret");
await writeFile(passwordFile, PASSWORD + "\n", { mode: 0o600 });
const state = join(dir, "state");
ORDER.length = 0;
return {
env: {
MESH_GITEA_URL: forge.url,
MESH_GITEA_ADMIN_USER: ADMIN,
MESH_GITEA_ADMIN_PASSWORD_FILE: passwordFile,
MESH_GITEA_STATE_DIR: state,
},
file: join(state, "token"),
logs: [],
MESH_GITEA_STATE_DIR: join(dir, "state"),
} as NodeJS.ProcessEnv,
bus: fakeBus(now, ORDER),
logs: [] as string[],
};
}
/** A client as a fresh process would build it: a new source over the kept file, its log captured. */
function minted(env: NodeJS.ProcessEnv, logs: string[]): GiteaClient {
const source = new MintedToken({
url: env.MESH_GITEA_URL!,
admin: env.MESH_GITEA_ADMIN_USER!,
passwordFile: env.MESH_GITEA_ADMIN_PASSWORD_FILE!,
file: join(env.MESH_GITEA_STATE_DIR!, "token"),
log: (l) => logs.push(l),
});
return new GiteaClient(env.MESH_GITEA_URL!, source);
/** A client as a fresh process would build it: a new source over the vault and the lease on the bus, its log captured. */
function minted(env: NodeJS.ProcessEnv, logs: string[], extra: Partial<MintedTokenOptions> = {}): GiteaClient {
return new GiteaClient(env.MESH_GITEA_URL!, source(env, logs, extra));
}
function source(env: NodeJS.ProcessEnv, logs: string[], extra: Partial<MintedTokenOptions> = {}): MintedToken {
return new MintedToken({
url: env.MESH_GITEA_URL!, admin: env.MESH_GITEA_ADMIN_USER!, passwordFile: env.MESH_GITEA_ADMIN_PASSWORD_FILE!,
account: "mesh-tools", oldName: TOKEN_NAME, vault: vaultOverBus(), lease: leaseOverState(), log: (l) => logs.push(l), ...extra,
});
}
const VAULT = "module/gitea/mesh-tools";
const forge = await fakeForge();
after(() => forge.close());
test("first start: mints with the admin account, keeps the token at 0600, asks for the tools' scopes only", async () => {
const { env, file, logs } = await delivered(forge);
test("first start: mints mesh-tools-1 with the admin account, writes it through own-put, asks for the tools' scopes only", async () => {
const { env, bus, logs } = await delivered(forge);
forge.tokens.clear();
const repos = await minted(env, logs).listRepos();
assert.equal(repos[0]?.full_name, "novox/hq");
assert.equal(forge.mints, 1);
// Never write:admin (novox/hq ADR 0266): the forge's administration is the operator's, not a tool's.
assert.deepEqual(forge.lastScopes, ["write:repository", "write:issue", "read:user", "read:package"]);
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
const token = forge.tokens.get("mesh-tools")!;
assert.equal(await readFile(file, "utf8"), token + "\n");
assert.equal(await readFile(file + ".scopes", "utf8"), "read:package,read:user,write:issue,write:repository\n");
assert.equal((await stat(file)).mode & 0o777, 0o600);
// Said that it minted, and where it keeps it — never what it is.
assert.ok(logs.some((l) => l.startsWith("minted a token")), logs.join("\n"));
// Design 47 H2 §808, H-A-422 (2), H-A-426: named per mint, kept as the module's own secret, never a file.
const token = forge.tokens.get("mesh-tools-1")!;
assert.ok(token);
assert.deepEqual([bus.vault.get(VAULT)?.value, bus.vault.get(VAULT)?.generation], [token, 1]);
await assert.rejects(stat(join(env.MESH_GITEA_STATE_DIR!, "token")), /ENOENT/);
assert.ok(logs.some((l) => l.startsWith("minted mesh-tools-1")), logs.join("\n"));
assert.ok(logs.every((l) => !l.includes(token) && !l.includes(PASSWORD)), logs.join("\n"));
});
test("second start: reuses the kept token, mints nothing", async () => {
test("second start: reuses the vault's token, mints nothing", async () => {
const { env, logs } = await delivered(forge);
await minted(env, logs).listRepos();
const before = forge.mints;
@@ -313,54 +319,83 @@ test("second start: reuses the kept token, mints nothing", async () => {
await minted(env, again).listRepos();
assert.equal(forge.mints, before);
assert.ok(again.some((l) => l.startsWith("reusing the token kept at")), again.join("\n"));
assert.ok(again.every((l) => !l.includes(forge.tokens.get("mesh-tools")!)), again.join("\n"));
assert.ok(again.some((l) => l.startsWith(`reusing the token the vault keeps as ${VAULT}`)), again.join("\n"));
});
test("the forge rejects the kept token (its data was restored): minted afresh, once, and the call goes through", async () => {
const { env, file, logs } = await delivered(forge);
test("the forge rejects the vault's token: mesh-tools-2 is minted once, written through own-put, and only then is mesh-tools-1 deleted by its id", async () => {
const { env, bus, logs } = await delivered(forge);
forge.tokens.clear();
const client = minted(env, logs);
await client.listRepos();
const before = forge.mints;
ORDER.length = 0;
forge.deletedBy.length = 0;
forge.tokens.clear(); // the forge no longer knows any token — a restore from the predecessor
forge.tokens.set("mesh-tools-1", "revoked-meanwhile"); // the forge no longer takes the vault's token
const repos = await client.listRepos();
assert.equal(repos.length, 1);
assert.equal(forge.mints, before + 1);
assert.equal(await readFile(file, "utf8"), forge.tokens.get("mesh-tools") + "\n");
assert.ok(logs.some((l) => l.startsWith("the forge rejected the kept token")), logs.join("\n"));
assert.equal(bus.vault.get(VAULT)?.value, forge.tokens.get("mesh-tools-2"));
// H-A-422 (2): the own-put first, the replaced token's delete after, by id; the forge holds one tools' token after.
assert.deepEqual(ORDER, [`own-put ${VAULT} 2`, "delete mesh-tools-1"]);
assert.deepEqual(forge.deletedBy, ["id"]);
assert.deepEqual([...forge.tokens.keys()].filter((n) => n.startsWith("mesh-tools")), ["mesh-tools-2"]);
assert.ok(logs.some((l) => l.startsWith("the forge rejected the vault's token")), logs.join("\n"));
});
test("a token kept with write:admin by an earlier build is minted again without it at the next start, and the forge drops the old one", async () => {
const { env, file, logs } = await delivered(forge);
await minted(env, logs).listRepos();
const old = forge.tokens.get("mesh-tools")!;
forge.scopesOf.set(old, ["write:repository", "write:issue", "read:user", "write:admin"]);
await rm(file + ".scopes"); // an earlier build kept no scopes beside it
test("an own-put that fails leaves the old token standing, and the next mint replaces the unused one it left", async () => {
const { env, bus, logs } = await delivered(forge);
forge.tokens.clear();
const client = minted(env, logs);
await client.listRepos();
const old = forge.tokens.get("mesh-tools-1")!;
forge.tokens.set("mesh-tools-1", "revoked-meanwhile");
ORDER.length = 0;
let failPut = true;
const failing = minted(env, logs, { vault: { get: vaultOverBus().get, put: async (a, t) => {
if (failPut) { failPut = false; throw new Error("the vault is away"); }
return vaultOverBus().put(a, t);
} } });
await assert.rejects(failing.listRepos(), /the vault is away/);
assert.deepEqual(ORDER, [], "nothing deleted while the vault holds the old token");
assert.equal(bus.vault.get(VAULT)?.generation, 1);
assert.ok(forge.tokens.has("mesh-tools-2"), "the unused token of the failed mint stands");
assert.equal((await failing.listRepos()).length, 1);
assert.deepEqual(ORDER, ["delete mesh-tools-2", `own-put ${VAULT} 2`, "delete mesh-tools-1"]);
assert.notEqual(bus.vault.get(VAULT)?.value, old);
});
test("a token older than token-ttl is minted again, and the one it replaces deleted", async () => {
let clock = Date.parse("2026-11-22T00:00:00Z");
const now = () => new Date(clock);
const { env, bus, logs } = await delivered(forge, now);
forge.tokens.clear();
const client = minted(env, logs, { now, ttlMs: 24 * 3600_000 });
await client.listRepos();
const before = forge.mints;
const again: string[] = [];
await minted(env, again).listRepos();
clock += 23 * 3600_000;
await client.listRepos();
assert.equal(forge.mints, before, "a token within token-ttl is used");
clock += 2 * 3600_000;
await client.listRepos();
assert.equal(forge.mints, before + 1);
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
assert.ok(!forge.lastScopes!.includes("write:admin"));
assert.notEqual(forge.tokens.get("mesh-tools"), old);
assert.ok(![...forge.tokens.values()].includes(old), "the old token still stands on the forge");
assert.ok(again.some((l) => l.includes("minted with other scopes")), again.join("\n"));
// An admin route through the escape hatch is refused before it is sent.
const tool = getGiteaTools(minted(env, again)).find((t) => t.name === "gitea_api")!;
await assert.rejects(tool.run({ path: "/admin/users/mesh_novox_builder", method: "PATCH", body: { admin: true } }),
/not a write gitea_api makes/);
assert.equal(bus.vault.get(VAULT)?.generation, 2);
assert.deepEqual([...forge.tokens.keys()].filter((n) => n.startsWith("mesh-tools")), ["mesh-tools-2"]);
assert.ok(logs.some((l) => l.includes("older than token-ttl")), logs.join("\n"));
});
test("a kept token lacking a scope the tools need: the forge refuses the call for the scope, the token is re-minted with the whole list, and the call goes through", async () => {
const { env, file, logs } = await delivered(forge);
test("a token lacking a scope the tools need: the forge refuses the call for the scope, the token is re-minted with the whole list, and the call goes through", async () => {
const { env, bus, logs } = await delivered(forge);
forge.tokens.clear();
const client = minted(env, logs);
await client.listRepos();
const before = forge.mints;
const old = forge.tokens.get("mesh-tools")!;
const old = forge.tokens.get("mesh-tools-1")!;
forge.scopesOf.set(old, ["write:repository", "write:issue"]); // minted before read:user was added
const repos = await client.api<unknown[]>("/user/repos");
@@ -368,37 +403,11 @@ test("a kept token lacking a scope the tools need: the forge refuses the call fo
assert.equal(repos.length, 1);
assert.equal(forge.mints, before + 1);
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
assert.notEqual(forge.tokens.get("mesh-tools"), old);
assert.equal(await readFile(file, "utf8"), forge.tokens.get("mesh-tools") + "\n");
assert.ok(logs.some((l) => l.startsWith("the forge rejected the kept token")), logs.join("\n"));
assert.equal(bus.vault.get(VAULT)?.value, forge.tokens.get("mesh-tools-2"));
});
test("the kept file is gone but the forge still holds a token by that name: replaced, not refused", async () => {
const { env, file, logs } = await delivered(forge);
await minted(env, logs).listRepos();
const before = forge.mints;
await rm(file);
const repos = await minted(env, logs).listRepos();
assert.equal(repos.length, 1);
assert.equal(forge.mints, before + 1);
assert.equal([...forge.tokens.keys()].filter((n) => n === "mesh-tools").length, 1);
assert.ok(logs.some((l) => l.includes('already holds a token named "mesh-tools"')), logs.join("\n"));
});
test("concurrent first calls share one mint", async () => {
test("the first mint deletes by id every token under the old fixed name, two raced ones too, and keeps the account's others", async () => {
const { env, logs } = await delivered(forge);
const client = minted(env, logs);
const before = forge.mints;
await Promise.all([client.listRepos(), client.listRepos(), client.listRepos()]);
assert.equal(forge.mints, before + 1);
});
test("two tokens hold the name (two mints raced): both are deleted by their id, and one fresh token is minted", async () => {
const { env, file, logs } = await delivered(forge);
forge.tokens.clear();
forge.twins.length = 0;
for (const other of ["laptop", "backup", "ci"]) forge.plant(other); // the account's other tokens: kept
@@ -411,44 +420,68 @@ test("two tokens hold the name (two mints raced): both are deleted by their id,
assert.equal(repos.length, 1);
assert.equal(forge.mints, before + 1);
// H-A-430 (1): the tokens the module kept before under the old fixed name are the ones the first mint replaces.
assert.deepEqual(forge.deletedBy, ["id", "id"]);
assert.deepEqual(ORDER, [`own-put ${VAULT} 1`, "delete mesh-tools", "delete mesh-tools"]);
assert.equal(forge.twins.length, 0);
assert.deepEqual([...forge.tokens.keys()].sort(), ["backup", "ci", "laptop", "mesh-tools"]);
assert.equal(await readFile(file, "utf8"), forge.tokens.get("mesh-tools") + "\n");
assert.ok(logs.some((l) => l.includes('holds 2 tokens named "mesh-tools"')), logs.join("\n"));
assert.deepEqual([...forge.tokens.keys()].sort(), ["backup", "ci", "laptop", "mesh-tools-1"]);
});
test("two processes on one kept file, both without a token, mint once: the second waits for the first and reuses it", async () => {
test("concurrent first calls share one mint", async () => {
const { env, logs } = await delivered(forge);
forge.tokens.clear();
forge.twins.length = 0;
const client = minted(env, logs);
const before = forge.mints;
// Two sources over one state directory, as the tools bundle and the watcher are two processes.
const first = minted(env, logs);
const second = minted(env, logs);
await Promise.all([first.listRepos(), second.listRepos()]);
await Promise.all([client.listRepos(), client.listRepos(), client.listRepos()]);
assert.equal(forge.mints, before + 1);
assert.equal(forge.twins.length, 0);
assert.ok(logs.some((l) => l.includes("another process minted it")), logs.join("\n"));
});
test("a lock left by a process that died mid-mint is taken over, not waited on for ever", async () => {
const { env, file, logs } = await delivered(forge);
test("two processes, one holding the old token, both refused, mint one token in all: the second waits for the lease and uses the vault's newer token", async () => {
const { env, bus, logs } = await delivered(forge);
forge.tokens.clear();
forge.twins.length = 0;
await mkdir(join(env.MESH_GITEA_STATE_DIR!), { recursive: true });
await writeFile(`${file}.lock`, `2147483646 ${Date.now()} gone\n`); // a pid no process holds
const first = minted(env, logs);
await first.listRepos();
const second = minted(env, logs);
await second.listRepos(); // both hold mesh-tools-1
const before = forge.mints;
assert.equal((await minted(env, logs).listRepos()).length, 1);
forge.tokens.set("mesh-tools-1", "revoked-meanwhile");
await Promise.all([first.listRepos(), second.listRepos()]);
// Design 47 H2 §808: one mint in all, and the forge holds one token for the account after.
assert.equal(forge.mints, before + 1);
await assert.rejects(stat(`${file}.lock`), /ENOENT/);
assert.deepEqual([...forge.tokens.keys()].filter((n) => n.startsWith("mesh-tools")), ["mesh-tools-2"]);
assert.equal(bus.vault.get(VAULT)?.generation, 2);
assert.ok(logs.some((l) => l.includes("another process holds the mint lease")), logs.join("\n"));
assert.equal(bus.kv.size, 0, "the lease is deleted after the mint");
});
test("a minter killed while holding the lease is replaced after mint-lease: the waiter creates the lease itself and mints", async () => {
let clock = Date.parse("2026-11-22T00:00:00Z");
const now = () => new Date(clock);
const { env, bus, logs } = await delivered(forge, now);
forge.tokens.clear();
// A process that took the lease and died: its key lives mint-lease (60 s) and nobody deletes it.
await bus.ask("mesh/state.create", { state: "token-lease", key: "mint", value: { holder: "ace/4242", since: now().toISOString() }, ttl_seconds: 60 });
const waited: number[] = [];
const lease = leaseOverState();
// Waiting for the key to change: nothing changes, so the wait lasts its bound, the clock moving on by it.
const before = forge.mints;
const repos = await minted(env, logs, { lease: { ...lease, changed: async (ms) => { waited.push(ms); clock += ms; } } }).listRepos();
assert.equal(repos.length, 1);
assert.deepEqual(waited, [60_000], "the waiter waited mint-lease once");
assert.equal(forge.mints, before + 1);
assert.ok(logs.some((l) => l.includes("another process holds the mint lease (ace/4242 since")), logs.join("\n"));
});
test("the admin account is refused: said plainly, nothing kept, and the next call fails the same way rather than crashing", async () => {
const { env, file, logs } = await delivered(forge);
const { env, bus, logs } = await delivered(forge);
forge.tokens.clear();
forge.admins.delete(ADMIN); // the forge's data came from a predecessor; mesh-admin was never created there
try {
const client = minted(env, logs);
@@ -464,7 +497,7 @@ test("the admin account is refused: said plainly, nothing kept, and the next cal
});
await assert.rejects(client.listRepos(), AdminRefused);
assert.equal(forge.mints, before);
await assert.rejects(stat(file), /ENOENT/);
assert.equal(bus.vault.size, 0);
// The account appears (the operator created it): the very next call mints and works.
forge.admins.set(ADMIN, PASSWORD);
@@ -475,25 +508,47 @@ test("the admin account is refused: said plainly, nothing kept, and the next cal
}
});
test("one process shares one source per kept file — the watcher and the tools never renew against each other", async () => {
test("one process shares one source per account — the watcher and the tools never renew against each other", async () => {
const { env } = await delivered(forge);
assert.equal(MintedToken.fromEnv(env.MESH_GITEA_URL!, env), MintedToken.fromEnv(env.MESH_GITEA_URL!, env));
});
test("a second process finds the token the first renewed, and reuses it instead of minting over it", async () => {
test("a second process finds the token the first renewed in the vault, and reuses it instead of minting over it", async () => {
const { env, logs } = await delivered(forge);
forge.tokens.clear();
const first = minted(env, logs);
const second = minted(env, logs);
await first.listRepos();
await second.listRepos(); // both hold the same kept token
await second.listRepos(); // both hold the same token
const before = forge.mints;
forge.tokens.clear();
forge.tokens.set("mesh-tools-1", "revoked-meanwhile");
await first.listRepos(); // renews: one mint
await second.listRepos(); // rejected too — but the kept file already carries the renewed one
await second.listRepos(); // rejected too — but the vault already holds the renewed one
assert.equal(forge.mints, before + 1);
assert.ok(logs.some((l) => l.includes("is newer — reusing it")), logs.join("\n"));
assert.ok(logs.some((l) => l.includes("is newer (generation 2) — reusing it")), logs.join("\n"));
});
test("forge_token_lease says the lease's holder and since when, and each token's age from the vault, never the token", async () => {
let clock = Date.parse("2026-11-22T00:00:00Z");
const now = () => new Date(clock);
const { env, bus, logs } = await delivered(forge, now);
forge.tokens.clear();
await minted(env, logs, { now }).listRepos();
await bus.ask("mesh/state.create", { state: "token-lease", key: "mint", value: { holder: "ace/4242", since: "2026-11-22T00:00:30Z" }, ttl_seconds: 60 });
clock += 90_000;
const tool = tokenLeaseTool(vaultOverBus(), leaseOverState(), ["mesh-tools", "mesh-packages"], now);
const said = await tool.run({}, undefined as never) as any;
assert.equal(tool.name, "forge_token_lease");
assert.deepEqual(said.tokens.map((t: any) => [t.account, t.held, t.generation, t["age-seconds"]]),
[["mesh-tools", true, 1, 90], ["mesh-packages", false, undefined, undefined]]);
assert.ok(!JSON.stringify(said).includes(bus.vault.get(VAULT)!.value), "the token is never said");
// The lease lives mint-lease: past it, nobody holds it.
clock += 30_000;
assert.deepEqual((await tool.run({}, undefined as never) as any).lease, { held: false });
});
test("a configured token wins, and is reported rather than minted over when the forge rejects it", async () => {
@@ -510,8 +565,8 @@ test("a configured token wins, and is reported rather than minted over when the
test("nothing to mint with and no token: the client says what is missing", async () => {
assert.throws(
() => GiteaClient.fromEnv({ MESH_GITEA_URL: forge.url, MESH_GITEA_ADMIN_USER: ADMIN }),
/set GITEA_TOKEN, or MESH_GITEA_ADMIN_PASSWORD_FILE, MESH_GITEA_STATE_DIR/,
() => GiteaClient.fromEnv({ MESH_GITEA_URL: forge.url, MESH_GITEA_ADMIN_USER: ADMIN, MESH_GITEA_TOOLS_USER: "mesh-tools-nothing" }),
/set GITEA_TOKEN, or MESH_GITEA_ADMIN_PASSWORD_FILE/,
);
});
+36 -26
View File
@@ -10,11 +10,12 @@
import { test, after } from "node:test";
import assert from "node:assert/strict";
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
import { mkdtemp, readFile, writeFile, mkdir } from "node:fs/promises";
import { mkdtemp, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { MintedToken, TOKEN_SCOPES } from "../dist/token.js";
import { MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, vaultOverBus } from "../dist/token.js";
import { fakeBus } from "./fakebus.ts";
import { GiteaClient } from "../dist/client.js";
import { TOOLS_ACCOUNT, TOOLS_TEAM_UNITS, BUILDER_ACCOUNT, AGENT_ACCOUNT, meshAccounts } from "../dist/tools-account.js";
import { PACKAGES_ACCOUNT, PACKAGES_TEAM_UNITS, PACKAGES_TOKEN_REFUSED, keepPackagesToken, packagesToken, renewOnRefusal } from "../dist/packages-account.js";
@@ -266,7 +267,12 @@ function fakeForge() {
const forge = await fakeForge();
after(() => forge.close());
/** The bus of the environment made last: the vault's secrets and the lease (design 47 H2 §808). */
let bus = fakeBus();
const vaulted = (account: string) => bus.vault.get(`module/gitea/${account}`)?.value;
async function environment(keepMainRules = false): Promise<NodeJS.ProcessEnv & { MESH_GITEA_STATE_DIR: string }> {
bus = fakeBus();
const dir = await mkdtemp(join(tmpdir(), "gitea-tools-account-"));
const passwordFile = join(dir, "admin.secret");
await writeFile(passwordFile, PASSWORD + "\n", { mode: 0o600 });
@@ -281,7 +287,7 @@ async function environment(keepMainRules = false): Promise<NodeJS.ProcessEnv & {
};
}
/** A source as a fresh process builds it from the manifest's environment (fromEnv shares one per file). */
/** A source as a fresh process builds it from the manifest's environment (fromEnv shares one per account and state). */
function fresh(env: NodeJS.ProcessEnv): GiteaClient {
return GiteaClient.fromEnv(env);
}
@@ -293,9 +299,9 @@ test("the tools' token is minted for an account of their own, which is no admini
const account = forge.users.get(TOOLS_ACCOUNT);
assert.ok(account, "the module made the tools' account");
assert.equal(account!.admin, false, "the tools' account is no administrator");
const held = forge.tokens.filter((t) => t.name === "mesh-tools");
assert.deepEqual(held.map((t) => t.user), [TOOLS_ACCOUNT], "the one token is the tools' account's, none is the administrator's");
assert.equal(await readFile(join(env.MESH_GITEA_STATE_DIR, "token"), "utf8"), held[0].sha1 + "\n");
const held = forge.tokens.filter((t) => t.name === "mesh-tools-1" || t.name === "mesh-tools");
assert.deepEqual(held.map((t) => [t.user, t.name]), [[TOOLS_ACCOUNT, "mesh-tools-1"]], "the one token is the tools' account's, none is the administrator's");
assert.equal(vaulted(TOOLS_ACCOUNT), held[0].sha1, "kept in the vault as the module's own (design 47 H2 §808)");
const team = forge.teams.find((t) => t.name === "mesh-tools");
assert.ok(team, "the team is made in the mesh's organisation");
@@ -334,17 +340,13 @@ test("the administrator's token from before goes: a kept admin token is minted a
// What the module kept before ADR 0365: a token of the administrator's own, by the same name, its scopes beside it.
const old = { id: 900, name: "mesh-tools", sha1: "sha-admin-old", user: ADMIN, scopes: ["write:repository", "write:issue", "read:user"] };
forge.tokens.push(old);
await mkdir(env.MESH_GITEA_STATE_DIR, { recursive: true });
await writeFile(join(env.MESH_GITEA_STATE_DIR, "token"), old.sha1 + "\n", { mode: 0o600 });
await writeFile(join(env.MESH_GITEA_STATE_DIR, "token.scopes"), "read:user,write:issue,write:repository\n", { mode: 0o600 });
await fresh(env).listRepos();
assert.ok(!forge.tokens.some((t) => t.user === ADMIN), "the administrator holds no tools' token any more");
const kept = (await readFile(join(env.MESH_GITEA_STATE_DIR, "token"), "utf8")).trim();
const kept = vaulted(TOOLS_ACCOUNT)!;
assert.notEqual(kept, old.sha1);
assert.equal(forge.tokens.find((t) => t.sha1 === kept)?.user, TOOLS_ACCOUNT);
assert.match(await readFile(join(env.MESH_GITEA_STATE_DIR, "token.scopes"), "utf8"), / for mesh-tools\n$/);
});
test("a tools' account someone made an administrator is made a plain account again at the next mint", async () => {
@@ -363,17 +365,24 @@ test("the manifest hands the tools their account, and the Go forge bundle holds
const manifest = JSON.parse(await readFile(new URL("../module.json", import.meta.url), "utf8"));
const code = manifest.build.artifacts.find((a: any) => a.name === "code");
assert.equal(code.env.MESH_GITEA_TOOLS_USER, TOOLS_ACCOUNT);
// The Go forge bundle reads the token the tools' account holds, never the administrator's password.
// The Go forge bundle reads the token the tools' account holds, never the administrator's password; through the
// vault seat's get, never a file (design 47 H2 §808, H-A-422 (1)).
const forgeBundle = manifest.build.artifacts.find((a: any) => a.name === "forge");
assert.ok(forgeBundle.env.MESH_GITEA_TOKEN_FILE);
assert.equal(forgeBundle.env.MESH_GITEA_TOOLS_USER, TOOLS_ACCOUNT);
assert.equal(forgeBundle.env.MESH_GITEA_ADMIN_PASSWORD_FILE, undefined);
// The package registry's bundle reads with the tools' token and writes with the packages account's (design 51).
const packages = manifest.build.artifacts.find((a: any) => a.name === "package-registry");
assert.equal(packages.env.MESH_GITEA_TOKEN_FILE, forgeBundle.env.MESH_GITEA_TOKEN_FILE);
assert.equal(packages.env.MESH_GITEA_PACKAGES_USER, PACKAGES_ACCOUNT);
assert.match(packages.env.MESH_GITEA_PACKAGES_TOKEN_FILE, /^\$\{dir:runtime-state\}\/packages-token$/);
assert.equal(packages.env.MESH_GITEA_ADMIN_PASSWORD_FILE, undefined);
assert.equal(code.env.MESH_GITEA_STATE_DIR, "${dir:runtime-state}", "the watcher mints the packages token where the bundle reads it");
for (const a of manifest.build.artifacts) {
assert.equal(a.env?.MESH_GITEA_TOKEN_FILE ?? a.env?.MESH_GITEA_PACKAGES_TOKEN_FILE, undefined, `${a.name} is handed a token file`);
}
assert.ok(manifest.invokes.includes("seat:mesh-vault.own-put") && manifest.invokes.includes("seat:mesh-vault.get"), manifest.invokes.join(","));
assert.ok(manifest.consumes.includes("mesh-vault.secret-changed"), "the Go bundles hear the vault's changes");
// The lease and the two settings (§808, §820).
assert.deepEqual(manifest.state.find((s: any) => s.name === "token-lease"), { name: "token-lease", "key-ttl": true, "max-mib": 1 });
assert.deepEqual([manifest.config["token-ttl"]?.default, manifest.config["token-ttl"]?.runtime], ["24h", true]);
assert.deepEqual([manifest.config["mint-lease"]?.default, manifest.config["mint-lease"]?.runtime], ["60s", true]);
});
test("the package seats write as an account of their own: plain, in a team writing only packages, its token write:package alone, renewed when the forge rejects it", async () => {
@@ -383,10 +392,10 @@ test("the package seats write as an account of their own: plain, in a team writi
const account = forge.users.get(PACKAGES_ACCOUNT);
assert.ok(account, "the module made the packages account");
assert.equal(account!.admin, false, "the packages account is no administrator");
const held = forge.tokens.filter((t) => t.name === "mesh-packages");
assert.deepEqual(held.map((t) => [t.user, t.scopes]), [[PACKAGES_ACCOUNT, ["write:package"]]]);
assert.equal(await readFile(join(env.MESH_GITEA_STATE_DIR, "packages-token"), "utf8"), held[0].sha1 + "\n");
assert.equal(await readFile(join(env.MESH_GITEA_STATE_DIR, "packages-token.scopes"), "utf8"), "write:package for mesh-packages\n");
const named = (t: { name: string }) => t.name === "mesh-packages" || t.name.startsWith("mesh-packages-");
const held = forge.tokens.filter(named);
assert.deepEqual(held.map((t) => [t.user, t.name, t.scopes]), [[PACKAGES_ACCOUNT, "mesh-packages-1", ["write:package"]]]);
assert.equal(vaulted(PACKAGES_ACCOUNT), held[0].sha1, "kept in the vault as module/gitea/mesh-packages (H-A-426)");
const team = forge.teams.find((t) => t.name === "mesh-packages");
assert.ok(team?.members.has(PACKAGES_ACCOUNT));
assert.deepEqual(team!.units.units_map, PACKAGES_TEAM_UNITS.units_map, "the team writes packages and nothing else");
@@ -398,11 +407,12 @@ test("the package seats write as an account of their own: plain, in a team writi
const renew = renewOnRefusal(source, forge.url, { delete: async (k) => { cleared.push(k); } }, (l) => said.push(l));
await renew({ key: "seat.gitea.other", op: "put" });
await renew({ key: PACKAGES_TOKEN_REFUSED, op: "delete" });
assert.equal(forge.tokens.filter((t) => t.name === "mesh-packages").length, 0, "nothing renews but the refusal");
assert.equal(forge.tokens.filter(named).length, 0, "nothing renews but the refusal");
await renew({ key: PACKAGES_TOKEN_REFUSED, op: "put" });
assert.deepEqual(said, ["the forge rejected the package seats' write token; minted afresh"]);
assert.deepEqual(cleared, [PACKAGES_TOKEN_REFUSED]);
assert.equal(forge.tokens.filter((t) => t.name === "mesh-packages").length, 1);
assert.deepEqual(forge.tokens.filter(named).map((t) => t.name), ["mesh-packages-2"]);
assert.equal(vaulted(PACKAGES_ACCOUNT), forge.tokens.find(named)!.sha1);
// The tools' token reads packages too: read:package (design 51's read-only tool token).
assert.ok(TOKEN_SCOPES.includes("read:package"));
});
@@ -433,12 +443,12 @@ test("an old token that cannot be deleted after the mint is said, the new token
} finally {
forge.faults.deleteToken = false;
}
const kept = (await readFile(join(env.MESH_GITEA_STATE_DIR, "token"), "utf8")).trim();
const kept = vaulted(TOOLS_ACCOUNT)!;
assert.equal(forge.tokens.find((t) => t.sha1 === kept)?.user, TOOLS_ACCOUNT, "the account's token is kept and works");
assert.ok(forge.tokens.some((t) => t.sha1 === old.sha1));
// The next process reuses the kept token and retires the old one it could not before.
// The next process reuses the vault's token and retires the old one it could not before.
const next = new MintedToken({ url: forge.url, admin: ADMIN, passwordFile: env.MESH_GITEA_ADMIN_PASSWORD_FILE!,
file: join(env.MESH_GITEA_STATE_DIR, "token"), account: TOOLS_ACCOUNT, log: () => {} });
account: TOOLS_ACCOUNT, oldName: TOKEN_NAME, vault: vaultOverBus(), lease: leaseOverState(), stateDir: env.MESH_GITEA_STATE_DIR, log: () => {} });
assert.equal(await next.current(), kept);
assert.ok(!forge.tokens.some((t) => t.sha1 === old.sha1), "the admin's old token was deleted at the next start");
});
@@ -548,7 +558,7 @@ test("the start's pass gives each of the mesh's accounts read, as a collaborator
assert.deepEqual((await fresh(env).listAllRepos()).map((r) => r.full_name).sort(), ["jschoubben/txt-game", "novox/hq"]);
// The account's own token cannot grant itself: the forge asks the repository's administration (the reason for the admin login).
const own = (await readFile(join(env.MESH_GITEA_STATE_DIR, "token"), "utf8")).trim();
const own = vaulted(TOOLS_ACCOUNT)!;
const self = await fetch(`${forge.url}/api/v1/repos/jschoubben/car-hunter/collaborators/${TOOLS_ACCOUNT}`,
{ method: "PUT", headers: { Authorization: `token ${own}`, "Content-Type": "application/json" }, body: JSON.stringify({ permission: "read" }) });
assert.equal(self.status, 404, "a repository the account cannot read is not even there for it");
+361 -253
View File
@@ -4,45 +4,85 @@
// paste in, and pasting one into settings would put a secret in the inventory in plaintext. What
// the mesh does deliver is the admin account: a login the manifest names and a password the vault
// minted and the host unsealed into a file (novox/hq ADR 0086). That account is enough to mint a
// token, so the module mints its own (hq issue 100, the forge's tools):
// token, so the module mints its own (hq issue 100, the forge's tools), for each of its two accounts, the tools'
// (`mesh-tools`) and the package seats' (`mesh-packages`):
//
// - at first use, when none is kept: POST /users/{admin}/tokens over basic auth, with the two
// scopes the tools and the watcher need, and no more;
// - kept in the module's own state, a 0600 file, and read back on the next start — the forge
// hands a token's value out exactly once, so a token not kept is a token lost;
// - re-minted when the forge rejects it (401) or the kept file is gone. The one case that is not
// a fault: the forge's data was restored from a predecessor and the token the file names never
// existed there;
// - minted by one process at a time. The runtime starts each of the bundle's entrypoints as a process
// of its own (novox/hq ADR 0193): the watcher (index.js) and the tools (tools/index.js) are two
// processes over one kept file. Two that minted at once left the forge two tokens by one name, which
// gitea then refuses to delete by name — for ever, and every forge tool with it (the 2026-10-14
// outage). So a mint holds a lock file beside the kept token, and reads the kept file again once it
// holds it: what another process minted meanwhile is reused, not minted over;
// - replaced by id: an old token by this name is found in the account's list and deleted by its id,
// every one of them, so a name held twice is cleared rather than refused.
//
// - minted for the tools' account, not the administrator (novox/hq ADR 0365 §2, issue 469): the administrator's
// login mints it for `mesh-tools` (tools-account.ts), after making that account and its team, and deletes any
// token by this name the administrator itself still holds — the token the tools used before. A token kept
// for another account than the one asked (the scopes line says whose) is minted again at the next start.
// - **kept as a secret, the module's own in the vault** (design 47 H2 §808, §822; H-A-422, H-A-426): written through
// the seat `mesh-vault`'s two-step sealed write `own-put` under the account's name, so the vault keeps it as
// `module/gitea/<account>`, its newest generation only, and read through the seat's `get`, sealed to this process's
// key. Never a file in the runtime state and never plain in a state: the forge hands a token's value out exactly
// once, and the vault is where the module keeps what it must not lose;
// - **one name per mint**, `<account>-<generation>`, the generation the one this own-put makes (get's generation plus
// one; 1 when the vault holds none; H-A-430 (1)). The token minted is written through own-put first, and only then
// is the token it replaces deleted at the forge, by its id: no moment is without a valid token, and a mint that
// fails leaves the old one standing. The first mint replaces the tokens the module kept before under the account's
// old fixed name;
// - **minted by one process at a time** (§808). The runtime starts each of the bundle's entrypoints as a process of its
// own (novox/hq ADR 0193), and two that minted at once left the forge two tokens by one name (the 2026-10-14 outage).
// A process the forge refuses first reads the vault's token; only when the refused token is the vault's current one
// does it take the lease — the key `mint` in the module's state `token-lease`, created only, living `mint-lease` (a
// setting, default 60 s) — and mint. A process that finds the lease taken waits for that key to change, at most the
// lease, then reads the vault again; past it, the lease has expired and the waiter creates it itself;
// - **minted again when older than `token-ttl`** (a setting, default 24 hours), its age the vault's `changed-at`;
// - minted for the account, not the administrator (novox/hq ADR 0365 §2, issue 469): the administrator's login mints
// it for the account, after making that account and its team, and deletes any token named `mesh-tools` the
// administrator itself still holds — the token the tools used before.
//
// An explicitly configured token still wins, and is never minted over: if it is rejected, that is
// reported, not repaired — somebody chose it.
//
// The token is never logged. Lines say that one was minted, reused or renewed, and where it is
// kept; never what it is.
// The token is never logged. Lines say that one was minted, reused or renewed, and under which name;
// never what it is.
import { chmodSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
import { existsSync, mkdirSync, rmSync, writeFileSync, readFileSync } from "node:fs";
import { randomBytes } from "node:crypto";
import { dirname, join } from "node:path";
import { join } from "node:path";
import { generateKeyPair, open, sealAnonymous, type Box, type KeyPair } from "@novox/mesh-sdk/seal";
import { broker } from "@novox/mesh-sdk/messaging";
import { askRuntime } from "@novox/mesh-sdk/runtime";
import { state, type State } from "@novox/mesh-sdk/state";
import { setUpToolsAccount, TOOLS_ACCOUNT, type AdminCall } from "./tools-account.js";
/** The name the token carries in the forge's own list — one per mesh runtime, found by name. */
import { keepMainRules } from "./mainrules.js";
import { durationMs } from "./taken.js";
/** The tools' token's old fixed name in the forge's list, the one the admin's own token carried too (ADR 0365 §2). */
export const TOKEN_NAME = "mesh-tools";
/** The module whose own secrets the vault keeps its tokens as (§822: the module named by the caller's account). */
export const VAULT_MODULE = "gitea";
/** The vault's name of an account's token: `module/gitea/<account>` (H-A-426). */
export function vaultName(account: string): string {
return `module/${VAULT_MODULE}/${account}`;
}
/** The forge's name of an account's token of one generation: `<account>-<generation>` (H-A-422 (2)). */
export function mintName(account: string, generation: number): string {
return `${account}-${generation}`;
}
/** The state the mint's lease is kept in, and its one key (§808, §820). */
export const LEASE_STATE = "token-lease";
export const LEASE_KEY = "mint";
/** token-ttl's and mint-lease's defaults (§808). */
export const TOKEN_TTL_DEFAULT = "24h";
export const MINT_LEASE_DEFAULT = "60s";
/** A setting the node-engine composes as `MESH_CONFIG_<NAME>`, a duration, in milliseconds; the default when unset, and
* when it is no duration (said). */
export function settingMs(env: NodeJS.ProcessEnv, setting: string, fallback: string, log: (line: string) => void = () => {}): number {
const raw = env[`MESH_CONFIG_${setting.toUpperCase().replace(/-/g, "_")}`];
const dflt = durationMs(fallback)!;
if (raw === undefined || raw.trim() === "") return dflt;
const ms = durationMs(raw);
if (ms === null) {
log(`the setting ${setting} is ${JSON.stringify(raw)}, no duration: the default ${fallback} holds`);
return dflt;
}
return ms;
}
/**
* The least the fifteen tools and the watcher need (gitea's route groups, 1.20+ scoped tokens):
* write:repository — create/delete repositories, pull requests (list/get/open/merge);
@@ -58,8 +98,6 @@ export const TOKEN_NAME = "mesh-tools";
* **Not write:admin** (novox/hq ADR 0266). It was here so a person could settle the forge's users through
* `gitea_api` (novox/hq 229); whoever may call a tool includes agents, and the forge's administration is the
* operator's. The provisioner settles users with the admin account's own login (GiteaAdmin), never this token.
* A token kept with a different list is minted again with this one at the next start (the scopes are kept
* beside it), and the old one is deleted from the forge by its id — so the scope a kept token had does not outlive this.
*
* A token kept from before a scope was added lacks it: the forge answers such a call with
* `403 token does not have at least one of required scope(s)`, and the client treats that like a
@@ -100,6 +138,103 @@ export class AdminRefused extends Error {
}
}
/** A token as the vault holds it: the value, the generation and when it changed (`get`'s answer, opened). */
export interface VaultToken {
readonly token: string;
readonly generation: number;
readonly changedAt: string;
}
/** The vault as the minter reaches it: the seat `mesh-vault`'s `get` and `own-put` (§808, §822). */
export interface TokenVault {
/** The account's token the vault holds, or null when it holds none. */
get(account: string): Promise<VaultToken | null>;
/** Write the account's token as the module's own; answers the generation the vault kept. */
put(account: string, token: string): Promise<number>;
}
/** The lease `mint` in the state `token-lease` (§808). */
export interface MintLease {
/** Create the lease for `holder`, living `ms`; false when another holds it. */
take(holder: string, ms: number): Promise<boolean>;
/** The lease's holder and since when, or null when nobody holds it. */
read(): Promise<{ holder: string; since: string } | null>;
/** Delete the lease, when `holder` still holds it. */
release(holder: string): Promise<void>;
/** Resolves when the lease's key changes (deleted, or written again), or after `ms`, whichever is first. */
changed(ms: number): Promise<void>;
}
/** The vault's words when it holds no secret by the name asked (mesh-vault's Get). */
const HOLDS_NONE = /the vault holds no secret named/;
/** The vault over the bus: `get` sealed to this process's own key, made once a process; `own-put` in its two steps. */
export function vaultOverBus(request: (verb: string, body: unknown) => Promise<unknown> = (v, b) => broker().request(v, b)): TokenVault {
let keys: KeyPair | undefined;
return {
async get(account) {
keys ??= generateKeyPair();
let answer: { generation?: number; "changed-at"?: string; sealed?: Box };
try {
answer = (await request("seat:mesh-vault.get", { name: vaultName(account), public_key: keys.publicKey })) as typeof answer;
} catch (err) {
if (HOLDS_NONE.test(err instanceof Error ? err.message : String(err))) return null;
throw err;
}
if (!answer?.sealed) throw new Error(`the vault answered no sealed ${vaultName(account)}`);
return { token: open(answer.sealed, keys.privateKey), generation: answer.generation ?? 0, changedAt: answer["changed-at"] ?? "" };
},
async put(account, token) {
const opened = (await request("seat:mesh-vault.own-put", { name: account })) as { call?: string; seal_to?: string };
if (!opened?.call || !opened.seal_to) throw new Error(`the vault opened no own-put call for ${vaultName(account)}`);
const kept = (await request("seat:mesh-vault.own-put", { call: opened.call, sealed: sealAnonymous(token, opened.seal_to) })) as { generation?: number };
return kept?.generation ?? 0;
},
};
}
/** The runtime's refusal of a create whose key has a value (mesh-sdk's CODE_CHANGED). */
const CODE_CHANGED = -32010;
/** The lease in the module's state `token-lease` over the runtime: a create with the lease as its time to live, and one
* watch of the key a process, handing each change to the waiters. */
export function leaseOverState(st: State<{ holder: string; since: string }> = state(LEASE_STATE),
ask: (method: string, params: unknown) => Promise<unknown> = askRuntime): MintLease {
const waiters = new Set<() => void>();
let watching: Promise<void> | undefined;
return {
async take(holder, ms) {
try {
await ask("mesh/state.create", { state: LEASE_STATE, key: LEASE_KEY, value: { holder, since: new Date().toISOString() },
ttl_seconds: Math.max(1, Math.ceil(ms / 1000)) });
return true;
} catch (err) {
if ((err as { code?: number }).code === CODE_CHANGED) return false;
throw err;
}
},
async read() {
return (await st.get(LEASE_KEY))?.value ?? null;
},
async release(holder) {
if ((await st.get(LEASE_KEY))?.value?.holder === holder) await st.delete(LEASE_KEY);
},
async changed(ms) {
watching ??= st.watch((change) => {
if (!change.current) for (const wake of [...waiters]) wake();
}, { key: LEASE_KEY });
await watching;
await new Promise<void>((resolve) => {
const wake = () => { clearTimeout(timer); waiters.delete(wake); resolve(); };
const timer = setTimeout(wake, ms);
waiters.add(wake);
// Released between the refused create and this wait: nothing to wait for.
void st.get(LEASE_KEY).then((e) => { if (!e) wake(); }, () => {});
});
},
};
}
export interface MintedTokenOptions {
/** The forge, e.g. http://127.0.0.1:3000. */
readonly url: string;
@@ -107,115 +242,118 @@ export interface MintedTokenOptions {
readonly admin: string;
/** The file the host unsealed the admin password into (ADR 0086). Read at mint time, so a rotation takes. */
readonly passwordFile: string;
/** Where the token is kept: a 0600 file in the module's own state. */
readonly file: string;
readonly name?: string;
/** The account the token is minted for, and the vault name it is kept under (H-A-426). */
readonly account: string;
/** The token's fixed name before a name per mint: the first mint deletes the account's tokens by it (H-A-430 (1)). */
readonly oldName: string;
readonly scopes?: readonly string[];
/**
* The account the token is minted for: the tools' own (novox/hq ADR 0365 §2). The admin login mints it, after
* `setUp` made the account, and the admin's own tokens by this name are deleted. Left out, the token is the
* admin's own, as it was before.
*/
readonly account?: string;
/** Run with the admin's password before each mint for `account`: makes the account and its team (tools-account.ts). */
/** Run with the admin's password before each mint: makes the account and its team (tools-account.ts). */
readonly setUp?: (password: string) => Promise<void>;
/** Where the module keeps its token, and the mint's lease (§808). */
readonly vault: TokenVault;
readonly lease: MintLease;
/** token-ttl and mint-lease, in milliseconds (§808). */
readonly ttlMs?: number;
readonly leaseMs?: number;
/** Who holds the lease when this process does: the node and the process. */
readonly holder?: string;
/** The module's runtime state directory, where a retirement of the admin's old token that failed is remembered. */
readonly stateDir?: string;
readonly now?: () => Date;
readonly log?: (line: string) => void;
readonly fetch?: typeof fetch;
}
/** The token the module mints for itself, kept in its state and renewed when the forge rejects it. */
/** The token the module mints for itself, kept in the vault and renewed when the forge rejects it. */
export class MintedToken implements TokenSource {
private held: string | null = null;
private readFile = false;
private held: VaultToken | null = null;
private inflight: Promise<string> | null = null;
private readonly name: string;
private readonly scopes: readonly string[];
private readonly account: string;
private readonly ttlMs: number;
private readonly leaseMs: number;
private readonly holder: string;
private readonly now: () => Date;
private readonly log: (line: string) => void;
private readonly fetchImpl: typeof fetch;
constructor(private readonly opts: MintedTokenOptions) {
this.name = opts.name ?? TOKEN_NAME;
this.scopes = opts.scopes ?? TOKEN_SCOPES;
this.account = opts.account ?? opts.admin;
this.ttlMs = opts.ttlMs ?? durationMs(TOKEN_TTL_DEFAULT)!;
this.leaseMs = opts.leaseMs ?? durationMs(MINT_LEASE_DEFAULT)!;
this.holder = opts.holder ?? `${process.env.MESH_NODE || "unknown"}/${process.pid}`;
this.now = opts.now ?? (() => new Date());
this.log = opts.log ?? ((line) => console.log(`[gitea] ${line}`));
this.fetchImpl = opts.fetch ?? fetch;
}
/** The account this source mints for. */
get account(): string {
return this.opts.account;
}
/**
* Build from the runtime's environment: the forge's URL, the admin login and password file the
* manifest hands the runtime, and the module's state directory (MESH_GITEA_STATE_DIR, a directory
* the runtime mounts writable). Throws, naming what is missing, rather than hand back a source
* that cannot mint.
* The tools' token from the runtime's environment: the forge's URL, the admin login and password file the manifest
* hands the runtime, the vault and the lease over the bus. Throws, naming what is missing, rather than hand back a
* source that cannot mint. One source per account and runtime state in a process: two would each renew on a 401.
*/
static fromEnv(url: string, env: NodeJS.ProcessEnv = process.env): MintedToken {
const opts = MintedToken.optionsFromEnv(url, env);
// One source per kept file in a process: two sources in one process would each renew on a 401.
// This covers one process only. The watcher and the tools are two processes (the runtime starts
// each entrypoint as its own), and those are kept apart by the mint's lock file (mint, below).
const shared = MintedToken.shared.get(opts.file);
const account = env.MESH_GITEA_TOOLS_USER || TOOLS_ACCOUNT;
const { admin, passwordFile } = adminFromEnv(env, "no Gitea token, and nothing to mint one with — set GITEA_TOKEN, or");
const key = `${account} ${env.MESH_GITEA_STATE_DIR ?? ""}`;
const shared = MintedToken.shared.get(key);
if (shared) return shared;
const source = new MintedToken(opts);
MintedToken.shared.set(opts.file, source);
if (account === admin) throw new Error(`MESH_GITEA_TOOLS_USER names the admin account "${admin}"; the tools act as an account of their own (novox/hq ADR 0365)`);
const source = new MintedToken({
...MintedToken.settingsFromEnv(env), url, admin, passwordFile, account, oldName: TOKEN_NAME,
// GiteaAdmin is loaded when first used: client.ts imports this file.
setUp: async (password) => {
const { GiteaAdmin } = await import("./client.js");
await setUpToolsAccount(new GiteaAdmin(url, admin, password), account, () => randomBytes(24).toString("base64url"), keepMainRules(env));
},
});
MintedToken.shared.set(key, source);
return source;
}
private static readonly shared = new Map<string, MintedToken>();
private static optionsFromEnv(url: string, env: NodeJS.ProcessEnv): MintedTokenOptions {
const admin = env.MESH_GITEA_ADMIN_USER;
const passwordFile = env.MESH_GITEA_ADMIN_PASSWORD_FILE;
const stateDir = env.MESH_GITEA_STATE_DIR;
const missing = [
admin ? null : "MESH_GITEA_ADMIN_USER",
passwordFile ? null : "MESH_GITEA_ADMIN_PASSWORD_FILE",
stateDir ? null : "MESH_GITEA_STATE_DIR",
].filter((v): v is string => v !== null);
if (missing.length) {
throw new Error(`no Gitea token, and nothing to mint one with — set GITEA_TOKEN, or ${missing.join(", ")}`);
}
// The tools' own account, never the administrator (novox/hq ADR 0365 §2): MESH_GITEA_TOOLS_USER names it.
const account = env.MESH_GITEA_TOOLS_USER || TOOLS_ACCOUNT;
if (account === admin) throw new Error(`MESH_GITEA_TOOLS_USER names the admin account "${admin}"; the tools act as an account of their own (novox/hq ADR 0365)`);
/** What every account's source takes from the environment: the vault and lease over the bus, the two settings. */
static settingsFromEnv(env: NodeJS.ProcessEnv): Pick<MintedTokenOptions, "vault" | "lease" | "ttlMs" | "leaseMs" | "stateDir"> {
const log = (line: string) => console.log(`[gitea] ${line}`);
return {
url, admin: admin!, passwordFile: passwordFile!, file: join(stateDir!, "token"), account,
// GiteaAdmin is loaded when first used: client.ts imports this file.
setUp: async (password) => {
const { GiteaAdmin } = await import("./client.js");
await setUpToolsAccount(new GiteaAdmin(url, admin!, password), account, () => randomBytes(24).toString("base64url"), keepMainRules(env));
},
vault: sharedVault(), lease: sharedLease(),
ttlMs: settingMs(env, "token-ttl", TOKEN_TTL_DEFAULT, log), leaseMs: settingMs(env, "mint-lease", MINT_LEASE_DEFAULT, log),
stateDir: env.MESH_GITEA_STATE_DIR || undefined,
};
}
/** The token held, read from the vault when none is, minted when the vault holds none or one older than token-ttl. */
async current(): Promise<string> {
if (this.held !== null) return this.held;
if (!this.readFile) {
this.readFile = true;
const kept = this.read();
if (kept !== null && this.keptScopes() === this.scopesLine()) {
this.held = kept;
this.log(`reusing the token kept at ${this.opts.file}`);
// A retirement of the admin's old token that failed after the mint is tried again, once a process.
if (existsSync(this.retireFile)) await this.locked(() => this.retireAdmin()).catch((err) => this.log(`${err}`));
return kept;
}
if (kept !== null) return this.mint("the kept token was minted with other scopes than the tools take now — minting one with them", kept);
if (this.held !== null && !this.expired(this.held)) return this.held.token;
const kept = await this.opts.vault.get(this.opts.account);
if (kept === null) return this.mint("the vault holds no token for this account — minting one", null);
if (this.expired(kept)) return this.mint(`the vault's token is older than token-ttl (${this.ttlMs / 1000}s) — minting one`, kept.token);
if (this.held === null) {
this.log(`reusing the token the vault keeps as ${vaultName(this.opts.account)} (generation ${kept.generation})`);
// A retirement of the admin's old token that failed after the mint is tried again, once a process.
if (this.retireFile && existsSync(this.retireFile)) await this.retireAdmin().catch((err) => this.log(`${err}`));
}
return this.mint("no token kept — minting one", null);
this.held = kept;
return kept.token;
}
async renew(rejected: string): Promise<string> {
// Another caller already renewed while this one was in flight with the old token.
if (this.held !== null && this.held !== rejected) return this.held;
// Or another process did, and kept it: use what is kept before minting over it.
const kept = this.read();
if (kept !== null && kept !== rejected) {
if (this.held !== null && this.held.token !== rejected) return this.held.token;
// A refusal of a stale token mints nothing: the vault's newer one is used (§808).
const kept = await this.opts.vault.get(this.opts.account);
if (kept !== null && kept.token !== rejected) {
this.held = kept;
this.log(`the forge rejected the token held; the kept one at ${this.opts.file} is newer — reusing it`);
return kept;
this.log(`the forge rejected the token held; the vault's ${vaultName(this.opts.account)} is newer (generation ${kept.generation}) — reusing it`);
return kept.token;
}
this.held = null;
return this.mint("the forge rejected the kept token — minting a fresh one", rejected);
return this.mint("the forge rejected the vault's token — minting a fresh one", rejected);
}
/** What the forge's scoped tokens say when a kept token predates a scope the tools now need. */
@@ -223,132 +361,55 @@ export class MintedToken implements TokenSource {
return status === 403 && /required scope/i.test(body);
}
private expired(t: VaultToken): boolean {
const at = Date.parse(t.changedAt);
return Number.isFinite(at) && this.now().getTime() - at > this.ttlMs;
}
/**
* One mint at a time: concurrent first calls in this process share it, and a mint in another process
* on this machine is waited for (the lock file). Once the lock is held, the kept file is read again:
* a token there that is not `stale` (the one rejected, or kept with other scopes) and carries the
* scopes asked for was minted by the process that held the lock before — it is reused.
* One mint at a time: concurrent calls in this process share it, and a mint in another process is waited for (the
* lease). Holding the lease, the vault is read again: a token there that is not `stale` (the one refused or expired)
* was minted by the process that held the lease before — it is used, not minted over.
*/
private mint(why: string, stale: string | null): Promise<string> {
if (this.inflight === null) {
this.log(why);
this.inflight = this.locked(async () => {
const kept = this.read();
if (kept !== null && kept !== stale && this.keptScopes() === this.scopesLine()) {
this.held = kept;
this.log(`another process minted it meanwhile — reusing the token kept at ${this.opts.file}`);
return kept;
}
return this.doMint();
}).finally(() => {
this.inflight = this.leased(stale).finally(() => {
this.inflight = null;
});
}
return this.inflight;
}
/** How long a mint may hold the lock before another process takes it as left behind, and how long one waits. */
static readonly LOCK_STALE_MS = 120_000;
static readonly LOCK_WAIT_MS = 150_000;
/**
* Run `work` holding the lock file beside the kept token: created exclusively, holding this process's
* pid, the time and a nonce; removed after, only when it is still ours. A lock whose process is gone,
* or older than LOCK_STALE_MS, was left by a process that died mid-mint and is taken over — never
* waited on for ever, which would be the same lock-out by another road.
*/
private async locked<T>(work: () => Promise<T>): Promise<T> {
const lock = `${this.opts.file}.lock`;
mkdirSync(dirname(this.opts.file), { recursive: true, mode: 0o700 });
const mine = `${process.pid} ${Date.now()} ${randomBytes(8).toString("hex")}\n`;
const deadline = Date.now() + MintedToken.LOCK_WAIT_MS;
private async leased(stale: string | null): Promise<string> {
for (;;) {
try {
const fd = openSync(lock, "wx", 0o600);
try { writeSync(fd, mine); } finally { closeSync(fd); }
break;
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== "EEXIST") {
throw new Error(`cannot take the mint lock at ${lock}: ${(err as Error).message}`);
if (await this.opts.lease.take(this.holder, this.leaseMs)) {
try {
const kept = await this.opts.vault.get(this.opts.account);
if (kept !== null && kept.token !== stale && !this.expired(kept)) {
this.held = kept;
this.log(`another process minted it meanwhile — reusing the vault's generation ${kept.generation}`);
return kept.token;
}
return await this.doMint(kept);
} finally {
await this.opts.lease.release(this.holder).catch((err) =>
this.log(`the mint lease could not be deleted; it ends with mint-lease — ${err instanceof Error ? err.message : err}`));
}
}
if (this.leftBehind(lock)) {
this.log(`the mint lock at ${lock} was left by a process that is gone — taking it over`);
try { unlinkSync(lock); } catch { /* another waiter took it over first */ }
continue;
const other = await this.opts.lease.read().catch(() => null);
this.log(`another process holds the mint lease${other ? ` (${other.holder} since ${other.since})` : ""} — waiting for it, at most mint-lease (${this.leaseMs / 1000}s)`);
await this.opts.lease.changed(this.leaseMs);
const kept = await this.opts.vault.get(this.opts.account);
if (kept !== null && kept.token !== stale && !this.expired(kept)) {
this.held = kept;
this.log(`the process that held the mint lease minted generation ${kept.generation} — reusing it`);
return kept.token;
}
if (Date.now() > deadline) {
throw new Error(`another process has held the mint lock at ${lock} for over ${MintedToken.LOCK_WAIT_MS / 1000}s — not minting beside it`);
}
await new Promise((r) => setTimeout(r, 50 + Math.floor(Math.random() * 50)));
}
try {
return await work();
} finally {
try {
if (readFileSync(lock, "utf8") === mine) unlinkSync(lock);
} catch { /* already gone */ }
}
}
/** Whether the lock's holder is gone: its pid answers no signal, or it is older than LOCK_STALE_MS. */
private leftBehind(lock: string): boolean {
let text: string;
let age: number;
try {
text = readFileSync(lock, "utf8");
age = Date.now() - statSync(lock).mtimeMs;
} catch {
return false; // released meanwhile: the next attempt takes it
}
if (age > MintedToken.LOCK_STALE_MS) return true;
const pid = Number(text.split(" ")[0]);
if (!Number.isInteger(pid) || pid <= 0) return age > 1_000; // half-written, or not ours: give it a moment
try {
process.kill(pid, 0);
return false;
} catch (err) {
return (err as NodeJS.ErrnoException).code === "ESRCH";
}
}
private read(): string | null {
try {
const token = readFileSync(this.opts.file, "utf8").replace(/\n$/, "");
return token.length ? token : null;
} catch (err) {
if ((err as NodeJS.ErrnoException).code === "ENOENT") return null;
throw new Error(`cannot read the kept Gitea token at ${this.opts.file}: ${(err as Error).message}`);
}
}
/** The scopes as kept beside the token: sorted, one line, and whose it is when it is not the admin's. */
private scopesLine(): string {
const scopes = [...this.scopes].sort().join(",");
return this.account === this.opts.admin ? scopes : `${scopes} for ${this.account}`;
}
/** The scopes the kept token was minted with, "" when not kept (a token kept before they were). */
private keptScopes(): string {
try {
return readFileSync(`${this.opts.file}.scopes`, "utf8").trim();
} catch {
return "";
}
}
/** Write the token at 0600, whole or not at all: a temp file beside it, then a rename. Its scopes beside it. */
private keep(token: string): void {
mkdirSync(dirname(this.opts.file), { recursive: true, mode: 0o700 });
const tmp = `${this.opts.file}.tmp`;
writeFileSync(tmp, token + "\n", { mode: 0o600 });
chmodSync(tmp, 0o600);
renameSync(tmp, this.opts.file);
writeFileSync(`${this.opts.file}.scopes.tmp`, this.scopesLine() + "\n", { mode: 0o600 });
renameSync(`${this.opts.file}.scopes.tmp`, `${this.opts.file}.scopes`);
}
/** One call to the forge's API with the admin login, the password read now. */
/** The admin password, read now. */
private password(): string {
try {
return readFileSync(this.opts.passwordFile, "utf8").replace(/\n$/, "");
@@ -357,6 +418,7 @@ export class MintedToken implements TokenSource {
}
}
/** One call to the forge's API with the admin login. */
private adminCall(password = this.password()): AdminCall {
const authorization = "Basic " + Buffer.from(`${this.opts.admin}:${password}`).toString("base64");
const api = `${this.opts.url.replace(/\/+$/, "")}/api/v1`;
@@ -377,71 +439,81 @@ export class MintedToken implements TokenSource {
}
/** Where a retirement of the admin's old token that failed is remembered, to be tried again (retireAdmin). */
private get retireFile(): string {
return `${this.opts.file}.retire-admin`;
private get retireFile(): string | null {
return this.opts.stateDir ? join(this.opts.stateDir, `${this.opts.account}.retire-admin`) : null;
}
/**
* The admin's own token by this name goes: what the tools used before must not outlive the move (novox/hq ADR
* 0365 §2). Run after the account's token is minted and kept, so a failure here never leaves the tools without
* a token: it is said, remembered beside the kept token, and tried again when a process next reads that token.
* The admin's own token named `mesh-tools` goes: what the tools used before must not outlive the move (novox/hq ADR
* 0365 §2). Run after the account's token is minted and kept, so a failure here never leaves the tools without a
* token: it is said, remembered, and tried again when a process next reads the vault's token.
*/
private async retireAdmin(admin: AdminCall = this.adminCall()): Promise<void> {
if (this.account === this.opts.admin) return;
if (this.opts.oldName !== TOKEN_NAME) return;
try {
const retired = await this.dropNamed(admin, `/users/${encodeURIComponent(this.opts.admin)}/tokens`, this.opts.admin);
if (retired > 0) this.log(`deleted ${retired} token(s) named "${this.name}" the admin account "${this.opts.admin}" held: the tools act as "${this.account}" now`);
rmSync(this.retireFile, { force: true });
const retired = await this.dropNamed(admin, `/users/${encodeURIComponent(this.opts.admin)}/tokens`, this.opts.admin, TOKEN_NAME);
if (retired > 0) this.log(`deleted ${retired} token(s) named "${TOKEN_NAME}" the admin account "${this.opts.admin}" held: the tools act as "${this.opts.account}" now`);
if (this.retireFile) rmSync(this.retireFile, { force: true });
} catch (err) {
writeFileSync(this.retireFile, `${new Date().toISOString()}\n`, { mode: 0o600 });
this.log(`FAILED to delete the token(s) named "${this.name}" the admin account "${this.opts.admin}" holds — tried again at the next start: ${(err as Error).message}`);
if (this.retireFile) {
mkdirSync(this.opts.stateDir!, { recursive: true, mode: 0o700 });
writeFileSync(this.retireFile, `${new Date().toISOString()}\n`, { mode: 0o600 });
}
this.log(`FAILED to delete the token(s) named "${TOKEN_NAME}" the admin account "${this.opts.admin}" holds — tried again at the next start: ${(err as Error).message}`);
}
}
private async doMint(): Promise<string> {
/**
* Mint `<account>-<generation>` (the generation this own-put makes), write it through own-put, and only then delete
* at the forge, by id, the token it replaces: `replaced`'s by its name, or at the first mint the tokens under the
* account's old fixed name (H-A-422 (2), H-A-430 (1)).
*/
private async doMint(replaced: VaultToken | null): Promise<string> {
const password = this.password();
const admin = this.adminCall(password);
const tokensOf = (account: string) => `/users/${encodeURIComponent(account)}/tokens`;
const call = (method: string, path = "", body?: unknown) => admin(method, tokensOf(this.account) + path, body);
const account = this.opts.account;
const tokens = `/users/${encodeURIComponent(account)}/tokens`;
const generation = (replaced?.generation ?? 0) + 1;
const name = mintName(account, generation);
if (this.account !== this.opts.admin) {
// The tools' account and its team first (novox/hq ADR 0365 §2): a failure here fails the mint, tried again
// at the next call.
const me = await admin("GET", "/user");
if (me.status === 401 || me.status === 403) throw new AdminRefused(this.opts.admin, me.status);
await this.opts.setUp?.(password);
}
// The account and its team first (novox/hq ADR 0365 §2): a failure here fails the mint, tried again at the next call.
const me = await admin("GET", "/user");
if (me.status === 401 || me.status === 403) throw new AdminRefused(this.opts.admin, me.status);
await this.opts.setUp?.(password);
let res = await call("POST", "", { name: this.name, scopes: this.scopes });
let res = await admin("POST", tokens, { name, scopes: this.scopes });
if (res.status === 401 || res.status === 403) throw new AdminRefused(this.opts.admin, res.status);
if (res.status === 400 || res.status === 422) {
// The forge still holds a token by this name whose value we no longer have — the kept file
// went while the forge's data stayed, or two processes once minted at once and left two. It is
// ours to replace: find every one by this name in the account's list and delete each by its id
// (gitea refuses a delete by a name held twice: "multiple matches for token name"), then mint.
await this.dropNamed(admin, tokensOf(this.account), this.account, true);
res = await call("POST", "", { name: this.name, scopes: this.scopes });
// A mint of this generation whose own-put never landed left its token at the forge, unused: deleted by its id
// (gitea refuses a delete by a name held twice), then minted again.
await this.dropNamed(admin, tokens, account, name, true);
res = await admin("POST", tokens, { name, scopes: this.scopes });
}
if (res.status !== 201 && res.status !== 200) {
throw new Error(`Gitea POST /users/${this.account}/tokens: ${res.status} ${detail(res.body)}`);
throw new Error(`Gitea POST /users/${account}/tokens: ${res.status} ${detail(res.body)}`);
}
const token = typeof res.body?.sha1 === "string" ? res.body.sha1 : null;
if (!token) throw new Error(`Gitea POST /users/${this.account}/tokens: ${res.status} but no token in the reply`);
if (!token) throw new Error(`Gitea POST /users/${account}/tokens: ${res.status} but no token in the reply`);
this.keep(token);
this.held = token;
this.log(`minted a token for "${this.account}" (${this.scopes.join(", ")}), kept at ${this.opts.file}`);
// Only now, with the account's own token kept, does the admin's old one go.
const kept = await this.opts.vault.put(account, token);
if (kept !== generation) this.log(`the vault kept ${vaultName(account)} at generation ${kept}, the forge names it ${name}`);
this.held = { token, generation: kept, changedAt: this.now().toISOString() };
this.log(`minted ${name} for "${account}" (${this.scopes.join(", ")}), kept in the vault as ${vaultName(account)}`);
// Only now, with the new token kept, does the one it replaces go.
const gone = replaced ? mintName(account, replaced.generation) : this.opts.oldName;
try {
const dropped = await this.dropNamed(admin, tokens, account, gone);
if (dropped > 0) this.log(`deleted ${dropped} token(s) named "${gone}" "${account}" held: ${name} replaces them`);
} catch (err) {
this.log(`FAILED to delete the token "${gone}" ${name} replaces; the forge holds both until the next mint — ${(err as Error).message}`);
}
await this.retireAdmin(admin);
return token;
}
/**
* Delete every token by this name an account holds, each by its id (gitea refuses a delete by a name held twice:
* "multiple matches for token name"); answers how many. `replacing` says the line for a mint that found its name
* taken.
*/
private async dropNamed(admin: AdminCall, tokens: string, account: string, replacing = false): Promise<number> {
/** Delete every token named `name` an account holds, each by its id; answers how many. */
private async dropNamed(admin: AdminCall, tokens: string, account: string, name: string, replacing = false): Promise<number> {
const held: number[] = [];
for (let page = 1; page <= 100; page++) {
const listed = await admin("GET", `${tokens}?page=${page}&limit=50`);
@@ -449,23 +521,59 @@ export class MintedToken implements TokenSource {
throw new Error(`Gitea GET /users/${account}/tokens: ${listed.status} ${detail(listed.body)}`);
}
if (listed.body.length === 0) break;
for (const t of listed.body) if (t?.name === this.name && typeof t.id === "number") held.push(t.id);
}
if (replacing) {
this.log(held.length === 1
? `the forge already holds a token named "${this.name}" — replacing it`
: `the forge holds ${held.length} tokens named "${this.name}" — deleting each by its id and minting one`);
for (const t of listed.body) if (t?.name === name && typeof t.id === "number") held.push(t.id);
}
if (replacing) this.log(`the forge already holds ${held.length} token(s) named "${name}" — deleting each by its id and minting one`);
for (const id of held) {
const dropped = await admin("DELETE", `${tokens}/${id}`);
if (dropped.status !== 204 && dropped.status !== 404) {
throw new Error(`Gitea DELETE /users/${account}/tokens/${id} (named ${this.name}): ${dropped.status} ${detail(dropped.body)}`);
throw new Error(`Gitea DELETE /users/${account}/tokens/${id} (named ${name}): ${dropped.status} ${detail(dropped.body)}`);
}
}
return held.length;
}
}
/** The admin login and password file the manifest hands the runtime, or an error naming what is missing. */
export function adminFromEnv(env: NodeJS.ProcessEnv, missingWords: string): { admin: string; passwordFile: string } {
const admin = env.MESH_GITEA_ADMIN_USER;
const passwordFile = env.MESH_GITEA_ADMIN_PASSWORD_FILE;
const missing = [admin ? null : "MESH_GITEA_ADMIN_USER", passwordFile ? null : "MESH_GITEA_ADMIN_PASSWORD_FILE"]
.filter((v): v is string => v !== null);
if (missing.length) throw new Error(`${missingWords} ${missing.join(", ")}`);
return { admin: admin!, passwordFile: passwordFile! };
}
// One vault and one lease a process, shared by both accounts' sources: one key and one watch.
let vaultOnce: TokenVault | undefined;
let leaseOnce: MintLease | undefined;
function sharedVault(): TokenVault {
return (vaultOnce ??= vaultOverBus());
}
function sharedLease(): MintLease {
return (leaseOnce ??= leaseOverState());
}
/** The tool forge_token_lease's reading (§808): the lease's holder and since when, and each account's token age, read
* from the vault's `changed-at`; never the token. */
export async function tokenLease(vault: TokenVault, lease: MintLease, accounts: readonly string[], now: Date = new Date()) {
const held = await lease.read();
const out: { account: string; vault: string; generation?: number; "changed-at"?: string; "age-seconds"?: number; held: boolean }[] = [];
for (const account of accounts) {
const t = await vault.get(account);
out.push(t === null ? { account, vault: vaultName(account), held: false } : {
account, vault: vaultName(account), held: true, generation: t.generation, "changed-at": t.changedAt,
"age-seconds": Math.max(0, Math.floor((now.getTime() - Date.parse(t.changedAt)) / 1000)),
});
}
return { lease: held === null ? { held: false } : { held: true, holder: held.holder, since: held.since }, tokens: out };
}
/** The vault and the lease this process mints through, for the tool forge_token_lease. */
export function processVaultAndLease(): { vault: TokenVault; lease: MintLease } {
return { vault: sharedVault(), lease: sharedLease() };
}
function detail(body: unknown): string {
return typeof body === "string" ? body : JSON.stringify(body);
}
+16
View File
@@ -0,0 +1,16 @@
// The tool forge_token_lease (design 47 H2 §808, the tools table): the mint lease's holder and since when, and each
// account's token age, read from the vault's `changed-at`; never the token.
import type { ToolDefinition } from "@novox/mesh-sdk/tools";
import { tokenLease, type MintLease, type TokenVault } from "../token.js";
export function tokenLeaseTool(vault: TokenVault, lease: MintLease, accounts: readonly string[], now: () => Date = () => new Date()): ToolDefinition {
return {
name: "forge_token_lease",
description: "The forge token's mint lease and each account's token age (design 47 H2 §808): who holds the lease `mint` in the " +
"state token-lease and since when, and for each account (" + accounts.join(", ") + ") the vault's generation of its token " +
"and when it changed, never the token itself.",
input: { type: "object", properties: {} },
run: async () => tokenLease(vault, lease, accounts, now()),
};
}