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)
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -61,8 +62,10 @@ type VaultToken struct {
|
|||||||
Account string
|
Account string
|
||||||
// File is the token file gitea's TypeScript bundle keeps for Account, "" when the manifest names none.
|
// File is the token file gitea's TypeScript bundle keeps for Account, "" when the manifest names none.
|
||||||
File string
|
File string
|
||||||
ask Ask
|
// FileByDefault says File is DefaultStateDir's, the manifest naming none.
|
||||||
keys seal.KeyPair
|
FileByDefault bool
|
||||||
|
ask Ask
|
||||||
|
keys seal.KeyPair
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
token string
|
token string
|
||||||
@@ -205,14 +208,20 @@ func TokenOf(account string) (*VaultToken, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
t.File = TokenFile(account)
|
t.File, t.FileByDefault = TokenFile(account)
|
||||||
processTokens[account] = t
|
processTokens[account] = t
|
||||||
return t, nil
|
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):
|
// 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.
|
// MESH_GITEA_PACKAGES_TOKEN_FILE for the packages account, MESH_GITEA_TOKEN_FILE for the tools' account; when the
|
||||||
func TokenFile(account string) string {
|
// 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")
|
packages := os.Getenv("MESH_GITEA_PACKAGES_USER")
|
||||||
if packages == "" {
|
if packages == "" {
|
||||||
packages = PackagesAccount
|
packages = PackagesAccount
|
||||||
@@ -221,13 +230,19 @@ func TokenFile(account string) string {
|
|||||||
if tools == "" {
|
if tools == "" {
|
||||||
tools = ToolsAccount
|
tools = ToolsAccount
|
||||||
}
|
}
|
||||||
|
variable, name := "", ""
|
||||||
switch account {
|
switch account {
|
||||||
case packages:
|
case packages:
|
||||||
return os.Getenv("MESH_GITEA_PACKAGES_TOKEN_FILE")
|
variable, name = "MESH_GITEA_PACKAGES_TOKEN_FILE", "packages-token"
|
||||||
case tools:
|
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
|
// 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:
|
case err != nil:
|
||||||
logf("%s is not read at the start (the module mints it): %v", VaultName(t.Account), err)
|
logf("%s is not read at the start (the module mints it): %v", VaultName(t.Account), err)
|
||||||
case t.Source() == SourceFile:
|
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)",
|
how := "named by the manifest"
|
||||||
VaultName(t.Account), t.Account, SourceFile, t.File)
|
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:
|
default:
|
||||||
logf("read %s at generation %d from %s", VaultName(t.Account), t.Generation(), SourceVault)
|
logf("read %s at generation %d from %s", VaultName(t.Account), t.Generation(), SourceVault)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,13 +93,46 @@ func TestATokenComesFromTheVaultOrWhenTheVaultRefusesTheCallersAccountFromTheFil
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// H-A-564: the manifest's file for each account.
|
// H-A-564: the manifest's file for each account wins; without it, the file in gitea's runtime-state directory, said
|
||||||
func TestTheTokenFileOfAnAccountIsTheOneTheManifestNamesForIt(t *testing.T) {
|
// as the default.
|
||||||
t.Setenv("MESH_GITEA_TOKEN_FILE", "/s/token")
|
func TestTheTokenFileOfAnAccountIsTheOneTheManifestNamesForItElseTheDefault(t *testing.T) {
|
||||||
t.Setenv("MESH_GITEA_PACKAGES_TOKEN_FILE", "/s/packages-token")
|
type file struct {
|
||||||
for account, want := range map[string]string{"mesh-tools": "/s/token", "mesh-packages": "/s/packages-token", "other": ""} {
|
path string
|
||||||
if got := TokenFile(account); got != want {
|
byDefault bool
|
||||||
t.Errorf("TokenFile(%s) = %q, want %q", account, got, want)
|
}
|
||||||
}
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user