Files
phishingclub/backend/config/databasePassword_test.go
2026-12-01 21:25:01 +01:00

96 lines
3.5 KiB
Go

package config
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/jackc/pgx/v5"
)
// writePasswordFile writes content to a new file and returns its path
func writePasswordFile(t *testing.T, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "db_password")
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
return path
}
func TestDatabasePasswordFile(t *testing.T) {
t.Setenv(EnvDatabaseDSN, "")
t.Setenv(EnvDatabasePasswordFile, "")
t.Setenv("PGPASSWORD", "")
const dsn = "postgres://pc@db.example.com:5432/pc?sslmode=verify-full"
for _, dsn := range []string{dsn, "host=db.example.com user=pc dbname=pc sslmode=verify-full"} {
path := writePasswordFile(t, "f1le pw:'\\\n")
c, err := newDatabaseConfig(t, Database{Engine: DatabaseEnginePostgres, DSN: dsn, PasswordFile: path})
if err != nil {
t.Fatalf("%s: %v", dsn, err)
}
cfg, err := pgx.ParseConfig(c.Database().DSN)
if err != nil {
t.Fatal(err)
}
if cfg.Password != `f1le pw:'\` {
t.Fatalf("password read as %q", cfg.Password)
}
if strings.Contains(c.Database().DisplayDSN(), "f1le") {
t.Fatalf("display DSN shows the password: %s", c.Database().DisplayDSN())
}
// the file keeps the DSN without the password
raw, err := json.Marshal(c.ToDTO().Database)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(raw), "f1le") || !strings.Contains(string(raw), `"password_file"`) {
t.Fatalf("config file part: %s", raw)
}
}
// the environment replaces the file setting
envPath := writePasswordFile(t, "env-pw\r\n")
t.Setenv(EnvDatabasePasswordFile, envPath)
c, err := newDatabaseConfig(t, Database{Engine: DatabaseEnginePostgres, DSN: dsn, PasswordFile: "/does/not/exist"})
if err != nil {
t.Fatalf("env file: %v", err)
}
if cfg, _ := pgx.ParseConfig(c.Database().DSN); cfg.Password != "env-pw" {
t.Fatalf("env password read as %q", cfg.Password)
}
t.Setenv(EnvDatabasePasswordFile, "")
refused := map[string]Database{
"missing file": {Engine: DatabaseEnginePostgres, DSN: dsn, PasswordFile: "/does/not/exist"},
"empty file": {Engine: DatabaseEnginePostgres, DSN: dsn, PasswordFile: writePasswordFile(t, "\n")},
"dsn with password": {Engine: DatabaseEnginePostgres, DSN: "postgres://pc:0ldpw@db/pc", PasswordFile: writePasswordFile(t, "s3cretfile")},
"query password": {Engine: DatabaseEnginePostgres, DSN: "postgres://pc@db/pc?password=0ldpw", PasswordFile: writePasswordFile(t, "s3cretfile")},
"trailing backslash": {Engine: DatabaseEnginePostgres, DSN: `host=db user=pc dbname=abc\`, PasswordFile: writePasswordFile(t, "s3cretfile")},
"no dsn": {Engine: DatabaseEnginePostgres, PasswordFile: writePasswordFile(t, "s3cretfile")},
}
for name, d := range refused {
_, err := newDatabaseConfig(t, d)
if err == nil {
t.Fatalf("%s: no error", name)
}
if strings.Contains(err.Error(), "s3cretfile") || strings.Contains(err.Error(), "0ldpw") {
t.Fatalf("%s: error holds a password: %v", name, err)
}
}
big := writePasswordFile(t, strings.Repeat("x", maxPasswordFileSize+1))
if _, err := newDatabaseConfig(t, Database{Engine: DatabaseEnginePostgres, DSN: dsn, PasswordFile: big}); !errors.Is(err, ErrDatabasePasswordFile) {
t.Fatalf("big file: %v", err)
}
// sqlite ignores it
c, err = newDatabaseConfig(t, Database{Engine: DatabaseEngineSQLite, DSN: "file:./db.sqlite3", PasswordFile: "/does/not/exist"})
if err != nil || c.Database().DSN != "file:./db.sqlite3" {
t.Fatalf("sqlite: %v %q", err, c.Database().DSN)
}
}