Repository navigation
RHINENG-33916: keep database credentials out of the connection string - #2399
MichaelMraka wants to merge 2 commits into
Conversation
Set user, password, and database on the pgx config so special characters in those values cannot change the host or TLS settings.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2399 +/- ##
==========================================
+ Coverage 66.82% 66.86% +0.04%
==========================================
Files 158 158
Lines 9081 9090 +9
==========================================
+ Hits 6068 6078 +10
+ Misses 3011 3010 -1
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Reviewer's GuideAPIExpose pgx-based connection configuration and database-pool helpers, and change migration connection setup to accept an already-open SQL pool.
Uses of the APIBuild the database-admin connection from structured credentials and pass the resulting pool into migration setup.
ImplementationUse the new pgx configuration and pool-opening APIs in the application database connection path.
Quote SQL identifiers and literals when updating database roles and passwords.
ConfigurationRemove per-user database password environment variables and secret references in favor of the shared configured database password.
TestsTest that credentials and statement timeout are assigned outside the pgx connection string, and update database-admin tests to use structured connection configuration and the shared password.
Lock files and vendored codePromote pgx to a direct module dependency because the connection configuration now imports it directly.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 7 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="base/database/setup.go" line_range="180" />
<code_context>
+ dbConfig.Host, dbConfig.Port, dbConfig.SSLMode)
if dbConfig.SSLRootCert != "" {
- dbsource = fmt.Sprintf("%s sslrootcert=%s", dbsource, dbConfig.SSLRootCert)
+ dsn += " sslrootcert=" + dbConfig.SSLRootCert
+ }
+ cfg, err := pgx.ParseConfigWithOptions(dsn, pgx.ParseConfigOptions{
</code_context>
<issue_to_address>
**TLS mode can be overridden**
When `SSLRootCert` contains whitespace followed by a keyword DSN option such as `sslmode=disable`, the root-certificate path is appended unescaped to the keyword DSN, so a value containing a token such as `sslmode=disable` is parsed as another allowed setting and overrides the configured TLS mode. The connection then stops verifying TLS.
Escape or quote DSN values before parsing, or avoid interpolating the certificate path into the connection string.
</issue_to_address>
### Comment 2
<location path="database_admin/update.go" line_range="288" />
<code_context>
- execOrPanic(db, "ALTER USER manager WITH PASSWORD '"+utils.GetenvOrFail("MANAGER_PASSWORD")+"'")
- execOrPanic(db, "ALTER USER vmaas_sync WITH PASSWORD '"+utils.GetenvOrFail("VMAAS_SYNC_PASSWORD")+"'")
+ for _, user := range lockUsers {
+ setUserPassword(db, user, utils.CoreCfg.DBPassword)
+ }
}
</code_context>
<issue_to_address>
**Shared password erases role isolation**
When an application service is compromised and its database credential is recovered, `setUserPassword` assigns the same `DBPassword` to every login in `lockUsers`, so a compromised service that knows its own database password can authenticate as the other application roles and obtain their database privileges.
Keep distinct passwords for each database role, or use a credential design that prevents one service from authenticating as the other roles.
</issue_to_address>
### Comment 3
<location path="database_admin/update.go" line_range="288" />
<code_context>
- execOrPanic(db, "ALTER USER manager WITH PASSWORD '"+utils.GetenvOrFail("MANAGER_PASSWORD")+"'")
- execOrPanic(db, "ALTER USER vmaas_sync WITH PASSWORD '"+utils.GetenvOrFail("VMAAS_SYNC_PASSWORD")+"'")
+ for _, user := range lockUsers {
+ setUserPassword(db, user, utils.CoreCfg.DBPassword)
+ }
}
</code_context>
<issue_to_address>
**Application pods lose database access**
When a deployment rotates per-service passwords while old pods still use those credentials, `setUserPassword` assigns the shared `CoreCfg.DBPassword` to every application role, so PostgreSQL rejects reconnects from pods using their previous per-service passwords; components expecting distinct credentials also fail authentication, and application database access is interrupted during the rollout.
Keep each application role's password aligned with the credentials used by its pods throughout the rollout.
Also at `deploy/clowdapp.yaml:96-97`.
</issue_to_address>
### Comment 4
<location path="database_admin/update.go" line_range="288" />
<code_context>
- execOrPanic(db, "ALTER USER manager WITH PASSWORD '"+utils.GetenvOrFail("MANAGER_PASSWORD")+"'")
- execOrPanic(db, "ALTER USER vmaas_sync WITH PASSWORD '"+utils.GetenvOrFail("VMAAS_SYNC_PASSWORD")+"'")
+ for _, user := range lockUsers {
+ setUserPassword(db, user, utils.CoreCfg.DBPassword)
+ }
}
</code_context>
<issue_to_address>
**New pods fail database authentication**
When `update_users` is unset and existing database role passwords differ from Clowder's password, `UpdateDB` skips `setUserPassword`, leaving the roles' old passwords in place while new pods use Clowder's password; PostgreSQL rejects their connections.
Ensure database roles are synchronized with the credentials supplied to new pods even when `update_users` is unset.
Also at `deploy/clowdapp.yaml:97`.
</issue_to_address>
### Comment 5
<location path="database_admin/update.go" line_range="288" />
<code_context>
- execOrPanic(db, "ALTER USER manager WITH PASSWORD '"+utils.GetenvOrFail("MANAGER_PASSWORD")+"'")
- execOrPanic(db, "ALTER USER vmaas_sync WITH PASSWORD '"+utils.GetenvOrFail("VMAAS_SYNC_PASSWORD")+"'")
+ for _, user := range lockUsers {
+ setUserPassword(db, user, utils.CoreCfg.DBPassword)
+ }
}
</code_context>
<issue_to_address>
**Some roles keep stale passwords**
When a password update fails after one or more roles have been updated, `setUserPassword` commits each role's change independently, so a later error leaves the roles with mixed passwords and the migration fails; services using the shared credential cannot connect to roles that were not updated.
Update all application-role passwords atomically so a failure leaves every role unchanged.
</issue_to_address>
### Comment 6
<location path="database_admin/update.go" line_range="222-224" />
<code_context>
+ if err != nil {
+ panic(err)
+ }
+ cfg.RuntimeParams["tcp_keepalives_idle"] = "60"
+ cfg.RuntimeParams["tcp_keepalives_interval"] = "30"
+ cfg.RuntimeParams["tcp_keepalives_count"] = "5"
+ cfg.OnNotice = func(_ *pgconn.PgConn, notice *pgconn.Notice) {
+ utils.LogInfo("Notice: " + notice.Severity + ": " + notice.Message)
</code_context>
<issue_to_address>
**Admin connections lose client keepalives**
When an admin connection is idle on a network that drops or silently blackholes database connections, `cfg.RuntimeParams` sends `tcp_keepalives_*` as PostgreSQL startup parameters, configuring server-side settings instead of the admin client's TCP socket; the connection loses the configured client keepalive behavior and can be dropped during idle periods or take longer to detect a blackhole.
Configure the keepalive settings on the admin client's TCP socket instead of passing them through `RuntimeParams`.
</issue_to_address>
### Comment 7
<location path="database_admin/migrate.go" line_range="28" />
<code_context>
-func NewConn(databaseURL string) (database.Driver, *sql.DB, error) {
- baseConn, err := pq.NewConnector(databaseURL)
- if err != nil {
- return nil, nil, err
- }
-
</code_context>
<issue_to_address>
**Connection error is obscured**
When `db.Exec` or `postgres.WithInstance` fails in `NewConn`, `NewConn` returns a nil `*sql.DB` with the error, so `dbConn` calls `Close` on nil and panics with a nil-pointer error instead of reporting the connection failure; the opened pool is also not closed.
Have `NewConn` return the original `db` on errors, or only close it in `dbConn` when it is non-nil.
Also at `database_admin/update.go:233`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 7 findings to address first, and on database-admin startup, this change assigns the same configured password to every application database role, replacing their separate passwords. Those credentials persist in PostgreSQL after a revert, so restoring the old role-specific access requires explicitly rotating them.
Blocking findings: base/database/setup.go:180, database_admin/update.go:288, database_admin/update.go:288, database_admin/update.go:288, database_admin/update.go:288, and 2 more
Secure Coding Practices Checklist GitHub Link
Secure Coding Checklist
Summary by Sourcery
Keep PostgreSQL credentials out of connection strings while standardizing secure database connection and administration handling.
Enhancements:
Build:
Deployment:
Tests: