Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,8 @@ jobs:
- run: helm lint charts/gameplane
- name: evaluate monitoring rules
run: bash charts/gameplane/tests/monitoring-rules.sh
- name: check MCP server RBAC scope
run: bash charts/gameplane/tests/mcp-server-rbac.sh

standalone-compose:
name: standalone Compose smoke
Expand Down
10 changes: 10 additions & 0 deletions changelog/unreleased/1046-mcp-namespace-scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Breaking: MCP server no longer reads every namespace

The break-glass MCP server (`mcpServer.enabled`) no longer has a cluster-wide
ClusterRole for Pods, Events and pod logs. The chart removes the old
`<release>-mcp-server-read` ClusterRole and ClusterRoleBinding on upgrade. Break-glass
reads now cover only the release namespace and the managed game namespaces, through
namespaced Roles. A ClusterRole remains for GameTemplate, Module and ModuleSource only.
After upgrading, use a personal MCP token (`/users/me/mcp-tokens`) for per-user reads
across clusters. See [`docs/security.md`](docs/security.md#mcp-server-optional).
Comment thread
ValgulNecron marked this conversation as resolved.
Resolved
[PR #1046](https://github.com/GameplanePanel/Gameplane/pull/1046)
74 changes: 59 additions & 15 deletions charts/gameplane/templates/mcp-server.yaml
Original file line number Diff line number Diff line change
@@ -1,54 +1,95 @@
{{- if not .Values.api.standalone }}
{{- if .Values.mcpServer.enabled }}
{{- $gameNamespaces := include "gameplane.operatorGameNamespaces" . | fromJsonArray }}
{{- $scopeNamespaces := uniq (concat (list .Release.Namespace) $gameNamespaces) }}
# Optional, strictly read-only MCP server. It speaks MCP (JSON-RPC 2.0) over
# stdio only, never a network port, so there is no Service here — reach a
# running instance with:
# kubectl exec -i deploy/gameplane-mcp-server -n {{ .Release.Namespace }} -- /mcp-server serve
# The container's own long-running process runs the `idle` subcommand (no
# args); it does nothing besides keep a container alive for `kubectl exec`
# to attach to. Each `serve` session gets its own isolated read-only client.
# to attach to.
#
# This is break-glass access only: `kubectl exec` requires cluster exec rights,
# so no Gameplane user check happens on this path. Per-user MCP access runs on
# the user's own machine and reads through the API with a personal token, so
# it needs no RBAC here.
#
# Scope is namespaced and read-only (get/list/watch, no write verb anywhere in
# this file). The server reads only the release namespace and the managed game
# namespaces, through namespaced Roles. The single cluster-scoped grant is a
# read-only ClusterRole for GameTemplate, Module and ModuleSource, which are
# cluster-scoped kinds. See mcp-server/README.md for the read-only guarantee
# this mirrors in code.
apiVersion: v1
kind: ServiceAccount
metadata:
name: gameplane-mcp-server
namespace: {{ .Release.Namespace }}
labels: {{- include "gameplane.labels" . | nindent 4 }}
---
# Cluster-wide, read-only: get/list/watch only, on exactly the resources the
# server's tools read. No write verb (create/update/patch/delete) appears
# anywhere in this file — see mcp-server/README.md for the read-only
# guarantee this mirrors in code (Client in mcp-server/internal/kube/client.go exposes no
# mutating method, and main_test.go asserts no tool is registered that could
# use one).
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ .Release.Name }}-mcp-server-read
name: {{ .Release.Name }}-mcp-server-catalog-read
labels: {{- include "gameplane.labels" . | nindent 4 }}
rules:
- apiGroups: ["gameplane.local"]
resources: [gameservers, gametemplates, backups, backupschedules, restores, modules, modulesources]
resources: [gametemplates, modules, modulesources]
verbs: [get, list, watch]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ .Release.Name }}-mcp-server-catalog-read
labels: {{- include "gameplane.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: {{ .Release.Name }}-mcp-server-catalog-read
subjects:
- kind: ServiceAccount
name: gameplane-mcp-server
namespace: {{ .Release.Namespace }}
{{- range $namespace := $scopeNamespaces }}
---
# Namespaced read-only Role for one in-scope namespace. A game namespace also
# gets the GameServer, backup and restore reads; the release namespace gets
# them only when it is also a game namespace.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ $.Release.Name }}-mcp-server-read
namespace: {{ $namespace }}
labels: {{- include "gameplane.labels" $ | nindent 4 }}
rules:
- apiGroups: [""]
resources: [pods, events]
verbs: [get, list, watch]
- apiGroups: [""]
resources: [pods/log]
verbs: [get]
{{- if has $namespace $gameNamespaces }}
- apiGroups: ["gameplane.local"]
resources: [gameservers, backups, backupschedules, restores]
verbs: [get, list, watch]
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
kind: RoleBinding
metadata:
name: {{ .Release.Name }}-mcp-server-read
labels: {{- include "gameplane.labels" . | nindent 4 }}
name: {{ $.Release.Name }}-mcp-server-read
namespace: {{ $namespace }}
labels: {{- include "gameplane.labels" $ | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: {{ .Release.Name }}-mcp-server-read
kind: Role
name: {{ $.Release.Name }}-mcp-server-read
subjects:
- kind: ServiceAccount
name: gameplane-mcp-server
namespace: {{ .Release.Namespace }}
namespace: {{ $.Release.Namespace }}
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -80,6 +121,9 @@ spec:
# No ports, no probes: this container exposes no HTTP/TCP surface to
# probe — its only interface is `kubectl exec -i ... -- /mcp-server
# serve`, which Kubernetes doesn't have a probe verb for.
env:
- name: GAMEPLANE_MCP_NAMESPACES
value: {{ join "," $scopeNamespaces | quote }}
resources: {{ toYaml .Values.mcpServer.resources | nindent 12 }}
securityContext:
allowPrivilegeEscalation: false
Expand Down
92 changes: 92 additions & 0 deletions charts/gameplane/tests/mcp-server-rbac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bash
# Render the MCP server RBAC and check that it stays namespaced, read-only and in scope.
set -euo pipefail
cd "$(dirname "$0")/../../.."
scratch="$(mktemp -d)"
trap 'rm -rf "$scratch"' EXIT
helm template gameplane charts/gameplane --namespace gameplane-system \
--set mcpServer.enabled=true --set 'operator.gameNamespaces={tenant-b}' > "$scratch/enabled.yaml"
helm template gameplane charts/gameplane --namespace gameplane-system \
--set mcpServer.enabled=false > "$scratch/disabled.yaml"
python3 - "$scratch" <<'PY'
import pathlib,sys,yaml
root=pathlib.Path(sys.argv[1])
SA='gameplane-mcp-server'
RELEASE_NS='gameplane-system'
EXPECTED_NS=['gameplane-system','gameplane-games','tenant-b']
READ_VERBS={'get','list','watch'}

def docs(path):
return [d for d in yaml.safe_load_all(path.read_text()) if d]

def is_sa_subject(subject):
return subject.get('kind')=='ServiceAccount' and subject.get('name')==SA

def sa_objects(all_docs):
return [d for d in all_docs if d.get('kind') in ('ClusterRoleBinding','RoleBinding')
and any(is_sa_subject(s) for s in d.get('subjects') or [])]

enabled=docs(root/'enabled.yaml')
# Map each SA binding to the role it references.
bound=[]
for b in sa_objects(enabled):
ref=b['roleRef']
bound.append((b['kind'], b['metadata'].get('namespace'), ref['kind'], ref['name']))

def rules_for(kind, name, namespace=None):
for d in enabled:
if d.get('kind')==kind and d['metadata']['name']==name:
if kind=='Role' and d['metadata'].get('namespace')!=namespace:
continue
return d.get('rules') or []
raise AssertionError(f'missing {kind} {name} {namespace}')

# 1. No ClusterRole bound to the SA grants pods, events or pods/log.
for bkind, bns, rkind, rname in bound:
if rkind!='ClusterRole':
continue
for rule in rules_for('ClusterRole', rname):
resources=set(rule.get('resources') or [])
assert not resources & {'pods','events','pods/log'}, (rname, rule)

# 2. Catalog ClusterRole has exactly gametemplates, modules, modulesources.
catalog=[b for b in bound if b[0]=='ClusterRoleBinding' and b[2]=='ClusterRole']
assert len(catalog)==1, catalog
catalog_rules=rules_for('ClusterRole', catalog[0][3])
assert len(catalog_rules)==1, catalog_rules
assert set(catalog_rules[0]['resources'])=={'gametemplates','modules','modulesources'}, catalog_rules
assert catalog_rules[0]['apiGroups']==['gameplane.local'], catalog_rules

# 3. Namespaced Roles with the expected name exist and are bound to the SA, once per namespace.
role_ns=sorted(ns for bkind, ns, rkind, rname in bound
if bkind=='RoleBinding' and rkind=='Role' and rname=='gameplane-mcp-server-read')
assert role_ns==sorted(EXPECTED_NS), role_ns
# Every RoleBinding for the SA, whatever role it names, stays inside the scope.
stray=[(ns, rname) for bkind, ns, rkind, rname in bound if bkind=='RoleBinding' and ns not in EXPECTED_NS]
assert not stray, stray
for ns in EXPECTED_NS:
rules_for('Role','gameplane-mcp-server-read',ns)

# 4. No rule bound to the SA has a verb other than get/list/watch (pods/log allows get only).
for bkind, bns, rkind, rname in bound:
for rule in rules_for(rkind, rname, bns if rkind=='Role' else None):
verbs=set(rule.get('verbs') or [])
assert verbs<=READ_VERBS, (rname, rule)
if set(rule.get('resources') or [])=={'pods/log'}:
assert verbs=={'get'}, rule

# 5. Deployment env lists exactly the in-scope namespaces.
deployments=[d for d in enabled if d.get('kind')=='Deployment' and d['metadata']['name']=='gameplane-mcp-server']
assert len(deployments)==1
env={e['name']:e.get('value') for e in deployments[0]['spec']['template']['spec']['containers'][0].get('env') or []}
assert sorted(env.get('GAMEPLANE_MCP_NAMESPACES','').split(','))==sorted(EXPECTED_NS), env

# 6. Disabled: no object references the SA.
disabled=docs(root/'disabled.yaml')
leftovers=[(d['kind'], d['metadata']['name']) for d in disabled
if d.get('kind') in ('ServiceAccount','Deployment','Role','RoleBinding','ClusterRole','ClusterRoleBinding')
and ('mcp-server' in d['metadata']['name'] or d['metadata']['name']==SA)]
assert not leftovers, leftovers
assert not sa_objects(disabled), sa_objects(disabled)
print('mcp-server RBAC scope ok')
PY
2 changes: 2 additions & 0 deletions docs/agent-architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ This document is the **first stop** for AI agents modifying or extending Gamepla
| Change the website | `website/AGENTS.md` (submodule) |
| Add a DB migration | `api/specs.md`, `api/internal/db/migrations/README.md` |
| Change CI or a workflow | `docs/contributing.md` ("CI and workflows", "How CI picks tests") |
| Change personal MCP tokens or the MCP read routes (`api/internal/auth/mcptokens.go`, `api/internal/handlers/mcp_tokens.go`, `api/internal/handlers/pods.go`) | `api/specs.md`, `docs/security.md` ("mcp-server (optional)") |
| Change MCP server RBAC or namespace scope (`charts/gameplane/templates/mcp-server.yaml`, `charts/gameplane/tests/mcp-server-rbac.sh`) | `docs/security.md` ("mcp-server (optional)"), `mcp-server/specs.md` |
| Decide where new code goes | `docs/layout.md` |

## How to use this index
Expand Down
5 changes: 5 additions & 0 deletions docs/multicluster-ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ Accounts, roles, audit and installation settings remain centrally managed. Their
requests do not inherit the inventory selection. Combined panels manage the local
module catalog; standalone panels require an explicit remote target for modules
and sources.

**MCP access.** Personal MCP tokens read through the API with the same per-cluster
grants as the dashboard. A session lists every cluster the user may read, and an
ungranted cluster answers like an unknown one. See
[`docs/security.md`](security.md#mcp-server-optional).
49 changes: 29 additions & 20 deletions docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -1125,26 +1125,35 @@ Secret read. Without the grant, the API omits "used" and the dashboard shows

## mcp-server (optional)

The optional MCP server [optional] (`mcpServer.enabled`, see [`mcp-server/README.md`](https://github.com/GameplanePanel/mcp/blob/main/README.md))
is strictly read-only — no tool it exposes can create, update, patch,
delete, or apply anything, enforced structurally (its tool handlers only
ever hold a client whose exported methods are List/Get-shaped) and by RBAC
(a ClusterRole granting only `get`/`list`/`watch`, plus `get` on
`pods/log`).

That RBAC grant is **cluster-wide**, not scoped to `gameplane-games` or any
other single namespace: the server can list/read Pods, Events, and pod logs
in every namespace, including `kube-system` and any other workload's
namespace sharing the cluster. Pod logs in particular can surface secrets
an application logs at startup or during errors (API keys, connection
strings, stack traces) — Kubernetes has no mechanism to redact those.
Combined with write-freedom and opt-in, admin-only installation
(`mcpServer.enabled` plus whatever gates `kubectl exec` access to the
`gameplane-mcp-server` pod), this is an accepted tradeoff, not an oversight
— but install it knowing that anyone who can reach a `serve` session gets
read access to cluster-wide pod state and logs, not just Gameplane-managed
namespaces. If that blast radius is wider than acceptable for a given
cluster, don't enable `mcpServer` there.
The optional MCP server (`mcpServer.enabled`, see [`mcp-server/README.md`](https://github.com/GameplanePanel/mcp/blob/main/README.md))
is strictly read-only: no tool it exposes can create, update, patch, delete,
or apply anything. It runs in one of two modes.

- **Per-user mode.** A user creates a personal MCP token in the dashboard and
runs the MCP client on their own machine. The client reads only through the
API, with the user's roles, namespace grants and per-cluster grants. The API
accepts an MCP token only for `GET` on a fixed list of read routes. The
server itself holds no Kubernetes permissions in this mode, so the RBAC
described below does not apply to it.
- **Break-glass mode.** `kubectl exec -i deploy/gameplane-mcp-server -n <release-namespace> -- /mcp-server serve`.
Access is admin-only by virtue of `kubectl exec` rights; no Gameplane user
check happens on this path. The pod's ServiceAccount has namespaced Roles
only, in the release namespace and in each managed game namespace, granting
`get`/`list`/`watch` on Pods, Events and `pods/log` (`get`), and
`get`/`list`/`watch` on GameServers, backups, backup schedules and restores
in the game namespaces. Its one ClusterRole covers GameTemplate, Module and
ModuleSource only, which are cluster-scoped kinds. No other cluster-wide
read is granted, and no write verb appears anywhere.

Break-glass reads therefore cover the release namespace and the managed game
namespaces, not every namespace in the cluster. Pod logs can still surface
secrets an application logs at startup or during errors (API keys, connection
strings, stack traces), and Kubernetes has no mechanism to redact those. Within
the scoped namespaces this is an accepted tradeoff for an opt-in, admin-only
install (`mcpServer.enabled` plus whatever gates `kubectl exec` access to the
`gameplane-mcp-server` pod). Anyone who can reach a break-glass `serve` session
can read pods, events and logs in those namespaces. If that is wider than
acceptable for a cluster, don't enable `mcpServer` there.

## Secrets

Expand Down
8 changes: 6 additions & 2 deletions test/e2e/buckets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,14 @@ EOF

# optional-components owns its cluster: TestOptionalComponents_Lifecycle runs
# `helm upgrade --reuse-values` to enable the MCP server and the audit-syslog
# bridge, so it is the bucket's only test. Login budget: 2 (one admin, one
# restricted user).
# bridge, so it owns the bucket's cluster. Login budget: 2 (one admin, one
# restricted user). TestOptionalComponents_MCPScope is the bucket's other test:
# it is non-parallel, so Go runs it before the parallel lifecycle test resumes,
# it re-enables the MCP server itself, scales the API to 0 and back, disables
# the MCP server again on cleanup, and makes no logins.
bucket_optional_components() { cat <<'EOF'
TestOptionalComponents_Lifecycle
TestOptionalComponents_MCPScope
EOF
}

Expand Down
Loading
Loading