From 1681722648e072086f58d0387ff9d58c5c6490b8 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 19:57:42 +0000 Subject: [PATCH] test: assert GameServer security fields pass through unredacted (spec 030) The generic get and list tools return a GameServer's spec.security and status.security as stored; only metadata.managedFields is stripped. A new round-trip test pins this, and README and specs.md say so (Gameplane spec 030 T067). Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- README.md | 2 + security_roundtrip_test.go | 126 +++++++++++++++++++++++++++++++++++++ specs.md | 1 + 3 files changed, 129 insertions(+) create mode 100644 security_roundtrip_test.go diff --git a/README.md b/README.md index c315a35..868b733 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,8 @@ during a list is reported in the response: its `metadata.resourceVersion` (or the list's `resourceVersion`) reads `unavailable clusters: `. Clusters that return 403 or 404 are skipped silently. +Example: `get_gameplane_resource` with `{"kind": "GameServer", "namespace": "games", "name": "my-server"}` returns the object as stored, including `spec.security` (the per-server uid/gid overrides) and `status.security` (the desired and applied security read model). Only `metadata.managedFields` is removed; no security field is redacted. + ## Transport: stdio only, no network port This server speaks MCP (JSON-RPC 2.0) over stdio — it never opens an HTTP diff --git a/security_roundtrip_test.go b/security_roundtrip_test.go new file mode 100644 index 0000000..9509bf4 --- /dev/null +++ b/security_roundtrip_test.go @@ -0,0 +1,126 @@ +package main + +import ( + "encoding/json" + "reflect" + "testing" + + dynamicfake "k8s.io/client-go/dynamic/fake" + k8sfake "k8s.io/client-go/kubernetes/fake" + + "github.com/GameplanePanel/gameplane/mcp-server/internal/kube" +) + +// securityRoundTripSpec and securityRoundTripStatus are the spec.security and +// status.security fixtures for TestGameServer_SecurityFieldsUnredacted. They +// cover every shape the GameServer security fields can take: an explicit +// uid override, a useImageDefault id override, and the reconciler's desired +// and applied read model with a pending flag. +func securityRoundTripSpec() map[string]any { + return map[string]any{ + "runAsUser": map[string]any{"value": int64(1000)}, + "runAsGroup": map[string]any{"useImageDefault": true}, + "fsGroup": map[string]any{"value": int64(2000)}, + } +} + +func securityRoundTripStatus() map[string]any { + applied := map[string]any{ + "runAsUser": map[string]any{"value": int64(1000), "source": "Override"}, + "runAsGroup": map[string]any{"source": "ImageDefaultOverride"}, + "fsGroup": map[string]any{"value": int64(2000), "source": "Override"}, + } + return map[string]any{ + "phase": "Running", + "security": map[string]any{ + "desired": applied, + "applied": applied, + "pending": true, + }, + } +} + +// securityRoundTripClient builds a kube.Client whose only GameServer carries +// spec.security and status.security. It is separate from the shared +// testClientWithFixtures object, which stays untouched. +func securityRoundTripClient(t *testing.T) *kube.Client { + t.Helper() + scheme := kube.NewScheme() + gs := newUnstructuredCRD("GameServer", "games", "secure-server", securityRoundTripStatus()) + gs.Object["spec"] = map[string]any{"security": securityRoundTripSpec()} + dyn := dynamicfake.NewSimpleDynamicClientWithCustomListKinds(scheme, gvrToListKindMap(), gs) + return kube.NewFrom(k8sfake.NewSimpleClientset(), dyn, scheme) +} + +// decodeJSONObject round-trips a value through JSON so that numeric fields +// compare as float64 on both sides, the way the tool's text output decodes. +func decodeJSONObject(t *testing.T, v any) map[string]any { + t.Helper() + b, err := json.Marshal(v) + if err != nil { + t.Fatalf("marshal fixture: %v", err) + } + var out map[string]any + if err := json.Unmarshal(b, &out); err != nil { + t.Fatalf("unmarshal fixture: %v", err) + } + return out +} + +// assertSecurityUnredacted checks that obj carries the same spec.security and +// status.security subtrees as the fixture, unchanged. +func assertSecurityUnredacted(t *testing.T, label string, obj map[string]any) { + t.Helper() + spec, ok := obj["spec"].(map[string]any) + if !ok { + t.Fatalf("%s: spec missing or not an object: %v", label, obj["spec"]) + } + status, ok := obj["status"].(map[string]any) + if !ok { + t.Fatalf("%s: status missing or not an object: %v", label, obj["status"]) + } + wantSpec := decodeJSONObject(t, securityRoundTripSpec()) + wantStatusSecurity := decodeJSONObject(t, securityRoundTripStatus()["security"]) + if !reflect.DeepEqual(spec["security"], any(wantSpec)) { + t.Errorf("%s: spec.security = %v, want %v", label, spec["security"], wantSpec) + } + if !reflect.DeepEqual(status["security"], any(wantStatusSecurity)) { + t.Errorf("%s: status.security = %v, want %v", label, status["security"], wantStatusSecurity) + } +} + +// TestGameServer_SecurityFieldsUnredacted asserts that the read-only generic +// tools return a GameServer's spec.security and status.security exactly as +// stored. The only transform in this server is managedFields stripping, so no +// security field may be dropped, masked or rewritten on the way out. +func TestGameServer_SecurityFieldsUnredacted(t *testing.T) { + c := securityRoundTripClient(t) + cs := connectInMemory(t, newMCPServer(c)) + + t.Run("get_gameplane_resource", func(t *testing.T) { + text := callToolText(t, cs, "get_gameplane_resource", map[string]any{ + "kind": "GameServer", "namespace": "games", "name": "secure-server", + }) + var obj map[string]any + if err := json.Unmarshal([]byte(text), &obj); err != nil { + t.Fatalf("decode get output: %v (text %q)", err, text) + } + assertSecurityUnredacted(t, "get", obj) + }) + + t.Run("list_gameplane_resources", func(t *testing.T) { + text := callToolText(t, cs, "list_gameplane_resources", map[string]any{ + "kind": "GameServer", "namespace": "games", + }) + var list struct { + Items []map[string]any `json:"items"` + } + if err := json.Unmarshal([]byte(text), &list); err != nil { + t.Fatalf("decode list output: %v (text %q)", err, text) + } + if len(list.Items) != 1 { + t.Fatalf("list: got %d items, want 1 (text %q)", len(list.Items), text) + } + assertSecurityUnredacted(t, "list", list.Items[0]) + }) +} diff --git a/specs.md b/specs.md index 92499a0..fec81f0 100644 --- a/specs.md +++ b/specs.md @@ -199,6 +199,7 @@ The per-user client uses only the standard library (`net/http`, `crypto/tls`, `c 4. **No listening port**: No network surface to expose. Reachable only via `kubectl exec` (break-glass) or the MCP host's spawned process (per-user). 5. **Deployment model**: Opt-in via `mcpServer.enabled=true` in the Helm chart (break-glass). Per-user mode runs wherever the operator chooses, with a token. Admin responsibility to decide if each is acceptable for their cluster. 6. **See [`docs/security.md`](https://github.com/GameplanePanel/Gameplane/blob/master/docs/security.md)** for the full threat model and per-cluster decisions (especially if cohabiting with unrelated sensitive workloads). +7. **GameServer security fields**: the generic get and list tools return `spec.security` and `status.security` unredacted, because the only transform is `metadata.managedFields` stripping; `security_roundtrip_test.go` asserts this. ## Testing & coverage