Files
mesh-catalog/modules/gitea/internal/forgehttp/vault_test.go
T
jschoubben 1ced7fce14 gitea: the Go bundles read the token gitea keeps when the vault refuses their account
Every package publish fails since about 11:45 CET: the package-registry and forge bundles ask the vault for
module/gitea/* from inside the tool runner, whose account the vault refuses (no module's account), and the
manifest since C4 hands them no token file. Until gitea runs as its own account, a bundle the vault refuses
reads the token gitea's TypeScript bundle keeps under its state directory (token, packages-token, each with
its scopes line naming the account), says once at start which source each token came from, and reads the
file again at each call. The manifest hands the two bundles MESH_GITEA_TOKEN_FILE and
MESH_GITEA_PACKAGES_TOKEN_FILE again. The fallback ends with gitea's own account.

Builds: design 47 H2 H-A-564 (hq 1b286366), H-A-543
2026-11-23 13:08:18 +01:00

106 lines
4.3 KiB
Go

package forgehttp
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")
vaultHolds := func(token string) Ask {
return func(address string, args any) (json.RawMessage, error) {
a := args.(map[string]any)
value, _ := json.Marshal(map[string]any{"token": token})
box, err := seal.Seal(string(value), a["public_key"].(string))
if err != nil {
return nil, err
}
return json.Marshal(map[string]any{"generation": 3, "sealed": box})
}
}
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 (<machine>.<module>), so it has no own secret here")
for _, c := range []struct {
name, account, file string
ask Ask
token, source, 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"},
} {
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()
if c.err != "" {
if err == nil || !strings.Contains(err.Error(), c.err) {
t.Fatalf("token %q, error %v; want an error saying %q", got, err, c.err)
}
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 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)
}
})
}
// 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.
func TestTheTokenFileOfAnAccountIsTheOneTheManifestNamesForIt(t *testing.T) {
t.Setenv("MESH_GITEA_TOKEN_FILE", "/s/token")
t.Setenv("MESH_GITEA_PACKAGES_TOKEN_FILE", "/s/packages-token")
for account, want := range map[string]string{"mesh-tools": "/s/token", "mesh-packages": "/s/packages-token", "other": ""} {
if got := TokenFile(account); got != want {
t.Errorf("TokenFile(%s) = %q, want %q", account, got, want)
}
}
}