diff --git a/modules/gitea/internal/forgehttp/vault.go b/modules/gitea/internal/forgehttp/vault.go index a78c1bf4e..3ed8dbeff 100644 --- a/modules/gitea/internal/forgehttp/vault.go +++ b/modules/gitea/internal/forgehttp/vault.go @@ -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) } diff --git a/modules/gitea/internal/forgehttp/vault_test.go b/modules/gitea/internal/forgehttp/vault_test.go index 5e3c8f446..b89e1a6a7 100644 --- a/modules/gitea/internal/forgehttp/vault_test.go +++ b/modules/gitea/internal/forgehttp/vault_test.go @@ -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) } }