Skip to content

P0: Secure DocumentDB internal PostgreSQL connections on CNPG #465

Open

Description

@xgerman

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_string for 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 postgres and then use SET 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 ... trust rules.
  • 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

  1. Route extension callbacks through the CNPG Unix socket

    • Set documentdb.localhost_connection_string to CNPG's socket directory and resolved port, rather than host=localhost TCP.
    • Discover or use the CNPG-supported socket path instead of hardcoding it, and verify it remains stable across CNPG versions.
  2. Use narrowly scoped local authentication

    • Add explicit local HBA 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.conf role-group mapping on PostgreSQL 16+ so the PostgreSQL process identity cannot impersonate arbitrary roles.
  3. Do not use postgres plus SET ROLE as 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.
  4. Separate extension callbacks from operator administration

    • Keep operator actions such as replication grants and ALTER EXTENSION on the Kubernetes exec path, authenticated by Kubernetes RBAC and pod access.
    • Remove any dependency of those actions on network trust rules.
    • Consider replacing free-form SQL execution with typed, allowlisted operator operations.
  5. Tighten network HBA rules incrementally

    • Replace host all all 0.0.0.0/0 trust and 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.
  6. Test the failure boundary explicitly

    • Add a test mode with TCP trust disabled so accidental fallback to localhost TCP 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.

Suggested implementation sequence

  1. Inventory the exact CNPG socket path, process UID, HBA ordering, and supported pg_ident.conf configuration surface.
  2. Prototype documentdb.localhost_connection_string over the socket with concurrent index, compact, explain, and background-job tests.
  3. Design least-privilege role mapping for original-user and service-job connections.
  4. Remove broad TCP trust and add explicit SCRAM/certificate/replication rules.
  5. 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_string to 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 trust rules 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.

Activity

  1. added
    enhancement
    go
    on Oct 5, 2026
  2. documentdb-triage-tool commented on Oct 5, 2026

    @documentdb-triage-tool

    🤖 Auto-triaged by documentdb-triage-tool.

    Applied: go, enhancement
    Project fields suggested: Component controllers · Priority P0 · Effort XL · Status In Progress
    Confidence: 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-chow so the rules can be tuned. The bot will not re-label items that already have component labels.

  3. documentdb-triage-tool commented on Oct 28, 2026

    @documentdb-triage-tool

    👋 @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.

  4. documentdb-triage-tool commented on Nov 11, 2026

    @documentdb-triage-tool

    👋 @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.

  5. utkuerol commented on Nov 20, 2026

    @utkuerol

    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 localhost TCP as its OS user (documentdb, a superuser) with no password, so pg_hba needs host ... localhost trust for that role. The gateway refuses a password outright (reject_password_environment / reject_password_in_config in configuration/setup.rs), and documentDbCredentialSecret only 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementgo

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions