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
+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)