backend: headlamp_test: Add negative test for unauthed callers#5657
Draft
vyncent-t wants to merge 1 commit into
Draft
backend: headlamp_test: Add negative test for unauthed callers#5657vyncent-t wants to merge 1 commit into
vyncent-t wants to merge 1 commit into
Conversation
Contributor
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: vyncent-t The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The backend's mutating endpoints (
/cluster,/plugins/{name}) are gated by theX-HEADLAMP_BACKEND-TOKENheader, which must match theHEADLAMP_BACKEND_TOKENenv var set by the desktop app. Until now there was no negative test proving that gate actually rejects unauthenticated callers, a regression that removedcheckHeadlampBackendTokenfrom any of those handlers would have shipped green.This PR adds two focused tests in
backend/cmd/headlamp_test.gothat pin the gate's behavior.Changes
Added in
backend/cmd/headlamp_test.go:TestRestrictedEndpointsRequireToken— table-driven over the three currently-gated routes:POST /clusterDELETE /cluster/{name}DELETE /plugins/{name}For each route it runs three subtests:
X-HEADLAMP_BACKEND-TOKENheader → expects403 Forbidden403 Forbidden401/403(we only pin the auth gate, not the response body)Each subtest builds a fresh handler with its own
t.TempDir()so the "valid token" subtest, which actually mutates state, can't bleed into the others. Env var is set witht.Setenvso it's restored automatically.TestRestrictedEndpointsRejectEmptyEnvToken— pins a second property ofcheckHeadlampBackendToken: whenHEADLAMP_BACKEND_TOKENis empty, every request must be rejected, including one with an empty header. Without this, clearing the env var would silently disable the gate.Steps to Test
cd backend && go test -count=1 -run 'TestRestrictedEndpoints' ./cmd/...— both tests should pass.checkHeadlampBackendTokencall inaddCluster,deleteCluster, or theDELETE /plugins/{name}handler inbackend/cmd/headlamp.goand re-run — the corresponding subtest should now fail with a clear message.npm run backend:test.Notes for the Reviewer
PUT /cluster/{name}(renameCluster) is deliberately excluded from the table. While writing these tests I noticedrenameClusterdoes not callcheckHeadlampBackendToken, unlike itsaddCluster/deleteClustersiblings — so it's currently reachable without the backend token. Adding it to this table would assert behavior that does not exist and fail CI. The omission is documented in a comment onTestRestrictedEndpointsRequireToken. I'll send the handler fix + the corresponding row in the test table as a separate follow-up PR so the security fix and the regression test land together and are easy to review.403 Forbidden(not401) because that's whatcheckHeadlampBackendTokenreturns viahttp.Error. The "valid token" branch asserts!= 403and!= 401, so it stays robust to handlers returning200/201/400/404depending on body.