Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 17902cf

Browse files
committedSep 19, 2017
Enable full advanced audit in origin
1 parent 0992638 commit 17902cf

File tree

7 files changed

+83
-4
lines changed

7 files changed

+83
-4
lines changed
 

‎contrib/completions/bash/openshift

+4
Original file line numberDiff line numberDiff line change
@@ -32067,6 +32067,8 @@ _openshift_start_kubernetes_apiserver()
3206732067
local_nonpersistent_flags+=("--anonymous-auth")
3206832068
flags+=("--apiserver-count=")
3206932069
local_nonpersistent_flags+=("--apiserver-count=")
32070+
flags+=("--audit-log-format=")
32071+
local_nonpersistent_flags+=("--audit-log-format=")
3207032072
flags+=("--audit-log-maxage=")
3207132073
local_nonpersistent_flags+=("--audit-log-maxage=")
3207232074
flags+=("--audit-log-maxbackup=")
@@ -33079,6 +33081,8 @@ _openshift_start_template-service-broker()
3307933081
flags_with_completion=()
3308033082
flags_completion=()
3308133083

33084+
flags+=("--audit-log-format=")
33085+
local_nonpersistent_flags+=("--audit-log-format=")
3308233086
flags+=("--audit-log-maxage=")
3308333087
local_nonpersistent_flags+=("--audit-log-maxage=")
3308433088
flags+=("--audit-log-maxbackup=")

‎contrib/completions/zsh/openshift

+4
Original file line numberDiff line numberDiff line change
@@ -32216,6 +32216,8 @@ _openshift_start_kubernetes_apiserver()
3221632216
local_nonpersistent_flags+=("--anonymous-auth")
3221732217
flags+=("--apiserver-count=")
3221832218
local_nonpersistent_flags+=("--apiserver-count=")
32219+
flags+=("--audit-log-format=")
32220+
local_nonpersistent_flags+=("--audit-log-format=")
3221932221
flags+=("--audit-log-maxage=")
3222032222
local_nonpersistent_flags+=("--audit-log-maxage=")
3222132223
flags+=("--audit-log-maxbackup=")
@@ -33228,6 +33230,8 @@ _openshift_start_template-service-broker()
3322833230
flags_with_completion=()
3322933231
flags_completion=()
3323033232

