-
Notifications
You must be signed in to change notification settings - Fork 178
Introduce middleware for audit logs and authentication checks #157
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package middleware | ||
|
||
|
|
||
| import ( | ||
| "bufio" | ||
| "net" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| func RequestMiddleware(next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| start := time.Now() | ||
|
|
||
| lrw := &loggingResponseWriter{ | ||
| ResponseWriter: w, | ||
| statusCode: http.StatusOK, | ||
| } | ||
|
|
||
| next.ServeHTTP(lrw, r) | ||
|
|
||
| duration := time.Since(start) | ||
| klog.V(5).Infof("%s %s %d %v", r.Method, r.URL.Path, lrw.statusCode, duration) | ||
| }) | ||
| } | ||
|
|
||
| type loggingResponseWriter struct { | ||
| http.ResponseWriter | ||
| statusCode int | ||
| headerWritten bool | ||
| } | ||
|
|
||
| func (lrw *loggingResponseWriter) WriteHeader(code int) { | ||
| if !lrw.headerWritten { | ||
| lrw.statusCode = code | ||
| lrw.headerWritten = true | ||
| lrw.ResponseWriter.WriteHeader(code) | ||
| } | ||
| } | ||
|
|
||
| func (lrw *loggingResponseWriter) Write(b []byte) (int, error) { | ||
| if !lrw.headerWritten { | ||
| lrw.statusCode = http.StatusOK | ||
| lrw.headerWritten = true | ||
| } | ||
| return lrw.ResponseWriter.Write(b) | ||
| } | ||
|
|
||
| func (lrw *loggingResponseWriter) Flush() { | ||
| if flusher, ok := lrw.ResponseWriter.(http.Flusher); ok { | ||
| flusher.Flush() | ||
| } | ||
| } | ||
|
|
||
| func (lrw *loggingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { | ||
| if hijacker, ok := lrw.ResponseWriter.(http.Hijacker); ok { | ||
| return hijacker.Hijack() | ||
| } | ||
| return nil, nil, http.ErrNotSupported | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not related to the changes in this PR. I updated this, because I think we want stateless streamable http server. If it is suggested that this change should be in a different PR, I can remove it from here.