Repository navigation
fix(security)!: require JWT authentication for admin APIs #144
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
7 commits
Select commit
Hold shift + click to select a range
4e2b311
fix(security): omit WireGuard private key from startup logs
alexlovelltroy 74e35d0
fix: require JWT authentication for admin APIs
alexlovelltroy d65d973
fix: Update dependencies
alexlovelltroy 7c2f5f7
fix: align Go security patch and tidy modules
alexlovelltroy 53e6f8f
docs: align admin clients and API contract with JWT protection
alexlovelltroy 43cbc0a
fix(docs): generate complete simulator node schemas
alexlovelltroy 46d7787
fix(docs): describe simulator NID as a JSON number
alexlovelltroy 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
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,5 @@ | ||
| // SPDX-FileCopyrightText: Copyright © 2026 OpenCHAMI a Series of LF Projects, LLC | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| // json.Number marshals as a JSON number, despite its underlying Go string type. | ||
| replace encoding/json.Number number |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // SPDX-FileCopyrightText: Copyright © 2026 OpenCHAMI a Series of LF Projects, LLC | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/http/httputil" | ||
| "net/url" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/OpenCHAMI/jwtauth/v5" | ||
| "github.com/lestrrat-go/jwx/v2/jwa" | ||
| "github.com/lestrrat-go/jwx/v2/jwk" | ||
| ) | ||
|
|
||
| func TestAdminJWTThroughJWKSAndProxy(t *testing.T) { | ||
| private, err := rsa.GenerateKey(rand.Reader, 2048) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| key, err := jwk.FromRaw(private) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err = key.Set(jwk.KeyIDKey, "fixture-admin-key"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err = key.Set(jwk.AlgorithmKey, jwa.RS256); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| public, err := key.PublicKey() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| set := jwk.NewSet() | ||
| if err = set.AddKey(public); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| keys := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if encodeErr := json.NewEncoder(w).Encode(set); encodeErr != nil { | ||
| t.Error(encodeErr) | ||
| } | ||
| })) | ||
| t.Cleanup(keys.Close) | ||
| verifier, err := fetchPublicKeyFromURL(keys.URL) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| token := adminTestToken(t, jwtauth.New("RS256", key, nil), adminTestClaims()) | ||
| if err = key.Set(jwk.KeyIDKey, "unknown-fixture-key"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| unknownKeyToken := adminTestToken(t, jwtauth.New("RS256", key, nil), adminTestClaims()) | ||
| router, data := adminTestRouter(t, verifier) | ||
| backend := httptest.NewServer(router) | ||
| t.Cleanup(backend.Close) | ||
| target, err := url.Parse(backend.URL) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| transport := &http.Transport{Proxy: nil} | ||
| t.Cleanup(transport.CloseIdleConnections) | ||
| proxy := httputil.NewSingleHostReverseProxy(target) | ||
| proxy.Transport = transport | ||
| frontend := httptest.NewServer(proxy) | ||
| t.Cleanup(frontend.Close) | ||
| client := &http.Client{Transport: transport, Timeout: 3 * time.Second} | ||
| for _, entry := range []struct{ name, address string }{{"direct", backend.URL}, {"proxied", frontend.URL}} { | ||
| t.Run(entry.name, func(t *testing.T) { | ||
| for _, test := range []struct { | ||
| name, method, path, token, body string | ||
| status int | ||
| }{ | ||
| {"anonymous-read", "GET", "/admin/groups", "", "", 401}, | ||
| {"invalid-read", "GET", "/admin/groups", "fixture-invalid-token", "", 401}, | ||
| {"unknown-key", "GET", "/admin/groups", unknownKeyToken, "", 401}, | ||
| {"authenticated-read", "GET", "/admin/groups", token, "", 200}, | ||
| {"authenticated-write", "POST", "/admin/cluster-defaults", token, `{"cluster-name":"fixture-authenticated"}`, 201}, | ||
| {"anonymous-write", "POST", "/admin/cluster-defaults", "", `{"cluster-name":"unexpected"}`, 401}, | ||
| {"impersonation", "GET", "/admin/impersonation/x3000c0b0n1/user-data", token, "", 200}, | ||
| {"tokenless-bootstrap", "GET", "/user-data", "", "", 200}, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| request, requestErr := http.NewRequest(test.method, entry.address+test.path, strings.NewReader(test.body)) | ||
| if requestErr != nil { | ||
| t.Fatal(requestErr) | ||
| } | ||
| if test.token != "" { | ||
| request.Header.Set("Authorization", "Bearer "+test.token) | ||
| } | ||
| // An attacker-controlled forwarding claim never bypasses authentication. | ||
| request.Header.Set("X-Forwarded-For", "127.0.0.1") | ||
| response, requestErr := client.Do(request) | ||
| if requestErr != nil { | ||
| t.Fatal(requestErr) | ||
| } | ||
| body, requestErr := io.ReadAll(response.Body) | ||
| _ = response.Body.Close() | ||
| if requestErr != nil { | ||
| t.Fatal(requestErr) | ||
| } | ||
| if response.StatusCode != test.status { | ||
| t.Fatalf("got %d, want %d", response.StatusCode, test.status) | ||
| } | ||
| if test.name == "tokenless-bootstrap" && string(body) != "#cloud-config" { | ||
| t.Fatal("bootstrap response changed") | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
| if data.ClusterDefaults.ClusterName != "fixture-authenticated" { | ||
| t.Fatal("JWT-rejected request changed cluster defaults") | ||
| } | ||
| } | ||
|
|
||
| func TestAdminJWKSLoadFailureClosesOnlyAdmin(t *testing.T) { | ||
| for _, test := range []struct { | ||
| name, body string | ||
| status int | ||
| }{ | ||
| {"non-success", `{"keys":[]}`, http.StatusServiceUnavailable}, | ||
| {"malformed", `not-a-jwks`, http.StatusOK}, | ||
| {"empty", `{"keys":[]}`, http.StatusOK}, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| keys := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(test.status) | ||
| _, _ = io.WriteString(w, test.body) | ||
| })) | ||
| t.Cleanup(keys.Close) | ||
| verifier, err := fetchPublicKeyFromURL(keys.URL) | ||
| if err == nil || verifier != nil { | ||
| t.Fatal("failed JWKS load returned an enabled verifier") | ||
| } | ||
| router, _ := adminTestRouter(t, verifier) | ||
| for _, probe := range []struct { | ||
| path string | ||
| status int | ||
| }{{"/admin/groups", 503}, {"/user-data", 200}} { | ||
| response := httptest.NewRecorder() | ||
| router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, probe.path, nil)) | ||
| if response.Code != probe.status { | ||
| t.Fatalf("%s: got %d, want %d", probe.path, response.Code, probe.status) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
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.