Skip to content

Security: dvrkn/kubepg

Security

docs/security.md

Security

Credential flow

Passwords and DSNs are only ever supplied via Secret references — never inline in a CR spec:

  • Instance.spec.passwordSecretRef / Instance.spec.dsnSecretRef
  • Instance.spec.sslRootCertSecretRef
  • Role.spec.passwordSecretRef
  • (planned) Subscription.spec.connInfoSecretRef

Each is a SecretKeySelector — {name, key, namespace} — resolved in-memory via readSecretKey / Manager.resolveSecret at reconcile time. The resolved value:

  • is used to build a connection string or a SQL parameter,
  • is never written back into any CR's spec or status,
  • is never emitted in a Kubernetes Event,
  • is never logged. Log lines and Ready condition messages carry only object names and PostgreSQL SQLSTATE codes (pg.SQLState(err)) — never the raw error string, since a raw pgconn error can embed the DSN it failed to connect with.

This invariant is enforced by convention across every controller, not by a redaction layer — if you're adding a new field or error path, thread it through pg.SQLState(err) rather than err.Error() in anything that becomes a condition message or log line, and never assign secret content to spec/status.

Connection pool caching

internal/pg.Manager caches one pgxpool.Pool per (instance, database, connection-fingerprint), where the fingerprint is a truncated SHA-256 hash of the fully resolved connection string — so the cache key itself never holds plaintext credentials, while still correctly evicting and rebuilding the pool when a referenced Secret's value changes (a password rotation, a new root CA). Pools are also explicitly evicted before a DROP DATABASE (which would otherwise be blocked by the operator's own idle connections) and when an Instance is deleted.

SSL/TLS

Instance.spec.sslmode defaults to require. For verify-ca / verify-full, supply a PEM root CA via sslRootCertSecretRef — it's written to a temp file per-connection-build (libpq's sslrootcert option needs a path, not inline PEM) and never persisted elsewhere.

RBAC

The operator's ClusterRole needs get/list/watch on the CRDs it reconciles, update on their status and finalizers subresources, and get/list/watch on secrets cluster-wide (it doesn't know in advance which namespaces hold connection Secrets, since SecretKeySelector.namespace is caller-supplied per resource). See config/rbac/role.yaml for the generated policy.

In production, scope this down: if all connection Secrets live in a small, known set of namespaces, replace the cluster-wide secrets rule with per-namespace Role/RoleBindings covering just those namespaces. This is a deployment-time hardening step, not something the chart does for you by default, because the right scope is environment-specific.

The chart also ships a hardened Pod-level securityContext (runAsNonRoot: true, seccompProfile: RuntimeDefault) and a container securityContext (allowPrivilegeEscalation: false, capabilities.drop: [ALL]) by default — see charts/kubepg/values.yaml. networkPolicy.enable=true additionally restricts the manager Pod's egress/ingress; it's off by default because the right policy depends on where your PostgreSQL server and the Kubernetes API server live relative to the cluster.

Multi-tenancy note

Instance is cluster-scoped by design — any namespace can create a managed resource referencing any Instance by name, there is no per-namespace access control on which Instances a namespace may reference. If you're running kubepg in a cluster with untrusted or lower-trust namespaces, treat "can create a Database/Role/Grant referencing Instance X" as equivalent to "has some access to the PostgreSQL server behind X," and gate CR creation with Kubernetes RBAC on the CRDs themselves (namespace-scoped Role/RoleBinding restricting who can create kubepg.dvrkn.com resources in a given namespace) accordingly.

There aren't any published security advisories