33233+
flags+=("--audit-log-format=")
33234+
local_nonpersistent_flags+=("--audit-log-format=")
3323133235
flags+=("--audit-log-maxage=")
3323233236
local_nonpersistent_flags+=("--audit-log-maxage=")
3323333237
flags+=("--audit-log-maxbackup=")

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ type AggregatorConfig struct {
464464
// AuditConfig holds configuration for the audit capabilities
465465
type AuditConfig struct {
466466
// If this flag is set, audit log will be printed in the logs.
467-
// The logs contains, method, user and a requested URL.
468467
Enabled bool
469468
// All requests coming to the apiserver will be logged to this file.
470469
AuditFilePath string
@@ -474,6 +473,17 @@ type AuditConfig struct {
474473
MaximumRetainedFiles int
475474
// Maximum size in megabytes of the log file before it gets rotated. Defaults to 100MB.
476475
MaximumFileSizeMegabytes int
476+
477+
// Path to the file that defines the audit policy configuration.
478+
PolicyFile string
479+
480+
// Format of saved audits (legacy or json).
481+
LogFormat string
482+
483+
// Path to a kubeconfig formatted filpe that defines the audit webhook configuration.
484+
WebhookConfigFile string
485+
// Strategy for sending audit events (block or batch).
486+
WebhookMode string
477487
}
478488

479489
// JenkinsPipelineConfig holds configuration for the Jenkins pipeline strategy

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

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ var map_AuditConfig = map[string]string{
9090
"maximumFileRetentionDays": "Maximum number of days to retain old log files based on the timestamp encoded in their filename.",
9191
"maximumRetainedFiles": "Maximum number of old log files to retain.",
9292
"maximumFileSizeMegabytes": "Maximum size in megabytes of the log file before it gets rotated. Defaults to 100MB.",
93+
"policyFile": "Path to the file that defines the audit policy configuration.",
94+
"logFormat": "Format of saved audits (legacy or json).",
95+
"webhookConfigFile": "Path to a kubeconfig formatted filpe that defines the audit webhook configuration.",
96+
"webhookMode": "Strategy for sending audit events (block or batch).",
9397
}
9498

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

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

+11
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ type AuditConfig struct {
331331
MaximumRetainedFiles int `json:"maximumRetainedFiles"`
332332
// Maximum size in megabytes of the log file before it gets rotated. Defaults to 100MB.
333333
MaximumFileSizeMegabytes int `json:"maximumFileSizeMegabytes"`
334+
335+
// Path to the file that defines the audit policy configuration.
336+
PolicyFile string `json:"policyFile"`
337+
338+
// Format of saved audits (legacy or json).
339+
LogFormat string `json:"logFormat"`
340+
341+
// Path to a kubeconfig formatted filpe that defines the audit webhook configuration.
342+
WebhookConfigFile string `json:"webhookConfigFile"`
343+
// Strategy for sending audit events (block or batch).
344+
WebhookMode string `json:"webhookMode"`
334345
}
335346

336347
// JenkinsPipelineConfig holds configuration for the Jenkins pipeline strategy

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

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
"k8s.io/apimachinery/pkg/util/sets"
1515
kuval "k8s.io/apimachinery/pkg/util/validation"
1616
"k8s.io/apimachinery/pkg/util/validation/field"
17+
auditpolicy "k8s.io/apiserver/pkg/audit/policy"
18+
auditlog "k8s.io/apiserver/plugin/pkg/audit/log"
19+
auditwebhook "k8s.io/apiserver/plugin/pkg/audit/webhook"
1720
apiserveroptions "k8s.io/kubernetes/cmd/kube-apiserver/app/options"
1821
kcmoptions "k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
1922
kvalidation "k8s.io/kubernetes/pkg/api/validation"
@@ -238,6 +241,9 @@ func ValidateAggregatorConfig(config api.AggregatorConfig, fldPath *field.Path)
238241

239242
func ValidateAuditConfig(config api.AuditConfig, fldPath *field.Path) ValidationResults {
240243
validationResults := ValidationResults{}
244+
if !config.Enabled {
245+
return validationResults
246+
}
241247

242248
if len(config.AuditFilePath) == 0 {
243249
// for backwards compatibility reasons we can't error this out
@@ -253,6 +259,24 @@ func ValidateAuditConfig(config api.AuditConfig, fldPath *field.Path) Validation
253259
validationResults.AddErrors(field.Invalid(fldPath.Child("maximumFileSizeMegabytes"), config.MaximumFileSizeMegabytes, "must be greater than or equal to 0"))
254260
}
255261

262+
if len(config.PolicyFile) > 0 {
263+
policy, err := auditpolicy.LoadPolicyFromFile(config.PolicyFile)
264+
if err != nil {
265+
validationResults.AddErrors(field.Invalid(fldPath.Child("policyFile"), config.PolicyFile, err.Error()))
266+
}
267+
if len(policy.Rules) == 0 {
268+
validationResults.AddErrors(field.Invalid(fldPath.Child("policyFile"), config.PolicyFile, "a policy file with 0 policies is not valid"))
269+
}
270+
}
271+
if len(config.LogFormat) > 0 && config.LogFormat != auditlog.FormatLegacy && config.LogFormat != auditlog.FormatJson {
272+
validationResults.AddErrors(field.Invalid(fldPath.Child("logFormat"), config.LogFormat,
273+
fmt.Sprintf("invalid audit log format, allowed formats are %q", strings.Join(auditlog.AllowedFormats, ","))))
274+
}
275+
if len(config.WebhookMode) > 0 && config.WebhookMode != auditwebhook.ModeBatch && config.WebhookMode != auditwebhook.ModeBlocking {
276+
validationResults.AddErrors(field.Invalid(fldPath.Child("logFormat"), config.WebhookMode,
277+
fmt.Sprintf("invalid audit webhook mode, allowed modes are %q", strings.Join(auditwebhook.AllowedModes, ","))))
278+
}
279+
256280
return validationResults
257281
}
258282

‎pkg/cmd/server/kubernetes/master/master_config.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/util/sets"
3030
"k8s.io/apiserver/pkg/admission"
3131
auditinternal "k8s.io/apiserver/pkg/apis/audit"
32+
"k8s.io/apiserver/pkg/audit"
3233
auditpolicy "k8s.io/apiserver/pkg/audit/policy"
3334
"k8s.io/apiserver/pkg/authentication/authenticator"
3435
"k8s.io/apiserver/pkg/authorization/authorizer"
@@ -45,6 +46,7 @@ import (
4546
storagefactory "k8s.io/apiserver/pkg/storage/storagebackend/factory"
4647
utilflag "k8s.io/apiserver/pkg/util/flag"
4748
auditlog "k8s.io/apiserver/plugin/pkg/audit/log"
49+
auditwebhook "k8s.io/apiserver/plugin/pkg/audit/webhook"
4850
kapiserveroptions "k8s.io/kubernetes/cmd/kube-apiserver/app/options"
4951
cmapp "k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
5052
kapi "k8s.io/kubernetes/pkg/api"
@@ -177,12 +179,11 @@ func BuildKubeAPIserverOptions(masterConfig configapi.MasterConfig) (*kapiserver
177179
args["feature-gates"] = []string{existing[0] + ",AdvancedAuditing=true"}
178180
} else {
179181
args["feature-gates"] = []string{"AdvancedAuditing=true"}
180-
181182
}
182183
}
183184
// TODO: this should be done in config validation (along with the above) so we can provide
184185
// proper errors
185-
if err := cmdflags.Resolve(masterConfig.KubernetesMasterConfig.APIServerArguments, server.AddFlags); len(err) > 0 {
186+
if err := cmdflags.Resolve(args, server.AddFlags); len(err) > 0 {
186187
return nil, kerrors.NewAggregate(err)
187188
}
188189

@@ -526,12 +527,33 @@ func buildKubeApiserverConfig(
526527
// backwards compatible writer to regular log
527528
writer = cmdutil.NewGLogWriterV(0)
528529
}
529-
genericConfig.AuditBackend = auditlog.NewBackend(writer)
530+
genericConfig.AuditBackend = auditlog.NewBackend(writer, auditlog.FormatLegacy)
530531
genericConfig.AuditPolicyChecker = auditpolicy.NewChecker(&auditinternal.Policy{
531532
// This is for backwards compatibility maintaining the old visibility, ie. just
532533
// raw overview of the requests comming in.
533534
Rules: []auditinternal.PolicyRule{{Level: auditinternal.LevelMetadata}},
534535
})
536+
537+
// when a policy file is defined we enable the advanced auditing
538+
if len(masterConfig.AuditConfig.PolicyFile) > 0 {
539+
// policy configuration
540+
p, _ := auditpolicy.LoadPolicyFromFile(masterConfig.AuditConfig.PolicyFile)
541+
genericConfig.AuditPolicyChecker = auditpolicy.NewChecker(p)
542+
543+
// log configuration, only when file path was provided
544+
if len(masterConfig.AuditConfig.AuditFilePath) > 0 {
545+
genericConfig.AuditBackend = auditlog.NewBackend(writer, masterConfig.AuditConfig.LogFormat)
546+
}
547+
548+
// webhook configuration, only when config file was provided
549+
if len(masterConfig.AuditConfig.WebhookConfigFile) > 0 {
550+
webhook, err := auditwebhook.NewBackend(masterConfig.AuditConfig.WebhookConfigFile, masterConfig.AuditConfig.WebhookMode)
551+
if err != nil {
552+
glog.Fatalf("Audit webhook initialization failed: %v", err)
553+
}
554+
genericConfig.AuditBackend = audit.Union(genericConfig.AuditBackend, webhook)
555+
}
556+
}
535557
}
536558

537559
kubeApiserverConfig := &master.Config{

0 commit comments

Comments
 (0)
Please sign in to comment.