Repository navigation
fix: show Mods and Modpacks tabs to server collaborators #1103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "github.com/GameplanePanel/gameplane/api/internal/auth" | ||
| "github.com/GameplanePanel/gameplane/api/internal/kube" | ||
| ) | ||
|
|
||
| // Owners and collaborators of a server read its template without | ||
| // templates:read, so the dashboard can show the Mods, Modpacks and Console | ||
| // tabs they manage (#1063). Anyone else still needs templates:read. | ||
| func TestServerTemplate_OwnerAndCollaboratorWithoutTemplatesRead(t *testing.T) { | ||
| k := fakeKubeClient(catalogWithoutPasswords()) | ||
| gs := serverWithConfig("alpha", plainConfig()) | ||
| gs.SetAnnotations(map[string]string{ | ||
| "gameplane.local/owner-id": "7", | ||
| collaboratorsAnnotation: "9, 42", | ||
| }) | ||
| if _, err := k.Dynamic.Resource(kube.GVRs["servers"]).Namespace(redactNS).Create(t.Context(), gs, metav1.CreateOptions{}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| router := mountResourcesRouter(k) | ||
| get := func(u *auth.User) *httptest.ResponseRecorder { | ||
| req := httptest.NewRequestWithContext(auth.WithUser(t.Context(), u), http.MethodGet, "/servers/alpha/template", nil) | ||
| rr := httptest.NewRecorder() | ||
| router.ServeHTTP(rr, req) | ||
| return rr | ||
| } | ||
|
|
||
| for name, u := range map[string]*auth.User{ | ||
| "collaborator": {ID: 42}, | ||
| "owner": {ID: 7}, | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| rr := get(u) | ||
| var got map[string]any | ||
| if rr.Code != http.StatusOK || json.Unmarshal(rr.Body.Bytes(), &got) != nil { | ||
| t.Fatalf("unexpected answer: %d %s", rr.Code, rr.Body) | ||
| } | ||
| md, _ := got["metadata"].(map[string]any) | ||
| if md["name"] != catalogWithoutPasswords().GetName() { | ||
| t.Fatalf("unexpected template: %v", got["metadata"]) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| t.Run("server reader without a server grant", func(t *testing.T) { | ||
| u := inventoryUser("local", "*", "servers:read") | ||
| u.ID = 100 | ||
| if rr := get(u); rr.Code != http.StatusForbidden { | ||
| t.Fatalf("template read without templates:read or a server grant: %d %s", rr.Code, rr.Body) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Server collaborators see the Mods and Modpacks tabs | ||
|
|
||
| A server's collaborators (and owner) no longer need the cluster-wide | ||
| `templates:read` permission to see the Mods, Modpacks and Console tabs on the | ||
| servers they work on. As before, each tab still appears only when the server's | ||
| template and active version support it. The server's template is read through | ||
| the server itself for them, so nothing changes for other users. | ||
| [PR #1103](https://github.com/GameplanePanel/Gameplane/pull/1103) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { http, HttpResponse } from "msw"; | ||
| import { server } from "@/test/server"; | ||
| import { makeTemplate } from "@/test/factories"; | ||
| import { createResourceClient } from "./endpoints"; | ||
| import { getServerTemplate } from "./serverTemplate"; | ||
|
|
||
| const client = () => createResourceClient({ cluster: "local", namespace: "gameplane-games" }); | ||
|
|
||
| describe("getServerTemplate", () => { | ||
| it("reads the catalog template when the caller may", async () => { | ||
| let scoped = 0; | ||
| server.use( | ||
| http.get("/templates/:name", ({ params }) => HttpResponse.json(makeTemplate({ metadata: { name: String(params.name) } }))), | ||
| http.get("/servers/alpha/template", () => { scoped += 1; return HttpResponse.json(makeTemplate()); }), | ||
| ); | ||
| const tmpl = await getServerTemplate(client(), "alpha", "gameplane-games", "valheim"); | ||
| expect(tmpl.metadata.name).toBe("valheim"); | ||
| expect(scoped).toBe(0); | ||
| }); | ||
|
|
||
| it("falls back to the server-scoped template when the catalog refuses", async () => { | ||
| let namespace: string | null = null; | ||
| server.use( | ||
| http.get("/templates/:name", () => HttpResponse.json({ detail: "forbidden" }, { status: 403 })), | ||
| http.get("/servers/alpha/template", ({ request }) => { | ||
| namespace = new URL(request.url).searchParams.get("namespace"); | ||
| return HttpResponse.json(makeTemplate({ metadata: { name: "scoped" } })); | ||
| }), | ||
| ); | ||
| const tmpl = await getServerTemplate(client(), "alpha", "gameplane-games", "valheim"); | ||
| expect(tmpl.metadata.name).toBe("scoped"); | ||
| expect(namespace).toBe("gameplane-games"); | ||
| }); | ||
|
|
||
| it("passes other errors through", async () => { | ||
| server.use(http.get("/templates/:name", () => HttpResponse.json({ detail: "boom" }, { status: 500 }))); | ||
| await expect(getServerTemplate(client(), "alpha", "gameplane-games", "valheim")).rejects.toMatchObject({ status: 500 }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { APIError } from "@/lib/api"; | ||
| import type { ResourceClient } from "@/lib/endpoints"; | ||
| import type { GameTemplate } from "@/types"; | ||
|
|
||
| // getServerTemplate reads the template a server was created from. The | ||
| // catalog read (GET /templates/{name}) needs templates:read on the cluster, | ||
| // which a server's owners and collaborators may not hold. They still manage | ||
| // the server's mods, modpacks and console, and those tabs need the template | ||
| // to render (#1063), so a refused catalog read falls back to the | ||
| // server-scoped GET /servers/{name}/template, which the API allows for the | ||
| // server's owners and collaborators. | ||
| export async function getServerTemplate( | ||
| client: ResourceClient, | ||
| name: string, | ||
| ns: string | undefined, | ||
| templateName: string, | ||
| ): Promise<GameTemplate> { | ||
| try { | ||
| return await client.Templates.get(templateName); | ||
| } catch (err) { | ||
| if (err instanceof APIError && err.status === 403) { | ||
| return client.Servers.effectiveTemplate(name, ns); | ||
| } | ||
| throw err; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.