diff --git a/.github/workflows/manifests.yml b/.github/workflows/manifests.yml index cde6c307e..a8cd0d870 100644 --- a/.github/workflows/manifests.yml +++ b/.github/workflows/manifests.yml @@ -124,12 +124,18 @@ jobs: echo "$preset: $(grep -c '^kind:' "/tmp/$preset.yaml") resources" done + # The CloudNativePG example's Cluster and Database are custom resources, not in the default schema + # set. They are validated against the CRD schemas in the datreeio CRDs-catalog (the second + # location: postgresql.cnpg.io/cluster_v1.json and database_v1.json), NOT skipped with + # -ignore-missing-schemas: a wrong field name in the example must fail here. -strict rejects an + # unknown field. - name: Schemas run: | kubeconform -strict -summary \ -schema-location default \ -schema-location 'https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json' \ - deploy/k8s/simple/spire.yaml deploy/k8s/production/spire.yaml + deploy/k8s/simple/spire.yaml deploy/k8s/production/spire.yaml \ + deploy/examples/cloudnative-pg/cluster.yaml kind-install: name: kind install (simple preset) diff --git a/deploy/README.md b/deploy/README.md index 5a18e90a0..91c644737 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -126,13 +126,103 @@ Point `SPIRE_OIDC_AUTH_SERVER_URL` at it and drop the `keycloak` service. The re - **Two realm roles**, `spire-viewer` and `spire-admin`, readable from the **access** token. Reading them from the ID token yields an operator with no roles, and every endpoint then denies. -`deploy/keycloak/realm-spire.json` is a working example of all of that. +`deploy/keycloak/realm-spire.json` is a working example of all of that, for development only: it has +users, client secrets, the password grant and `localhost` URIs. For a real identity provider, start +from `deploy/keycloak/realm-spire-production.json` (see "Install on any cluster", step 4). **The issuer must be reachable under one name from both the browser and the containers.** An unpinned Keycloak derives its issuer from the `Host` it was called on, so the two would disagree and every token would fail validation. The bundled instance pins `KC_HOSTNAME` and sets `KC_HOSTNAME_BACKCHANNEL_DYNAMIC` so front- and backchannel can differ while the issuer stays fixed. +## Install on any cluster + +The Helm chart with the `production` preset, Postgres you run (or CloudNativePG), the chart's bundled +Redpanda, and an identity provider you already run. This installs the **reviewer only**: gateway, +orchestrator, review worker and dashboard. Agent work (harness reviews, `/fix`, the factory) needs the +run worker, which comes to Kubernetes with M5 (below). + +**Not yet run on a real cluster.** The chart renders and its invariants are checked in CI, but no +install with external Postgres and an external identity provider has been done yet +(`docs/UNVERIFIED.md`). Do the steps in this order. + +1. **Namespace.** `kubectl create namespace spire`. Everything below goes in it: a pod reads a Secret + only from its own namespace. + +2. **Postgres.** Version 15 or later (the migrations use `NULLS NOT DISTINCT`). Pick one: + - **Bundled**: the `simple` preset's single Postgres. For evaluation only (no backup). + - **External**: set `postgres.host`, make the gateway role and schema with the two statements in + "The gateway's database role" above, and set `postgres.provisionGatewayRole: false` if the chart + must not make them. + - **CloudNativePG** (operator 1.26 or later): first make the gateway role's basic-auth Secret, then + apply the example: + + ```bash + kubectl -n spire create secret generic spire-db-gateway --type=kubernetes.io/basic-auth \ + --from-literal=username=gateway --from-literal=password="$(openssl rand -hex 24)" + kubectl -n spire apply -f deploy/examples/cloudnative-pg/cluster.yaml + ``` + + CloudNativePG makes the owner `spire` and its Secret `spire-db-app`; a `Database` object makes the + `gateway` schema, owned by `gateway`. The chart reads both passwords from those two Secrets + (`postgres.passwordSecret`, `postgres.gatewayPasswordSecret`), so each password is stored once. + Connect to the `-rw` Service, never through a Pooler in transaction mode: the orchestrator holds + a `LISTEN` session. + +3. **The two Secrets.** The chart takes Secret names only ("The four secrets" above says why). + + | Secret | Keys | + |---|---| + | `secrets.existingSecret` | `SPIRE_ENCRYPTION_KEYSET`, `SPIRE_OIDC_ORCHESTRATOR_SECRET`, `SPIRE_OIDC_GATEWAY_SECRET`, `SPIRE_OIDC_WORKER_SECRET`, and `POSTGRES_PASSWORD` unless `postgres.passwordSecret` is set | + | `secrets.gatewayExistingSecret` | `SPIRE_ENCRYPTION_WEBHOOK_KEYSET`, and `GATEWAY_POSTGRES_PASSWORD` unless `postgres.gatewayPasswordSecret` is set | + + They must be two different Secrets. The three OIDC secrets come from step 4, so make the main Secret + after it, or update it then. + +4. **The realm.** Copy `deploy/keycloak/realm-spire-production.json` and replace the two + placeholders: `SPIRE_REALM` with the realm name, `SPIRE_HOST` with the host the browser uses (no + scheme, no path). Create a **new** realm from the file (a partial import into an existing realm is + a different path). Then, for each of the three clients, open its Credentials tab, press + **Regenerate**, and copy the secret into the main Secret's matching key. Whether Keycloak makes a + secret on import when the file has none is not proven, so do not skip Regenerate. Give your + operators the realm roles `spire-viewer` and `spire-admin`. + +5. **Install.** Copy `deploy/examples/cloudnative-pg/values.yaml`, replace each placeholder + (`example.invalid`, the pod CIDR in `trustedProxies`, the database host), then: + + ```bash + helm install spire deploy/helm/spire -n spire \ + -f deploy/helm/spire/values-production.yaml -f my-values.yaml + ``` + + With an empty `image.tag` the chart pulls its own `appVersion`: a chart from a release pulls that + release's images; this repository's chart pulls `edge`. For settings the chart does not model, + each service takes `extraEnv` and `extraEnvFrom`. The render refuses a name the chart already sets, + and on the gateway the master keyset or any reference to the main Secret. It cannot see a + different Secret that also holds the master keyset: keep that key out of anything the gateway + loads. + +6. **Point the TLS terminator at the dashboard.** The `spire-ui` Service is `ClusterIP`. A terminator + in the cluster forwards to `spire-ui.spire.svc.cluster.local:8080`; for one outside it, + `ui.service.type` makes the Service a `NodePort` or `LoadBalancer` (`docs/TLS.md`, "Reaching the + dashboard from outside the cluster"). + +7. **Check it worked.** `deploy/e2e.sh` cannot check this install: it signs in with the password grant + and the dev realm's users, assumes the realm name `spire`, and runs `docker compose exec`. Check + these by hand: + - every pod is Ready: `kubectl -n spire get pods -l app.kubernetes.io/name=spire`; + - each service answers `/q/health/ready` with `200`, for example + `kubectl -n spire port-forward svc/spire-orchestrator 8080:8080` then + `curl -fsS http://localhost:8080/q/health/ready` (the same for `spire-gateway` and `spire-worker`); + - `https://` signs you in through the identity provider and the dashboard shows your roles; + - the gateway answers a forge's webhook ping (`https:///webhooks/...`); + - one review on a test pull request. + +**No NetworkPolicy is shipped.** The services believe forwarded headers from `trustedProxies`, the pod +network, so on a shared cluster any pod can reach a service port and forge its apparent client +address. Add a NetworkPolicy that lets only the `spire-ui` pod reach the three services if other +workloads share the cluster (`techdebt/global/3-2-no-networkpolicy-isolates-the-services.md`). + ## The factory, and why it is not on by default The run worker — the service that executes agent runs — is behind a compose **profile**, so diff --git a/deploy/examples/cloudnative-pg/cluster.yaml b/deploy/examples/cloudnative-pg/cluster.yaml new file mode 100644 index 000000000..3c7b2ed4d --- /dev/null +++ b/deploy/examples/cloudnative-pg/cluster.yaml @@ -0,0 +1,67 @@ +# A CloudNativePG Postgres for Code Spire. An example: read it, change the names and the size, then apply +# it in the SAME namespace as Code Spire (a pod can read a Secret only from its own namespace, and the +# services read the passwords from the Secrets below): +# +# kubectl -n spire apply -f deploy/examples/cloudnative-pg/cluster.yaml +# +# NOT YET RUN on a real cluster (docs/UNVERIFIED.md). The field names are checked against the +# CloudNativePG 1.28 schemas by kubeconform in CI (.github/workflows/manifests.yml). +# +# Needs: the CloudNativePG operator 1.26 or later (the Database object's `schemas` field), and +# Postgres 15 or later (the migrations use NULLS NOT DISTINCT). The image is the operator's default +# unless you set spec.imageName. +# +# What this makes: +# - the database `spire`, owned by the role `spire`. CloudNativePG makes the role and its password and +# writes them to the basic-auth Secret `spire-db-app` (keys `username`, `password`). The chart reads +# that Secret (postgres.passwordSecret), so the password is stored once. +# - the role `gateway`, login, password from the basic-auth Secret `spire-db-gateway`, which YOU make +# first (deploy/README.md, "Install on any cluster"). The chart reads the same Secret +# (postgres.gatewayPasswordSecret). +# - the schema `gateway` in the database `spire`, owned by `gateway`. That is the only thing the +# gateway role gets: no grant on any other schema, no membership in `spire` (deploy/README.md, "The +# gateway's database role"). +# +# Why a Database object and not bootstrap.initdb.postInitApplicationSQL: post-init SQL runs once, at +# bootstrap, BEFORE the managed role exists, so `CREATE SCHEMA gateway AUTHORIZATION gateway` would fail +# and the cluster would not come up. The Database object is reconciled after the role exists, and it +# works on a cluster that already exists too. +# +# Connect the services to `spire-db-rw` (the read-write Service). Do not put a transaction-mode Pooler +# between them: the orchestrator holds a LISTEN session. +apiVersion: postgresql.cnpg.io/v1 +kind: Cluster +metadata: + name: spire-db +spec: + instances: 1 + storage: + size: 10Gi + # storageClass: your-storage-class # empty means the cluster's default + bootstrap: + initdb: + database: spire + owner: spire + managed: + roles: + - name: gateway + ensure: present + login: true + passwordSecret: + name: spire-db-gateway +--- +apiVersion: postgresql.cnpg.io/v1 +kind: Database +metadata: + name: spire-db-spire +spec: + cluster: + name: spire-db + name: spire + owner: spire + # Keep the database when this object is deleted. + databaseReclaimPolicy: retain + schemas: + - name: gateway + owner: gateway + ensure: present diff --git a/deploy/examples/cloudnative-pg/values.yaml b/deploy/examples/cloudnative-pg/values.yaml new file mode 100644 index 000000000..2d6ee063e --- /dev/null +++ b/deploy/examples/cloudnative-pg/values.yaml @@ -0,0 +1,65 @@ +# Code Spire on any cluster: Postgres on CloudNativePG (cluster.yaml beside this file), the chart's +# bundled Redpanda, and an identity provider you already run. Use it on top of the production preset: +# +# helm install spire deploy/helm/spire -n spire \ +# -f deploy/helm/spire/values-production.yaml \ +# -f deploy/examples/cloudnative-pg/values.yaml +# +# Every value here is a placeholder (example.invalid, a sample pod CIDR). Change each one. The steps, +# in order, are in deploy/README.md, "Install on any cluster". NOT YET RUN on a real cluster +# (docs/UNVERIFIED.md). +# +# tests/render.sh renders this file with NO --set, so it must stay complete: a value the chart +# requires that is missing here fails CI. + +# The two Secrets you make before the install (deploy/README.md lists their keys). With the passwords +# below read from the CloudNativePG Secrets, neither needs a database password key. +secrets: + existingSecret: spire-secrets + gatewayExistingSecret: spire-gateway-secrets + +# Your cluster's pod CIDR (k3s uses 10.42.0.0/16 by default). The services believe forwarded headers only +# from this network (docs/TLS.md requirement 4). +trustedProxies: 10.244.0.0/16 + +# The host the browser uses. Set it whenever the Ingress is off: nothing else pins the Host that the +# login redirect is built from. +publicHost: spire.example.invalid + +postgres: + bundled: false + # The CloudNativePG read-write Service: -rw..svc.cluster.local. Not a Pooler in + # transaction mode: the orchestrator holds a LISTEN session. + host: spire-db-rw.spire.svc.cluster.local + database: spire + user: spire + gatewayUser: gateway + # CloudNativePG's application owner cannot CREATE ROLE, and cluster.yaml makes the role and the schema + # already, so the chart's role Job must not run. + provisionGatewayRole: false + # The basic-auth Secret CloudNativePG made for the owner `spire` (-app). + passwordSecret: + name: spire-db-app + key: password + # The basic-auth Secret you made for the managed role `gateway` (cluster.yaml). + gatewayPasswordSecret: + name: spire-db-gateway + key: password + +# The chart's single-replica Redpanda. Enough while every Kafka client is in the cluster. +kafka: + bundled: true + # storageClass: your-storage-class # empty means the cluster's default + +oidc: + bundled: false + # Must be reachable from the pods AND be the issuer the browser sees. The realm: + # deploy/keycloak/realm-spire-production.json. + authServerUrl: https://auth.example.invalid/realms/spire + +# No ingress controller: a TLS terminator in the cluster forwards to the spire-ui Service +# (spire-ui..svc.cluster.local:8080). See docs/TLS.md. +ingress: + enabled: false + +# image.tag is left empty: the chart's appVersion. A chart from a release pulls that release's images. diff --git a/deploy/helm/spire/templates/NOTES.txt b/deploy/helm/spire/templates/NOTES.txt index aa1a6f54c..c40217797 100644 --- a/deploy/helm/spire/templates/NOTES.txt +++ b/deploy/helm/spire/templates/NOTES.txt @@ -18,6 +18,9 @@ Dashboard: {{- if .Values.ingress.enabled }} http{{ if .Values.ingress.tls.enabled }}s{{ end }}://{{ .Values.ingress.host }} {{- else }} + No Ingress. Point your TLS terminator at the spire-ui Service + (spire-ui.{{ .Release.Namespace }}.svc.cluster.local:8080, type {{ .Values.ui.service.type }}). + See docs/TLS.md. For a quick look without TLS: kubectl port-forward svc/spire-ui 34700:8080 then http://localhost:34700 {{- end }} @@ -33,15 +36,23 @@ is what puts all four services on one origin. Two things follow: Check it: kubectl get pods -l app.kubernetes.io/name=spire +{{- if .Values.oidc.bundled }} kubectl port-forward svc/spire-ui 34700:8080 ./deploy/e2e.sh http://localhost:34700 +{{- else }} + + deploy/e2e.sh cannot check this install: it signs in with the bundled realm's + dev users. Use the checks in deploy/README.md, "Install on any cluster": + every pod Ready, /q/health/ready of each service, a sign-in at the dashboard + through your identity provider, a webhook ping, and one review. +{{- end }} {{ if not .Values.oidc.bundled }} Using your own identity provider. It must offer three confidential clients (spire-orchestrator, spire-gateway, spire-review-worker), each with an audience mapper, plus the roles spire-viewer and spire-admin readable from the ACCESS -token. See deploy/README.md; deploy/keycloak/realm-spire.json is a worked -example. +token. See deploy/README.md. deploy/keycloak/realm-spire-production.json is a +template for a production realm. {{ end }} {{- if .Values.gateway.giveMasterKeyset }} diff --git a/deploy/helm/spire/templates/_helpers.tpl b/deploy/helm/spire/templates/_helpers.tpl index 0ca79d4e4..e9cacca3e 100644 --- a/deploy/helm/spire/templates/_helpers.tpl +++ b/deploy/helm/spire/templates/_helpers.tpl @@ -61,6 +61,114 @@ at boot and silently falls back to localhost for the broker. value: {{ include "spire.trustedProxies" . | quote }} {{- end -}} +{{/* +One service image. An empty image.tag means the chart's appVersion, so a chart packaged at a release +(release.yml sets --app-version) pulls that release's images rather than a moving tag. +*/}} +{{- define "spire.image" -}} +"{{ .root.Values.image.registry }}/{{ .name }}:{{ .root.Values.image.tag | default .root.Chart.AppVersion }}" +{{- end -}} + +{{/* +A password reference: the chart's own Secret and key, or the Secret and key the operator names (a +CloudNativePG basic-auth Secret's `password`, so a password lives in one place). Half a reference is +refused: a name without a key, or a key without a name, would silently read the wrong place. + + include "spire.passwordRef" (dict "value" "postgres.passwordSecret" "ref" .Values.postgres.passwordSecret + "secret" (include "spire.secretName" .) "key" "POSTGRES_PASSWORD") +*/}} +{{- define "spire.passwordRef" -}} +{{- $ref := .ref | default dict -}} +{{- $name := $ref.name | default "" -}} +{{- $key := $ref.key | default "" -}} +{{- if ne (empty $name) (empty $key) -}} +{{- fail (printf "%s needs both name and key, or neither. Got name=%q key=%q." .value $name $key) -}} +{{- end -}} +secretKeyRef: + name: {{ $name | default .secret }} + key: {{ $key | default .key }} +{{- end -}} + +{{- define "spire.dbPasswordRef" -}} +{{ include "spire.passwordRef" (dict "value" "postgres.passwordSecret" "ref" .Values.postgres.passwordSecret "secret" (include "spire.secretName" .) "key" "POSTGRES_PASSWORD") }} +{{- end -}} + +{{/* +The gateway's database password. Its Secret may never be the main one: the main Secret holds the +master keyset, and the gateway is the internet-facing edge (ADR-015). +*/}} +{{- define "spire.gatewayDbPasswordRef" -}} +{{- $ref := .Values.postgres.gatewayPasswordSecret | default dict -}} +{{- if and $ref.name (eq $ref.name .Values.secrets.existingSecret) -}} +{{- fail (printf "postgres.gatewayPasswordSecret.name is %q, the main Secret (secrets.existingSecret). That Secret holds the master keyset, and the gateway must never reference it. Name the Secret that holds only the gateway role's password." $ref.name) -}} +{{- end -}} +{{- $owner := dig "passwordSecret" "name" "" .Values.postgres -}} +{{- if and $ref.name $owner (eq $ref.name $owner) -}} +{{- fail (printf "postgres.gatewayPasswordSecret.name is %q, the database owner's Secret (postgres.passwordSecret). The gateway must never hold the owner's password (ADR-015). Name the Secret that holds only the gateway role's password." $ref.name) -}} +{{- end -}} +{{ include "spire.passwordRef" (dict "value" "postgres.gatewayPasswordSecret" "ref" $ref "secret" (include "spire.gatewaySecretName" .) "key" "GATEWAY_POSTGRES_PASSWORD") }} +{{- end -}} + +{{/* +A container's env: the chart's own entries, then .extraEnv. The chart's names are read from +the rendered list itself, not from a second list kept by hand, so a new chart entry is protected the +moment it exists. A repeated name is refused: the later entry would override a chart-owned setting +(the gateway's database user, for one) and server-side apply rejects the object anyway. + + include "spire.env" (dict "root" . "service" "orchestrator" "own" (include "spire.orchestrator.env" .)) +*/}} +{{- define "spire.env" -}} +{{- $svc := index .root.Values .service | default dict -}} +{{- $names := list -}} +{{- range (fromYamlArray .own) -}} +{{- if not (and (kindIs "map" .) (hasKey . "name")) -}} +{{- fail (printf "internal: the chart's own env could not be read as a list of named entries: %v" .) -}} +{{- end -}} +{{- $names = append $names .name -}} +{{- end -}} +{{- range $i, $e := $svc.extraEnv -}} +{{- if has $e.name $names -}} +{{- fail (printf "%s.extraEnv[%d] sets %s, which this container already has. extraEnv may only add names; it cannot override a setting the chart owns." $.service $i $e.name) -}} +{{- end -}} +{{- $names = append $names $e.name -}} +{{- end -}} +{{ .own }} +{{- with $svc.extraEnv }} +{{ toYaml . }} +{{- end -}} +{{- end -}} + +{{/* +The refusals that keep the master keyset off the gateway when extraEnv and extraEnvFrom are used. +render.sh greps env NAMES, so it cannot see an envFrom; these refusals are the guard. A different +Secret that also holds the master keyset cannot be policed here, and deploy/README.md says so. +*/}} +{{- define "spire.gatewayExtraGuards" -}} +{{- $main := .Values.secrets.existingSecret -}} +{{- $owner := dig "passwordSecret" "name" "" .Values.postgres -}} +{{- range $i, $e := .Values.gateway.extraEnv -}} +{{- if eq $e.name "SPIRE_ENCRYPTION_KEYSET" -}} +{{- fail (printf "gateway.extraEnv[%d] sets SPIRE_ENCRYPTION_KEYSET. The gateway is the internet-facing edge and must never hold the master keyset (ADR-015)." $i) -}} +{{- end -}} +{{- $ref := dig "valueFrom" "secretKeyRef" "name" "" $e -}} +{{- if and $main (eq $ref $main) -}} +{{- fail (printf "gateway.extraEnv[%d] reads from %q, the main Secret (secrets.existingSecret), which holds the master keyset. The gateway must never reference it." $i $main) -}} +{{- end -}} +{{- if and $owner (eq $ref $owner) -}} +{{- fail (printf "gateway.extraEnv[%d] reads from %q, the database owner's Secret (postgres.passwordSecret). The gateway must never hold the owner's password (ADR-015)." $i $owner) -}} +{{- end -}} +{{- end -}} +{{- range $i, $e := .Values.gateway.extraEnvFrom -}} +{{- $ref := dig "secretRef" "name" "" $e -}} +{{- if and $main (eq $ref $main) -}} +{{- fail (printf "gateway.extraEnvFrom[%d] loads %q, the main Secret (secrets.existingSecret), which holds the master keyset. The gateway must never reference it." $i $main) -}} +{{- end -}} +{{- if and $owner (eq $ref $owner) -}} +{{- fail (printf "gateway.extraEnvFrom[%d] loads %q, the database owner's Secret (postgres.passwordSecret). The gateway must never hold the owner's password (ADR-015)." $i $owner) -}} +{{- end -}} +{{- end -}} +{{- end -}} + {{- define "spire.probes" -}} livenessProbe: httpGet: diff --git a/deploy/helm/spire/templates/bundled-infra.yaml b/deploy/helm/spire/templates/bundled-infra.yaml index e774d3ebe..56279cd0a 100644 --- a/deploy/helm/spire/templates/bundled-infra.yaml +++ b/deploy/helm/spire/templates/bundled-infra.yaml @@ -38,9 +38,7 @@ spec: value: {{ .Values.postgres.user | quote }} - name: POSTGRES_PASSWORD valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: POSTGRES_PASSWORD + {{- include "spire.dbPasswordRef" . | nindent 16 }} # postgres:18+ wants the PARENT directory mounted: data lives in a versioned subdirectory, # which is what makes an in-place pg_upgrade --link possible later. - name: PGDATA @@ -65,6 +63,9 @@ spec: name: data spec: accessModes: ["ReadWriteOnce"] + {{- with .Values.postgres.storageClass }} + storageClassName: {{ . | quote }} + {{- end }} resources: requests: storage: {{ .Values.postgres.storage }} @@ -146,6 +147,9 @@ spec: name: data spec: accessModes: ["ReadWriteOnce"] + {{- with .Values.kafka.storageClass }} + storageClassName: {{ . | quote }} + {{- end }} resources: requests: storage: {{ .Values.kafka.storage }} diff --git a/deploy/helm/spire/templates/gateway-role-job.yaml b/deploy/helm/spire/templates/gateway-role-job.yaml index 5f33e0a5b..a71c9b8c6 100644 --- a/deploy/helm/spire/templates/gateway-role-job.yaml +++ b/deploy/helm/spire/templates/gateway-role-job.yaml @@ -64,16 +64,12 @@ spec: value: {{ .Values.postgres.user | quote }} - name: PGPASSWORD valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: POSTGRES_PASSWORD + {{- include "spire.dbPasswordRef" . | nindent 16 }} - name: GATEWAY_USER value: {{ .Values.postgres.gatewayUser | quote }} - name: GATEWAY_PASSWORD valueFrom: - secretKeyRef: - name: {{ include "spire.gatewaySecretName" . }} - key: GATEWAY_POSTGRES_PASSWORD + {{- include "spire.gatewayDbPasswordRef" . | nindent 16 }} command: - /bin/sh - -c diff --git a/deploy/helm/spire/templates/gateway.yaml b/deploy/helm/spire/templates/gateway.yaml index fec79b7f0..a8074ead9 100644 --- a/deploy/helm/spire/templates/gateway.yaml +++ b/deploy/helm/spire/templates/gateway.yaml @@ -11,6 +11,39 @@ Two things about it are deliberate and easy to undo by accident: Both are asserted by tests/render.sh, because flattening either would fail nothing and break everything. */}} +{{- define "spire.gateway.env" -}} +{{ include "spire.commonEnv" . }} +- name: QUARKUS_DATASOURCE_USERNAME + valueFrom: + configMapKeyRef: + name: spire-config + key: GATEWAY_POSTGRES_USER +- name: QUARKUS_DATASOURCE_PASSWORD + valueFrom: + {{- include "spire.gatewayDbPasswordRef" . | nindent 4 }} +- name: SPIRE_ENCRYPTION_WEBHOOK_KEYSET + valueFrom: + secretKeyRef: + name: {{ include "spire.gatewaySecretName" . }} + key: SPIRE_ENCRYPTION_WEBHOOK_KEYSET +{{- if .Values.gateway.giveMasterKeyset }} +# TEST-ONLY, and forbidden in any real install. Present so tests/render.sh --self-test can +# render the shape assertion 1 must catch: a negative assertion passes trivially when a key +# is renamed, so it has to be shown failing. +- name: SPIRE_ENCRYPTION_KEYSET + valueFrom: + secretKeyRef: + name: {{ include "spire.secretName" . }} + key: SPIRE_ENCRYPTION_KEYSET +{{- end }} +- name: SPIRE_OIDC_CLIENT_ID + value: spire-gateway +- name: SPIRE_OIDC_CLIENT_SECRET + valueFrom: + secretKeyRef: + name: {{ include "spire.secretName" . }} + key: SPIRE_OIDC_GATEWAY_SECRET +{{- end -}} apiVersion: apps/v1 kind: Deployment metadata: @@ -32,45 +65,18 @@ spec: spec: containers: - name: gateway - image: "{{ .Values.image.registry }}/spire-gateway:{{ .Values.image.tag }}" + image: {{ include "spire.image" (dict "root" . "name" "spire-gateway") }} imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: 8080 + {{- include "spire.gatewayExtraGuards" . }} env: - {{- include "spire.commonEnv" . | nindent 12 }} - - name: QUARKUS_DATASOURCE_USERNAME - valueFrom: - configMapKeyRef: - name: spire-config - key: GATEWAY_POSTGRES_USER - - name: QUARKUS_DATASOURCE_PASSWORD - valueFrom: - secretKeyRef: - name: {{ include "spire.gatewaySecretName" . }} - key: GATEWAY_POSTGRES_PASSWORD - - name: SPIRE_ENCRYPTION_WEBHOOK_KEYSET - valueFrom: - secretKeyRef: - name: {{ include "spire.gatewaySecretName" . }} - key: SPIRE_ENCRYPTION_WEBHOOK_KEYSET - {{- if .Values.gateway.giveMasterKeyset }} - # TEST-ONLY, and forbidden in any real install. Present so tests/render.sh --self-test can - # render the shape assertion 1 must catch: a negative assertion passes trivially when a key - # is renamed, so it has to be shown failing. - - name: SPIRE_ENCRYPTION_KEYSET - valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: SPIRE_ENCRYPTION_KEYSET - {{- end }} - - name: SPIRE_OIDC_CLIENT_ID - value: spire-gateway - - name: SPIRE_OIDC_CLIENT_SECRET - valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: SPIRE_OIDC_GATEWAY_SECRET + {{- include "spire.env" (dict "root" . "service" "gateway" "own" (include "spire.gateway.env" .)) | nindent 12 }} + {{- with .Values.gateway.extraEnvFrom }} + envFrom: + {{- toYaml . | nindent 12 }} + {{- end }} {{- include "spire.probes" . | nindent 10 }} {{- include "spire.securityContext" . | nindent 10 }} resources: diff --git a/deploy/helm/spire/templates/orchestrator.yaml b/deploy/helm/spire/templates/orchestrator.yaml index 6e054c998..a0118ced2 100644 --- a/deploy/helm/spire/templates/orchestrator.yaml +++ b/deploy/helm/spire/templates/orchestrator.yaml @@ -2,6 +2,29 @@ The orchestrator: deciders, sagas, the event store and the operator API. Holds the MASTER keyset, which decrypts event payloads and provider credentials. */}} +{{- define "spire.orchestrator.env" -}} +{{ include "spire.commonEnv" . }} +- name: QUARKUS_DATASOURCE_USERNAME + valueFrom: + configMapKeyRef: + name: spire-config + key: POSTGRES_USER +- name: QUARKUS_DATASOURCE_PASSWORD + valueFrom: + {{- include "spire.dbPasswordRef" . | nindent 4 }} +- name: SPIRE_ENCRYPTION_KEYSET + valueFrom: + secretKeyRef: + name: {{ include "spire.secretName" . }} + key: SPIRE_ENCRYPTION_KEYSET +- name: SPIRE_OIDC_CLIENT_ID + value: spire-orchestrator +- name: SPIRE_OIDC_CLIENT_SECRET + valueFrom: + secretKeyRef: + name: {{ include "spire.secretName" . }} + key: SPIRE_OIDC_ORCHESTRATOR_SECRET +{{- end -}} apiVersion: apps/v1 kind: Deployment metadata: @@ -23,35 +46,17 @@ spec: spec: containers: - name: orchestrator - image: "{{ .Values.image.registry }}/spire-orchestrator:{{ .Values.image.tag }}" + image: {{ include "spire.image" (dict "root" . "name" "spire-orchestrator") }} imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: 8080 env: - {{- include "spire.commonEnv" . | nindent 12 }} - - name: QUARKUS_DATASOURCE_USERNAME - valueFrom: - configMapKeyRef: - name: spire-config - key: POSTGRES_USER - - name: QUARKUS_DATASOURCE_PASSWORD - valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: POSTGRES_PASSWORD - - name: SPIRE_ENCRYPTION_KEYSET - valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: SPIRE_ENCRYPTION_KEYSET - - name: SPIRE_OIDC_CLIENT_ID - value: spire-orchestrator - - name: SPIRE_OIDC_CLIENT_SECRET - valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: SPIRE_OIDC_ORCHESTRATOR_SECRET + {{- include "spire.env" (dict "root" . "service" "orchestrator" "own" (include "spire.orchestrator.env" .)) | nindent 12 }} + {{- with .Values.orchestrator.extraEnvFrom }} + envFrom: + {{- toYaml . | nindent 12 }} + {{- end }} {{- include "spire.probes" . | nindent 10 }} {{- include "spire.securityContext" . | nindent 10 }} resources: diff --git a/deploy/helm/spire/templates/ui.yaml b/deploy/helm/spire/templates/ui.yaml index 49ed6fd14..8258890de 100644 --- a/deploy/helm/spire/templates/ui.yaml +++ b/deploy/helm/spire/templates/ui.yaml @@ -8,6 +8,24 @@ asserts over directly because it is baked into the image rather than templated h This is the only workload reachable from outside the cluster. */}} +{{- define "spire.ui.env" -}} +# Service DNS, resolved at REQUEST time by the template's resolver — so this pod does not +# crash-loop when it starts before its siblings, and does not pin a stale address if a +# Service is recreated. +- name: SPIRE_ORCHESTRATOR_URL + value: http://spire-orchestrator:8080 +- name: SPIRE_GATEWAY_URL + value: http://spire-gateway:8080 +- name: SPIRE_WORKER_URL + value: http://spire-worker:8080 +{{- with .Values.publicHost }} +# Pins the Host the proxy forwards, which is what OIDC builds redirect_uri from. Omitted +# entirely when unset, so the image's own empty default applies and every Host is +# forwarded as sent — including the pod IP a kubelet probe addresses. +- name: SPIRE_PUBLIC_HOST + value: {{ . | quote }} +{{- end }} +{{- end -}} apiVersion: apps/v1 kind: Deployment metadata: @@ -29,28 +47,17 @@ spec: spec: containers: - name: ui - image: "{{ .Values.image.registry }}/spire-ui:{{ .Values.image.tag }}" + image: {{ include "spire.image" (dict "root" . "name" "spire-ui") }} imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: 8080 env: - # Service DNS, resolved at REQUEST time by the template's resolver — so this pod does not - # crash-loop when it starts before its siblings, and does not pin a stale address if a - # Service is recreated. - - name: SPIRE_ORCHESTRATOR_URL - value: http://spire-orchestrator:8080 - - name: SPIRE_GATEWAY_URL - value: http://spire-gateway:8080 - - name: SPIRE_WORKER_URL - value: http://spire-worker:8080 - {{- with .Values.publicHost }} - # Pins the Host the proxy forwards, which is what OIDC builds redirect_uri from. Omitted - # entirely when unset, so the image's own empty default applies and every Host is - # forwarded as sent — including the pod IP a kubelet probe addresses. - - name: SPIRE_PUBLIC_HOST - value: {{ . | quote }} - {{- end }} + {{- include "spire.env" (dict "root" . "service" "ui" "own" (include "spire.ui.env" .)) | nindent 12 }} + {{- with .Values.ui.extraEnvFrom }} + envFrom: + {{- toYaml . | nindent 12 }} + {{- end }} livenessProbe: httpGet: path: /healthz @@ -78,8 +85,24 @@ metadata: name: spire-ui labels: {{- include "spire.labels" . | nindent 4 }} + {{- with .Values.ui.service.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} spec: - type: ClusterIP + {{- $type := .Values.ui.service.type | default "ClusterIP" }} + {{- if not (has $type (list "ClusterIP" "NodePort" "LoadBalancer")) }} + {{- fail (printf "ui.service.type is %q. It must be ClusterIP, NodePort or LoadBalancer." $type) }} + {{- end }} + {{- /* The only Service that may leave the cluster. A NodePort or LoadBalancer publishes plaintext + beside the TLS terminator (docs/TLS.md requirement 3), so ClusterIP stays the default. */}} + type: {{ $type }} + {{- if eq $type "LoadBalancer" }} + {{- with .Values.ui.service.loadBalancerSourceRanges }} + loadBalancerSourceRanges: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- end }} selector: app.kubernetes.io/name: spire app.kubernetes.io/component: ui diff --git a/deploy/helm/spire/templates/worker.yaml b/deploy/helm/spire/templates/worker.yaml index 2ba9ac11c..df8b5bdf5 100644 --- a/deploy/helm/spire/templates/worker.yaml +++ b/deploy/helm/spire/templates/worker.yaml @@ -3,6 +3,29 @@ The review worker: diff, context and review work, plus the assembled-context rea Context card needs. Holds the master keyset because it decrypts the per-command SCM credential the orchestrator brokers to it (ADR-015). */}} +{{- define "spire.worker.env" -}} +{{ include "spire.commonEnv" . }} +- name: QUARKUS_DATASOURCE_USERNAME + valueFrom: + configMapKeyRef: + name: spire-config + key: POSTGRES_USER +- name: QUARKUS_DATASOURCE_PASSWORD + valueFrom: + {{- include "spire.dbPasswordRef" . | nindent 4 }} +- name: SPIRE_ENCRYPTION_KEYSET + valueFrom: + secretKeyRef: + name: {{ include "spire.secretName" . }} + key: SPIRE_ENCRYPTION_KEYSET +- name: SPIRE_OIDC_CLIENT_ID + value: spire-review-worker +- name: SPIRE_OIDC_CLIENT_SECRET + valueFrom: + secretKeyRef: + name: {{ include "spire.secretName" . }} + key: SPIRE_OIDC_WORKER_SECRET +{{- end -}} apiVersion: apps/v1 kind: Deployment metadata: @@ -24,35 +47,17 @@ spec: spec: containers: - name: worker - image: "{{ .Values.image.registry }}/spire-review-worker:{{ .Values.image.tag }}" + image: {{ include "spire.image" (dict "root" . "name" "spire-review-worker") }} imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: 8080 env: - {{- include "spire.commonEnv" . | nindent 12 }} - - name: QUARKUS_DATASOURCE_USERNAME - valueFrom: - configMapKeyRef: - name: spire-config - key: POSTGRES_USER - - name: QUARKUS_DATASOURCE_PASSWORD - valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: POSTGRES_PASSWORD - - name: SPIRE_ENCRYPTION_KEYSET - valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: SPIRE_ENCRYPTION_KEYSET - - name: SPIRE_OIDC_CLIENT_ID - value: spire-review-worker - - name: SPIRE_OIDC_CLIENT_SECRET - valueFrom: - secretKeyRef: - name: {{ include "spire.secretName" . }} - key: SPIRE_OIDC_WORKER_SECRET + {{- include "spire.env" (dict "root" . "service" "worker" "own" (include "spire.worker.env" .)) | nindent 12 }} + {{- with .Values.worker.extraEnvFrom }} + envFrom: + {{- toYaml . | nindent 12 }} + {{- end }} {{- include "spire.probes" . | nindent 10 }} {{- include "spire.securityContext" . | nindent 10 }} resources: diff --git a/deploy/helm/spire/tests/render.sh b/deploy/helm/spire/tests/render.sh index 25257a6bb..746336ce1 100755 --- a/deploy/helm/spire/tests/render.sh +++ b/deploy/helm/spire/tests/render.sh @@ -21,9 +21,11 @@ pass() { printf ' PASS %s\n' "$1"; } fail() { printf ' FAIL %s\n' "$1"; FAILED=1; } # render [extra helm args...] +# RENDER_CHART renders a copy of the chart instead (the appVersion check edits one); values still come +# from the real chart. render() { local values="$1"; shift - helm template spire "$CHART" -f "$CHART/$values" \ + helm template spire "${RENDER_CHART:-$CHART}" -f "$CHART/$values" \ --set secrets.existingSecret=TEST-spire-secrets \ --set secrets.gatewayExistingSecret=TEST-spire-gateway-secrets \ --set trustedProxies=10.244.0.0/16 \ @@ -50,6 +52,55 @@ absent() { # absent if deployment "$1" "$2" | grep -q -- "$3"; then fail "$3 must NOT reach $2"; else pass "$3 absent from $2"; fi } +# One rendered object of any kind, by its metadata name (indented two spaces, so a container or port +# called the same does not match). +object() { # object + printf '%s' "$1" | awk -v kind="$2" -v want="$3" ' + BEGIN { RS = "\n---\n" } + $0 ~ ("\nkind: " kind "\n") && $0 ~ ("\n name: " want "\n") { print } + ' +} + +# The Secret and key an env entry of a Deployment reads, as "/". +secret_ref() { # secret_ref + deployment "$1" "$2" | grep -A4 -- "- name: $3\$" \ + | awk '/secretKeyRef:/ { f = 1 } f && /^ *name:/ { n = $2 } f && /^ *key:/ { k = $2 } END { print n "/" k }' +} + +expect_ref() { # expect_ref + local got; got="$(secret_ref "$1" "$2" "$3")" + if [ "$got" = "$4" ]; then pass "$2 reads $3 from $4"; else fail "$2 reads $3 from '$got', expected $4"; fi +} + +# The names of the Services whose type reaches outside the cluster. +external_services() { # external_services + printf '%s' "$1" | awk ' + BEGIN { RS = "\n---\n" } + { + n = split($0, l, "\n"); svc = 0; ext = 0; name = "" + for (i = 1; i <= n; i++) { + if (l[i] == "kind: Service") svc = 1 + if (name == "" && l[i] ~ /^ name: /) name = substr(l[i], 9) + if (l[i] ~ /^ type: (NodePort|LoadBalancer)$/) ext = 1 + } + if (svc && ext) print name + } + ' +} + +# 5 — the dashboard is the only Service that may leave the cluster. trusted-proxies means nothing if a +# service port is reachable directly, because anything that reaches it can forge X-Forwarded-For. +# ui.service.type makes spire-ui external through values; nothing may make another Service external. +only_ui_external() { # only_ui_external