Skip to content

Commit 5173151

Browse files
stephenfinzetaab
andauthored
[k8s-keystone-auth] Remove use of github.com/go-chi/chi/v5 (#3007)
* Add tests for keystone webhooks Signed-off-by: Stephen Finucane <[email protected]> * Remove use of github.com/go-chi/chi/v5 Our use of this is very minimal. There is no good reason to pull in a dependency when the stdlib variant is more than sufficient. Signed-off-by: Stephen Finucane <[email protected]> * Remove unnecessary go.mod replaces Signed-off-by: Stephen Finucane <[email protected]> --------- Signed-off-by: Stephen Finucane <[email protected]> Co-authored-by: Jesse Haka <[email protected]>
1 parent 715d92d commit 5173151

File tree

4 files changed

+96
-11
lines changed

4 files changed

+96
-11
lines changed

go.mod

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ toolchain go1.24.5
66

77
require (
88
github.com/container-storage-interface/spec v1.11.0
9-
github.com/go-chi/chi/v5 v5.2.2
109
github.com/google/uuid v1.6.0
1110
github.com/gophercloud/gophercloud/v2 v2.8.0
1211
github.com/gophercloud/utils/v2 v2.0.0-20250212084022-725b94822eeb
@@ -48,15 +47,11 @@ require (
4847
// the below fixes the "go list -m all" execution
4948
replace (
5049
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.33.3
51-
k8s.io/cri-client => k8s.io/cri-client v0.33.3
52-
k8s.io/dynamic-resource-allocation => k8s.io/dynamic-resource-allocation v0.33.3
5350
k8s.io/endpointslice => k8s.io/endpointslice v0.33.3
5451
k8s.io/externaljwt => k8s.io/externaljwt v0.33.3
5552
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.33.3
5653
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.33.3
5754
k8s.io/kube-proxy => k8s.io/kube-proxy v0.33.3
58-
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.33.3
59-
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.33.3
6055
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.33.3
6156
)
6257

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZ
105105
github.com/gkampitakis/go-diff v1.3.2/go.mod h1:LLgOrpqleQe26cte8s36HTWcTmMEur6OPYerdAAS9tk=
106106
github.com/gkampitakis/go-snaps v0.5.14 h1:3fAqdB6BCPKHDMHAKRwtPUwYexKtGrNuw8HX/T/4neo=
107107
github.com/gkampitakis/go-snaps v0.5.14/go.mod h1:HNpx/9GoKisdhw9AFOBT1N7DBs9DiHo/hGheFGBZ+mc=
108-
github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
109-
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
110108
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
111109
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
112110
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=

pkg/identity/keystone/keystone.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"net/http"
2525
"time"
2626

27-
"github.com/go-chi/chi/v5"
2827
"github.com/gophercloud/gophercloud/v2"
2928
"github.com/gophercloud/gophercloud/v2/openstack"
3029
"github.com/gophercloud/gophercloud/v2/openstack/utils"
@@ -116,11 +115,11 @@ func (k *Auth) Run() {
116115
go wait.Until(k.runWorker, time.Second, k.stopCh)
117116
}
118117

119-
r := chi.NewRouter()
120-
r.HandleFunc("/webhook", k.Handler)
118+
mux := http.NewServeMux()
119+
mux.HandleFunc("/webhook", k.Handler)
121120

122121
klog.Infof("Starting webhook server...")
123-
klog.Fatal(http.ListenAndServeTLS(k.config.Address, k.config.CertFile, k.config.KeyFile, r))
122+
klog.Fatal(http.ListenAndServeTLS(k.config.Address, k.config.CertFile, k.config.KeyFile, mux))
124123
}
125124

126125
func (k *Auth) enqueueConfigMap(obj interface{}) {

pkg/identity/keystone/keystone_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ limitations under the License.
1717
package keystone
1818

1919
import (
20+
"bytes"
21+
"context"
22+
"encoding/json"
23+
"fmt"
24+
"net/http"
25+
"net/http/httptest"
2026
"reflect"
2127
"testing"
2228

@@ -59,3 +65,90 @@ func TestUserAgentFlag(t *testing.T) {
5965
})
6066
}
6167
}
68+
69+
// mockKeystoner is a mock implementation of IKeystone for testing
70+
type mockKeystoner struct{}
71+
72+
func (m *mockKeystoner) GetTokenInfo(ctx context.Context, token string) (*tokenInfo, error) {
73+
return nil, fmt.Errorf("invalid token")
74+
}
75+
76+
func (m *mockKeystoner) GetGroups(ctx context.Context, token string, userID string) ([]string, error) {
77+
return nil, fmt.Errorf("invalid token")
78+
}
79+
80+
func TestWebhookRouting(t *testing.T) {
81+
// Create a minimal Auth instance for testing
82+
auth := &Auth{
83+
authn: &Authenticator{
84+
keystoner: &mockKeystoner{},
85+
},
86+
authz: &Authorizer{
87+
pl: nil,
88+
},
89+
syncer: &Syncer{
90+
syncConfig: nil,
91+
},
92+
}
93+
94+
tests := []struct {
95+
name string
96+
path string
97+
method string
98+
body map[string]interface{}
99+
expectedStatus int
100+
}{
101+
{
102+
name: "valid_webhook_path",
103+
path: "/webhook",
104+
method: http.MethodPost,
105+
body: map[string]interface{}{
106+
"apiVersion": "authentication.k8s.io/v1beta1",
107+
"kind": "TokenReview",
108+
"spec": map[string]interface{}{
109+
"token": "test-token",
110+
},
111+
},
112+
// Handler will try to authenticate and fail, but we're testing routing
113+
expectedStatus: http.StatusUnauthorized,
114+
},
115+
{
116+
name: "invalid_path",
117+
path: "/invalid",
118+
method: http.MethodPost,
119+
body: map[string]interface{}{},
120+
expectedStatus: http.StatusNotFound,
121+
},
122+
{
123+
name: "root_path",
124+
path: "/",
125+
method: http.MethodPost,
126+
body: map[string]interface{}{},
127+
expectedStatus: http.StatusNotFound,
128+
},
129+
}
130+
131+
for _, tc := range tests {
132+
t.Run(tc.name, func(t *testing.T) {
133+
// Create a request
134+
bodyBytes, _ := json.Marshal(tc.body)
135+
req := httptest.NewRequest(tc.method, tc.path, bytes.NewReader(bodyBytes))
136+
req.Header.Set("Content-Type", "application/json")
137+
138+
// Create a response recorder
139+
rr := httptest.NewRecorder()
140+
141+
// Create router and register handler
142+
mux := http.NewServeMux()
143+
mux.HandleFunc("/webhook", auth.Handler)
144+
145+
// Serve the request
146+
mux.ServeHTTP(rr, req)
147+
148+
// Check status code
149+
if rr.Code != tc.expectedStatus {
150+
t.Errorf("Expected status %d, got %d", tc.expectedStatus, rr.Code)
151+
}
152+
})
153+
}
154+
}

0 commit comments

Comments
 (0)