Skip to content

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

feat(helm): switch-core's own ServiceAccount, and a federated Teams app that can deploy (CHOO-3187) (#755)

Christian McDermottcbc8327032ff
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1e798a2ee..c62bb23d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -167,6 +167,15 @@ version of their own to them without also giving them a release of their own.
`status` and `list` report `instance_type_allowed` for each machine. A
database bound before this release is accepted and rebound on first start,
as long as `allowed_instance_types` has not changed in the same deploy.
+- **The Helm chart runs switch-core as a ServiceAccount of its own.**
+ It is `<release>-switch-core` (`switchCore.serviceAccount`) where it used to be
+ the namespace's `default`, which every pod that names no account shares, so
+ anything trusting switch-core's identity, such as the distributed Teams app's
+ federated credential, trusts switch-core alone. `create: false` with a `name`
+ runs it as an account made elsewhere. A Teams app whose federated
+ credential trusts `default` stops getting tokens after this upgrade: add a
+ credential for `system:serviceaccount:<namespace>:<release>-switch-core`
+ first, and remove the old one once the new pod is up.
- **Agents and controllers are pinged every 10 seconds instead of every 2.**
A connection is now declared dead after 30 seconds without a beat, up from
6, so a client that goes away holds its room slot and shows online for up to
@@ -207,6 +216,13 @@ version of their own to them without also giving them a release of their own.
(a Codex login in the OS keyring, say). The refusal now gives what the
machine said instead, and `list_machines` shows it as each provider's
`problem`.
+- **A Teams app on a federated credential can deploy from the Helm chart.**
+ switch-core's migrate init container and the pre-upgrade migration Job were
+ given the Teams app's settings but not its projected token, so
+ `TEAMS_APP_FEDERATED_TOKEN_FILE` named a missing file and both refused to
+ load their configuration: with `switchCore.teamsApp.credential: federated`,
+ no release could migrate or start. The two that migrate are no longer given
+ the Teams app at all.
- **The agent HTTP API answers a refused or malformed request with a 4xx, not
a 500.** Inviting an agent to a room the caller may not change answered 500
instead of 403, and so did looking up a room the agent is not in. A room
diff --git a/core/tests/test_migrate_entrypoint.py b/core/tests/test_migrate_entrypoint.py
index 4449669d4..b799ccf53 100644
--- a/core/tests/test_migrate_entrypoint.py
+++ b/core/tests/test_migrate_entrypoint.py
@@ -28,6 +28,7 @@ MIGRATE_JOB = (
DEPLOYMENT = (
REPO_ROOT / "deploy/remote/helm/switch/templates/switch-core/deployment.yaml"
)
+HELPERS = REPO_ROOT / "deploy/remote/helm/switch/templates/_helpers.tpl"
CORE_PYPROJECT = REPO_ROOT / "core/pyproject.toml"
ENTRY_POINT = "switch-migrate"
@@ -61,6 +62,28 @@ def test_the_server_pods_init_container_runs_it_too() -> None:
assert [ENTRY_POINT] in _commands(DEPLOYMENT)
+def test_neither_migration_container_is_given_the_teams_app() -> None:
+ # Both load a full SwitchConfig, and neither mounts the federated token
+ # the serving container presents to Microsoft: given the Teams app's
+ # settings, TEAMS_APP_FEDERATED_TOKEN_FILE would name a missing file and
+ # the config would refuse to load, so the release could never migrate.
+ assert '"omitTeamsApp" true' in MIGRATE_JOB.read_text()
+ init_env = re.search(
+ r'command: \["switch-migrate"\]\n\s*env:\n\s*(.*)$',
+ DEPLOYMENT.read_text(),
+ flags=re.MULTILINE,
+ )
+ assert init_env is not None
+ assert '"omitTeamsApp" true' in init_env.group(1)
+ guard = re.search(
+ r"^(\{\{-? if .*\}\})\n- name: TEAMS_APP_CLIENT_ID$",
+ HELPERS.read_text(),
+ flags=re.MULTILINE,
+ )
+ assert guard is not None
+ assert "(not .omitTeamsApp)" in guard.group(1)
+
+
def test_the_entry_point_is_installed_by_the_package() -> None:
# The image installs the project, so anything declared here lands on PATH
# in the same container the Job runs — and anything not declared does not.
diff --git a/deploy/remote/helm/switch/templates/_helpers.tpl b/deploy/remote/helm/switch/templates/_helpers.tpl
index e83adb562..1cd442e31 100644
--- a/deploy/remote/helm/switch/templates/_helpers.tpl
+++ b/deploy/remote/helm/switch/templates/_helpers.tpl
@@ -649,6 +649,10 @@ Pass `omitOwnerCredentials: true` in the context for the serving container:
it gets no DB_OWNER_* and DB_MIGRATE_ON_BOOT=false, because its init container
has already migrated as the owner. Only the init container and the Job hold
the owner's password.
+
+Pass `omitTeamsApp: true` for the two that migrate: they run no Teams app and
+mount no federated token, so TEAMS_APP_FEDERATED_TOKEN_FILE would name a file
+that is not there, and SwitchConfig refuses to load on that.
*/}}
{{- define "switch.coreEnv" -}}
- name: DB_HOST
@@ -997,7 +1001,7 @@ this one. Drop it once the oldest supported image reads ID_SERVER_NAME. */}}
value: "true"
{{- end }}
{{- end }}
-{{- if .Values.switchCore.teamsApp.enabled }}
+{{- if and .Values.switchCore.teamsApp.enabled (not .omitTeamsApp) }}
- name: TEAMS_APP_CLIENT_ID
value: {{ required "switchCore.teamsApp.clientId is required when switchCore.teamsApp.enabled" .Values.switchCore.teamsApp.clientId | quote }}
- name: TEAMS_APP_TENANT_ID
@@ -1074,6 +1078,22 @@ releases took, so an existing Slack or Discord deployment keeps working.
{{- coalesce .Values.switchCore.messagingPublicUrl .Values.switchCore.slackApp.messagingPublicUrl .Values.switchCore.discordApp.messagingPublicUrl | default "" -}}
{{- end }}
+{{/*
+The ServiceAccount switch-core runs as. An account made elsewhere has to be
+named: falling back to `default` would quietly hand switch-core's identity,
+and whatever trusts it, to every pod in the namespace that names no account.
+*/}}
+{{- define "switch.coreServiceAccountName" -}}
+{{- $sa := .Values.switchCore.serviceAccount -}}
+{{- if $sa.name -}}
+{{- $sa.name -}}
+{{- else if $sa.create -}}
+{{- printf "%s-switch-core" (include "switch.fullname" .) -}}
+{{- else -}}
+{{- fail "switchCore.serviceAccount.name is required when switchCore.serviceAccount.create is false: name the existing ServiceAccount switch-core should run as." -}}
+{{- end -}}
+{{- end }}
+
{{/*
Where the projected service-account token the distributed Teams app presents
to Microsoft (a federated credential, so no secret) is mounted.
diff --git a/deploy/remote/helm/switch/templates/switch-core/deployment.yaml b/deploy/remote/helm/switch/templates/switch-core/deployment.yaml
index f07f6d4e7..3ef838d76 100644
--- a/deploy/remote/helm/switch/templates/switch-core/deployment.yaml
+++ b/deploy/remote/helm/switch/templates/switch-core/deployment.yaml
@@ -42,6 +42,7 @@ spec:
{{- include "switch.labels" . | nindent 8 }}
{{- include "switch.selectorLabels" (dict "Release" .Release "component" "switch-core") | nindent 8 }}
spec:
+ serviceAccountName: {{ include "switch.coreServiceAccountName" . }}
{{- if .Values.global.imagePullSecrets }}
{{- include "switch.imagePullSecrets" . | nindent 6 }}
{{- end }}
@@ -67,7 +68,7 @@ spec:
imagePullPolicy: {{ include "switch.imagePullPolicy" (dict "policy" .Values.switchCore.imagePullPolicy "image" .Values.switchCore.image) }}
command: ["switch-migrate"]
env:
- {{- include "switch.coreEnv" . | nindent 12 }}
+ {{- include "switch.coreEnv" (merge (dict "omitTeamsApp" true) .) | nindent 12 }}
resources:
{{- toYaml .Values.switchCore.resources | nindent 12 }}
{{- if include "switch.dbCaBundleConfigMap" . }}
diff --git a/deploy/remote/helm/switch/templates/switch-core/migrate-job.yaml b/deploy/remote/helm/switch/templates/switch-core/migrate-job.yaml
index 4ccd59e1b..59c40116f 100644
--- a/deploy/remote/helm/switch/templates/switch-core/migrate-job.yaml
+++ b/deploy/remote/helm/switch/templates/switch-core/migrate-job.yaml
@@ -7,7 +7,7 @@
{{- if and .Values.postgresql.caBundle.contents (not .Values.postgresql.caBundle.existingConfigMap) }}
{{- $caOverride = printf "%s-db-ca-migrate" (include "switch.fullname" .) }}
{{- end }}
-{{- $ctx := merge (dict "secretNameOverride" $secretOverride "dbCaConfigMapOverride" $caOverride) . }}
+{{- $ctx := merge (dict "secretNameOverride" $secretOverride "dbCaConfigMapOverride" $caOverride "omitTeamsApp" true) . }}
# Migrates before an upgrade rolls the new switch-core pods, so a failed
# migration blocks the release instead of crash-looping the new pods.
# pre-upgrade only: on a fresh install the managed Postgres does not exist yet
@@ -19,6 +19,11 @@
# it and a still-running old replica restarting could apply DDL at the same
# time — and reissues the runtime role's grants, without which a table this
# migration created is unreadable by the pods that follow it.
+#
+# It runs as the namespace's default account, not switch-core's: a pre-upgrade
+# hook runs before the release's own objects are applied, so on the upgrade
+# that introduces switch-core's account the Job could not be created. It needs
+# no identity of its own, and mounts no token anything outside trusts.
apiVersion: batch/v1
kind: Job
metadata:
diff --git a/deploy/remote/helm/switch/templates/switch-core/serviceaccount.yaml b/deploy/remote/helm/switch/templates/switch-core/serviceaccount.yaml
new file mode 100644
index 000000000..384a86ddb
--- /dev/null
+++ b/deploy/remote/helm/switch/templates/switch-core/serviceaccount.yaml
@@ -0,0 +1,13 @@
+{{- if .Values.switchCore.serviceAccount.create }}
+apiVersion: v1
+kind: ServiceAccount
+metadata:
+ name: {{ include "switch.coreServiceAccountName" . }}
+ labels:
+ {{- include "switch.labels" . | nindent 4 }}
+ app.kubernetes.io/component: switch-core
+ {{- with .Values.switchCore.serviceAccount.annotations }}
+ annotations:
+ {{- toYaml . | nindent 4 }}
+ {{- end }}
+{{- end }}
diff --git a/deploy/remote/helm/switch/values.yaml b/deploy/remote/helm/switch/values.yaml
index 852a8d46f..0924b8e26 100644
--- a/deploy/remote/helm/switch/values.yaml
+++ b/deploy/remote/helm/switch/values.yaml
@@ -228,6 +228,18 @@ switchCore:
imagePullPolicy: ""
logLevel: INFO
+ # The ServiceAccount switch-core runs as: one of its own, never the
+ # namespace's `default`, which every pod that names no account shares.
+ # Anything outside the cluster that trusts switch-core's identity — the
+ # distributed Teams app's federated credential (teamsApp.credential:
+ # federated) above all — then trusts switch-core alone. Empty name means
+ # <release>-switch-core. Set create: false to run as an account made
+ # elsewhere (by terraform, say), and name it.
+ serviceAccount:
+ create: true
+ name: ""
+ annotations: {}
+
# Logging. "text" is for reading in a terminal; "json" is what a log pipeline
# wants, and the only format that carries the context fields as fields — the
# tenant, and the request, agent and user a line belongs to — rather than
@@ -530,7 +542,9 @@ switchCore:
# (secrets.teamsAppClientSecret), certificate
# (secrets.teamsAppCertificate + secrets.teamsAppCertificatePrivateKey), or
# federated — a projected service-account token the app registration
- # trusts as a federated credential, so no secret exists at all.
+ # trusts as a federated credential, so no secret exists at all. The
+ # credential's subject is switch-core's own account (serviceAccount
+ # above): system:serviceaccount:<namespace>:<release>-switch-core.
credential: secret
# Shown to the admin approving the app, and required in its package.
privacyUrl: ""
diff --git a/docs/old/bridges/TEAMS_DISTRIBUTED_APP.md b/docs/old/bridges/TEAMS_DISTRIBUTED_APP.md
index 0112b4c53..7d47f2f4b 100644
--- a/docs/old/bridges/TEAMS_DISTRIBUTED_APP.md
+++ b/docs/old/bridges/TEAMS_DISTRIBUTED_APP.md
@@ -94,7 +94,12 @@ environment's app itself, to test against.
cluster's service-account issuer, subject
`system:serviceaccount:<namespace>:<service account>`, audience
`api://AzureADTokenExchange`. No secret exists. In the chart,
- `switchCore.teamsApp.credential: federated` mounts the projected token.
+ `switchCore.teamsApp.credential: federated` mounts the projected token,
+ and the service account is switch-core's own, `<release>-switch-core`
+ (`switchCore.serviceAccount`). Never trust `default`: every pod in the
+ namespace that names no account runs as it. An app accepts several
+ federated credentials, so to change the account, add the new subject
+ before deploying and remove the old one after.
- **Certificate:** upload the public certificate; give Switch the
certificate and its key (`TEAMS_APP_CERTIFICATE`,
`TEAMS_APP_CERTIFICATE_PRIVATE_KEY`).