From c1abd7e210fd6ef8d6638a136152d1b673ba55fe Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:07:30 +0100 Subject: [PATCH] gitea: the Go bundles read a token from the vault alone, no token file stands in G2 token-stand-in-goes (Go half): TokenFile, fromFile, DefaultStateDir, SourceFile, the line at start naming the source and the two env lines go. Builds: design 47 H2 H-A-575, H-A-602, H-A-642 --- modules/gitea/cmd/forge/forge_test.go | 12 +- modules/gitea/internal/forgehttp/vault.go | 137 ++---------------- .../gitea/internal/forgehttp/vault_test.go | 106 ++------------ modules/gitea/module.json | 3 - modules/gitea/test/tools-account.test.ts | 9 +- 5 files changed, 34 insertions(+), 233 deletions(-) diff --git a/modules/gitea/cmd/forge/forge_test.go b/modules/gitea/cmd/forge/forge_test.go index 1113d946b..a1f320dd2 100644 --- a/modules/gitea/cmd/forge/forge_test.go +++ b/modules/gitea/cmd/forge/forge_test.go @@ -694,13 +694,6 @@ func TestToolsAreTheOnesTheReadmeNamesAndTheManifestBuilds(t *testing.T) { "cmd/forge": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER"}, "cmd/package-registry": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_PACKAGES_USER"}, } - // H-A-564: until gitea runs as its own account, each Go bundle is handed the token files gitea's TypeScript bundle - // keeps under its state directory, read when the vault refuses the caller's account. - files := map[string]map[string]string{ - "cmd/forge": {"MESH_GITEA_TOKEN_FILE": "${dir:runtime-state}/token"}, - "cmd/package-registry": {"MESH_GITEA_TOKEN_FILE": "${dir:runtime-state}/token", - "MESH_GITEA_PACKAGES_TOKEN_FILE": "${dir:runtime-state}/packages-token"}, - } for _, a := range m.Build.Artifacts { want, ok := wants[a.From] if !ok { @@ -720,9 +713,10 @@ 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) } } + // H-A-575: the vault alone hands a token; no bundle is given a token file. for _, k := range []string{"MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_TOKEN_FILE"} { - if a.Env[k] != files[a.From][k] { - t.Errorf("%s is given %s %q, want %q (H-A-564)", a.From, k, a.Env[k], files[a.From][k]) + if a.Env[k] != "" { + t.Errorf("%s is given %s %q: a token is read from the vault alone (H-A-575)", a.From, k, a.Env[k]) } } } diff --git a/modules/gitea/internal/forgehttp/vault.go b/modules/gitea/internal/forgehttp/vault.go index 3ed8dbeff..a78552200 100644 --- a/modules/gitea/internal/forgehttp/vault.go +++ b/modules/gitea/internal/forgehttp/vault.go @@ -5,18 +5,13 @@ package forgehttp // 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. The bundle holds the token in memory only, and says nothing of it. // -// Until gitea runs as its own account (H-A-543), the vault refuses the tool runner's account these bundles are called -// under, so (H-A-564) when the vault refuses the caller's account, the token is read from the file gitea's TypeScript -// bundle keeps it in under its state directory (MESH_GITEA_TOKEN_FILE for the tools' account, -// MESH_GITEA_PACKAGES_TOKEN_FILE for the packages account, each with its scopes line beside it), and Follow says once -// at start which source each token came from. The fallback ends with H-A-543's delivery. +// Gitea runs as its own account, so the vault hands these bundles its own secrets; no file stands in for the vault +// (H-A-575: the stand-in of H-A-564 ended with H-A-543's delivery). import ( "encoding/json" "errors" "fmt" - "os" - "path/filepath" "strings" "sync" @@ -36,41 +31,15 @@ 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) -// Refusals are what the vault answers when it refuses the caller's account a module's own secret (mesh-vault's -// callerModule and its own-secret judgement): the only answers the token file is read after (H-A-564). -var Refusals = []string{"is no module's account", "handed only to an account of"} - -// refused is whether err is the vault refusing the caller's account. -func refused(err error) bool { - for _, r := range Refusals { - if strings.Contains(err.Error(), r) { - return true - } - } - return false -} - -// Sources of a token: the vault, or the file gitea's TypeScript bundle keeps (H-A-564). -const ( - SourceVault = "the vault" - SourceFile = "the file gitea keeps" -) - -// VaultToken is one account's token as this process holds it, read from the vault, or from File when the vault -// refuses the caller's account (H-A-564). +// VaultToken is one account's token as this process holds it, read from the vault alone. type VaultToken struct { Account string - // File is the token file gitea's TypeScript bundle keeps for Account, "" when the manifest names none. - File string - // FileByDefault says File is DefaultStateDir's, the manifest naming none. - FileByDefault bool - ask Ask - keys seal.KeyPair + ask Ask + keys seal.KeyPair mu sync.Mutex token string generation int - source string } // NewVaultToken is an account's token read through ask, sealed to a key made for this process. @@ -82,20 +51,10 @@ func NewVaultToken(account string, ask Ask) (*VaultToken, error) { return &VaultToken{Account: account, ask: ask, keys: keys}, nil } -// Load reads the account's token from the vault again; from File when the vault refuses the caller's account. +// 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 { - if t.File != "" && refused(err) { - token, ferr := t.fromFile() - if ferr != nil { - return fmt.Errorf("the vault did not hand %s (%v), and %w", VaultName(t.Account), err, ferr) - } - t.mu.Lock() - t.token, t.generation, t.source = token, 0, SourceFile - t.mu.Unlock() - return nil - } return fmt.Errorf("the vault did not hand %s: %w", VaultName(t.Account), err) } var got struct { @@ -121,45 +80,16 @@ func (t *VaultToken) Load() error { return fmt.Errorf("the vault's %s is empty", VaultName(t.Account)) } t.mu.Lock() - t.token, t.generation, t.source = token, got.Generation, SourceVault + t.token, t.generation = token, got.Generation t.mu.Unlock() return nil } -// fromFile is the token gitea's TypeScript bundle keeps in File, refused unless its scopes line names Account (as the -// bundle writes it: " for "). -func (t *VaultToken) fromFile() (string, error) { - raw, err := os.ReadFile(t.File) - if err != nil { - return "", fmt.Errorf("the token file gitea keeps for %s cannot be read: %w", t.Account, err) - } - line, err := os.ReadFile(t.File + ".scopes") - if err != nil || !strings.HasSuffix(strings.TrimSpace(string(line)), " for "+t.Account) { - return "", fmt.Errorf("the token kept at %s is not %s's (its scopes line does not name it); nothing is sent", t.File, t.Account) - } - token := strings.TrimSpace(string(raw)) - if token == "" { - return "", fmt.Errorf("the token kept at %s is empty; nothing is sent", t.File) - } - return token, nil -} - -// Source is where the token held came from, SourceVault or SourceFile; "" when none is held. -func (t *VaultToken) Source() string { - t.mu.Lock() - defer t.mu.Unlock() - return t.source -} - // 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, source := t.token, t.source + token := t.token t.mu.Unlock() - if source == SourceFile { - // gitea's TypeScript bundle mints into its file again on a refusal: the file is read at each call, as before C4. - return t.fromFile() - } if token != "" { return token, nil } @@ -208,45 +138,12 @@ func TokenOf(account string) (*VaultToken, error) { if err != nil { return nil, err } - t.File, t.FileByDefault = TokenFile(account) processTokens[account] = t return t, nil } -// DefaultStateDir is gitea's runtime-state directory (the manifest's `${dir:mesh-state}/state`), where its TypeScript -// bundle keeps the token files, for a composition that hands the Go bundles no token file (gitea 0.1.316 hands them -// no variable naming that directory either; H-A-564). -const DefaultStateDir = "/var/lib/mesh/gitea/state" - -// TokenFile is the file the manifest names for account's token as gitea's TypeScript bundle keeps it (H-A-564): -// MESH_GITEA_PACKAGES_TOKEN_FILE for the packages account, MESH_GITEA_TOKEN_FILE for the tools' account; when the -// variable is not handed, the file in DefaultStateDir, and byDefault says so. "" for any other account. -func TokenFile(account string) (file string, byDefault bool) { - packages := os.Getenv("MESH_GITEA_PACKAGES_USER") - if packages == "" { - packages = PackagesAccount - } - tools := os.Getenv("MESH_GITEA_TOOLS_USER") - if tools == "" { - tools = ToolsAccount - } - variable, name := "", "" - switch account { - case packages: - variable, name = "MESH_GITEA_PACKAGES_TOKEN_FILE", "packages-token" - case tools: - variable, name = "MESH_GITEA_TOKEN_FILE", "token" - default: - return "", false - } - if f := os.Getenv(variable); f != "" { - return f, false - } - return filepath.Join(DefaultStateDir, name), true -} - // Follow reads each account's token once the runtime serves, and again at each `secret-changed` naming it, saying -// through logf what it read and, once at start, from which source (never the token). It returns when the +// 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 @@ -273,21 +170,13 @@ func Follow(logf func(format string, args ...any), accounts ...string) error { }) } -// Start reads each token once and says, through logf, from which source it came (H-A-564), never the token. +// Start reads each token once and says, through logf, that it did (never the token). func Start(logf func(format string, args ...any), held ...*VaultToken) { for _, t := range held { - switch err := t.Load(); { - case err != nil: + if err := t.Load(); err != nil { logf("%s is not read at the start (the module mints it): %v", VaultName(t.Account), err) - case t.Source() == SourceFile: - how := "named by the manifest" - if t.FileByDefault { - how = "the default path, the manifest naming none" - } - logf("the vault refuses %s to this process's account, so %s's token is read from %s, %s (%s; H-A-564)", - VaultName(t.Account), t.Account, SourceFile, t.File, how) - default: - logf("read %s at generation %d from %s", VaultName(t.Account), t.Generation(), SourceVault) + continue } + logf("read %s at generation %d", VaultName(t.Account), t.Generation()) } } diff --git a/modules/gitea/internal/forgehttp/vault_test.go b/modules/gitea/internal/forgehttp/vault_test.go index b89e1a6a7..1f485cb50 100644 --- a/modules/gitea/internal/forgehttp/vault_test.go +++ b/modules/gitea/internal/forgehttp/vault_test.go @@ -4,33 +4,15 @@ import ( "encoding/json" "errors" "fmt" - "os" - "path/filepath" "strings" "testing" "git.novox.be/novox/mesh-sdk/go/seal" ) -// H-A-564: until gitea runs as its own account, a Go bundle the vault refuses reads the token gitea's TypeScript -// bundle keeps in its state directory, and says once at start which source it used; any other refusal reads no file. -func TestATokenComesFromTheVaultOrWhenTheVaultRefusesTheCallersAccountFromTheFileGiteaKeeps(t *testing.T) { - dir := t.TempDir() - keep := func(name, token, scopes string) string { - p := filepath.Join(dir, name) - if err := os.WriteFile(p, []byte(token+"\n"), 0o600); err != nil { - t.Fatal(err) - } - if scopes != "" { - if err := os.WriteFile(p+".scopes", []byte(scopes+"\n"), 0o600); err != nil { - t.Fatal(err) - } - } - return p - } - tools := keep("token", "file-tools", "read:package,read:user,write:issue,write:repository for mesh-tools") - packages := keep("packages-token", "file-packages", "write:package for mesh-packages") - admins := keep("admins-token", "file-admin", "write:admin") +// H-A-575: a Go bundle reads its token from the vault alone and says it did at start; whatever the vault refuses, no +// token file stands in (the stand-in of H-A-564 is gone). +func TestATokenComesFromTheVaultAloneAndNoFileStandsInWhenTheVaultRefuses(t *testing.T) { vaultHolds := func(token string) Ask { return func(address string, args any) (json.RawMessage, error) { a := args.(map[string]any) @@ -45,27 +27,23 @@ func TestATokenComesFromTheVaultOrWhenTheVaultRefusesTheCallersAccountFromTheFil vaultSays := func(said string) Ask { return func(string, any) (json.RawMessage, error) { return nil, errors.New(said) } } - notModule := vaultSays("novox.node-tools is no module's account (.), so it has no own secret here") for _, c := range []struct { - name, account, file string - ask Ask - token, source, err string + name, account string + ask Ask + token, err string }{ - {"the vault hands it", "mesh-tools", tools, vaultHolds("vault-tools"), "vault-tools", SourceVault, ""}, - {"the vault refuses the tool runner: the tools' file", "mesh-tools", tools, notModule, "file-tools", SourceFile, ""}, - {"the vault refuses the tool runner: the packages file", "mesh-packages", packages, notModule, "file-packages", SourceFile, ""}, - {"another module's account: the file", "mesh-tools", tools, - vaultSays("module/gitea/mesh-tools is gitea's own secret, handed only to an account of gitea"), "file-tools", SourceFile, ""}, - {"the vault holds none: no file is read", "mesh-tools", tools, vaultSays("the vault holds no secret named module/gitea/mesh-tools"), "", "", "holds no secret"}, - {"no file named: the refusal stands", "mesh-tools", "", notModule, "", "", "no module's account"}, - {"a file whose scopes line names another account is not sent", "mesh-tools", admins, notModule, "", "", "is not mesh-tools's"}, + {"the vault hands it", "mesh-tools", vaultHolds("vault-tools"), "vault-tools", ""}, + {"the vault refuses the tool runner: no file is read", "mesh-packages", + vaultSays("novox.node-tools is no module's account (.), so it has no own secret here"), "", "no module's account"}, + {"another module's account: no file is read", "mesh-tools", + vaultSays("module/gitea/mesh-tools is gitea's own secret, handed only to an account of gitea"), "", "handed only to an account of gitea"}, + {"the vault holds none", "mesh-tools", vaultSays("the vault holds no secret named module/gitea/mesh-tools"), "", "holds no secret"}, } { t.Run(c.name, func(t *testing.T) { v, err := NewVaultToken(c.account, c.ask) if err != nil { t.Fatal(err) } - v.File = c.file var said []string Start(func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }, v) got, err := v.Token() @@ -75,64 +53,12 @@ func TestATokenComesFromTheVaultOrWhenTheVaultRefusesTheCallersAccountFromTheFil } return } - if err != nil || got != c.token || v.Source() != c.source { - t.Fatalf("token %q from %q (%v); want %q from %q", got, v.Source(), err, c.token, c.source) + if err != nil || got != c.token { + t.Fatalf("token %q (%v); want %q", got, err, c.token) } - if len(said) != 1 || !strings.Contains(said[0], c.source) || strings.Contains(said[0], c.token) { - t.Fatalf("said %q: one line naming %q, never the token", said, c.source) + if len(said) != 1 || strings.Contains(said[0], c.token) || strings.Contains(said[0], "file") { + t.Fatalf("said %q: one line, never the token and naming no file", said) } }) } - // A token gitea mints into its file again is the one sent next. - v, _ := NewVaultToken("mesh-tools", notModule) - v.File = tools - Start(func(string, ...any) {}, v) - keep("token", "file-tools-2", "read:package for mesh-tools") - if got, _ := v.Token(); got != "file-tools-2" { - t.Fatalf("after gitea minted again, %q is sent", got) - } -} - -// H-A-564: the manifest's file for each account wins; without it, the file in gitea's runtime-state directory, said -// as the default. -func TestTheTokenFileOfAnAccountIsTheOneTheManifestNamesForItElseTheDefault(t *testing.T) { - type file struct { - path string - byDefault bool - } - for _, c := range []struct { - name string - tools, packs string - want map[string]file - }{ - {"named by the manifest", "/s/token", "/s/packages-token", map[string]file{ - "mesh-tools": {"/s/token", false}, "mesh-packages": {"/s/packages-token", false}, "other": {"", false}}}, - {"named by none: the default", "", "", map[string]file{ - "mesh-tools": {"/var/lib/mesh/gitea/state/token", true}, - "mesh-packages": {"/var/lib/mesh/gitea/state/packages-token", true}, "other": {"", false}}}, - } { - t.Run(c.name, func(t *testing.T) { - t.Setenv("MESH_GITEA_TOKEN_FILE", c.tools) - t.Setenv("MESH_GITEA_PACKAGES_TOKEN_FILE", c.packs) - for account, want := range c.want { - if p, d := TokenFile(account); (file{p, d}) != want { - t.Errorf("TokenFile(%s) = %q, %v; want %q, %v", account, p, d, want.path, want.byDefault) - } - } - }) - } - // The start line says the default as such. - dir := t.TempDir() - p := filepath.Join(dir, "token") - _ = os.WriteFile(p, []byte("t\n"), 0o600) - _ = os.WriteFile(p+".scopes", []byte("read:user for mesh-tools\n"), 0o600) - v, _ := NewVaultToken("mesh-tools", func(string, any) (json.RawMessage, error) { - return nil, errors.New("novox.node-tools is no module's account") - }) - v.File, v.FileByDefault = p, true - var said string - Start(func(f string, a ...any) { said = fmt.Sprintf(f, a...) }, v) - if !strings.Contains(said, "the default path") { - t.Fatalf("said %q, not that the file is the default", said) - } } diff --git a/modules/gitea/module.json b/modules/gitea/module.json index 1456d59a6..7d11eb504 100644 --- a/modules/gitea/module.json +++ b/modules/gitea/module.json @@ -559,9 +559,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" } }, @@ -578,7 +576,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}" } }, diff --git a/modules/gitea/test/tools-account.test.ts b/modules/gitea/test/tools-account.test.ts index 69f4efc43..db8193b6a 100644 --- a/modules/gitea/test/tools-account.test.ts +++ b/modules/gitea/test/tools-account.test.ts @@ -374,14 +374,9 @@ test("the manifest hands the tools their account, and the Go forge bundle holds const packages = manifest.build.artifacts.find((a: any) => a.name === "package-registry"); assert.equal(packages.env.MESH_GITEA_PACKAGES_USER, PACKAGES_ACCOUNT); assert.equal(packages.env.MESH_GITEA_ADMIN_PASSWORD_FILE, undefined); - // H-A-564: until gitea runs as its own account, the Go bundles are handed the token files the TypeScript bundle keeps - // under its state directory, read when the vault refuses the caller's account; no other artifact is. - const files: Record = { - forge: ["${dir:runtime-state}/token", undefined], - "package-registry": ["${dir:runtime-state}/token", "${dir:runtime-state}/packages-token"], - }; + // H-A-575: the vault alone hands a token: no artifact is given a token file. for (const a of manifest.build.artifacts) { - assert.deepEqual([a.env?.MESH_GITEA_TOKEN_FILE, a.env?.MESH_GITEA_PACKAGES_TOKEN_FILE], files[a.name] ?? [undefined, undefined], + assert.deepEqual([a.env?.MESH_GITEA_TOKEN_FILE, a.env?.MESH_GITEA_PACKAGES_TOKEN_FILE], [undefined, undefined], `${a.name}'s token files`); } assert.ok(manifest.invokes.includes("seat:mesh-vault.own-put") && manifest.invokes.includes("seat:mesh-vault.get"), manifest.invokes.join(","));