From 6a9e62fd106c2d7ef34a19a44e18c88604d016b2 Mon Sep 17 00:00:00 2001 From: danc094codetogether Date: Tue, 20 Oct 2026 18:35:42 +0000 Subject: [PATCH 1/3] AITRAX-2721: codetogether-ai chart - Gateway API (HTTPRoute) and client IP hops Clusters moving from ingress-nginx to Gateway API (Claritev, NGINX Gateway Fabric) need the chart to attach to an existing Gateway. - gateway.enabled (off by default): HTTPRoute for the service_fqdn host, / to the chart Service, attached through gateway.parentRefs. Fails with a clear message without parentRefs or without Gateway API CRDs. - NGINX Gateway Fabric ClientSettingsPolicy (body max 16m), the equivalent of the proxy-body-size Ingress annotation; auto renders only when the cluster serves the API. - clientIp.trustedProxyHops (default 2) passed as CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS. The server defaults to 1 (Docker), so on Kubernetes client addresses resolved to the Ingress controller. - NOTES, README, chart version 0.2.0, CI render in Gateway mode. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/helm-template-check.yml | 11 ++++ charts/ai/Chart.yaml | 2 +- charts/ai/README.md | 52 +++++++++++++++++++ charts/ai/templates/NOTES.txt | 30 +++++++++-- charts/ai/templates/_helpers.tpl | 38 ++++++++++++++ charts/ai/templates/clientsettingspolicy.yaml | 18 +++++++ charts/ai/templates/deployment.yaml | 3 ++ charts/ai/templates/httproute.yaml | 35 +++++++++++++ charts/ai/values.yaml | 47 +++++++++++++++++ 9 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 charts/ai/templates/clientsettingspolicy.yaml create mode 100644 charts/ai/templates/httproute.yaml diff --git a/.github/workflows/helm-template-check.yml b/.github/workflows/helm-template-check.yml index df5e251..e8cc1ca 100644 --- a/.github/workflows/helm-template-check.yml +++ b/.github/workflows/helm-template-check.yml @@ -40,6 +40,17 @@ jobs: --set service.fqdn=https://example.codetogether.io \ --set imageCredentials.username=test \ --set imageCredentials.password=test + helm template test ./charts/ai \ + --set-file ctaiPropertiesFile=./charts/ai/ctai-configuration.properties.template \ + --set service.fqdn=https://example.codetogether.io \ + --set imageCredentials.username=test \ + --set imageCredentials.password=test \ + --set ingress.enabled=false \ + --set gateway.enabled=true \ + --set 'gateway.parentRefs[0].name=shared-gateway' \ + --set 'gateway.parentRefs[0].namespace=gateway-system' \ + --api-versions gateway.networking.k8s.io/v1/HTTPRoute \ + --api-versions gateway.nginx.org/v1alpha1/ClientSettingsPolicy - name: Validate Live Chart run: | diff --git a/charts/ai/Chart.yaml b/charts/ai/Chart.yaml index 71962b1..9817f81 100644 --- a/charts/ai/Chart.yaml +++ b/charts/ai/Chart.yaml @@ -3,7 +3,7 @@ name: codetogether-ai description: CodeTogether AI on-premises Kubernetes deployment type: application -version: 0.1.0 +version: 0.2.0 appVersion: "0.1.1204" icon: https://www.codetogether.com/wp-content/uploads/2020/02/codetogether-circle-128.png diff --git a/charts/ai/README.md b/charts/ai/README.md index 565cd4f..c9ea6aa 100644 --- a/charts/ai/README.md +++ b/charts/ai/README.md @@ -13,6 +13,8 @@ storage master key is stored in a separate Kubernetes Secret. - PostgreSQL reachable from the cluster. This chart does not install Postgres. - A DNS-routable HTTPS host for CodeTogether AI, configured as `service_fqdn`. - A TLS Secret for Ingress when `ingress.enabled=true`. +- Or, instead of an Ingress controller, Gateway API CRDs and a `Gateway` when + `gateway.enabled=true` (see "Gateway API" below). - CodeTogether-provided credentials for `hub.edge.codetogether.com`. ## PostgreSQL Prerequisites @@ -161,6 +163,50 @@ explicitly: If you set `ingress.className=codetogether-nginx`, the chart also creates a matching `IngressClass`. +### Gateway API (NGINX Gateway Fabric) + +Clusters that route with Gateway API instead of an Ingress controller (for +example NGINX Gateway Fabric, which replaces the retired ingress-nginx) can have +the chart attach an `HTTPRoute` to an existing `Gateway`: + +```yaml +ingress: + enabled: false +gateway: + enabled: true + parentRefs: + - name: shared-gateway # your Gateway + namespace: gateway-system # omit if it is in the release namespace + sectionName: https # the HTTPS listener with the certificate +``` + +The chart renders: + +- an `HTTPRoute` for the `service_fqdn` host, `/` to the chart Service; +- with NGINX Gateway Fabric, a `ClientSettingsPolicy` that raises the request + body limit to `16m` (`gateway.nginxGatewayFabric.clientSettingsPolicy`). It + replaces the Ingress annotation `nginx.ingress.kubernetes.io/proxy-body-size`; + without it, tracker uploads over 1MB fail with 413. With `enabled: auto` it is + rendered only when the cluster serves that API. For another Gateway API + implementation, set the equivalent limit on your side. + +The chart doesn't create the `Gateway`, its listeners or certificates. On the +Gateway side, check that: + +- the listener allows routes from the release namespace + (`allowedRoutes.namespaces`); +- the listener can read its certificate Secret. A Secret in another namespace + needs a `ReferenceGrant` there. +- the route is accepted: + `kubectl get httproute codetogether-ai -o jsonpath='{.status.parents[*].conditions}'`. + +### Client IP addresses + +`clientIp.trustedProxyHops` (default `2`: Ingress controller or Gateway, plus the +nginx in the pod) tells the server how many proxies in front of the pod append +to `X-Forwarded-For`. Add one for each extra proxy in front (load balancer, WAF, +corporate proxy). Too high resolves to no address, never to a wrong one. + ## Configuration The following table lists the primary configurable parameters and defaults. @@ -193,6 +239,12 @@ The following table lists the primary configurable parameters and defaults. | `ingress.enabled` | Create an Ingress resource | `true` | | `ingress.annotations` | Ingress annotations | `nginx.ingress.kubernetes.io/proxy-body-size: "16m"` | | `ingress.tls.secretName` | TLS Secret for Ingress | `codetogether-ai-tls` | +| `gateway.enabled` | Create a Gateway API `HTTPRoute` | `false` | +| `gateway.parentRefs` | Gateway(s) the `HTTPRoute` attaches to; required when enabled | `[]` | +| `gateway.annotations` / `gateway.labels` | Extra `HTTPRoute` metadata | `{}` | +| `gateway.nginxGatewayFabric.clientSettingsPolicy.enabled` | NGINX Gateway Fabric body-size policy: `auto`, `true` or `false` | `auto` | +| `gateway.nginxGatewayFabric.clientSettingsPolicy.bodyMaxSize` | Request body limit on the Gateway | `16m` | +| `clientIp.trustedProxyHops` | Proxies in front of the pod that append to `X-Forwarded-For` | `2` | | `serviceAccount.create` | Create a ServiceAccount | `true` | | `serviceAccount.name` | ServiceAccount name | `codetogether-ai` | | `replicaCount` | Deployment replica count | `1` | diff --git a/charts/ai/templates/NOTES.txt b/charts/ai/templates/NOTES.txt index 07dbbff..03371c9 100644 --- a/charts/ai/templates/NOTES.txt +++ b/charts/ai/templates/NOTES.txt @@ -1,7 +1,6 @@ Application URL: -{{- if .Values.ingress.enabled }} - {{- $host := (urlParse (include "ctai.serviceFqdn" .)).host }} - https://{{ $host }}/ +{{- if or .Values.ingress.enabled .Values.gateway.enabled }} + https://{{ include "ctai.publicHost" . }}/ {{- else }} Use: kubectl port-forward svc/{{ include "ctai.fullname" . }} 1080:{{ .Values.service.port }} {{- end }} @@ -34,3 +33,28 @@ Protected storage master key: Production: run ./generate-master-key.sh in this chart directory before install. {{- end }} {{- end }} + +{{- if .Values.gateway.enabled }} + +Gateway API: + {{- $parents := list }} + {{- range .Values.gateway.parentRefs }} + {{- $p := .name }} + {{- with .namespace }}{{ $p = printf "%s (namespace %s)" $p . }}{{ end }} + {{- with .sectionName }}{{ $p = printf "%s listener %s" $p . }}{{ end }} + {{- $parents = append $parents $p }} + {{- end }} + HTTPRoute {{ include "ctai.fullname" . }} attaches to: {{ join ", " $parents }} + Check it was accepted: kubectl get httproute {{ include "ctai.fullname" . }} -n {{ .Release.Namespace }} -o jsonpath='{.status.parents[*].conditions}' +{{- if eq (include "ctai.renderClientSettingsPolicy" .) "true" }} + Request body limit: ClientSettingsPolicy {{ include "ctai.fullname" . }} ({{ .Values.gateway.nginxGatewayFabric.clientSettingsPolicy.bodyMaxSize }}). +{{- else }} + WARNING: no request body limit was rendered for the Gateway. OTLP uploads over + the gateway's default (1MB on NGINX Gateway Fabric) will fail with 413. Set the + equivalent limit for your Gateway implementation. +{{- end }} +{{- if .Values.ingress.enabled }} + WARNING: ingress.enabled is also true, so both an Ingress and an HTTPRoute + serve this host. Set ingress.enabled=false once the Gateway path works. +{{- end }} +{{- end }} diff --git a/charts/ai/templates/_helpers.tpl b/charts/ai/templates/_helpers.tpl index ccff0b2..db1a303 100644 --- a/charts/ai/templates/_helpers.tpl +++ b/charts/ai/templates/_helpers.tpl @@ -229,3 +229,41 @@ Host:port for CTAI_SERVICE_EXTERNAL_IDENTIFIER from the resolved service URL. {{- $parsed.host -}} {{- end -}} {{- end }} + +{{/* +Proxies in front of the pod (Ingress controller or Gateway + in-pod nginx), +passed to the server as CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS. +*/}} +{{- define "ctai.trustedProxyHops" -}} +{{- $clientIp := .Values.clientIp | default dict -}} +{{- $hops := 2 -}} +{{- if hasKey $clientIp "trustedProxyHops" -}} +{{- $hops = $clientIp.trustedProxyHops -}} +{{- end -}} +{{- $n := int $hops -}} +{{- if lt $n 1 -}} +{{- fail (printf "clientIp.trustedProxyHops must be a whole number of at least 1 (the in-pod nginx is always a hop), got %v" $hops) -}} +{{- end -}} +{{- $n -}} +{{- end }} + +{{/* +Public host for the Ingress and the HTTPRoute. +*/}} +{{- define "ctai.publicHost" -}} +{{- (urlParse (include "ctai.serviceFqdn" .)).host -}} +{{- end }} + +{{/* +Whether to render the NGINX Gateway Fabric ClientSettingsPolicy: "true" or "". +*/}} +{{- define "ctai.renderClientSettingsPolicy" -}} +{{- if .Values.gateway.enabled -}} +{{- $mode := toString ((.Values.gateway.nginxGatewayFabric | default dict).clientSettingsPolicy | default dict).enabled -}} +{{- if eq $mode "true" -}} +true +{{- else if and (eq $mode "auto") (.Capabilities.APIVersions.Has "gateway.nginx.org/v1alpha1/ClientSettingsPolicy") -}} +true +{{- end -}} +{{- end -}} +{{- end }} diff --git a/charts/ai/templates/clientsettingspolicy.yaml b/charts/ai/templates/clientsettingspolicy.yaml new file mode 100644 index 0000000..08f9e54 --- /dev/null +++ b/charts/ai/templates/clientsettingspolicy.yaml @@ -0,0 +1,18 @@ +{{- if eq (include "ctai.renderClientSettingsPolicy" .) "true" }} +{{- $fullName := include "ctai.fullname" . }} +# NGINX Gateway Fabric: request body limit for this route (replaces the Ingress +# annotation nginx.ingress.kubernetes.io/proxy-body-size). +apiVersion: gateway.nginx.org/v1alpha1 +kind: ClientSettingsPolicy +metadata: + name: {{ $fullName }} + labels: + {{- include "ctai.labels" . | nindent 4 }} +spec: + targetRef: + group: gateway.networking.k8s.io + kind: HTTPRoute + name: {{ $fullName }} + body: + maxSize: {{ .Values.gateway.nginxGatewayFabric.clientSettingsPolicy.bodyMaxSize | quote }} +{{- end }} diff --git a/charts/ai/templates/deployment.yaml b/charts/ai/templates/deployment.yaml index 6e34fa3..b023890 100644 --- a/charts/ai/templates/deployment.yaml +++ b/charts/ai/templates/deployment.yaml @@ -71,6 +71,9 @@ spec: key: {{ .Values.masterKey.existingSecretVersionKey | default "CTAI_PROTECTED_STORAGE_MASTER_KEY_VERSION" }} optional: true {{- end }} + # X-Forwarded-For trust: see clientIp in values.yaml. + - name: CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS + value: {{ include "ctai.trustedProxyHops" . | quote }} {{- with .Values.extraEnvVars }} {{- toYaml . | nindent 12 }} {{- end }} diff --git a/charts/ai/templates/httproute.yaml b/charts/ai/templates/httproute.yaml new file mode 100644 index 0000000..175889d --- /dev/null +++ b/charts/ai/templates/httproute.yaml @@ -0,0 +1,35 @@ +{{- if .Values.gateway.enabled }} +{{- if not .Values.gateway.parentRefs }} +{{- fail "gateway.enabled=true requires gateway.parentRefs: the Gateway (name, and namespace/sectionName if needed) the HTTPRoute attaches to." }} +{{- end }} +{{- if not (.Capabilities.APIVersions.Has "gateway.networking.k8s.io/v1/HTTPRoute") }} +{{- fail "gateway.enabled=true but the cluster does not serve gateway.networking.k8s.io/v1 HTTPRoute. Install the Gateway API CRDs (or pass --api-versions gateway.networking.k8s.io/v1/HTTPRoute to helm template)." }} +{{- end }} +{{- $fullName := include "ctai.fullname" . }} +apiVersion: gateway.networking.k8s.io/v1 +kind: HTTPRoute +metadata: + name: {{ $fullName }} + labels: + {{- include "ctai.labels" . | nindent 4 }} + {{- with .Values.gateway.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.gateway.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + parentRefs: + {{- toYaml .Values.gateway.parentRefs | nindent 4 }} + hostnames: + - {{ include "ctai.publicHost" . | quote }} + rules: + - matches: + - path: + type: PathPrefix + value: / + backendRefs: + - name: {{ $fullName }} + port: {{ .Values.service.port }} +{{- end }} diff --git a/charts/ai/values.yaml b/charts/ai/values.yaml index 5ae934f..fecc728 100644 --- a/charts/ai/values.yaml +++ b/charts/ai/values.yaml @@ -78,6 +78,53 @@ ingress: tls: secretName: codetogether-ai-tls +# Gateway API (gateway.networking.k8s.io/v1), for clusters that route with a +# Gateway instead of an Ingress controller (for example NGINX Gateway Fabric, +# replacing the retired ingress-nginx). Off by default. +# +# The chart attaches an HTTPRoute to a Gateway you already run; it does not +# create the Gateway, its listeners or its certificate. Set ingress.enabled=false +# when you switch, unless you deliberately run both during a migration. +gateway: + enabled: false + # Gateway(s) the HTTPRoute attaches to. Required when enabled. Example: + # parentRefs: + # - name: shared-gateway + # namespace: gateway-system + # sectionName: https # the HTTPS listener that holds the certificate + parentRefs: [] + annotations: {} + labels: {} + # NGINX Gateway Fabric only. Replaces the Ingress annotation + # nginx.ingress.kubernetes.io/proxy-body-size: OTLP telemetry batches exceed + # the 1MB default and would be rejected with 413 before reaching the backend. + nginxGatewayFabric: + clientSettingsPolicy: + # auto: render only when the cluster serves + # gateway.nginx.org/v1alpha1 ClientSettingsPolicy + # true: always render (e.g. `helm template` without cluster access) + # false: never render (another Gateway API implementation) + enabled: auto + bodyMaxSize: "16m" + +# Client address handling (X-Forwarded-For trust). +# +# The server counts proxies from the right-hand end of +# `X-Forwarded-For ++ [socket peer]`, so a client-supplied header can't choose +# the recorded address. It needs to know how many proxies in front of the pod +# this deployment vouches for: +# +# 2 your Ingress controller or Gateway, plus the nginx inside the pod. +# Correct for a stock Kubernetes install (the default). +# 3+ add one for each proxy in front that appends to X-Forwarded-For +# (external load balancer, WAF, corporate reverse proxy). +# +# Too high resolves to no address (recorded as degraded), never to a wrong one. +# Without this setting the server assumes 1 (Docker), and every address +# resolves to the Ingress controller or Gateway. +clientIp: + trustedProxyHops: 2 + serviceAccount: create: true annotations: {} From aa1238b952bb993c91666b19308663062aef72bb Mon Sep 17 00:00:00 2001 From: danc094codetogether Date: Tue, 20 Oct 2026 18:40:25 +0000 Subject: [PATCH 2/3] AITRAX-2721: strip the port from the HTTPRoute hostname; no duplicate hops env var - Gateway API hostnames can't carry a port; a service_fqdn like https://host:8443 produced an HTTPRoute the API server would reject. - When extraEnvVars already sets CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS the chart no longer renders its own, so the container doesn't get the variable twice. - Document hops=1 for a Service reached directly, with no proxy in front. Co-Authored-By: Claude Opus 5.5 --- charts/ai/README.md | 6 ++++-- charts/ai/templates/_helpers.tpl | 20 +++++++++++++++++++- charts/ai/templates/deployment.yaml | 2 ++ charts/ai/templates/httproute.yaml | 2 +- charts/ai/values.yaml | 3 +++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/charts/ai/README.md b/charts/ai/README.md index c9ea6aa..8c9fa80 100644 --- a/charts/ai/README.md +++ b/charts/ai/README.md @@ -198,14 +198,16 @@ Gateway side, check that: - the listener can read its certificate Secret. A Secret in another namespace needs a `ReferenceGrant` there. - the route is accepted: - `kubectl get httproute codetogether-ai -o jsonpath='{.status.parents[*].conditions}'`. + `kubectl get httproute -o jsonpath='{.status.parents[*].conditions}'` + (the install notes print the exact command). ### Client IP addresses `clientIp.trustedProxyHops` (default `2`: Ingress controller or Gateway, plus the nginx in the pod) tells the server how many proxies in front of the pod append to `X-Forwarded-For`. Add one for each extra proxy in front (load balancer, WAF, -corporate proxy). Too high resolves to no address, never to a wrong one. +corporate proxy). Too high resolves to no address, never to a wrong one. A +`CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS` entry in `extraEnvVars` takes precedence. ## Configuration diff --git a/charts/ai/templates/_helpers.tpl b/charts/ai/templates/_helpers.tpl index db1a303..24fbf46 100644 --- a/charts/ai/templates/_helpers.tpl +++ b/charts/ai/templates/_helpers.tpl @@ -248,12 +248,30 @@ passed to the server as CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS. {{- end }} {{/* -Public host for the Ingress and the HTTPRoute. +Public host (with any port) from service_fqdn, as shown in the application URL. */}} {{- define "ctai.publicHost" -}} {{- (urlParse (include "ctai.serviceFqdn" .)).host -}} {{- end }} +{{/* +HTTPRoute hostname: the public host without a port (Gateway API hostnames +can't carry one). +*/}} +{{- define "ctai.routeHostname" -}} +{{- regexReplaceAll ":[0-9]+$" (include "ctai.publicHost" .) "" -}} +{{- end }} + +{{/* +"true" when extraEnvVars already sets CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS, so +the chart doesn't render a duplicate. +*/}} +{{- define "ctai.trustedProxyHopsInExtraEnv" -}} +{{- range .Values.extraEnvVars -}} +{{- if eq (toString .name) "CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS" -}}true{{- end -}} +{{- end -}} +{{- end }} + {{/* Whether to render the NGINX Gateway Fabric ClientSettingsPolicy: "true" or "". */}} diff --git a/charts/ai/templates/deployment.yaml b/charts/ai/templates/deployment.yaml index b023890..dd87f27 100644 --- a/charts/ai/templates/deployment.yaml +++ b/charts/ai/templates/deployment.yaml @@ -71,9 +71,11 @@ spec: key: {{ .Values.masterKey.existingSecretVersionKey | default "CTAI_PROTECTED_STORAGE_MASTER_KEY_VERSION" }} optional: true {{- end }} + {{- if not (include "ctai.trustedProxyHopsInExtraEnv" .) }} # X-Forwarded-For trust: see clientIp in values.yaml. - name: CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS value: {{ include "ctai.trustedProxyHops" . | quote }} + {{- end }} {{- with .Values.extraEnvVars }} {{- toYaml . | nindent 12 }} {{- end }} diff --git a/charts/ai/templates/httproute.yaml b/charts/ai/templates/httproute.yaml index 175889d..af005aa 100644 --- a/charts/ai/templates/httproute.yaml +++ b/charts/ai/templates/httproute.yaml @@ -23,7 +23,7 @@ spec: parentRefs: {{- toYaml .Values.gateway.parentRefs | nindent 4 }} hostnames: - - {{ include "ctai.publicHost" . | quote }} + - {{ include "ctai.routeHostname" . | quote }} rules: - matches: - path: diff --git a/charts/ai/values.yaml b/charts/ai/values.yaml index fecc728..7712d1f 100644 --- a/charts/ai/values.yaml +++ b/charts/ai/values.yaml @@ -118,10 +118,13 @@ gateway: # Correct for a stock Kubernetes install (the default). # 3+ add one for each proxy in front that appends to X-Forwarded-For # (external load balancer, WAF, corporate reverse proxy). +# 1 nothing proxies in front of the Service (for example a LoadBalancer +# Service reached directly, with no Ingress or Gateway). # # Too high resolves to no address (recorded as degraded), never to a wrong one. # Without this setting the server assumes 1 (Docker), and every address # resolves to the Ingress controller or Gateway. +# A CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS entry in extraEnvVars takes precedence. clientIp: trustedProxyHops: 2 From 8d717d13806641e4dac30a1c4a14bd7de635e4f6 Mon Sep 17 00:00:00 2001 From: danc094codetogether Date: Tue, 20 Oct 2026 18:54:50 +0000 Subject: [PATCH 3/3] AITRAX-2721: validate trustedProxyHops as given; correct the X-Forwarded-For guidance Copilot review: - int coerced the value before the check, so 2.9 became 2 and true became 1 and both passed. The value must now be a whole number as written (number or numeric string), then at least 1. - Overestimating the hop count is not always safe: when the outermost proxy keeps an incoming X-Forwarded-For, too high records the client-supplied entry. values.yaml and README now say to match the real chain and explain both failure directions. Co-Authored-By: Claude Opus 5.5 --- charts/ai/README.md | 4 +++- charts/ai/templates/_helpers.tpl | 4 ++++ charts/ai/values.yaml | 12 ++++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/charts/ai/README.md b/charts/ai/README.md index 8c9fa80..7acccaa 100644 --- a/charts/ai/README.md +++ b/charts/ai/README.md @@ -206,7 +206,9 @@ Gateway side, check that: `clientIp.trustedProxyHops` (default `2`: Ingress controller or Gateway, plus the nginx in the pod) tells the server how many proxies in front of the pod append to `X-Forwarded-For`. Add one for each extra proxy in front (load balancer, WAF, -corporate proxy). Too high resolves to no address, never to a wrong one. A +corporate proxy). Set it to exactly the proxies you control: too low records a +proxy's address, and too high can record an address the client put in its own +`X-Forwarded-For` when your outermost proxy keeps that header. A `CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS` entry in `extraEnvVars` takes precedence. ## Configuration diff --git a/charts/ai/templates/_helpers.tpl b/charts/ai/templates/_helpers.tpl index 24fbf46..7422a00 100644 --- a/charts/ai/templates/_helpers.tpl +++ b/charts/ai/templates/_helpers.tpl @@ -240,6 +240,10 @@ passed to the server as CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS. {{- if hasKey $clientIp "trustedProxyHops" -}} {{- $hops = $clientIp.trustedProxyHops -}} {{- end -}} +{{- /* Validate the value as given: int would turn 2.9 into 2 and true into 1. */ -}} +{{- if or (kindIs "bool" $hops) (not (regexMatch "^[0-9]+$" (toString $hops))) -}} +{{- fail (printf "clientIp.trustedProxyHops must be a whole number of at least 1 (the in-pod nginx is always a hop), got %v" $hops) -}} +{{- end -}} {{- $n := int $hops -}} {{- if lt $n 1 -}} {{- fail (printf "clientIp.trustedProxyHops must be a whole number of at least 1 (the in-pod nginx is always a hop), got %v" $hops) -}} diff --git a/charts/ai/values.yaml b/charts/ai/values.yaml index 7712d1f..4f9f431 100644 --- a/charts/ai/values.yaml +++ b/charts/ai/values.yaml @@ -110,9 +110,9 @@ gateway: # Client address handling (X-Forwarded-For trust). # # The server counts proxies from the right-hand end of -# `X-Forwarded-For ++ [socket peer]`, so a client-supplied header can't choose -# the recorded address. It needs to know how many proxies in front of the pod -# this deployment vouches for: +# `X-Forwarded-For ++ [socket peer]` and records the entry just past the proxies +# it trusts. Set the count to exactly the proxies in front of the pod that you +# control and that append to X-Forwarded-For: # # 2 your Ingress controller or Gateway, plus the nginx inside the pod. # Correct for a stock Kubernetes install (the default). @@ -121,7 +121,11 @@ gateway: # 1 nothing proxies in front of the Service (for example a LoadBalancer # Service reached directly, with no Ingress or Gateway). # -# Too high resolves to no address (recorded as degraded), never to a wrong one. +# Match the real chain. Too low records one of your proxies' addresses. Too high +# reads past the client: if your outermost proxy keeps an incoming +# X-Forwarded-For (many Gateways append to it), that entry is whatever the +# client sent, so it can be spoofed. Only when the outermost proxy replaces the +# header does too high resolve to no address (recorded as degraded). # Without this setting the server assumes 1 (Docker), and every address # resolves to the Ingress controller or Gateway. # A CTAI_INGEST_OTEL_TRUSTED_PROXY_HOPS entry in extraEnvVars takes precedence.