Skip to content

Commit 38b9522

Browse files
authored
ateapi: stop logging the raw PostgreSQL connection string (which might contain db password) at startup (agent-substrate#1833)
logFlagValues writes every flag to the log when the server starts, and that included --postgres-connection-string verbatim. The bundled database and Cloud SQL IAM auth use passwordless strings, but an external database DSN can carry a password, and it was written to the log on every restart. Log the parsed, non-secret parts instead: host, port, database, user, whether a password is set and whether TLS is on. pgconn.ParseConfig handles both the URI and the libpq key=value forms, so no string rewriting is needed and nothing is echoed. A string that does not parse is logged as "<unparseable>"; connectStore still reports the real error. Fixes agent-substrate#1832 > It's a good idea to open an issue first for discussion. - [x] Tests pass - [x] Appropriate changes to documentation are included in the PR
1 parent 7317e08 commit 38b9522

2 files changed

Lines changed: 119 additions & 1 deletion

File tree

‎cmd/ateapi/main.go‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
5151
"github.com/aws/aws-sdk-go-v2/config"
5252
"github.com/aws/aws-sdk-go-v2/service/s3"
53+
"github.com/jackc/pgx/v5/pgconn"
5354
"github.com/jackc/pgx/v5/pgxpool"
5455
"github.com/spf13/pflag"
5556
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
@@ -396,7 +397,7 @@ func logFlagValues(ctx context.Context) {
396397
slog.String("grpc-listen-addr", *listenAddr),
397398
slog.String("grpc-server-cred-bundle", *grpcServerCredBundle),
398399
slog.String("authentication-config", *authenticationConfigFile),
399-
slog.String("postgres-connection-string", *postgresConnectionString),
400+
postgresConnectionAttr(*postgresConnectionString),
400401
slog.String("postgres-schema", *postgresSchema),
401402
slog.Bool("experimental-enable-authz", *experimentalEnableAuthz),
402403
slog.String("actor-id-jwt-pool", *actorIDJWTPoolFile),
@@ -438,6 +439,31 @@ func newObjectStore(ctx context.Context) (objectstore.Store, error) {
438439
}
439440
}
440441

442+
// postgresConnectionAttr describes the connection string for the startup log
443+
// without echoing it. The bundled and Cloud SQL IAM setups use passwordless
444+
// strings, but an external database DSN can carry a password, and the raw
445+
// value would otherwise be written to the log on every restart. Only the
446+
// parsed, non-secret parts are logged; a string that does not parse is
447+
// reported as invalid and connectStore surfaces the actual error.
448+
func postgresConnectionAttr(connString string) slog.Attr {
449+
const key = "postgres-connection-string"
450+
if connString == "" {
451+
return slog.String(key, "")
452+
}
453+
cfg, err := pgconn.ParseConfig(connString)
454+
if err != nil {
455+
return slog.String(key, "<invalid pg connection string>")
456+
}
457+
return slog.Group(key,
458+
slog.String("host", cfg.Host),
459+
slog.Int("port", int(cfg.Port)),
460+
slog.String("database", cfg.Database),
461+
slog.String("user", cfg.User),
462+
slog.Bool("password-set", cfg.Password != ""),
463+
slog.Bool("tls", cfg.TLSConfig != nil),
464+
)
465+
}
466+
441467
// connectStore builds the PostgreSQL-backed *atepg.Persistence. Startup fails if
442468
// its configuration is missing or the database can't be reached.
443469
func connectStore(ctx context.Context) (*atepg.Persistence, error) {

‎cmd/ateapi/main_test.go‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
package main
1616

1717
import (
18+
"bytes"
1819
"context"
20+
"log/slog"
21+
"net/url"
1922
"strings"
2023
"testing"
2124
)
@@ -64,3 +67,92 @@ func TestResolveActorJWTIssuer(t *testing.T) {
6467
})
6568
}
6669
}
70+
71+
func TestPostgresConnectionAttrNeverLogsThePassword(t *testing.T) {
72+
const password = "hunter2-very-secret"
73+
render := func(connString string) string {
74+
var buf bytes.Buffer
75+
slog.New(slog.NewJSONHandler(&buf, nil)).LogAttrs(context.Background(), slog.LevelInfo, "Final flag values", postgresConnectionAttr(connString))
76+
return buf.String()
77+
}
78+
79+
for name, connString := range map[string]string{
80+
"uri": "postgresql://ateapi:" + password + "@db.example.internal:5433/atepg?sslmode=disable",
81+
"libpq": "host=db.example.internal port=5433 user=ateapi password=" + password + " dbname=atepg sslmode=disable",
82+
// A password with URI-reserved characters is percent-encoded in the
83+
// string; neither the encoded nor the decoded form may appear.
84+
"uri_percent_encoded": "postgresql://ateapi:" + url.QueryEscape(password+"/#@:?") + "@db.example.internal:5433/atepg?sslmode=disable",
85+
"libpq_quoted": "host=db.example.internal port=5433 user=ateapi password='" + password + " with space' dbname=atepg sslmode=disable",
86+
} {
87+
t.Run(name, func(t *testing.T) {
88+
got := render(connString)
89+
if strings.Contains(got, password) {
90+
t.Fatalf("log line contains the password: %s", got)
91+
}
92+
for _, want := range []string{`"host":"db.example.internal"`, `"port":5433`, `"database":"atepg"`, `"user":"ateapi"`, `"password-set":true`, `"tls":false`} {
93+
if !strings.Contains(got, want) {
94+
t.Errorf("log line missing %s: %s", want, got)
95+
}
96+
}
97+
})
98+
}
99+
100+
t.Run("tls", func(t *testing.T) {
101+
got := render("postgresql://ateapi:" + password + "@db.example.internal:5432/atepg?sslmode=require")
102+
if strings.Contains(got, password) || !strings.Contains(got, `"tls":true`) {
103+
t.Errorf("expected tls true and no password: %s", got)
104+
}
105+
})
106+
107+
t.Run("passwordless", func(t *testing.T) {
108+
got := render("postgresql://[email protected]:5432/atepg?sslmode=disable")
109+
if !strings.Contains(got, `"password-set":false`) {
110+
t.Errorf("expected password-set false: %s", got)
111+
}
112+
})
113+
114+
t.Run("unparseable", func(t *testing.T) {
115+
raw := "postgresql://ateapi:" + password + "@[broken"
116+
got := render(raw)
117+
if strings.Contains(got, password) || strings.Contains(got, raw) {
118+
t.Fatalf("log line echoes an unparseable connection string: %s", got)
119+
}
120+
if !strings.Contains(got, `"postgres-connection-string":"<invalid pg connection string>"`) {
121+
t.Errorf("expected the unparseable marker: %s", got)
122+
}
123+
})
124+
125+
t.Run("empty", func(t *testing.T) {
126+
if got := render(""); !strings.Contains(got, `"postgres-connection-string":""`) {
127+
t.Errorf("expected empty marker: %s", got)
128+
}
129+
})
130+
}
131+
132+
// TestLogFlagValuesDoesNotLogThePostgresPassword goes through the real startup
133+
// log call, so a change at the call site (logging the flag directly again)
134+
// fails here even if the helper stays correct.
135+
func TestLogFlagValuesDoesNotLogThePostgresPassword(t *testing.T) {
136+
const password = "hunter2-very-secret"
137+
orig := *postgresConnectionString
138+
t.Cleanup(func() { *postgresConnectionString = orig })
139+
*postgresConnectionString = "postgresql://ateapi:" + password + "@db.example.internal:5432/atepg?sslmode=disable"
140+
141+
var buf bytes.Buffer
142+
origLogger := slog.Default()
143+
t.Cleanup(func() { slog.SetDefault(origLogger) })
144+
slog.SetDefault(slog.New(slog.NewJSONHandler(&buf, nil)))
145+
146+
logFlagValues(context.Background())
147+
148+
got := buf.String()
149+
if !strings.Contains(got, `"msg":"Final flag values"`) {
150+
t.Fatalf("startup line not emitted: %s", got)
151+
}
152+
if strings.Contains(got, password) {
153+
t.Fatalf("startup line contains the database password: %s", got)
154+
}
155+
if !strings.Contains(got, `"postgres-connection-string":{"host":"db.example.internal"`) {
156+
t.Errorf("startup line missing the structured connection summary: %s", got)
157+
}
158+
}

0 commit comments

Comments
 (0)