fix(releaseupdate): load the env file beside the config so update install gets the database password from a shell (#1431)
This commit is contained in:
1 parent
4f2338810b
commit
c5802cf86d
7 files changed
+458
-7
No files matched your search
@@ -0,0 +1,91 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// EnvFileName is the env file beside config.toml that setup writes secrets
|
||||
// to and the systemd units load with EnvironmentFile=-.../env. A process
|
||||
// started outside systemd -- a login shell running `archied update install`
|
||||
// -- does not have those variables, so anything that needs one loads the
|
||||
// file itself.
|
||||
const EnvFileName = "env"
|
||||
|
||||
// EnvFilePath returns the env file beside the given config file, the same
|
||||
// path the systemd units' EnvironmentFile names.
|
||||
func EnvFilePath(configPath string) string {
|
||||
return filepath.Join(filepath.Dir(configPath), EnvFileName)
|
||||
}
|
||||
|
||||
// defaultInstance mirrors configuration.DefaultInstance: the instance name
|
||||
// when ARCHIE_INSTANCE is unset.
|
||||
const defaultInstance = "archie"
|
||||
|
||||
// DefaultConfigPath returns the config file archie binaries read when
|
||||
// -config is not given: $XDG_CONFIG_HOME/<instance>/config.toml, or
|
||||
// ~/.config/<instance>/config.toml. It mirrors configuration.DefaultConfigPath
|
||||
// so a process outside the configuration loader can still find the env file
|
||||
// beside that config.
|
||||
func DefaultConfigPath() string {
|
||||
dir := os.Getenv("XDG_CONFIG_HOME")
|
||||
if dir == "" {
|
||||
if home, err := os.UserHomeDir(); err == nil {
|
||||
dir = filepath.Join(home, ".config")
|
||||
}
|
||||
}
|
||||
instance := os.Getenv("ARCHIE_INSTANCE")
|
||||
if instance == "" {
|
||||
instance = defaultInstance
|
||||
}
|
||||
return filepath.Join(dir, instance, "config.toml")
|
||||
}
|
||||
|
||||
// LoadEnvFile reads the env file at path. A missing file yields no entries
|
||||
// and no error: an install whose database_url carries its own password has
|
||||
// no env file to read.
|
||||
func LoadEnvFile(path string) (map[string]string, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ParseEnvFile(data), nil
|
||||
}
|
||||
|
||||
// ParseEnvFile parses the env file format setup writes and systemd loads:
|
||||
// KEY='value' lines where a literal quote is escaped as '\” , plus unquoted
|
||||
// KEY=value and double-quoted KEY="value" lines. Blank lines and #-comments
|
||||
// are skipped, as are lines without a key.
|
||||
func ParseEnvFile(data []byte) map[string]string {
|
||||
entries := map[string]string{}
|
||||
for line := range strings.Lines(string(data)) {
|
||||
line = strings.TrimRight(line, "\r\n")
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" || strings.HasPrefix(trimmed, "#") {
|
||||
continue
|
||||
}
|
||||
key, value, ok := strings.Cut(trimmed, "=")
|
||||
if !ok || key == "" {
|
||||
continue
|
||||
}
|
||||
entries[key] = parseEnvValue(value)
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
// parseEnvValue strips the quoting one env-file value carries. The
|
||||
// single-quoted form is the one setup writes, so its '\” escape is undone.
|
||||
func parseEnvValue(value string) string {
|
||||
if len(value) >= 2 && value[0] == '\'' && value[len(value)-1] == '\'' {
|
||||
return strings.ReplaceAll(value[1:len(value)-1], `'\''`, "'")
|
||||
}
|
||||
if len(value) >= 2 && value[0] == '"' && value[len(value)-1] == '"' {
|
||||
return value[1 : len(value)-1]
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEnvFilePath(t *testing.T) {
|
||||
if got := EnvFilePath("/home/sam/.config/archie/config.toml"); got != "/home/sam/.config/archie/env" {
|
||||
t.Errorf("EnvFilePath = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultConfigPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
xdgConfigHome string
|
||||
instance string
|
||||
want string
|
||||
}{
|
||||
{name: "defaults", want: filepath.Join(homeConfigDir(t), "archie", "config.toml")},
|
||||
{name: "xdg config home", xdgConfigHome: "/xdg", want: filepath.Join("/xdg", "archie", "config.toml")},
|
||||
{name: "named instance", xdgConfigHome: "/xdg", instance: "archie-dev", want: filepath.Join("/xdg", "archie-dev", "config.toml")},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", tt.xdgConfigHome)
|
||||
t.Setenv("ARCHIE_INSTANCE", tt.instance)
|
||||
if got := DefaultConfigPath(); got != tt.want {
|
||||
t.Errorf("DefaultConfigPath = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func homeConfigDir(t *testing.T) string {
|
||||
t.Helper()
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return filepath.Join(home, ".config")
|
||||
}
|
||||
|
||||
func TestParseEnvFile(t *testing.T) {
|
||||
data := `# archie secrets
|
||||
PGPASSWORD='s3cret'
|
||||
GITEA_TOKEN='a'\''b'
|
||||
PLAIN=unquoted
|
||||
DOUBLE="double-quoted"
|
||||
|
||||
no-equals-sign-line
|
||||
=orphan
|
||||
`
|
||||
got := ParseEnvFile([]byte(data))
|
||||
want := map[string]string{
|
||||
"PGPASSWORD": "s3cret",
|
||||
"GITEA_TOKEN": "a'b",
|
||||
"PLAIN": "unquoted",
|
||||
"DOUBLE": "double-quoted",
|
||||
}
|
||||
if !maps.Equal(got, want) {
|
||||
t.Errorf("ParseEnvFile = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadEnvFile(t *testing.T) {
|
||||
t.Run("missing file is no entries and no error", func(t *testing.T) {
|
||||
entries, err := LoadEnvFile(filepath.Join(t.TempDir(), "env"))
|
||||
if err != nil || entries != nil {
|
||||
t.Errorf("LoadEnvFile = %v, %v, want nil, nil", entries, err)
|
||||
}
|
||||
})
|
||||
t.Run("existing file parses", func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "env")
|
||||
if err := os.WriteFile(path, []byte("PGPASSWORD='pw'\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err := LoadEnvFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if entries["PGPASSWORD"] != "pw" {
|
||||
t.Errorf("entries = %v", entries)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package releaseupdate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
// DatabasePasswordEnv is the variable the PostgreSQL clients (pgx and the
|
||||
// pg_dump/pg_restore tools) read the database password from. Setup stores it
|
||||
// in the env file beside the config, which only the systemd units load.
|
||||
const DatabasePasswordEnv = "PGPASSWORD"
|
||||
|
||||
// configPath returns the config file this transaction runs against: the
|
||||
// installer's ConfigPath, or the default one when it was not given.
|
||||
func (t *Transaction) configPath() string {
|
||||
if t.ConfigPath != "" {
|
||||
return t.ConfigPath
|
||||
}
|
||||
return config.DefaultConfigPath()
|
||||
}
|
||||
|
||||
// loadEnvCredentials makes the env file beside the config visible to this
|
||||
// process and refuses to run without the database password. The units load
|
||||
// that file with EnvironmentFile; run from a login shell, the installer has
|
||||
// to load it itself, so the documented `archied update install` works there.
|
||||
// The process environment wins over the file, so an explicit export always
|
||||
// overrides it.
|
||||
func (t *Transaction) loadEnvCredentials() error {
|
||||
configPath := t.configPath()
|
||||
if urlCarriesPassword(readDatabaseURL(configPath)) {
|
||||
return nil
|
||||
}
|
||||
envPath := config.EnvFilePath(configPath)
|
||||
entries, err := config.LoadEnvFile(envPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read the env file beside the config: %w", err)
|
||||
}
|
||||
password, err := resolveDatabasePassword(os.Getenv, entries)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the update needs the database password but cannot find one: %w; %s is loaded by the systemd units, not by a shell -- export %s or add it to %s",
|
||||
err, envPath, DatabasePasswordEnv, envPath)
|
||||
}
|
||||
if os.Getenv(DatabasePasswordEnv) == "" {
|
||||
if err := os.Setenv(DatabasePasswordEnv, password); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// The rest of the file (forge and chat tokens, provider keys) backs the
|
||||
// "env" secret engine, which reads the process environment: export every
|
||||
// entry the process does not already have, never overriding one.
|
||||
for key, value := range entries {
|
||||
if os.Getenv(key) == "" {
|
||||
if err := os.Setenv(key, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolveDatabasePassword returns the database password in resolution order:
|
||||
// the process environment first (an explicit export, or the systemd unit's
|
||||
// EnvironmentFile), then the env file beside the config. It errors when
|
||||
// neither holds one, so a missing password is never silent.
|
||||
func resolveDatabasePassword(getenv func(string) string, envFile map[string]string) (string, error) {
|
||||
if password := getenv(DatabasePasswordEnv); password != "" {
|
||||
return password, nil
|
||||
}
|
||||
if password := envFile[DatabasePasswordEnv]; password != "" {
|
||||
return password, nil
|
||||
}
|
||||
return "", fmt.Errorf("%s is not set and the env file has no %s entry", DatabasePasswordEnv, DatabasePasswordEnv)
|
||||
}
|
||||
|
||||
// readDatabaseURL reads database_url from the config file at path. It is
|
||||
// best-effort: a config directory, a YAML config or a missing file yields
|
||||
// "", and the caller then falls back to the environment password.
|
||||
func readDatabaseURL(path string) string {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil || !info.Mode().IsRegular() {
|
||||
return ""
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var file struct {
|
||||
DatabaseURL string `toml:"database_url"`
|
||||
}
|
||||
if toml.Unmarshal(data, &file) != nil {
|
||||
return ""
|
||||
}
|
||||
return file.DatabaseURL
|
||||
}
|
||||
|
||||
// urlCarriesPassword reports whether a PostgreSQL connection string already
|
||||
// holds its password -- in a URL's userinfo or as a password= keyword -- in
|
||||
// which case no environment password is needed.
|
||||
func urlCarriesPassword(databaseURL string) bool {
|
||||
if strings.Contains(databaseURL, "password=") {
|
||||
return true
|
||||
}
|
||||
u, err := url.Parse(databaseURL)
|
||||
if err != nil || u.User == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := u.User.Password()
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package releaseupdate
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
func TestResolveDatabasePassword(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env map[string]string
|
||||
envFile map[string]string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "environment wins over the env file", env: map[string]string{"PGPASSWORD": "from-env"}, envFile: map[string]string{"PGPASSWORD": "from-file"}, want: "from-env"},
|
||||
{name: "env file supplies it when the environment does not", envFile: map[string]string{"PGPASSWORD": "from-file"}, want: "from-file"},
|
||||
{name: "neither holds one", wantErr: true},
|
||||
{name: "empty entries do not count", env: map[string]string{"PGPASSWORD": ""}, envFile: map[string]string{"PGPASSWORD": ""}, wantErr: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
getenv := func(key string) string { return tt.env[key] }
|
||||
got, err := resolveDatabasePassword(getenv, tt.envFile)
|
||||
if tt.wantErr {
|
||||
if err == nil || !strings.Contains(err.Error(), DatabasePasswordEnv) {
|
||||
t.Fatalf("resolveDatabasePassword error = %v, want one naming %s", err, DatabasePasswordEnv)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("resolveDatabasePassword: %v", err)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("resolveDatabasePassword = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestURLCarriesPassword(t *testing.T) {
|
||||
tests := []struct {
|
||||
url string
|
||||
want bool
|
||||
}{
|
||||
{"postgres://archie:secret@localhost/archie", true},
|
||||
{"postgres://archie@/archie?host=/run/archie/postgres", false},
|
||||
{"postgres://archie@localhost/archie", false},
|
||||
{"host=localhost user=archie password=secret", true},
|
||||
{"host=localhost user=archie", false},
|
||||
{"", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := urlCarriesPassword(tt.url); got != tt.want {
|
||||
t.Errorf("urlCarriesPassword(%q) = %v, want %v", tt.url, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadEnvCredentials(t *testing.T) {
|
||||
writeConfig := func(t *testing.T, databaseURL string) (configPath string) {
|
||||
t.Helper()
|
||||
configPath = filepath.Join(t.TempDir(), "config.toml")
|
||||
if err := os.WriteFile(configPath, []byte("database_url = \""+databaseURL+"\"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return configPath
|
||||
}
|
||||
|
||||
t.Run("env file beside the config supplies the password", func(t *testing.T) {
|
||||
t.Setenv(DatabasePasswordEnv, "")
|
||||
configPath := writeConfig(t, "postgres://archie@/archie?host=/run/archie/postgres")
|
||||
envPath := config.EnvFilePath(configPath)
|
||||
body := "# secrets\nPGPASSWORD='from-file'\nARCHIE_TEST_TOKEN='file-token'\n"
|
||||
if err := os.WriteFile(envPath, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tx := &Transaction{ConfigPath: configPath}
|
||||
if err := tx.loadEnvCredentials(); err != nil {
|
||||
t.Fatalf("loadEnvCredentials: %v", err)
|
||||
}
|
||||
if got := os.Getenv(DatabasePasswordEnv); got != "from-file" {
|
||||
t.Errorf("%s = %q, want %q", DatabasePasswordEnv, got, "from-file")
|
||||
}
|
||||
if got := os.Getenv("ARCHIE_TEST_TOKEN"); got != "file-token" {
|
||||
t.Errorf("ARCHIE_TEST_TOKEN = %q, want %q", got, "file-token")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("the environment wins over the env file", func(t *testing.T) {
|
||||
t.Setenv(DatabasePasswordEnv, "from-env")
|
||||
t.Setenv("ARCHIE_TEST_TOKEN", "env-token")
|
||||
configPath := writeConfig(t, "postgres://archie@/archie?host=/run/archie/postgres")
|
||||
if err := os.WriteFile(config.EnvFilePath(configPath), []byte("PGPASSWORD='from-file'\nARCHIE_TEST_TOKEN='file-token'\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tx := &Transaction{ConfigPath: configPath}
|
||||
if err := tx.loadEnvCredentials(); err != nil {
|
||||
t.Fatalf("loadEnvCredentials: %v", err)
|
||||
}
|
||||
if got := os.Getenv(DatabasePasswordEnv); got != "from-env" {
|
||||
t.Errorf("%s = %q, want %q", DatabasePasswordEnv, got, "from-env")
|
||||
}
|
||||
if got := os.Getenv("ARCHIE_TEST_TOKEN"); got != "env-token" {
|
||||
t.Errorf("ARCHIE_TEST_TOKEN = %q, want %q", got, "env-token")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("a database_url with its own password needs no env entry", func(t *testing.T) {
|
||||
t.Setenv(DatabasePasswordEnv, "")
|
||||
configPath := writeConfig(t, "postgres://archie:secret@localhost/archie")
|
||||
tx := &Transaction{ConfigPath: configPath}
|
||||
if err := tx.loadEnvCredentials(); err != nil {
|
||||
t.Fatalf("loadEnvCredentials: %v", err)
|
||||
}
|
||||
if got := os.Getenv(DatabasePasswordEnv); got != "" {
|
||||
t.Errorf("%s = %q, want unset", DatabasePasswordEnv, got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no password anywhere is a clear error", func(t *testing.T) {
|
||||
t.Setenv(DatabasePasswordEnv, "")
|
||||
configPath := writeConfig(t, "postgres://archie@/archie?host=/run/archie/postgres")
|
||||
tx := &Transaction{ConfigPath: configPath}
|
||||
err := tx.loadEnvCredentials()
|
||||
if err == nil {
|
||||
t.Fatal("loadEnvCredentials succeeded without any password")
|
||||
}
|
||||
if !strings.Contains(err.Error(), DatabasePasswordEnv) || !strings.Contains(err.Error(), config.EnvFilePath(configPath)) {
|
||||
t.Errorf("error = %v, want it to name %s and the env file path", err, DatabasePasswordEnv)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -59,6 +59,9 @@ func (t *Transaction) Prepare(ctx context.Context, req PrepareRequest) (Result,
|
||||
if !releaseVersion.MatchString(req.Version) {
|
||||
return Result{}, fmt.Errorf("refusing non-release version %q", req.Version)
|
||||
}
|
||||
if err := t.loadEnvCredentials(); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
unlock, held, err := lockState(ctx, t.StateDir, false)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
|
||||
@@ -84,12 +84,17 @@ type Health interface {
|
||||
type Transaction struct {
|
||||
StateDir string
|
||||
BinDir string
|
||||
Units Units
|
||||
Images Images
|
||||
Policy ImagePolicy
|
||||
Database Database
|
||||
Health Health
|
||||
Progress func(string)
|
||||
// ConfigPath is the bootstrap config file the install runs against. Its
|
||||
// sibling env file supplies the credentials the systemd units would
|
||||
// otherwise provide, above all the database password; empty means the
|
||||
// default config path.
|
||||
ConfigPath string
|
||||
Units Units
|
||||
Images Images
|
||||
Policy ImagePolicy
|
||||
Database Database
|
||||
Health Health
|
||||
Progress func(string)
|
||||
|
||||
// HealthTimeout bounds the wait for the restarted daemon to report the
|
||||
// release; it must then stay healthy for Observe, checked every Poll.
|
||||
@@ -125,6 +130,9 @@ func JournalPath(stateDir string) string { return filepath.Join(stateDir, journa
|
||||
// on boot: it never waits for the lock, and a transaction that was prepared
|
||||
// but never started is abandoned rather than started unattended.
|
||||
func (t *Transaction) Run(ctx context.Context, recovering bool) error {
|
||||
if err := t.loadEnvCredentials(); err != nil {
|
||||
return err
|
||||
}
|
||||
unlock, held, err := lockState(ctx, t.StateDir, !recovering)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -134,6 +134,14 @@ func crash(t *testing.T, tx *Transaction, h *fakeHost, phase string) {
|
||||
}
|
||||
|
||||
func TestTransaction(t *testing.T) {
|
||||
// The installer resolves the database password from the environment or
|
||||
// the env file beside its config; pin both to this test's own files so
|
||||
// the result never depends on the machine's real archie config.
|
||||
t.Setenv(DatabasePasswordEnv, "test-password")
|
||||
configPath := filepath.Join(t.TempDir(), "config.toml")
|
||||
if err := os.WriteFile(configPath, []byte("database_url = \"postgres://archie@/archie?host=/tmp/archie-test\"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
type outcome struct {
|
||||
version, image string
|
||||
report string // health_check, or "" for no report
|
||||
@@ -179,7 +187,7 @@ func TestTransaction(t *testing.T) {
|
||||
}
|
||||
report := filepath.Join(root, "report.json")
|
||||
tx := &Transaction{
|
||||
StateDir: filepath.Join(root, "state"), BinDir: host.bin,
|
||||
StateDir: filepath.Join(root, "state"), BinDir: host.bin, ConfigPath: configPath,
|
||||
Units: &host, Images: &host, Policy: &host, Database: &host, Health: &host,
|
||||
HealthTimeout: 20 * time.Millisecond, Observe: 5 * time.Millisecond, Poll: time.Millisecond,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user