Repository navigation
Description
Problem
The DocumentDB extension opens additional libpq connections back to the same PostgreSQL server for operations that must execute outside the caller's transaction. These include concurrent index create/drop/reindex, compact (VACUUM/ANALYZE), some explain metadata lookups, and background maintenance jobs.
The extension defaults documentdb.localhost_connection_string to host=localhost. The operator does not currently set this GUC, so these internal connections depend on the operator's broad TCP trust entries in pg_hba.conf.
This is distinct from the operator's administrative SQL execution used for replication and extension upgrades. The operator currently executes psql directly in the CNPG primary container, while the WAL replication slot is created through CNPG postInitSQL.
Why this is P0
We need a production-safe authentication design before hardening PostgreSQL authentication. Removing TCP trust without configuring the extension's internal connection will break concurrent index operations and background maintenance. Retaining broad TCP trust is not an acceptable long-term security boundary.
Scope
- Configure
documentdb.localhost_connection_stringfor CNPG's Unix socket and actual PostgreSQL port. - Define authentication and role behavior for internal connections initiated while serving non-superuser application sessions.
- Avoid requiring applications to connect as
postgresand then useSET ROLE. - Preserve operator-driven administrative SQL for replication setup and extension upgrades without broad TCP trust.
- Confirm compatibility with primary/standby pods, failover, TLS configuration, and the gateway sidecar.
Acceptance criteria
- Normal application credentials work for CRUD and every operation that opens an internal libpq connection.
- Concurrent
createIndexes,dropIndexes, reindex,compact, explain, and enabled background jobs are covered by tests. - PostgreSQL does not require broad
host all all ... trustrules. - Replication permission grants, physical replication slots, and extension upgrades continue to work.
- The chosen socket path, authentication rules, and role mapping are documented.
Hardening ideas to evaluate
-
Route extension callbacks through the CNPG Unix socket
- Set
documentdb.localhost_connection_stringto CNPG's socket directory and resolved port, rather thanhost=localhostTCP. - Discover or use the CNPG-supported socket path instead of hardcoding it, and verify it remains stable across CNPG versions.
- Set
-
Use narrowly scoped local authentication
- Add explicit
localHBA rules before broader defaults. - Prefer peer authentication only if the OS-to-database-role mapping can safely preserve the authenticated application identity.
- Evaluate
pg_ident.confrole-group mapping on PostgreSQL 16+ so the PostgreSQL process identity cannot impersonate arbitrary roles.
- Add explicit
-
Do not use
postgresplusSET ROLEas the application boundary- The additional connection should run as the original authenticated role, or as a purpose-built internal role with only the exact privileges required by each operation.
- If a service role is necessary, split privileges between index maintenance, compact/maintenance, metadata lookup, and background jobs rather than using one superuser-equivalent role.
-
Separate extension callbacks from operator administration
- Keep operator actions such as replication grants and
ALTER EXTENSIONon the Kubernetes exec path, authenticated by Kubernetes RBAC and pod access. - Remove any dependency of those actions on network
trustrules. - Consider replacing free-form SQL execution with typed, allowlisted operator operations.
- Keep operator actions such as replication grants and
-
Tighten network HBA rules incrementally
- Replace
host all all 0.0.0.0/0 trustand IPv6 equivalent with explicit gateway/application, replication, monitoring, and local-internal rules. - Require SCRAM or certificate authentication for TCP clients and reserve certificate-based replication rules for replication identities.
- Verify rule order so a permissive fallback cannot shadow the intended rule.
- Replace
-
Test the failure boundary explicitly
- Add a test mode with TCP trust disabled so accidental fallback to
localhostTCP fails immediately. - Assert the additional backend uses the expected socket, database role, and authentication method.
- Exercise failover and pod replacement to ensure the socket configuration is valid on every primary candidate.
- Add a test mode with TCP trust disabled so accidental fallback to
Suggested implementation sequence
- Inventory the exact CNPG socket path, process UID, HBA ordering, and supported
pg_ident.confconfiguration surface. - Prototype
documentdb.localhost_connection_stringover the socket with concurrent index, compact, explain, and background-job tests. - Design least-privilege role mapping for original-user and service-job connections.
- Remove broad TCP trust and add explicit SCRAM/certificate/replication rules.
- Harden the operator's administrative SQL interface and document the resulting trust boundaries.
Likely target architecture
- Tighten DocumentDB extension callbacks to the PostgreSQL Unix-domain socket exposed by CNPG. Set
documentdb.localhost_connection_stringto that socket and use narrowly scoped local authentication instead of localhost TCP trust. - Reserve PostgreSQL TCP connectivity for the gateway. The likely direction is mutual TLS for gateway-to-PostgreSQL authentication, with explicit role and certificate mapping.
- Remove broad TCP
trustrules once both paths are validated end to end: extension callbacks over the local socket, and gateway traffic over mTLS. - Confirm the CNPG-supported socket path and authentication configuration, then cover failover, pod replacement, non-superuser users, concurrent index operations, compact, explain, and background jobs in E2E tests.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
documentdb-triage-tool commentedon Oct 5, 2026
🤖 Auto-triaged by documentdb-triage-tool.
Applied:
go,enhancementProject fields suggested: Component
controllers· PriorityP0· EffortXL· StatusIn ProgressConfidence: 0.92 (llm)
Reasoning
LLM: Security hardening of internal PostgreSQL connections requiring cross-component changes (operator config, authentication rules, GUC settings, socket paths, TLS, replication) before broad TCP trust can be removed — explicitly self-labeled P0 and release-blocking.
If a label is wrong, remove it manually and ping
@patty-chowso the rules can be tuned. The bot will not re-label items that already have component labels.documentdb-triage-tool commentedon Oct 28, 2026
👋 @alaye-ms — this issue has been assigned for 22 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.
documentdb-triage-tool commentedon Nov 11, 2026
👋 @alaye-ms — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.
utkuerol commentedon Nov 20, 2026
Adding a use case from the plugin-only side: we run the gateway via the sidecar-injector plugin on our own CNPG operator, without the DocumentDB operator.
Today the gateway connects to Postgres over
localhostTCP as its OS user (documentdb, a superuser) with no password, sopg_hbaneedshost ... localhost trustfor that role. The gateway refuses a password outright (reject_password_environment/reject_password_in_configinconfiguration/setup.rs), anddocumentDbCredentialSecretonly feeds the Mongo admin bootstrap, not the gateway's own connection.Whatever this issue lands on (mTLS client cert, or a password/secret file), could it be exposed through sidecar-injector parameters, e.g. a client cert/key secret or a password secret mounted as a file, with the gateway reading it? Then plugin users can drop the trust rule without depending on the operator's pg_hba handling. A non-superuser system role would be a welcome follow-up.