mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 09:20:53 +08:00
server: probe every database host of the DSN; compose: SCRAM for TCP
The self-test probe tried only the first host of the DSN. It now tries each host and port the DSN lists, fallbacks included, and nothing else, and a DSN it cannot parse fails the self-test instead of skipping the database check. docker-compose.yml sets POSTGRES_HOST_AUTH_METHOD=scram-sha-256: a TCP connection from another container, artex's included, needs the password. It takes effect when a new pgdata volume is initialised.
This commit is contained in:
@@ -5,6 +5,12 @@ services:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-artex}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Please set POSTGRES_PASSWORD in .env}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-artex}
|
||||
# Password (SCRAM) for every TCP connection from another container, artex's
|
||||
# included; the server's isolation self-test checks that the agent cannot
|
||||
# connect without it. Applied when the database is first initialised (a new
|
||||
# pgdata volume); the image keeps trust for connections from inside this
|
||||
# container only, which no agent process reaches.
|
||||
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
|
||||
@@ -39,8 +39,10 @@ type isolationProbeSpec struct {
|
||||
Unreadable []string `json:"unreadable,omitempty"`
|
||||
// Unwritable are directories no agent process may create an entry in.
|
||||
Unwritable []string `json:"unwritable,omitempty"`
|
||||
// DB names the server's database; the probe connects without a password.
|
||||
DB *isolationProbeDB `json:"db,omitempty"`
|
||||
// DB names the server's database at each host its DSN lists, fallbacks included:
|
||||
// the real address the server connects to, never one the self-test picks. The
|
||||
// probe connects to each without a password.
|
||||
DB []isolationProbeDB `json:"db,omitempty"`
|
||||
}
|
||||
|
||||
type isolationProbeDB struct {
|
||||
@@ -65,6 +67,9 @@ func IsolationSelfTest(p ExecPrivilege, t IsolationTargets) error {
|
||||
}
|
||||
spec := isolationSpec(p, t)
|
||||
failures := fileToolIsolationFailures(spec.Unreadable)
|
||||
if t.DSN != "" && len(spec.DB) == 0 {
|
||||
failures = append(failures, "the database DSN names no host the self-test can check")
|
||||
}
|
||||
failures = append(failures, replaceableDirFailures(spec.Unwritable, p.UID, p.GID)...)
|
||||
failures = append(failures, skillTreeFailures(t.SkillDir, p.UID, p.GID)...)
|
||||
rep, err := runIsolationProbe(spec)
|
||||
@@ -103,12 +108,34 @@ func isolationSpec(p ExecPrivilege, t IsolationTargets) isolationProbeSpec {
|
||||
}
|
||||
if t.DSN != "" {
|
||||
if cfg, err := pgx.ParseConfig(t.DSN); err == nil {
|
||||
spec.DB = &isolationProbeDB{Host: cfg.Host, Port: cfg.Port, User: cfg.User, Database: cfg.Database}
|
||||
spec.DB = dsnProbeHosts(cfg)
|
||||
}
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
// dsnProbeHosts lists each distinct host and port of a parsed DSN, the first one
|
||||
// and its fallbacks (pgx adds a fallback per TLS attempt, so hosts repeat).
|
||||
func dsnProbeHosts(cfg *pgx.ConnConfig) []isolationProbeDB {
|
||||
var out []isolationProbeDB
|
||||
add := func(host string, port uint16) {
|
||||
if host == "" {
|
||||
return
|
||||
}
|
||||
for _, db := range out {
|
||||
if db.Host == host && db.Port == port {
|
||||
return
|
||||
}
|
||||
}
|
||||
out = append(out, isolationProbeDB{Host: host, Port: port, User: cfg.User, Database: cfg.Database})
|
||||
}
|
||||
add(cfg.Host, cfg.Port)
|
||||
for _, fb := range cfg.Fallbacks {
|
||||
add(fb.Host, fb.Port)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func configDir(configPath string) string {
|
||||
if configPath == "" {
|
||||
return ""
|
||||
@@ -321,9 +348,10 @@ func checkIsolationAsExecutor(spec isolationProbeSpec) []string {
|
||||
fail("the variable %s reached Bash: it is not on the agent environment allowlist", name)
|
||||
}
|
||||
}
|
||||
if spec.DB != nil {
|
||||
if err := connectWithoutPassword(spec.DB); err == nil {
|
||||
fail("Bash opened a database session as %q on %s without the server's password", spec.DB.User, spec.DB.Host)
|
||||
for i := range spec.DB {
|
||||
db := &spec.DB[i]
|
||||
if err := connectWithoutPassword(db); err == nil {
|
||||
fail("Bash opened a database session as %q on %s:%d without the server's password", db.User, db.Host, db.Port)
|
||||
}
|
||||
}
|
||||
return failures
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net"
|
||||
"os"
|
||||
@@ -91,7 +93,7 @@ func TestCheckIsolationReportsEachExposure(t *testing.T) {
|
||||
UID: 10001, GID: 10001, ServerPID: os.Getpid(),
|
||||
Unreadable: []string{secret, filepath.Join(dir, "absent.key")},
|
||||
Unwritable: []string{dir},
|
||||
DB: &isolationProbeDB{Host: host, Port: port, User: "artex", Database: "artex"},
|
||||
DB: []isolationProbeDB{{Host: host, Port: port, User: "artex", Database: "artex"}},
|
||||
})
|
||||
for _, want := range []string{
|
||||
"not the executor uid 10001",
|
||||
@@ -133,15 +135,48 @@ func TestConnectWithoutPasswordRealPG(t *testing.T) {
|
||||
t.Skip("ARTEX_PG_DSN not set")
|
||||
}
|
||||
spec := isolationSpec(ExecPrivilege{Enabled: true, UID: 10001, GID: 10001}, IsolationTargets{DSN: dsn})
|
||||
if spec.DB == nil {
|
||||
if len(spec.DB) == 0 {
|
||||
t.Fatal("no database in the spec")
|
||||
}
|
||||
err := connectWithoutPassword(spec.DB)
|
||||
var pgErr *pgconn.PgError
|
||||
if !errors.As(err, &pgErr) || (pgErr.Code != "28P01" && pgErr.Code != "28000") {
|
||||
t.Fatalf("connection without password: %v, want an authentication failure (28P01/28000)", err)
|
||||
for i := range spec.DB {
|
||||
err := connectWithoutPassword(&spec.DB[i])
|
||||
var pgErr *pgconn.PgError
|
||||
if !errors.As(err, &pgErr) || (pgErr.Code != "28P01" && pgErr.Code != "28000") {
|
||||
t.Fatalf("connection without password to %s:%d: %v, want an authentication failure (28P01/28000)", spec.DB[i].Host, spec.DB[i].Port, err)
|
||||
}
|
||||
t.Logf("%s:%d refused as expected: SQLSTATE %s", spec.DB[i].Host, spec.DB[i].Port, pgErr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// The probe tries the hosts the DSN names, every one of them, and nothing else: no
|
||||
// loopback of its own. A DSN it cannot use fails the self-test instead of skipping
|
||||
// the database check.
|
||||
func TestIsolationSpecUsesTheDSNHosts(t *testing.T) {
|
||||
p := ExecPrivilege{Enabled: true, UID: 10001, GID: 10001}
|
||||
for _, c := range []struct {
|
||||
dsn string
|
||||
want string
|
||||
}{
|
||||
{"postgres://artex:pw@db.internal:6543/artex?sslmode=disable", "db.internal:6543"},
|
||||
{"postgres://artex:pw@db1.internal:6543,db2.internal:6544/artex?sslmode=prefer", "db1.internal:6543 db2.internal:6544"},
|
||||
{"host=10.20.30.40 port=5433 user=artex password=pw dbname=artex", "10.20.30.40:5433"},
|
||||
{"host=/var/run/postgresql user=artex dbname=artex", "/var/run/postgresql:5432"},
|
||||
} {
|
||||
var got []string
|
||||
for _, db := range isolationSpec(p, IsolationTargets{DSN: c.dsn}).DB {
|
||||
got = append(got, fmt.Sprintf("%s:%d", db.Host, db.Port))
|
||||
if db.User != "artex" || db.Database != "artex" {
|
||||
t.Errorf("%s: user %q database %q", c.dsn, db.User, db.Database)
|
||||
}
|
||||
}
|
||||
if strings.Join(got, " ") != c.want {
|
||||
t.Errorf("%s: probe hosts %v, want %s", c.dsn, got, c.want)
|
||||
}
|
||||
}
|
||||
err := IsolationSelfTest(p, IsolationTargets{DSN: "postgres://%zz"})
|
||||
if err == nil || !strings.Contains(err.Error(), "names no host the self-test can check") {
|
||||
t.Fatalf("self-test with an unusable DSN: %v", err)
|
||||
}
|
||||
t.Logf("refused as expected: SQLSTATE %s", pgErr.Code)
|
||||
}
|
||||
|
||||
// The parent rule: owned by the executor, or writable by it without the sticky bit,
|
||||
@@ -236,13 +271,13 @@ func TestIsolationSpecHasNoPassword(t *testing.T) {
|
||||
t.Errorf("%s missing from the unreadable files %v", want, spec.Unreadable)
|
||||
}
|
||||
}
|
||||
if spec.DB == nil || spec.DB.Host != "postgres" || spec.DB.Port != 5432 || spec.DB.User != "artex" || spec.DB.Database != "artex" {
|
||||
if len(spec.DB) != 1 || spec.DB[0].Host != "postgres" || spec.DB[0].Port != 5432 || spec.DB[0].User != "artex" || spec.DB[0].Database != "artex" {
|
||||
t.Fatalf("db = %+v", spec.DB)
|
||||
}
|
||||
if spec.UID != 10001 || spec.GID != 10002 || spec.ServerPID != os.Getpid() {
|
||||
t.Fatalf("ids = %d:%d pid %d", spec.UID, spec.GID, spec.ServerPID)
|
||||
}
|
||||
if b := strings.Join(append(spec.Unreadable, spec.DB.Host, spec.DB.User), " "); strings.Contains(b, "s3cr3t") {
|
||||
if b, _ := json.Marshal(spec); strings.Contains(string(b), "s3cr3t") {
|
||||
t.Fatal("the password reached the spec")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user