Skip to content

Commit 8a79d34

Browse files
author
OpenShift Bot
authored
Merge pull request #11192 from soltysh/issue11037
Merged by openshift-bot
2 parents 30bbfa2 + 59e59e3 commit 8a79d34

File tree

13 files changed

+105
-165
lines changed

13 files changed

+105
-165
lines changed

pkg/cmd/server/api/helpers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ func GetMasterFileReferences(config *MasterConfig) []*string {
256256
refs = append(refs, &config.ControllerConfig.ServiceServingCert.Signer.KeyFile)
257257
}
258258

259+
refs = append(refs, &config.AuditConfig.AuditFilePath)
260+
259261
return refs
260262
}
261263

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+
AuditFilePath string
330+
// Maximum number of days to retain old log files based on the timestamp encoded in their filename.
331+
MaximumFileRetentionDays int
332+
// Maximum number of old log files to retain.
333+
MaximumRetainedFiles int
334+
// Maximum size in megabytes of the log file before it gets rotated. Defaults to 100MB.
335+
MaximumFileSizeMegabytes int
328336
}
329337

330338
// JenkinsPipelineConfig holds configuration for the Jenkins pipeline strategy

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ func (AssetExtensionsConfig) SwaggerDoc() map[string]string {
7575
}
7676

7777
var map_AuditConfig = map[string]string{
78-
"": "AuditConfig holds configuration for the audit capabilities",
79-
"enabled": "If this flag is set, basic audit log will be printed in the logs. The logs contains, method, user and a requested URL.",
78+
"": "AuditConfig holds configuration for the audit capabilities",
79+
"enabled": "If this flag is set, audit log will be printed in the logs. The logs contains, method, user and a requested URL.",
80+
"auditFilePath": "All requests coming to the apiserver will be logged to this file.",
81+
"maximumFileRetentionDays": "Maximum number of days to retain old log files based on the timestamp encoded in their filename.",
82+
"maximumRetainedFiles": "Maximum number of old log files to retain.",
83+
"maximumFileSizeMegabytes": "Maximum size in megabytes of the log file before it gets rotated. Defaults to 100MB.",
8084
}
8185

8286
func (AuditConfig) SwaggerDoc() map[string]string {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,17 @@ type MasterConfig struct {
248248

249249
// AuditConfig holds configuration for the audit capabilities
250250
type AuditConfig struct {
251-
// If this flag is set, basic audit log will be printed in the logs.
251+
// If this flag is set, 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+
AuditFilePath string `json:"auditFilePath"`
256+
// Maximum number of days to retain old log files based on the timestamp encoded in their filename.
257+
MaximumFileRetentionDays int `json:"maximumFileRetentionDays"`
258+
// Maximum number of old log files to retain.
259+
MaximumRetainedFiles int `json:"maximumRetainedFiles"`
260+
// Maximum size in megabytes of the log file before it gets rotated. Defaults to 100MB.
261+
MaximumFileSizeMegabytes int `json:"maximumFileSizeMegabytes"`
254262
}
255263

256264
// JenkinsPipelineConfig holds configuration for the Jenkins pipeline strategy

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ assetConfig:
105105
namedCertificates: null
106106
requestTimeoutSeconds: 0
107107
auditConfig:
108+
auditFilePath: ""
108109
enabled: false
110+
maximumFileRetentionDays: 0
111+
maximumFileSizeMegabytes: 0
112+
maximumRetainedFiles: 0
109113
controllerConfig:
110114
serviceServingCert:
111115
signer: null

pkg/cmd/server/api/validation/master.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,28 @@ func ValidateMasterConfig(config *api.MasterConfig, fldPath *field.Path) Validat
191191

192192
validationResults.Append(ValidateControllerConfig(config.ControllerConfig, fldPath.Child("controllerConfig")))
193193

194+
validationResults.Append(ValidateAuditConfig(config.AuditConfig, fldPath.Child("auditConfig")))
195+
196+
return validationResults
197+
}
198+
199+
func ValidateAuditConfig(config api.AuditConfig, fldPath *field.Path) ValidationResults {
200+
validationResults := ValidationResults{}
201+
202+
if len(config.AuditFilePath) == 0 {
203+
// for backwards compatibility reasons we can't error this out
204+
validationResults.AddWarnings(field.Required(fldPath.Child("auditFilePath"), "audit can now be logged to a separate file"))
205+
}
206+
if config.MaximumFileRetentionDays < 0 {
207+
validationResults.AddErrors(field.Invalid(fldPath.Child("maximumFileRetentionDays"), config.MaximumFileRetentionDays, "must be greater than or equal to 0"))
208+
}
209+
if config.MaximumRetainedFiles < 0 {
210+
validationResults.AddErrors(field.Invalid(fldPath.Child("maximumRetainedFiles"), config.MaximumRetainedFiles, "must be greater than or equal to 0"))
211+
}
212+
if config.MaximumFileSizeMegabytes < 0 {
213+
validationResults.AddErrors(field.Invalid(fldPath.Child("maximumFileSizeMegabytes"), config.MaximumFileSizeMegabytes, "must be greater than or equal to 0"))
214+
}
215+
194216
return validationResults
195217
}
196218

pkg/cmd/server/api/validation/master_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func TestValidateAdmissionPluginConfigConflicts(t *testing.T) {
383383
// these fields have warnings in the empty case
384384
defaultWarningFields := sets.NewString(
385385
"serviceAccountConfig.managedNames", "serviceAccountConfig.publicKeyFiles", "serviceAccountConfig.privateKeyFile", "serviceAccountConfig.masterCA",
386-
"projectConfig.securityAllocator", "kubernetesMasterConfig.proxyClientInfo")
386+
"projectConfig.securityAllocator", "kubernetesMasterConfig.proxyClientInfo", "auditConfig.auditFilePath")
387387

388388
for _, tc := range testCases {
389389
results := ValidateMasterConfig(&tc.options, nil)

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: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/tls"
55
"encoding/json"
66
"fmt"
7+
"io"
78
"net/http"
89
"os"
910
"regexp"
@@ -16,6 +17,7 @@ import (
1617
"github.com/go-openapi/spec"
1718
"github.com/golang/glog"
1819
"github.com/prometheus/client_golang/prometheus"
20+
"gopkg.in/natefinch/lumberjack.v2"
1921

2022
kapi "k8s.io/kubernetes/pkg/api"
2123
"k8s.io/kubernetes/pkg/api/meta"
@@ -25,6 +27,7 @@ import (
2527
"k8s.io/kubernetes/pkg/apimachinery/registered"
2628
v1beta1extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
2729
"k8s.io/kubernetes/pkg/apiserver"
30+
"k8s.io/kubernetes/pkg/apiserver/audit"
2831
"k8s.io/kubernetes/pkg/client/restclient"
2932
kclient "k8s.io/kubernetes/pkg/client/unversioned"
3033
clientadapter "k8s.io/kubernetes/pkg/client/unversioned/adapters/internalclientset"
@@ -185,7 +188,22 @@ func (c *MasterConfig) Run(protected []APIInstaller, unprotected []APIInstaller)
185188
handler = c.authorizationFilter(handler)
186189
handler = c.impersonationFilter(handler)
187190
// audit handler must comes before the impersonationFilter to read the original user
188-
handler = c.auditHandler(handler)
191+
if c.Options.AuditConfig.Enabled {
192+
attributeGetter := apiserver.NewRequestAttributeGetter(c.getRequestContextMapper(), c.getRequestInfoResolver())
193+
var writer io.Writer
194+
if len(c.Options.AuditConfig.AuditFilePath) > 0 {
195+
writer = &lumberjack.Logger{
196+
Filename: c.Options.AuditConfig.AuditFilePath,
197+
MaxAge: c.Options.AuditConfig.MaximumFileRetentionDays,
198+
MaxBackups: c.Options.AuditConfig.MaximumRetainedFiles,
199+
MaxSize: c.Options.AuditConfig.MaximumFileSizeMegabytes,
200+
}
201+
} else {
202+
// backwards compatible writer to regular log
203+
writer = cmdutil.NewGLogWriterV(0)
204+
}
205+
handler = audit.WithAudit(handler, attributeGetter, writer)
206+
}
189207
handler = authenticationHandlerFilter(handler, c.Authenticator, c.getRequestContextMapper())
190208
handler = namespacingFilter(handler, c.getRequestContextMapper())
191209
handler = cacheControlFilter(handler, "no-store") // protected endpoints should not be cached
@@ -915,6 +933,23 @@ func (c *MasterConfig) getRequestContextMapper() kapi.RequestContextMapper {
915933
return c.RequestContextMapper
916934
}
917935

936+
// getRequestInfoResolver returns a request resolver.
937+
func (c *MasterConfig) getRequestInfoResolver() *apiserver.RequestInfoResolver {
938+
if c.RequestInfoResolver == nil {
939+
c.RequestInfoResolver = &apiserver.RequestInfoResolver{
940+
APIPrefixes: sets.NewString(strings.Trim(LegacyOpenShiftAPIPrefix, "/"),
941+
strings.Trim(OpenShiftAPIPrefix, "/"),
942+
strings.Trim(KubernetesAPIPrefix, "/"),
943+
strings.Trim(KubernetesAPIGroupPrefix, "/")), // all possible API prefixes
944+
GrouplessAPIPrefixes: sets.NewString(strings.Trim(LegacyOpenShiftAPIPrefix, "/"),
945+
strings.Trim(OpenShiftAPIPrefix, "/"),
946+
strings.Trim(KubernetesAPIPrefix, "/"),
947+
), // APIPrefixes that won't have groups (legacy)
948+
}
949+
}
950+
return c.RequestInfoResolver
951+
}
952+
918953
// RouteAllocator returns a route allocation controller.
919954
func (c *MasterConfig) RouteAllocator() *routeallocationcontroller.RouteAllocationController {
920955
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

vendor/k8s.io/kubernetes/pkg/apiserver/audit/audit.go

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/pkg/apiserver/audit/audit_test.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)