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 }