Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/dockerfiles/Dockerfile_gateway_public_image
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions .github/dockerfiles/gateway_entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}' \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (impl Implementation) reconcileMetadata(
},
},
Env: envVars,
SecurityContext: gatewaySecurityContext(),
SecurityContext: gatewaySecurityContext(pod),
LivenessProbe: gatewayLivenessProbe(configuration.GatewayPort),
}
if resources := buildResources(
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down