gitea's Go bundles read the token gitea keeps when the vault refuses their account (design 47 H2, H-A-564) #747

Merged
mesh-tools merged 2 commits from fix/gitea-token-fallback into main 2026-11-23 12:16:02 +00:00
2 changed files with 70 additions and 18 deletions
Showing only changes of commit 263b5d9aba - Show all commits
+29 -10
View File
@@ -16,6 +16,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
@@ -61,8 +62,10 @@ type VaultToken struct {
Account string
// File is the token file gitea's TypeScript bundle keeps for Account, "" when the manifest names none.
File string
ask Ask
keys seal.KeyPair
// FileByDefault says File is DefaultStateDir's, the manifest naming none.
FileByDefault bool
ask Ask
keys seal.KeyPair
mu sync.Mutex
token string
@@ -205,14 +208,20 @@ func TokenOf(account string) (*VaultToken, error) {
if err != nil {
return nil, err
}
t.File = TokenFile(account)
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.
func TokenFile(account string) string {
// 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
@@ -221,13 +230,19 @@ func TokenFile(account string) string {
if tools == "" {
tools = ToolsAccount
}
variable, name := "", ""
switch account {
case packages:
return os.Getenv("MESH_GITEA_PACKAGES_TOKEN_FILE")
variable, name = "MESH_GITEA_PACKAGES_TOKEN_FILE", "packages-token"
case tools:
return os.Getenv("MESH_GITEA_TOKEN_FILE")
variable, name = "MESH_GITEA_TOKEN_FILE", "token"
default:
return "", false
}
return ""
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
@@ -265,8 +280,12 @@ func Start(logf func(format string, args ...any), held ...*VaultToken) {
case err != nil:
logf("%s is not read at the start (the module mints it): %v", VaultName(t.Account), err)
case t.Source() == SourceFile:
logf("the vault refuses %s to this process's account, so %s's token is read from %s, %s (H-A-564)",
VaultName(t.Account), t.Account, SourceFile, t.File)
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)
}
+41 -8
View File
@@ -93,13 +93,46 @@ func TestATokenComesFromTheVaultOrWhenTheVaultRefusesTheCallersAccountFromTheFil
}
}
// 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)
}
// 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)
}
}