mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-18 04:54:54 +00:00
add unit-test for ensuring authn latency annotation
Signed-off-by: Min Jin <minkimzz@amazon.com>
This commit is contained in:
@@ -704,3 +704,35 @@ func TestUnauthenticatedHTTP2ClientConnectionClose(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticationLatencyTracked(t *testing.T) {
|
||||
successHandlerCalled := false
|
||||
handler := WithAuthentication(
|
||||
http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
|
||||
// latency annotations should be added by the time the success handler is called
|
||||
annotations := genericapirequest.AuditAnnotationsFromLatencyTrackers(req.Context())
|
||||
if annotations["apiserver.latency.k8s.io/authentication"] == "" {
|
||||
t.Errorf("missing authentication latency annotation: %v", annotations)
|
||||
}
|
||||
successHandlerCalled = true
|
||||
}),
|
||||
authenticator.RequestFunc(func(req *http.Request) (*authenticator.Response, bool, error) {
|
||||
return &authenticator.Response{User: &user.DefaultInfo{Name: "user"}}, true, nil
|
||||
}),
|
||||
http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
|
||||
t.Error("unexpected call to error handler")
|
||||
}),
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
handler = WithLatencyTrackers(handler)
|
||||
handler = WithRequestInfo(handler, &fakeRequestResolver{})
|
||||
req, err := http.NewRequest(http.MethodGet, "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler.ServeHTTP(httptest.NewRecorder(), req)
|
||||
if !successHandlerCalled {
|
||||
t.Fatal("no call to success handler")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,9 +71,9 @@ func withAuthorization(handler http.Handler, a authorizer.Authorizer, s runtime.
|
||||
authorized, reason, err := a.Authorize(ctx, attributes)
|
||||
|
||||
authorizationFinish := time.Now()
|
||||
request.TrackAuthorizationLatency(ctx, authorizationFinish.Sub(authorizationStart))
|
||||
defer func() {
|
||||
metrics(ctx, authorized, err, authorizationStart, authorizationFinish)
|
||||
request.TrackAuthorizationLatency(ctx, authorizationFinish.Sub(authorizationStart))
|
||||
}()
|
||||
|
||||
// an authorizer like RBAC could encounter evaluation errors and still allow the request, so authorizer decision is checked before error here.
|
||||
|
||||
@@ -71,9 +71,7 @@ func (wt *writeLatencyTracker) Unwrap() http.ResponseWriter {
|
||||
|
||||
func (wt *writeLatencyTracker) Write(bs []byte) (int, error) {
|
||||
startedAt := time.Now()
|
||||
defer func() {
|
||||
request.TrackResponseWriteLatency(wt.ctx, time.Since(startedAt))
|
||||
}()
|
||||
|
||||
return wt.ResponseWriter.Write(bs)
|
||||
n, err := wt.ResponseWriter.Write(bs)
|
||||
request.TrackResponseWriteLatency(wt.ctx, time.Since(startedAt))
|
||||
return n, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user