mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
612 lines
20 KiB
Go
612 lines
20 KiB
Go
package install
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/phishingclub/phishingclub/config"
|
|
)
|
|
|
|
var hardPasswords = []string{
|
|
"simple",
|
|
"with space",
|
|
`quote'inside`,
|
|
`back\slash`,
|
|
`\'`,
|
|
`\\'`,
|
|
`ends with \`,
|
|
"at@colon:slash/hash#query?amp&eq=",
|
|
" lead and trail ",
|
|
"ünïcødé",
|
|
}
|
|
|
|
// TestDSNWithPassword checks a hard password is added to both DSN forms and
|
|
// read back as written, with the other settings kept
|
|
func TestDSNWithPassword(t *testing.T) {
|
|
for _, base := range []string{
|
|
"host=db.example.com user=pc dbname=pc sslmode=verify-full",
|
|
"postgres://pc@db.example.com:5432/pc?sslmode=verify-full",
|
|
} {
|
|
for _, pw := range hardPasswords {
|
|
dsn, err := dsnWithPassword(base, pw)
|
|
if err != nil {
|
|
t.Fatalf("%s %q: %v", base, pw, err)
|
|
}
|
|
cfg, err := pgx.ParseConfig(dsn)
|
|
if err != nil {
|
|
t.Fatalf("%s %q: parse: %v", base, pw, err)
|
|
}
|
|
if cfg.Password != pw {
|
|
t.Fatalf("%s: password %q read back as %q", base, pw, cfg.Password)
|
|
}
|
|
if cfg.User != "pc" || cfg.Database != "pc" || cfg.Host != "db.example.com" || cfg.Port != 5432 {
|
|
t.Fatalf("%s %q: other settings changed: %s %s %s %d", base, pw, cfg.User, cfg.Database, cfg.Host, cfg.Port)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestDSNWithPasswordRefused checks a DSN that already holds a password, has
|
|
// no user, or whose typo would move the password into another field or let
|
|
// another password win, is refused without showing a password
|
|
func TestDSNWithPasswordRefused(t *testing.T) {
|
|
cases := map[string]string{
|
|
"url with password": "postgres://pc:oldsecret@db/pc",
|
|
"url without user": "postgres://db/pc",
|
|
"key value with password": "host=db user=pc password=oldsecret",
|
|
"key value trailing backslash": `host=db user=pc dbname=abc\`,
|
|
"url query password": "postgres://pc@db/pc?password=oldsecret",
|
|
"url query password case": "postgres://pc@db/pc?sslmode=disable&password=oldsecret",
|
|
}
|
|
for name, dsn := range cases {
|
|
if merged, err := dsnWithPassword(dsn, "Secr3tPw"); err == nil {
|
|
t.Fatalf("%s: no error, got %q", name, merged)
|
|
} else if strings.Contains(err.Error(), "Secr3tPw") || strings.Contains(err.Error(), "oldsecret") {
|
|
t.Fatalf("%s: error holds a password: %v", name, err)
|
|
}
|
|
}
|
|
// no password keeps the DSN as written
|
|
if dsn, err := dsnWithPassword("postgres://pc@db/pc", ""); err != nil || dsn != "postgres://pc@db/pc" {
|
|
t.Fatalf("empty password changed the DSN: %q %v", dsn, err)
|
|
}
|
|
// the same DSN as the trailing backslash with a closed value works
|
|
if _, err := dsnWithPassword(`host=db user=pc dbname='abc\\'`, "Secr3tPw"); err != nil {
|
|
t.Fatalf("escaped backslash: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestApplyDatabaseConfig(t *testing.T) {
|
|
t.Setenv(config.EnvDatabaseDSN, "")
|
|
|
|
conf := config.NewProductionDefaultConfig()
|
|
if err := applyDatabaseConfig(conf, "sqlite3", config.DefaultAdministrationDSN, ""); err != nil {
|
|
t.Fatalf("sqlite default: %v", err)
|
|
}
|
|
if usesPostgres(conf) {
|
|
t.Fatal("sqlite reported as postgres")
|
|
}
|
|
|
|
refused := []struct {
|
|
name, engine, dsn, password string
|
|
}{
|
|
{"unknown engine", "mysql", "x", ""},
|
|
{"sqlite with password", "sqlite3", config.DefaultAdministrationDSN, "secretpw"},
|
|
{"sqlite with postgres dsn", "sqlite3", "postgres://pc@db/pc", ""},
|
|
{"postgres without dsn", "postgres", "", ""},
|
|
{"postgres with sqlite default", "postgres", config.DefaultAdministrationDSN, ""},
|
|
{"postgres simple protocol", "postgres", "postgres://pc@db/pc?default_query_exec_mode=simple_protocol", "secretpw"},
|
|
{"postgres exec", "postgres", "host=db user=pc default_query_exec_mode=exec", "secretpw"},
|
|
{"postgres bad sslmode", "postgres", "postgres://pc@db/pc?sslmode=nope", "secretpw"},
|
|
}
|
|
for _, c := range refused {
|
|
conf := config.NewProductionDefaultConfig()
|
|
err := applyDatabaseConfig(conf, c.engine, c.dsn, c.password)
|
|
if err == nil {
|
|
t.Fatalf("%s: no error", c.name)
|
|
}
|
|
if strings.Contains(err.Error(), "secretpw") {
|
|
t.Fatalf("%s: error holds the password", c.name)
|
|
}
|
|
if conf.Database().Engine != config.DatabaseEngineSQLite || conf.Database().DSN != config.DefaultAdministrationDSN {
|
|
t.Fatalf("%s: config changed on error", c.name)
|
|
}
|
|
}
|
|
|
|
conf = config.NewProductionDefaultConfig()
|
|
if err := applyDatabaseConfig(conf, " Postgres ", "postgres://pc@db.example.com/pc?sslmode=verify-full", "p'w"); err != nil {
|
|
t.Fatalf("postgres: %v", err)
|
|
}
|
|
if !usesPostgres(conf) {
|
|
t.Fatal("postgres not set")
|
|
}
|
|
cfg, err := pgx.ParseConfig(conf.Database().DSN)
|
|
if err != nil || cfg.Password != "p'w" {
|
|
t.Fatalf("stored DSN does not hold the password: %v", err)
|
|
}
|
|
if w := conf.Database().TransportWarning(); w != "" {
|
|
t.Fatalf("verify-full warned: %s", w)
|
|
}
|
|
}
|
|
|
|
// fakePostgres is a server that asks every client for a cleartext password
|
|
// and keeps all bytes it is sent
|
|
type fakePostgres struct {
|
|
ln net.Listener
|
|
mu sync.Mutex
|
|
got bytes.Buffer
|
|
conns int
|
|
}
|
|
|
|
func startFakePostgres(t *testing.T) *fakePostgres {
|
|
t.Helper()
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := &fakePostgres{ln: ln}
|
|
t.Cleanup(func() { _ = ln.Close() })
|
|
go func() {
|
|
for {
|
|
c, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
go f.serve(c)
|
|
}
|
|
}()
|
|
return f
|
|
}
|
|
|
|
func (f *fakePostgres) port() int {
|
|
return f.ln.Addr().(*net.TCPAddr).Port
|
|
}
|
|
|
|
func (f *fakePostgres) record(b []byte) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.got.Write(b)
|
|
}
|
|
|
|
// readMessage reads a message with a length after an optional type byte
|
|
func (f *fakePostgres) readMessage(c net.Conn, typed bool) ([]byte, error) {
|
|
head := 4
|
|
if typed {
|
|
head = 5
|
|
}
|
|
h := make([]byte, head)
|
|
if _, err := io.ReadFull(c, h); err != nil {
|
|
return nil, err
|
|
}
|
|
n := int(binary.BigEndian.Uint32(h[head-4:]))
|
|
if n < 4 || n > 1<<16 {
|
|
return nil, fmt.Errorf("bad length %d", n)
|
|
}
|
|
body := make([]byte, n-4)
|
|
if _, err := io.ReadFull(c, body); err != nil {
|
|
return nil, err
|
|
}
|
|
f.record(h)
|
|
f.record(body)
|
|
return body, nil
|
|
}
|
|
|
|
func (f *fakePostgres) serve(c net.Conn) {
|
|
defer c.Close()
|
|
_ = c.SetDeadline(time.Now().Add(5 * time.Second))
|
|
f.mu.Lock()
|
|
f.conns++
|
|
f.mu.Unlock()
|
|
for {
|
|
body, err := f.readMessage(c, false)
|
|
if err != nil || len(body) < 4 {
|
|
return
|
|
}
|
|
// SSL and GSS encryption requests are refused, the startup follows
|
|
code := binary.BigEndian.Uint32(body[:4])
|
|
if code == 80877103 || code == 80877104 {
|
|
if _, err := c.Write([]byte{'N'}); err != nil {
|
|
return
|
|
}
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
// AuthenticationCleartextPassword
|
|
if _, err := c.Write([]byte{'R', 0, 0, 0, 8, 0, 0, 0, 3}); err != nil {
|
|
return
|
|
}
|
|
_, _ = f.readMessage(c, true)
|
|
}
|
|
|
|
func (f *fakePostgres) seen() (int, string) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.conns, f.got.String()
|
|
}
|
|
|
|
// writePlaced writes a postgres config with the DSN and password file
|
|
func writePlaced(t *testing.T, dir, dsn, passwordFile string) string {
|
|
t.Helper()
|
|
dto := config.NewProductionDefaultConfig().ToDTO()
|
|
dto.Database.Engine = config.DatabaseEnginePostgres
|
|
dto.Database.DSN = dsn
|
|
dto.Database.PasswordFile = passwordFile
|
|
raw, err := json.Marshal(dto)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(dir, "config.json")
|
|
if err := os.WriteFile(path, raw, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
// writeSecret writes a file only its owner can read and change
|
|
func writeSecret(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte(content), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// firstInstall makes the checks see no service user and no unit file
|
|
func firstInstall(t *testing.T) {
|
|
t.Helper()
|
|
savedLookup, savedUnit := lookupServiceUser, serviceUnitPath
|
|
lookupServiceUser = func() error { return user.UnknownUserError(serviceUser) }
|
|
serviceUnitPath = filepath.Join(t.TempDir(), "phishingclub.service")
|
|
t.Cleanup(func() { lookupServiceUser, serviceUnitPath = savedLookup, savedUnit })
|
|
}
|
|
|
|
// trustTestUser lets the files of the test user count as owned by root on a
|
|
// first install
|
|
func trustTestUser(t *testing.T) {
|
|
t.Helper()
|
|
firstInstall(t)
|
|
saved := trustedOwners
|
|
trustedOwners = []uint32{0, uint32(os.Getuid())} // #nosec G115 a user id fits
|
|
t.Cleanup(func() { trustedOwners = saved })
|
|
if reason := pathNotTrusted(filepath.Join(t.TempDir())); !strings.Contains(reason, "not a regular file") {
|
|
t.Fatalf("the test folder is not trusted: %s", reason)
|
|
}
|
|
}
|
|
|
|
// TestPlacedConfigOfOtherUserNotUsed checks a config that a user other than
|
|
// root can change makes the root installer read no file it names and send
|
|
// nothing: not the password file, not a passfile in the DSN and not the
|
|
// password files or variables of root
|
|
func TestPlacedConfigOfOtherUserNotUsed(t *testing.T) {
|
|
t.Setenv(config.EnvDatabaseDSN, "")
|
|
t.Setenv(config.EnvDatabasePasswordFile, "")
|
|
firstInstall(t)
|
|
srv := startFakePostgres(t)
|
|
dir := t.TempDir()
|
|
secret := filepath.Join(dir, "id_ed25519")
|
|
writeSecret(t, secret, "SECRET-KEY-LINE-1\nSECRET-KEY-LINE-2\n")
|
|
pgpass := filepath.Join(dir, "pgpass")
|
|
writeSecret(t, pgpass, "*:*:*:*:SECRET-PGPASS\n")
|
|
t.Setenv("PGPASSFILE", pgpass)
|
|
t.Setenv("PGPASSWORD", "SECRET-ENV")
|
|
dsn := fmt.Sprintf("postgres://x@127.0.0.1:%d/x?sslmode=disable", srv.port())
|
|
|
|
cases := map[string]struct{ dsn, passwordFile string }{
|
|
"password file": {dsn, secret},
|
|
"passfile in dsn": {dsn + "&passfile=" + pgpass, ""},
|
|
"pgpass of root": {dsn, ""},
|
|
}
|
|
for name, c := range cases {
|
|
// the test user is not root, so the files count as owned by another user
|
|
path := writePlaced(t, t.TempDir(), c.dsn, c.passwordFile)
|
|
placed, err := readPlacedConfig(path)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if placed == nil || !placed.postgres || placed.conf != nil || !strings.Contains(placed.untrusted, "not root") {
|
|
t.Fatalf("%s: config of another user trusted: %+v", name, placed)
|
|
}
|
|
postgres, err := checkPlacedDatabase(path)
|
|
if err != nil || !postgres {
|
|
t.Fatalf("%s: %v %v", name, postgres, err)
|
|
}
|
|
}
|
|
if conns, got := srv.seen(); conns != 0 || got != "" {
|
|
t.Fatalf("the installer connected %d times and sent %q", conns, got)
|
|
}
|
|
}
|
|
|
|
// TestPlacedConfigFilesOthersCanChange checks the files and folders others
|
|
// can change are refused even when their owner is trusted
|
|
func TestPlacedConfigFilesOthersCanChange(t *testing.T) {
|
|
t.Setenv(config.EnvDatabaseDSN, "")
|
|
t.Setenv(config.EnvDatabasePasswordFile, "")
|
|
trustTestUser(t)
|
|
srv := startFakePostgres(t)
|
|
dsn := fmt.Sprintf("postgres://x@127.0.0.1:%d/x?sslmode=disable", srv.port())
|
|
secretDir := t.TempDir()
|
|
secret := filepath.Join(secretDir, "id_ed25519")
|
|
writeSecret(t, secret, "SECRET-KEY")
|
|
|
|
check := func(name, path, want string) {
|
|
t.Helper()
|
|
placed, err := readPlacedConfig(path)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if placed == nil || placed.conf != nil || !strings.Contains(placed.untrusted, want) {
|
|
t.Fatalf("%s: want untrusted %q, got %+v", name, want, placed)
|
|
}
|
|
if _, err := checkPlacedDatabase(path); err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
}
|
|
|
|
// config writable by others
|
|
dir := t.TempDir()
|
|
path := writePlaced(t, dir, dsn, "")
|
|
if err := os.Chmod(path, 0602); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("config writable", path, "writable by group or others")
|
|
|
|
// folder of the config writable by others
|
|
dir = t.TempDir()
|
|
path = writePlaced(t, dir, dsn, "")
|
|
if err := os.Chmod(dir, 0777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("folder writable", path, "writable by group or others")
|
|
|
|
// password file a symlink to a secret
|
|
dir = t.TempDir()
|
|
link := filepath.Join(dir, "db_password")
|
|
if err := os.Symlink(secret, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("password file symlink", writePlaced(t, dir, dsn, link), "is a symlink")
|
|
|
|
// password file below a folder others can write
|
|
openDir := t.TempDir()
|
|
if err := os.Chmod(openDir, 0777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pw := filepath.Join(openDir, "db_password")
|
|
writeSecret(t, pw, "SECRET-KEY")
|
|
check("password folder writable", writePlaced(t, t.TempDir(), dsn, pw), "writable by group or others")
|
|
|
|
// password file writable by group
|
|
pw = filepath.Join(t.TempDir(), "db_password")
|
|
writeSecret(t, pw, "SECRET-KEY")
|
|
if err := os.Chmod(pw, 0620); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("password file writable", writePlaced(t, t.TempDir(), dsn, pw), "writable by group or others")
|
|
|
|
// a relative password file
|
|
check("relative password file", writePlaced(t, t.TempDir(), dsn, "db_password"), "not an absolute path")
|
|
|
|
// a path with a .. part, the kernel follows a symlink before it
|
|
linkDir := t.TempDir()
|
|
if err := os.Symlink(t.TempDir(), filepath.Join(linkDir, "link")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
unclean := filepath.Join(linkDir, "link") + "/../" + filepath.Base(secretDir) + "/id_ed25519"
|
|
check("password file path not clean", writePlaced(t, t.TempDir(), dsn, unclean), "not a clean path")
|
|
|
|
// a password file with a second hard link
|
|
pw = filepath.Join(t.TempDir(), "db_password")
|
|
writeSecret(t, pw, "SECRET-KEY")
|
|
if err := os.Link(pw, filepath.Join(t.TempDir(), "other")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("password file hard link", writePlaced(t, t.TempDir(), dsn, pw), "more than one hard link")
|
|
|
|
// a FIFO as the password file
|
|
pw = filepath.Join(t.TempDir(), "db_password")
|
|
if err := syscall.Mkfifo(pw, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("password file FIFO", writePlaced(t, t.TempDir(), dsn, pw), "not a regular file")
|
|
|
|
// a symlink in place of the config in a folder others can write is not
|
|
// read, the engine comes from the unit file
|
|
dir = t.TempDir()
|
|
target := writePlaced(t, t.TempDir(), dsn, "")
|
|
if err := os.Symlink(target, filepath.Join(dir, "config.json")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(dir, 0777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
placed, err := readPlacedConfig(filepath.Join(dir, "config.json"))
|
|
if err != nil || placed == nil || placed.unread == "" || placed.conf != nil || placed.postgres {
|
|
t.Fatalf("symlink config of others read: %+v %v", placed, err)
|
|
}
|
|
if postgres, err := checkPlacedDatabase(filepath.Join(dir, "config.json")); err != nil || postgres {
|
|
t.Fatalf("symlink config of others: %v %v", postgres, err)
|
|
}
|
|
|
|
if conns, got := srv.seen(); conns != 0 || got != "" {
|
|
t.Fatalf("the installer connected %d times and sent %q", conns, got)
|
|
}
|
|
}
|
|
|
|
// TestPlacedConfigOfRootTested checks a config only root can change is
|
|
// tested with its password file, and the libpq variables and password file
|
|
// of root are not used for the test and are put back after it
|
|
func TestPlacedConfigOfRootTested(t *testing.T) {
|
|
t.Setenv(config.EnvDatabaseDSN, "")
|
|
t.Setenv(config.EnvDatabasePasswordFile, "")
|
|
trustTestUser(t)
|
|
srv := startFakePostgres(t)
|
|
dsn := fmt.Sprintf("postgres://x@127.0.0.1:%d/x?sslmode=disable", srv.port())
|
|
pgpass := filepath.Join(t.TempDir(), "pgpass")
|
|
writeSecret(t, pgpass, "*:*:*:*:SECRET-PGPASS\n")
|
|
t.Setenv("PGPASSFILE", pgpass)
|
|
t.Setenv("PGPASSWORD", "SECRET-ENV")
|
|
t.Setenv("PGSSLMODE", "require")
|
|
|
|
// with the password file of the config
|
|
dir := t.TempDir()
|
|
pw := filepath.Join(dir, "db_password")
|
|
writeSecret(t, pw, "config-password\n")
|
|
postgres, err := checkPlacedDatabase(writePlaced(t, dir, dsn, pw))
|
|
if err != nil || !postgres {
|
|
t.Fatalf("root config: %v %v", postgres, err)
|
|
}
|
|
// pgx can open a second connection to cancel after the failed login
|
|
conns, got := srv.seen()
|
|
if conns < 1 || !strings.Contains(got, "config-password\x00") {
|
|
t.Fatalf("the test did not send the config password: %d %q", conns, got)
|
|
}
|
|
|
|
// without a password nothing of root is sent
|
|
srv2 := startFakePostgres(t)
|
|
dsn2 := fmt.Sprintf("postgres://x@127.0.0.1:%d/x?sslmode=disable", srv2.port())
|
|
if _, err := checkPlacedDatabase(writePlaced(t, t.TempDir(), dsn2, "")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conns, got = srv2.seen()
|
|
if conns < 1 {
|
|
t.Fatalf("connections %d, want one", conns)
|
|
}
|
|
for _, s := range []string{"SECRET-PGPASS", "SECRET-ENV"} {
|
|
if strings.Contains(got, s) {
|
|
t.Fatalf("the test sent %s: %q", s, got)
|
|
}
|
|
}
|
|
for name, want := range map[string]string{"PGPASSFILE": pgpass, "PGPASSWORD": "SECRET-ENV", "PGSSLMODE": "require"} {
|
|
if v := os.Getenv(name); v != want {
|
|
t.Fatalf("%s is %q after the test, want %q", name, v, want)
|
|
}
|
|
}
|
|
if _, set := os.LookupEnv("PGSERVICE"); set {
|
|
t.Fatal("PGSERVICE set by the test")
|
|
}
|
|
}
|
|
|
|
// TestPlacedConfigNotFirstInstall checks a config only root can change is
|
|
// not tested once the service user or its unit file exist, as a process of
|
|
// the service user can hold the file open for writing from before
|
|
func TestPlacedConfigNotFirstInstall(t *testing.T) {
|
|
t.Setenv(config.EnvDatabaseDSN, "")
|
|
t.Setenv(config.EnvDatabasePasswordFile, "")
|
|
trustTestUser(t)
|
|
srv := startFakePostgres(t)
|
|
dsn := fmt.Sprintf("postgres://x@127.0.0.1:%d/x?sslmode=disable", srv.port())
|
|
dir := t.TempDir()
|
|
pw := filepath.Join(dir, "db_password")
|
|
writeSecret(t, pw, "config-password\n")
|
|
path := writePlaced(t, dir, dsn, pw)
|
|
|
|
check := func(name, want string) {
|
|
t.Helper()
|
|
placed, err := readPlacedConfig(path)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if placed == nil || !placed.postgres || placed.conf != nil || !strings.Contains(placed.untrusted, want) {
|
|
t.Fatalf("%s: want untrusted %q, got %+v", name, want, placed)
|
|
}
|
|
if postgres, err := checkPlacedDatabase(path); err != nil || !postgres {
|
|
t.Fatalf("%s: %v %v", name, postgres, err)
|
|
}
|
|
}
|
|
|
|
saved := lookupServiceUser
|
|
lookupServiceUser = func() error { return nil }
|
|
check("service user exists", "user exists")
|
|
lookupServiceUser = func() error { return errors.New("lookup failed") }
|
|
check("service user lookup failed", "can not be looked up")
|
|
lookupServiceUser = saved
|
|
|
|
if err := os.WriteFile(serviceUnitPath, []byte("[Unit]\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("unit file exists", "this is not a first install")
|
|
|
|
if conns, got := srv.seen(); conns != 0 || got != "" {
|
|
t.Fatalf("the installer connected %d times and sent %q", conns, got)
|
|
}
|
|
}
|
|
|
|
// TestDescribeDatabaseWithoutLibpqEnv checks the shown target and the TLS
|
|
// warning come from the DSN only, like the connection test, not from the
|
|
// PG variables of root
|
|
func TestDescribeDatabaseWithoutLibpqEnv(t *testing.T) {
|
|
t.Setenv("PGSSLMODE", "verify-full")
|
|
d := config.Database{Engine: config.DatabaseEnginePostgres, DSN: "postgres://x@db.example.com/x"}
|
|
warning, target := describeDatabase(d)
|
|
if !strings.Contains(warning, "may not use TLS") {
|
|
t.Fatalf("warning %q", warning)
|
|
}
|
|
if !strings.Contains(target, "sslmode=prefer") {
|
|
t.Fatalf("target %q", target)
|
|
}
|
|
if os.Getenv("PGSSLMODE") != "verify-full" {
|
|
t.Fatal("PGSSLMODE not put back")
|
|
}
|
|
}
|
|
|
|
// TestPlacedConfigTrustedSymlinkAndInvalid checks a config symlink only
|
|
// root could have made is followed and tested on a first install, and a
|
|
// config that is not valid JSON is not an error: the engine then comes
|
|
// from the installed unit file
|
|
func TestPlacedConfigTrustedSymlinkAndInvalid(t *testing.T) {
|
|
t.Setenv(config.EnvDatabaseDSN, "")
|
|
t.Setenv(config.EnvDatabasePasswordFile, "")
|
|
trustTestUser(t)
|
|
srv := startFakePostgres(t)
|
|
dsn := fmt.Sprintf("postgres://x@127.0.0.1:%d/x?sslmode=disable", srv.port())
|
|
targetDir := t.TempDir()
|
|
pw := filepath.Join(targetDir, "db_password")
|
|
writeSecret(t, pw, "linked-password\n")
|
|
target := writePlaced(t, targetDir, dsn, pw)
|
|
link := filepath.Join(t.TempDir(), "config.json")
|
|
if err := os.Symlink(target, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
placed, err := readPlacedConfig(link)
|
|
if err != nil || placed == nil || !placed.postgres || placed.conf == nil || placed.unread != "" {
|
|
t.Fatalf("trusted symlink config: %+v %v", placed, err)
|
|
}
|
|
if postgres, err := checkPlacedDatabase(link); err != nil || !postgres {
|
|
t.Fatalf("trusted symlink config: %v %v", postgres, err)
|
|
}
|
|
if conns, got := srv.seen(); conns < 1 || !strings.Contains(got, "linked-password\x00") {
|
|
t.Fatalf("the linked config was not tested: %d %q", conns, got)
|
|
}
|
|
|
|
invalid := filepath.Join(t.TempDir(), "config.json")
|
|
writeSecret(t, invalid, "{not json")
|
|
for _, unit := range []string{"", "After=network.target network-online.target postgresql.service\n"} {
|
|
if unit != "" {
|
|
if err := os.WriteFile(serviceUnitPath, []byte(unit), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
placed, err := readPlacedConfig(invalid)
|
|
if err != nil || placed == nil || !strings.Contains(placed.unread, "not a valid config") || placed.conf != nil {
|
|
t.Fatalf("invalid config: %+v %v", placed, err)
|
|
}
|
|
postgres, err := checkPlacedDatabase(invalid)
|
|
if err != nil || postgres != (unit != "") {
|
|
t.Fatalf("invalid config with unit %q: %v %v", unit, postgres, err)
|
|
}
|
|
}
|
|
}
|