@@ -29,6 +29,7 @@ import (
29
29
"k8s.io/apimachinery/pkg/util/sets"
30
30
"k8s.io/apiserver/pkg/admission"
31
31
auditinternal "k8s.io/apiserver/pkg/apis/audit"
32
+ "k8s.io/apiserver/pkg/audit"
32
33
auditpolicy "k8s.io/apiserver/pkg/audit/policy"
33
34
"k8s.io/apiserver/pkg/authentication/authenticator"
34
35
"k8s.io/apiserver/pkg/authorization/authorizer"
@@ -45,6 +46,7 @@ import (
45
46
storagefactory "k8s.io/apiserver/pkg/storage/storagebackend/factory"
46
47
utilflag "k8s.io/apiserver/pkg/util/flag"
47
48
auditlog "k8s.io/apiserver/plugin/pkg/audit/log"
49
+ auditwebhook "k8s.io/apiserver/plugin/pkg/audit/webhook"
48
50
kapiserveroptions "k8s.io/kubernetes/cmd/kube-apiserver/app/options"
49
51
cmapp "k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
50
52
kapi "k8s.io/kubernetes/pkg/api"
@@ -177,12 +179,11 @@ func BuildKubeAPIserverOptions(masterConfig configapi.MasterConfig) (*kapiserver
177
179
args ["feature-gates" ] = []string {existing [0 ] + ",AdvancedAuditing=true" }
178
180
} else {
179
181
args ["feature-gates" ] = []string {"AdvancedAuditing=true" }
180
-
181
182
}
182
183
}
183
184
// TODO: this should be done in config validation (along with the above) so we can provide
184
185
// 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 {
186
187
return nil , kerrors .NewAggregate (err )
187
188
}
188
189
@@ -526,12 +527,33 @@ func buildKubeApiserverConfig(
526
527
// backwards compatible writer to regular log
527
528
writer = cmdutil .NewGLogWriterV (0 )
528
529
}
529
- genericConfig .AuditBackend = auditlog .NewBackend (writer )
530
+ genericConfig .AuditBackend = auditlog .NewBackend (writer , auditlog . FormatLegacy )
530
531
genericConfig .AuditPolicyChecker = auditpolicy .NewChecker (& auditinternal.Policy {
531
532
// This is for backwards compatibility maintaining the old visibility, ie. just
532
533
// raw overview of the requests comming in.
533
534
Rules : []auditinternal.PolicyRule {{Level : auditinternal .LevelMetadata }},
534
535
})
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
+ }
535
557
}
536
558
537
559
kubeApiserverConfig := & master.Config {
0 commit comments