Skip to content

Commit 08c985e

Browse files
committed
Switch to use upstream audit handler
1 parent 53c556d commit 08c985e

File tree

6 files changed

+48
-157
lines changed

6 files changed

+48
-157
lines changed

pkg/cmd/server/api/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ type AuditConfig struct {
325325
// If this flag is set, audit log will be printed in the logs.
326326
// The logs contains, method, user and a requested URL.
327327
Enabled bool
328+
// All requests coming to the apiserver will be logged to this file.
329+
Path string
330+
// Maximum number of days to retain old audit log files based on the timestamp encoded in their filename.
331+
MaxAge int
332+
// Maximum number of old audit log files to retain.
333+
MaxBackups int
334+
// Maximum size in megabytes of the audit log file before it gets rotated. Defaults to 100MB.
335+
MaxSize int
328336
}
329337

330338
// JenkinsPipelineConfig holds configuration for the Jenkins pipeline strategy

pkg/cmd/server/api/v1/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ type AuditConfig struct {
251251
// If this flag is set, basic audit log will be printed in the logs.
252252
// The logs contains, method, user and a requested URL.
253253
Enabled bool `json:"enabled"`
254+
// All requests coming to the apiserver will be logged to this file.
255+
Path string `json:"path"`
256+
// Maximum number of days to retain old audit log files based on the timestamp encoded in their filename.
257+
MaxAge int `json:"maxAge"`
258+
// Maximum number of old audit log files to retain.
259+
MaxBackups int `json:"maxBackups"`
260+
// Maximum size in megabytes of the audit log file before it gets rotated. Defaults to 100MB.
261+
MaxSize int `json:"maxSize"`
254262
}
255263

256264
// JenkinsPipelineConfig holds configuration for the Jenkins pipeline strategy

pkg/cmd/server/origin/audit.go

Lines changed: 0 additions & 113 deletions
This file was deleted.

pkg/cmd/server/origin/audit_test.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

pkg/cmd/server/origin/master.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/go-openapi/spec"
1717
"github.com/golang/glog"
1818
"github.com/prometheus/client_golang/prometheus"
19+
"gopkg.in/natefinch/lumberjack.v2"
1920

2021
kapi "k8s.io/kubernetes/pkg/api"
2122
"k8s.io/kubernetes/pkg/api/meta"
@@ -25,6 +26,7 @@ import (
2526
"k8s.io/kubernetes/pkg/apimachinery/registered"
2627
v1beta1extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
2728
"k8s.io/kubernetes/pkg/apiserver"
29+
"k8s.io/kubernetes/pkg/apiserver/audit"
2830
"k8s.io/kubernetes/pkg/client/restclient"
2931
kclient "k8s.io/kubernetes/pkg/client/unversioned"
3032
clientadapter "k8s.io/kubernetes/pkg/client/unversioned/adapters/internalclientset"
@@ -177,7 +179,17 @@ func (c *MasterConfig) Run(protected []APIInstaller, unprotected []APIInstaller)
177179
handler = c.authorizationFilter(handler)
178180
handler = c.impersonationFilter(handler)
179181
// audit handler must comes before the impersonationFilter to read the original user
180-
handler = c.auditHandler(handler)
182+
if c.Options.AuditConfig.Enabled {
183+
attributeGetter := apiserver.NewRequestAttributeGetter(c.getRequestContextMapper(), c.getRequestInfoResolver())
184+
writer := &lumberjack.Logger{
185+
Filename: c.Options.AuditConfig.Path,
186+
MaxAge: c.Options.AuditConfig.MaxAge,
187+
MaxBackups: c.Options.AuditConfig.MaxBackups,
188+
MaxSize: c.Options.AuditConfig.MaxSize,
189+
}
190+
handler = audit.WithAudit(handler, attributeGetter, writer)
191+
defer writer.Close()
192+
}
181193
handler = authenticationHandlerFilter(handler, c.Authenticator, c.getRequestContextMapper())
182194
handler = namespacingFilter(handler, c.getRequestContextMapper())
183195
handler = cacheControlFilter(handler, "no-store") // protected endpoints should not be cached
@@ -861,6 +873,23 @@ func (c *MasterConfig) getRequestContextMapper() kapi.RequestContextMapper {
861873
return c.RequestContextMapper
862874
}
863875

876+
// getRequestInfoResolver returns a request resolver.
877+
func (c *MasterConfig) getRequestInfoResolver() *apiserver.RequestInfoResolver {
878+
if c.RequestInfoResolver == nil {
879+
c.RequestInfoResolver = &apiserver.RequestInfoResolver{
880+
APIPrefixes: sets.NewString(strings.Trim(LegacyOpenShiftAPIPrefix, "/"),
881+
strings.Trim(OpenShiftAPIPrefix, "/"),
882+
strings.Trim(KubernetesAPIPrefix, "/"),
883+
strings.Trim(KubernetesAPIGroupPrefix, "/")), // all possible API prefixes
884+
GrouplessAPIPrefixes: sets.NewString(strings.Trim(LegacyOpenShiftAPIPrefix, "/"),
885+
strings.Trim(OpenShiftAPIPrefix, "/"),
886+
strings.Trim(KubernetesAPIPrefix, "/"),
887+
), // APIPrefixes that won't have groups (legacy)
888+
}
889+
}
890+
return c.RequestInfoResolver
891+
}
892+
864893
// RouteAllocator returns a route allocation controller.
865894
func (c *MasterConfig) RouteAllocator() *routeallocationcontroller.RouteAllocationController {
866895
osclient, kclient := c.RouteAllocatorClients()

pkg/cmd/server/origin/master_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ type MasterConfig struct {
111111

112112
// RequestContextMapper maps requests to contexts
113113
RequestContextMapper kapi.RequestContextMapper
114+
// RequestInfoResolver is responsible for reading request attributes
115+
RequestInfoResolver *apiserver.RequestInfoResolver
114116

115117
AdmissionControl admission.Interface
116118

0 commit comments

Comments
 (0)