Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Protect the chart's decision service with a generated token

agent-runner3537f206db8e328
diff --git a/deploy/remote/helm/switch/README.md b/deploy/remote/helm/switch/README.md
index c155707b2..76ef1c371 100644
--- a/deploy/remote/helm/switch/README.md
+++ b/deploy/remote/helm/switch/README.md
@@ -286,10 +286,25 @@ decision:
With `decision.enabled` the chart renders one Deployment (the `decision`
container, plus the `model-server` sidecar serving the model), a ClusterIP
Service on port 8230, a PVC the weights are downloaded into at first start, a
-ServiceAccount, and a NetworkPolicy that admits only this release's switch-core
-pods to port 8230. It sets `DECISION_ENDPOINT_URL` on switch-core to the
-Service, so every workspace's Decision routing settings in Switch Console show
-it as the server default with no further setup.
+ServiceAccount, a NetworkPolicy that admits only this release's switch-core
+pods to port 8230, and a Secret holding the service's bearer token. It sets
+`DECISION_ENDPOINT_URL` on switch-core to the Service, so every workspace's
+Decision routing settings in Switch Console show it as the server default with
+no further setup.
+
+- **The service token.** The decision service requires a bearer token on
+ everything but `/health` (`DECISION_SERVICE_TOKEN`), and switch-core sends it
+ (`DECISION_API_KEY`), so another pod in the cluster gets 401 even where the
+ NetworkPolicy isn't enforced. The chart generates it into the Secret
+ `<release>-decision-token` on the first install, and reads it back with
+ `lookup` on every upgrade, so it stays the same. `helm template` can't read
+ the cluster, so it renders a new value each time; that is expected, and only
+ matters if you apply its output directly (do it with
+ `decision.token.existingSecret` then). To manage the token yourself, put it
+ in a Secret of your own and name it in `decision.token.existingSecret` (the
+ entry `decision.token.existingSecretKey`, `token` by default); the chart then
+ renders no Secret. Rotating it: change the Secret's value and restart both
+ the decision and switch-core pods.
- **The model.** `decision.modelServer.kind` picks the model server:
- `strands` (default): **Strands Decider 4B** (the E4B checkpoint) on its own
@@ -335,9 +350,9 @@ it as the server default with no further setup.
`decision.backend` to `anthropic` or `bedrock`.
- **Other settings.** `decision.env` passes anything else in the service's
environment (`DECISION_TIMEOUT_S`, `DECISION_HISTORY_LIMIT`, …).
-- **The NetworkPolicy** needs a CNI that enforces it. Turn it off with
- `decision.networkPolicy.enabled: false` only if something else restricts who
- reaches the service: it has no key of its own in this setup.
+- **The NetworkPolicy** needs a CNI that enforces it; where none does, the
+ service token still keeps other pods out. It stays on by default as a
+ second layer; turn it off with `decision.networkPolicy.enabled: false`.
To use a decision service run elsewhere instead, leave `decision.enabled` off
and set `switchCore.decision.endpointUrl` (and `bedrockEnabled` if that service
diff --git a/deploy/remote/helm/switch/templates/_helpers.tpl b/deploy/remote/helm/switch/templates/_helpers.tpl
index ff9828919..05fafb21d 100644
--- a/deploy/remote/helm/switch/templates/_helpers.tpl
+++ b/deploy/remote/helm/switch/templates/_helpers.tpl
@@ -397,6 +397,26 @@ auto-route is unavailable until a workspace admin sets a backend up).
{{- end -}}
{{- end }}
+{{/*
+The Secret holding the decision service's bearer token:
+decision.token.existingSecret, or the one the chart generates.
+*/}}
+{{- define "switch.decisionTokenSecret" -}}
+{{- .Values.decision.token.existingSecret | default (printf "%s-decision-token" (include "switch.fullname" .)) -}}
+{{- end }}
+
+{{/*
+An env entry `name` read from the decision token Secret.
+Usage: {{ include "switch.decisionTokenEnv" (dict "root" . "name" "DECISION_API_KEY") }}
+*/}}
+{{- define "switch.decisionTokenEnv" -}}
+- name: {{ .name }}
+ valueFrom:
+ secretKeyRef:
+ name: {{ include "switch.decisionTokenSecret" .root }}
+ key: {{ .root.Values.decision.token.existingSecretKey }}
+{{- end }}
+
{{/*
Whether workspaces may choose Haiku on Bedrock (DECISION_BEDROCK_ENABLED).
Non-empty for yes, nothing for no.
@@ -901,6 +921,9 @@ this one. Drop it once the oldest supported image reads ID_SERVER_NAME. */}}
- name: DECISION_ENDPOINT_URL
value: {{ . | quote }}
{{- end }}
+{{- if .Values.decision.enabled }}
+{{ include "switch.decisionTokenEnv" (dict "root" . "name" "DECISION_API_KEY") }}
+{{- end }}
{{- if include "switch.decisionBedrockEnabled" . }}
- name: DECISION_BEDROCK_ENABLED
value: "true"
diff --git a/deploy/remote/helm/switch/templates/decision/deployment.yaml b/deploy/remote/helm/switch/templates/decision/deployment.yaml
index d7b795a71..db6e49c9a 100644
--- a/deploy/remote/helm/switch/templates/decision/deployment.yaml
+++ b/deploy/remote/helm/switch/templates/decision/deployment.yaml
@@ -112,6 +112,7 @@ spec:
env:
- name: PORT
value: {{ $d.port | quote }}
+ {{- include "switch.decisionTokenEnv" (dict "root" $ "name" "DECISION_SERVICE_TOKEN") | nindent 12 }}
- name: DECISION_BACKEND
value: {{ $backend | quote }}
{{- with include "switch.decisionModel" . }}
diff --git a/deploy/remote/helm/switch/templates/decision/secret.yaml b/deploy/remote/helm/switch/templates/decision/secret.yaml
new file mode 100644
index 000000000..13b5adb2e
--- /dev/null
+++ b/deploy/remote/helm/switch/templates/decision/secret.yaml
@@ -0,0 +1,24 @@
+{{- if and .Values.decision.enabled (not .Values.decision.token.existingSecret) }}
+{{- $name := include "switch.decisionTokenSecret" . }}
+{{- $key := .Values.decision.token.existingSecretKey }}
+{{- $stored := lookup "v1" "Secret" .Release.Namespace $name }}
+# The bearer token the decision service requires (DECISION_SERVICE_TOKEN) and
+# switch-core sends (DECISION_API_KEY), so nothing else in the cluster can use
+# the service even where the NetworkPolicy isn't enforced. Generated on the
+# first install and kept on every upgrade: `lookup` reads the stored one back.
+# `helm template` can't read the cluster and renders a fresh value each time.
+apiVersion: v1
+kind: Secret
+metadata:
+ name: {{ $name }}
+ labels:
+ {{- include "switch.labels" . | nindent 4 }}
+ {{- include "switch.decisionSelectorLabels" . | nindent 4 }}
+type: Opaque
+data:
+ {{- if and $stored $stored.data (hasKey $stored.data $key) }}
+ {{ $key }}: {{ index $stored.data $key }}
+ {{- else }}
+ {{ $key }}: {{ randAlphaNum 48 | b64enc }}
+ {{- end }}
+{{- end }}
diff --git a/deploy/remote/helm/switch/values.yaml b/deploy/remote/helm/switch/values.yaml
index 31079c234..b5d248615 100644
--- a/deploy/remote/helm/switch/values.yaml
+++ b/deploy/remote/helm/switch/values.yaml
@@ -1017,9 +1017,18 @@ decision:
# e.g. eks.amazonaws.com/role-arn for Bedrock through IRSA.
annotations: {}
# Admit only this release's switch-core pods to the decision port. Needs a
- # CNI that enforces NetworkPolicy; without one it is accepted and ignored.
+ # CNI that enforces NetworkPolicy; without one it is accepted and ignored,
+ # and the token below is what keeps other pods out.
networkPolicy:
enabled: true
+ # The bearer token the service requires (DECISION_SERVICE_TOKEN) and
+ # switch-core sends (DECISION_API_KEY). By default the chart generates it
+ # into the Secret <release>-decision-token on the first install and keeps it
+ # on upgrades. Name a Secret of your own instead with existingSecret; the
+ # token is its existingSecretKey entry.
+ token:
+ existingSecret: ""
+ existingSecretKey: token
# ── Setup Job ─────────────────────────────────────────────────────────────────
setup:
diff --git a/scripts/check_chart_decision.py b/scripts/check_chart_decision.py
index a9074e82c..79dec2852 100755
--- a/scripts/check_chart_decision.py
+++ b/scripts/check_chart_decision.py
@@ -4,7 +4,8 @@
The decision service (auto-route) is optional in the chart. Off, nothing of it
renders and switch-core gets no endpoint. On, the chart runs it beside its
model server (Strands Decider by default, or Qwen on llama.cpp), points
-switch-core at it and admits only switch-core to it. An endpoint run elsewhere
+switch-core at it and admits only switch-core to it, both by NetworkPolicy and
+by a bearer token the chart generates. An endpoint run elsewhere
can be named instead, but not as well as the chart's own. These are the
promises deploy/remote/helm/switch/README.md makes about `decision.*`; a render
that breaks one fails here, before a cluster finds out.
@@ -16,6 +17,7 @@ Needs `helm` on PATH and PyYAML. Run from the repo root:
from __future__ import annotations
+import base64
import subprocess
import sys
from collections.abc import Callable
@@ -84,12 +86,31 @@ def env_of(container: dict[str, Any]) -> dict[str, Any]:
return {e["name"]: e.get("value") for e in container.get("env", [])}
+def secret_refs_of(container: dict[str, Any]) -> dict[str, Any]:
+ return {
+ e["name"]: e["valueFrom"]["secretKeyRef"]
+ for e in container.get("env", [])
+ if "secretKeyRef" in e.get("valueFrom", {})
+ }
+
+
+def core_container(docs: list[dict[str, Any]]) -> dict[str, Any]:
+ deployment = next(
+ d
+ for d in docs
+ if d["kind"] == "Deployment"
+ and d["metadata"]["name"] == f"{RELEASE}-switch-core"
+ )
+ return containers(deployment)["switch-core"]
+
+
def test_off_by_default_renders_nothing_and_sets_no_endpoint() -> None:
docs = render()
assert decision_objects(docs) == {}
env = core_env(docs)
assert "DECISION_ENDPOINT_URL" not in env
assert "DECISION_BEDROCK_ENABLED" not in env
+ assert "DECISION_API_KEY" not in env
def test_on_runs_the_service_and_points_switch_core_at_it() -> None:
@@ -101,6 +122,7 @@ def test_on_runs_the_service_and_points_switch_core_at_it() -> None:
"PersistentVolumeClaim",
"ServiceAccount",
"NetworkPolicy",
+ "Secret",
}, sorted(objects)
assert core_env(docs)["DECISION_ENDPOINT_URL"] == f"http://{RELEASE}-decision:8230"
@@ -314,6 +336,57 @@ def test_an_endpoint_elsewhere() -> None:
assert env["DECISION_ENDPOINT_URL"] == "http://decider.example:8230"
assert env["DECISION_BEDROCK_ENABLED"] == "true"
assert env["DECISION_TIMEOUT_SECONDS"] == "8"
+ assert "DECISION_API_KEY" not in env
+
+
+def test_the_service_requires_a_token_that_switch_core_sends() -> None:
+ docs = render("--set", "decision.enabled=true", *FLAGS)
+ objects = decision_objects(docs)
+ secret = objects["Secret"]
+ assert secret["metadata"]["name"] == f"{RELEASE}-decision-token"
+ assert secret["type"] == "Opaque"
+ assert set(secret["data"]) == {"token"}
+ # `helm template` can't `lookup`, so this is a fresh value; an install
+ # keeps the stored one. Long and random either way.
+ token = base64.b64decode(secret["data"]["token"]).decode()
+ assert len(token) == 48 and token.isalnum(), token
+ assert (
+ token
+ != base64.b64decode(
+ decision_objects(render("--set", "decision.enabled=true"))["Secret"][
+ "data"
+ ]["token"]
+ ).decode()
+ )
+
+ ref = {"name": f"{RELEASE}-decision-token", "key": "token"}
+ decision = containers(objects["Deployment"])["decision"]
+ assert secret_refs_of(decision)["DECISION_SERVICE_TOKEN"] == ref
+ assert secret_refs_of(core_container(docs))["DECISION_API_KEY"] == ref
+ # The model server is reached only on localhost and takes no token.
+ model_server = containers(objects["Deployment"])["model-server"]
+ assert "DECISION_SERVICE_TOKEN" not in secret_refs_of(model_server)
+
+
+def test_an_existing_token_secret_replaces_the_generated_one() -> None:
+ docs = render(
+ "--set",
+ "decision.enabled=true",
+ "--set",
+ "decision.token.existingSecret=my-decision-token",
+ "--set",
+ "decision.token.existingSecretKey=bearer",
+ *FLAGS,
+ )
+ objects = decision_objects(docs)
+ assert "Secret" not in objects
+ assert not any(
+ d["kind"] == "Secret" and "decision" in d["metadata"]["name"] for d in docs
+ )
+ ref = {"name": "my-decision-token", "key": "bearer"}
+ decision = containers(objects["Deployment"])["decision"]
+ assert secret_refs_of(decision)["DECISION_SERVICE_TOKEN"] == ref
+ assert secret_refs_of(core_container(docs))["DECISION_API_KEY"] == ref
def test_refuses_contradictions() -> None:
@@ -356,6 +429,8 @@ TESTS: list[Callable[[], None]] = [
test_without_a_model_server_there_is_no_gpu_or_volume,
test_network_policy_can_be_left_out,
test_an_endpoint_elsewhere,
+ test_the_service_requires_a_token_that_switch_core_sends,
+ test_an_existing_token_secret_replaces_the_generated_one,
test_refuses_contradictions,
]