From 8999307f572d3a93f3cc7fe2eb0ddbec58110413 Mon Sep 17 00:00:00 2001 From: jedi-tal <289941616+jedi-tal@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:46:58 +0200 Subject: [PATCH] fix: let OpenShift assign the gateway sidecar's UID (restricted-v2) On OpenShift, CloudNativePG leaves the instance pod's securityContext unset so that the platform assigns a UID from the namespace's range, but the sidecar injector pinned the gateway to UID/GID 1000, so restricted-v2 rejected every instance pod. The gateway image also could not run as any other UID. - sidecar-injector: gatewaySecurityContext(pod) pins 1000 only when the pod pins its own user (every cluster without SCCs) and leaves the UID to the platform otherwise. - gateway image: /home/documentdb is owned by group 0 and group-writable, so an arbitrary UID (always in group 0 on OpenShift) can read the entrypoint and write its working config. - gateway entrypoint: names the gateway's PostgreSQL role (PG_USER, default documentdb) instead of inheriting the OS user's name, which an arbitrary UID does not have. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EMu2w11RXcEs2P7xNWD15f Signed-off-by: jedi-tal <289941616+jedi-tal@users.noreply.github.com> --- .../Dockerfile_gateway_public_image | 3 ++- .github/dockerfiles/gateway_entrypoint.sh | 5 ++++ .../internal/lifecycle/lifecycle.go | 20 +++++++++++--- .../internal/lifecycle/lifecycle_test.go | 26 +++++++++++++++++-- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.github/dockerfiles/Dockerfile_gateway_public_image b/.github/dockerfiles/Dockerfile_gateway_public_image index 1a343307f..5d06712f0 100644 --- a/.github/dockerfiles/Dockerfile_gateway_public_image +++ b/.github/dockerfiles/Dockerfile_gateway_public_image @@ -37,7 +37,8 @@ RUN chmod +x \ /usr/bin/documentdb_gateway \ /home/documentdb/gateway/scripts/utils.sh \ /home/documentdb/gateway/scripts/gateway_entrypoint.sh && \ - chown -R documentdb:documentdb /home/documentdb + chown -R documentdb:0 /home/documentdb && \ + chmod -R g=u /home/documentdb USER documentdb WORKDIR /home/documentdb/gateway diff --git a/.github/dockerfiles/gateway_entrypoint.sh b/.github/dockerfiles/gateway_entrypoint.sh index dedcdcabb..352f41705 100644 --- a/.github/dockerfiles/gateway_entrypoint.sh +++ b/.github/dockerfiles/gateway_entrypoint.sh @@ -40,6 +40,11 @@ if ! [[ "$PG_PORT" =~ ^[0-9]+$ ]]; then fi jq --argjson port "$PG_PORT" '.PostgresPort = $port' "$CONFIG" > "$CONFIG.tmp" && mv "$CONFIG.tmp" "$CONFIG" +# The gateway connects to PostgreSQL as the OS user's name unless the config names a role. On OpenShift the container +# runs as an arbitrary UID with no user name, so it would connect as the bare UID: name the role the operator provisions. +PG_USER="${PG_USER:-documentdb}" +jq --arg user "$PG_USER" '.PostgresSystemUser = $user | .PostgresDataUser = $user' "$CONFIG" > "$CONFIG.tmp" && mv "$CONFIG.tmp" "$CONFIG" + if [ -n "$CERT_PATH" ] && [ -n "$KEY_FILE" ]; then jq --arg c "$CERT_PATH" --arg k "$KEY_FILE" \ '.CertificateOptions = {"CertType":"PemFile","FilePath":$c,"KeyFilePath":$k}' \ diff --git a/operator/cnpg-plugins/sidecar-injector/internal/lifecycle/lifecycle.go b/operator/cnpg-plugins/sidecar-injector/internal/lifecycle/lifecycle.go index d2fc1c7ab..71682099a 100644 --- a/operator/cnpg-plugins/sidecar-injector/internal/lifecycle/lifecycle.go +++ b/operator/cnpg-plugins/sidecar-injector/internal/lifecycle/lifecycle.go @@ -171,7 +171,7 @@ func (impl Implementation) reconcileMetadata( }, }, Env: envVars, - SecurityContext: gatewaySecurityContext(), + SecurityContext: gatewaySecurityContext(pod), LivenessProbe: gatewayLivenessProbe(configuration.GatewayPort), } if resources := buildResources( @@ -578,10 +578,22 @@ func gatewayLivenessProbe(port int32) *corev1.Probe { } // gatewaySecurityContext returns the SecurityContext for the documentdb-gateway -// sidecar: the shared PSA-restricted hardening plus an explicit UID/GID of -// 1000, the non-root user the gateway image is built to run as. -func gatewaySecurityContext() *corev1.SecurityContext { +// sidecar: the shared PSA-restricted hardening, plus an explicit UID/GID of +// 1000 (the non-root user the gateway image is built to run as) when the pod +// pins its own user. +// +// CloudNativePG leaves the pod's securityContext unset where the cluster has +// SecurityContextConstraints (OpenShift; see GetPodSecurityContext in +// pkg/specs/pods.go), so that the platform assigns the UID from the +// namespace's range. A fixed UID on this sidecar makes the restricted-v2 SCC +// reject the whole pod there ("runAsUser: Invalid value: 1000: must be in the +// ranges: [...]"), so the gateway follows CloudNativePG's choice and runs as +// the assigned UID, like the postgres container beside it. +func gatewaySecurityContext(pod *corev1.Pod) *corev1.SecurityContext { sc := hardenedSecurityContext() + if pod == nil || pod.Spec.SecurityContext == nil || pod.Spec.SecurityContext.RunAsUser == nil { + return sc + } sc.RunAsUser = pointer.Int64(1000) sc.RunAsGroup = pointer.Int64(1000) return sc diff --git a/operator/cnpg-plugins/sidecar-injector/internal/lifecycle/lifecycle_test.go b/operator/cnpg-plugins/sidecar-injector/internal/lifecycle/lifecycle_test.go index d35fb925f..0beb46cd0 100644 --- a/operator/cnpg-plugins/sidecar-injector/internal/lifecycle/lifecycle_test.go +++ b/operator/cnpg-plugins/sidecar-injector/internal/lifecycle/lifecycle_test.go @@ -504,9 +504,13 @@ func TestHardenedSecurityContext_NoForcedUID(t *testing.T) { } // TestGatewaySecurityContext_PSARestrictedAsUID1000 asserts the gateway sidecar -// is PSA restricted and pinned to the non-root UID/GID 1000 its image expects. +// is PSA restricted and pinned to the non-root UID/GID 1000 its image expects +// when CloudNativePG pins the pod's user, as it does wherever the cluster has +// no SecurityContextConstraints. func TestGatewaySecurityContext_PSARestrictedAsUID1000(t *testing.T) { - sc := gatewaySecurityContext() + postgresUID := int64(26) + pod := &corev1.Pod{Spec: corev1.PodSpec{SecurityContext: &corev1.PodSecurityContext{RunAsUser: &postgresUID}}} + sc := gatewaySecurityContext(pod) assertPSARestricted(t, "gatewaySecurityContext", sc) if sc.RunAsUser == nil || *sc.RunAsUser != 1000 { t.Errorf("gateway must run as UID 1000, got %v", sc.RunAsUser) @@ -516,6 +520,24 @@ func TestGatewaySecurityContext_PSARestrictedAsUID1000(t *testing.T) { } } +// TestGatewaySecurityContext_PlatformAssignedUID asserts the gateway sidecar +// pins no UID/GID when CloudNativePG leaves the pod's user to the platform +// (OpenShift's SecurityContextConstraints), so the restricted-v2 SCC can assign +// one from the namespace's range instead of rejecting the pod, and that it +// stays PSA restricted. +func TestGatewaySecurityContext_PlatformAssignedUID(t *testing.T) { + for name, pod := range map[string]*corev1.Pod{ + "no pod securityContext": {}, + "pod securityContext without runAsUser": {Spec: corev1.PodSpec{SecurityContext: &corev1.PodSecurityContext{}}}, + } { + sc := gatewaySecurityContext(pod) + assertPSARestricted(t, name, sc) + if sc.RunAsUser != nil || sc.RunAsGroup != nil { + t.Errorf("%s: gateway must leave the UID/GID to the platform, got %v/%v", name, sc.RunAsUser, sc.RunAsGroup) + } + } +} + // TestNewOtelCollectorSidecar_Hardened is the injection-layer guard for the // otel-collector sidecar (which the e2e suite does not exercise unless // monitoring is enabled). It asserts the constructed container is PSA