Files
mesh-catalog/modules/gitea/internal/forgehttp/vault_test.go
T
jschoubben 263b5d9aba gitea: the Go bundles fall back to the token files in gitea's state directory by default
The binary is placed by hand on novox first, under gitea 0.1.316's composition, which hands the Go bundles
neither MESH_GITEA_TOKEN_FILE nor MESH_GITEA_PACKAGES_TOKEN_FILE, nor any variable naming gitea's state
directory. Without them the fallback reads token and packages-token in /var/lib/mesh/gitea/state (the
manifest's runtime-state directory) and says at start that the path is the default; the manifest's
variables still win when handed.

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

139 lines
5.6 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 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)
}
}