mirror of https://github.com/k3s-io/k3s
Remove closing audit log file and add error check when writing to audit
parent
d17c6b903a
commit
5873c2679c
|
@ -24,6 +24,7 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/pborman/uuid"
|
||||
|
||||
"k8s.io/kubernetes/pkg/apiserver"
|
||||
|
@ -39,7 +40,11 @@ type auditResponseWriter struct {
|
|||
}
|
||||
|
||||
func (a *auditResponseWriter) WriteHeader(code int) {
|
||||
fmt.Fprintf(a.out, "%s AUDIT: id=%q response=\"%d\"\n", time.Now().Format(time.RFC3339Nano), a.id, code)
|
||||
line := fmt.Sprintf("%s AUDIT: id=%q response=\"%d\"\n", time.Now().Format(time.RFC3339Nano), a.id, code)
|
||||
if _, err := fmt.Fprint(a.out, line); err != nil {
|
||||
glog.Errorf("Unable to write audit log: %s, the error is: %v", line, err)
|
||||
}
|
||||
|
||||
a.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
|
@ -92,8 +97,11 @@ func WithAudit(handler http.Handler, attributeGetter apiserver.RequestAttributeG
|
|||
}
|
||||
id := uuid.NewRandom().String()
|
||||
|
||||
fmt.Fprintf(out, "%s AUDIT: id=%q ip=%q method=%q user=%q as=%q namespace=%q uri=%q\n",
|
||||
line := fmt.Sprintf("%s AUDIT: id=%q ip=%q method=%q user=%q as=%q namespace=%q uri=%q\n",
|
||||
time.Now().Format(time.RFC3339Nano), id, utilnet.GetClientIP(req), req.Method, attribs.GetUser().GetName(), asuser, namespace, req.URL)
|
||||
if _, err := fmt.Fprint(out, line); err != nil {
|
||||
glog.Errorf("Unable to write audit log: %s, the error is: %v", line, err)
|
||||
}
|
||||
respWriter := decorateResponseWriter(w, out, id)
|
||||
handler.ServeHTTP(respWriter, req)
|
||||
})
|
||||
|
|
|
@ -377,6 +377,7 @@ func (c Config) New() (*GenericAPIServer, error) {
|
|||
|
||||
attributeGetter := apiserver.NewRequestAttributeGetter(c.RequestContextMapper, s.NewRequestInfoResolver())
|
||||
handler = apiserver.WithAuthorizationCheck(handler, attributeGetter, c.Authorizer)
|
||||
handler = apiserver.WithImpersonation(handler, c.RequestContextMapper, c.Authorizer)
|
||||
if len(c.AuditLogPath) != 0 {
|
||||
// audit handler must comes before the impersonationFilter to read the original user
|
||||
writer := &lumberjack.Logger{
|
||||
|
@ -386,9 +387,7 @@ func (c Config) New() (*GenericAPIServer, error) {
|
|||
MaxSize: c.AuditLogMaxSize,
|
||||
}
|
||||
handler = audit.WithAudit(handler, attributeGetter, writer)
|
||||
defer writer.Close()
|
||||
}
|
||||
handler = apiserver.WithImpersonation(handler, c.RequestContextMapper, c.Authorizer)
|
||||
|
||||
// Install Authenticator
|
||||
if c.Authenticator != nil {
|
||||
|
|
Loading…
Reference in New Issue