audit.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package audit
  14. import (
  15. "bufio"
  16. "fmt"
  17. "io"
  18. "net"
  19. "net/http"
  20. "time"
  21. "github.com/pborman/uuid"
  22. "k8s.io/kubernetes/pkg/api"
  23. utilnet "k8s.io/kubernetes/pkg/util/net"
  24. )
  25. var _ http.ResponseWriter = &auditResponseWriter{}
  26. type auditResponseWriter struct {
  27. http.ResponseWriter
  28. out io.Writer
  29. id string
  30. }
  31. func (a *auditResponseWriter) WriteHeader(code int) {
  32. fmt.Fprintf(a.out, "%s AUDIT: id=%q response=\"%d\"\n", time.Now().Format(time.RFC3339Nano), a.id, code)
  33. a.ResponseWriter.WriteHeader(code)
  34. }
  35. // fancyResponseWriterDelegator implements http.CloseNotifier, http.Flusher and
  36. // http.Hijacker which are needed to make certain http operation (eg. watch, rsh, etc)
  37. // working.
  38. type fancyResponseWriterDelegator struct {
  39. *auditResponseWriter
  40. }
  41. func (f *fancyResponseWriterDelegator) CloseNotify() <-chan bool {
  42. return f.ResponseWriter.(http.CloseNotifier).CloseNotify()
  43. }
  44. func (f *fancyResponseWriterDelegator) Flush() {
  45. f.ResponseWriter.(http.Flusher).Flush()
  46. }
  47. func (f *fancyResponseWriterDelegator) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  48. return f.ResponseWriter.(http.Hijacker).Hijack()
  49. }
  50. var _ http.CloseNotifier = &fancyResponseWriterDelegator{}
  51. var _ http.Flusher = &fancyResponseWriterDelegator{}
  52. var _ http.Hijacker = &fancyResponseWriterDelegator{}
  53. // WithAudit decorates a http.Handler with audit logging information for all the
  54. // requests coming to the server. Each audit log contains two entries:
  55. // 1. the request line containing:
  56. // - unique id allowing to match the response line (see 2)
  57. // - source ip of the request
  58. // - HTTP method being invoked
  59. // - original user invoking the operation
  60. // - impersonated user for the operation
  61. // - namespace of the request or <none>
  62. // - uri is the full URI as requested
  63. // 2. the response line containing:
  64. // - the unique id from 1
  65. // - response code
  66. func WithAudit(handler http.Handler, requestContextMapper api.RequestContextMapper, out io.Writer) http.Handler {
  67. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  68. ctx, _ := requestContextMapper.Get(req)
  69. user, _ := api.UserFrom(ctx)
  70. asuser := req.Header.Get("Impersonate-User")
  71. if len(asuser) == 0 {
  72. asuser = "<self>"
  73. }
  74. namespace := api.NamespaceValue(ctx)
  75. if len(namespace) == 0 {
  76. namespace = "<none>"
  77. }
  78. id := uuid.NewRandom().String()
  79. fmt.Fprintf(out, "%s AUDIT: id=%q ip=%q method=%q user=%q as=%q namespace=%q uri=%q\n",
  80. time.Now().Format(time.RFC3339Nano), id, utilnet.GetClientIP(req), req.Method, user.GetName(), asuser, namespace, req.URL)
  81. respWriter := decorateResponseWriter(w, out, id)
  82. handler.ServeHTTP(respWriter, req)
  83. })
  84. }
  85. func decorateResponseWriter(responseWriter http.ResponseWriter, out io.Writer, id string) http.ResponseWriter {
  86. delegate := &auditResponseWriter{ResponseWriter: responseWriter, out: out, id: id}
  87. // check if the ResponseWriter we're wrapping is the fancy one we need
  88. // or if the basic is sufficient
  89. _, cn := responseWriter.(http.CloseNotifier)
  90. _, fl := responseWriter.(http.Flusher)
  91. _, hj := responseWriter.(http.Hijacker)
  92. if cn && fl && hj {
  93. return &fancyResponseWriterDelegator{delegate}
  94. }
  95. return delegate
  96. }