Skip to content

Commit

Permalink
[endpoint] - Add more info in audit log
Browse files Browse the repository at this point in the history
  • Loading branch information
nyodas committed Nov 7, 2024
1 parent 5864a46 commit a826cb2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
45 changes: 45 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/endpoints/filters/ddog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package filters

import (
"crypto/tls"
"k8s.io/apiserver/pkg/audit"
"net/http"
)

// WithDDOGAudits adds additional information about the request to the audit logs.
// This is useful for debugging and troubleshooting.
// TLS Cipher for fips
// Content-Type of the response to track JSON vs protobuf
func WithDDOGAudits(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()

// Set the content type annotation if it is set
if _, ok := w.Header()["Content-Type"]; ok {
audit.AddAuditAnnotation(ctx, "audit.datadoghq.com/contentType", w.Header().Get("Content-Type"))
}

// Set the TLS Cipher annotation if cipher is set
if &req.TLS.CipherSuite != nil && req.TLS.CipherSuite > 0 {
audit.AddAuditAnnotation(ctx, "audit.datadoghq.com/cipher", tls.CipherSuiteName(req.TLS.CipherSuite))
}

handler.ServeHTTP(w, req)
})
}
1 change: 1 addition & 0 deletions staging/src/k8s.io/apiserver/pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
handler = genericapifilters.WithMuxAndDiscoveryComplete(handler, c.lifecycleSignals.MuxAndDiscoveryComplete.Signaled())
handler = genericfilters.WithPanicRecovery(handler, c.RequestInfoResolver)
handler = genericapifilters.WithAuditInit(handler)
handler = genericapifilters.WithDDOGAudits(handler)
return handler
}

Expand Down

0 comments on commit a826cb2

Please sign in to comment.