Skip to content

Commit c63e7d5

Browse files
committed
[apiserver] add audit logging options for policy file and log rotation
Add WithAuditPolicyFile, WithAuditLogPath, and WithAuditLogRotation options to wire k8s apiserver audit middleware with lumberjack-based rotation. When a policy file is provided, audit events are written as JSON to disk filtered by the policy rules.
1 parent 0f86e0f commit c63e7d5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

pkg/apiserver/manager.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ type options struct {
244244
openAPIDefinitions common.GetOpenAPIDefinitions
245245
addToScheme func(*runtime.Scheme) error
246246
admissionPlugins []admissionPlugin
247+
auditPolicyFile string
248+
auditLogPath string
249+
auditLogMaxAge int // days
250+
auditLogMaxBackups int
251+
auditLogMaxSizeMB int // megabytes
247252
}
248253

249254
type admissionPlugin struct {
@@ -464,6 +469,26 @@ func WithAdmissionPlugin(name string, factory admission.Factory) Option {
464469
}
465470
}
466471

472+
// WithAuditPolicyFile sets the path to an audit policy YAML file.
473+
// When set, the apiserver will emit audit events filtered by the policy.
474+
func WithAuditPolicyFile(path string) Option {
475+
return func(o *options) { o.auditPolicyFile = path }
476+
}
477+
478+
// WithAuditLogPath sets the file path for audit log output.
479+
func WithAuditLogPath(path string) Option {
480+
return func(o *options) { o.auditLogPath = path }
481+
}
482+
483+
// WithAuditLogRotation configures lumberjack-based rotation for the audit log.
484+
func WithAuditLogRotation(maxAgeDays, maxBackups, maxSizeMB int) Option {
485+
return func(o *options) {
486+
o.auditLogMaxAge = maxAgeDays
487+
o.auditLogMaxBackups = maxBackups
488+
o.auditLogMaxSizeMB = maxSizeMB
489+
}
490+
}
491+
467492
func defaultResources() []resource.Object {
468493
// Higher versions need to be registered first as storage resources.
469494
return []resource.Object{
@@ -974,6 +999,22 @@ func start(
974999
o.RecommendedOptions.Authorization = nil
9751000
}
9761001

1002+
if opts.auditPolicyFile != "" {
1003+
o.RecommendedOptions.Audit = apiserveropts.NewAuditOptions()
1004+
o.RecommendedOptions.Audit.PolicyFile = opts.auditPolicyFile
1005+
o.RecommendedOptions.Audit.LogOptions.Path = opts.auditLogPath
1006+
o.RecommendedOptions.Audit.LogOptions.Format = "json"
1007+
if opts.auditLogMaxAge > 0 {
1008+
o.RecommendedOptions.Audit.LogOptions.MaxAge = opts.auditLogMaxAge
1009+
}
1010+
if opts.auditLogMaxBackups > 0 {
1011+
o.RecommendedOptions.Audit.LogOptions.MaxBackups = opts.auditLogMaxBackups
1012+
}
1013+
if opts.auditLogMaxSizeMB > 0 {
1014+
o.RecommendedOptions.Audit.LogOptions.MaxSize = opts.auditLogMaxSizeMB
1015+
}
1016+
}
1017+
9771018
return o
9781019
}).
9791020
WithConfigFns(func(c *apiserver.RecommendedConfig) *apiserver.RecommendedConfig {

0 commit comments

Comments
 (0)