Files
mesh-catalog/modules/gitea/cmd/forge/account_test.go
T
jschoubben 39ed20c5bc 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)
2026-11-22 01:41:30 +01:00

137 lines
5.2 KiB
Go

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` 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"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/seal"
"gitea/internal/forgehttp"
)
const toolsToken = "tools-token-never-shown"
// 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) {
seen = append(seen, r.Method+" "+r.URL.Path)
if _, _, basic := r.BasicAuth(); basic || r.Header.Get("Authorization") != "token "+toolsToken {
http.Error(w, "unauthorized", 401)
return
}
if r.URL.Path == repo+"/branch_protections" {
http.Error(w, `{"message":"token does not have the administration of this repository"}`, 403)
return
}
if name := strings.TrimPrefix(r.URL.Path, repo+"/branches/"); r.Method == "GET" && name != r.URL.Path {
if rule, ok := protected[name]; ok {
_, _ = w.Write([]byte(`{"name":"` + name + `","commit":{"id":"c0"},"protected":true,"effective_branch_protection_name":"` + rule + `"}`))
return
}
}
r.SetBasicAuth("mesh-admin", f.password) // the fake's own door, past the token checked above
f.handle(w, r)
}))
t.Cleanup(srv.Close)
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, 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 {
t.Fatal(err)
}
if len(f.writes) != 1 || !got.Created {
t.Fatalf("%d writes, %+v", len(f.writes), got)
}
}
func TestTheToolsAccountIsRefusedAProtectedBranchTheForgeSaysItGuardsAndTheDefaultBranch(t *testing.T) {
for branch, rule := range map[string]string{"release/1.0": "release/*", "fix/1-old": "fix/1-old", "main": ""} {
f := newFake(t)
protected := map[string]string{}
if rule != "" {
protected[branch] = rule
}
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") {
t.Errorf("%s: %v", branch, err)
}
if rule != "" && !strings.Contains(err.Error(), rule) {
t.Errorf("%s: the rule %s is not named: %v", branch, rule, err)
}
if len(f.writes)+len(f.patches) != 0 {
t.Errorf("%s: something was written", branch)
}
}
}
func TestNoTokenIsSentWhileTheVaultHoldsNoneForTheAccount(t *testing.T) {
f := newFake(t)
// 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 kept yet") {
t.Fatalf("%v", err)
}
if len(*seen) != 0 {
t.Fatalf("the forge was asked %v", *seen)
}
}
func TestTheBundleReadsItsTokenFromTheVaultAndNeedsNoAdminLogin(t *testing.T) {
t.Setenv("MESH_GITEA_URL", "http://127.0.0.1:3000")
t.Setenv("MESH_GITEA_TOOLS_USER", "")
t.Setenv("MESH_GITEA_ADMIN_USER", "")
t.Setenv("MESH_GITEA_ADMIN_PASSWORD_FILE", "")
f, err := ForgeFromEnv()
if err != nil {
t.Fatal(err)
}
if f.Token == nil || f.Token.Account != "mesh-tools" || f.Account != "mesh-tools" || f.User != "" || f.PasswordFile != "" {
t.Fatalf("%+v", f.Client)
}
}