@@ -188,16 +188,30 @@ API can be found [here][audit-api] with more details about the exact fields capt
188188The behavior of the `--audit-log-path` flag changes when enabling the `AdvancedAuditing` 
189189feature flag. This includes the cleanups discussed above, such as changes to the `method` 
190190values and the introduction of a "stage" for each event. As before, the `id` field of 
191- the log line  indicates which events were generated from the same request. Events are  
192- formatted as follows :
191+ the log indicates which events were generated from the same request. With default legacy  
192+ format, events are  formatted as follows
193193
194194` ` ` 
1951952017-06-15T21:50:50.259470834Z AUDIT: id="591e9fde-6a98-46f6-b7bc-ec8ef575696d" stage="RequestReceived" ip="10.2.1.3" method="update" user="system:serviceaccount:kube-system:default" groups="\" system:serviceaccounts\" ,\" system:serviceaccounts:kube-system\" ,\" system:authenticated\" " as="<self>" asgroups="<lookup>" namespace="kube-system" uri="/api/v1/namespaces/kube-system/endpoints/kube-controller-manager" response="<deferred>" 
1961962017-06-15T21:50:50.259470834Z AUDIT: id="591e9fde-6a98-46f6-b7bc-ec8ef575696d" stage="ResponseComplete" ip="10.2.1.3" method="update" user="system:serviceaccount:kube-system:default" groups="\" system:serviceaccounts\" ,\" system:serviceaccounts:kube-system\" ,\" system:authenticated\" " as="<self>" asgroups="<lookup>" namespace="kube-system" uri="/api/v1/namespaces/kube-system/endpoints/kube-controller-manager" response="200" 
197197` ` ` 
198198
199199Logged events omit the request and response bodies. The `Request` and 
200- ` RequestResponse` levels are equivalent to `Metadata` for this backend. 
200+ ` RequestResponse` levels are equivalent to `Metadata` for legacy format. 
201+ 
202+ Since Kubernetes 1.8, structed json fromat is supported for log backend. 
203+ Use the following option to switch log to json format :
204+ 
205+ ` ` ` 
206+ --audit-log-format=json 
207+ ` ` ` 
208+ 
209+ With json format, events are formatted as follows :
210+ 
211+ ` ` ` 
212+ {"kind":"Event","apiVersion":"audit.k8s.io/v1alpha1","metadata":{"creationTimestamp":null},"level":"Metadata","timestamp":"2017-07-12T11:02:43Z","auditID":"2e79f0c9-a941-45ae-a9ce-663a1b19ff14","stage":"RequestReceived","requestURI":"/api/v1/namespaces/default/pods","verb":"list","user":{"username":"kubecfg","groups":["system:masters","system:authenticated"]},"sourceIPs":["172.16.116.128"],"objectRef":{"resource":"pods","namespace":"default","apiVersion":"/v1"}} 
213+ {"kind":"Event","apiVersion":"audit.k8s.io/v1alpha1","metadata":{"creationTimestamp":null},"level":"Metadata","timestamp":"2017-07-12T11:02:43Z","auditID":"2e79f0c9-a941-45ae-a9ce-663a1b19ff14","stage":"ResponseComplete","requestURI":"/api/v1/namespaces/default/pods","verb":"list","user":{"username":"kubecfg","groups":["system:masters","system:authenticated"]},"sourceIPs":["172.16.116.128"],"objectRef":{"resource":"pods","namespace":"default","apiVersion":"/v1"},"responseStatus":{"metadata":{},"code":200}} 
214+ ` ` ` 
201215
202216# ### Webhook backend
203217
@@ -284,6 +298,139 @@ Events are POSTed as a JSON serialized `EventList`. An example payload:
284298} 
285299``` 
286300
301+ ### Log Collector Examples  
302+ 
303+ #### Use fluentd to collect and distribute audit events from log file  
304+ 
305+ [ Fluentd] [ fluentd ]  is an open source data collector for unified logging layer.
306+ In this example, we will use fluentd to split audit events by different namespaces.
307+ Note that this example requries json format output support in Kubernetes 1.8.
308+ 
309+ 1 .  install [ fluentd, fluent-plugin-forest and fluent-plugin-rewrite-tag-filter] [ fluentd_install_doc ]  in the kube-apiserver node
310+ 1 .  create a config file for fluentd
311+ 
312+           $ cat <<EOF > /etc/fluentd/config 
313+           # fluentd conf runs in the same host with kube-apiserver 
314+           <source> 
315+               @type tail 
316+               # audit log path of kube-apiserver 
317+               path /var/log/audit 
318+               pos_file /var/log/audit.pos 
319+               format json 
320+               time_key time 
321+               time_format %Y-%m-%dT%H:%M:%S.%N%z 
322+               tag audit 
323+           </source> 
324+            
325+           <filter audit> 
326+               #https://github.com/fluent/fluent-plugin-rewrite-tag-filter/issues/13 
327+               type record_transformer 
328+               enable_ruby 
329+               <record> 
330+               	namespace ${record["objectRef"].nil? ? "none":(record["objectRef"]["namespace"].nil? ?  "none":record["objectRef"]["namespace"])} 
331+               </record> 
332+           </filter> 
333+            
334+           <match audit> 
335+               # route audit according to namespace element in context 
336+               @type rewrite_tag_filter 
337+               rewriterule1 namespace ^(.+) ${tag}.$1 
338+           </match> 
339+            
340+           <filter audit.**> 
341+           	  @type record_transformer 
342+           	  remove_keys namespace 
343+           </filter> 
344+            
345+           <match audit.**> 
346+               @type forest 
347+               subtype file 
348+               remove_prefix audit 
349+               <template> 
350+                   time_slice_format %Y%m%d%H 
351+                   compress gz 
352+                   path /var/log/audit-${tag}.*.log 
353+                   format json 
354+                   include_time_key true 
355+               </template> 
356+           </match> 
357+ 1 .  start fluentd
358+ 
359+           $ fluentd -c /etc/fluentd/config  -vv 
360+ 1 .  start kube-apiserver with the following options:
361+ 
362+           --audit-policy-file=/etc/kubernetes/audit-policy.yaml --audit-log-path=/var/log/kube-audit --audit-log-format=json 
363+ 1 .  check audits for different namespaces in /var/log/audit-* .log
364+ 
365+ #### Use logstash to collect and distribute audit events from webhook backend  
366+ 
367+ [ Logstash] [ logstash ]  is an open source, server-side data processing tool. In this example,
368+ we will use logstash to collect audit events from webhook backend, and save events of
369+ different users into different files.
370+ 
371+ 1 .  install [ logstash] [ logstash_install_doc ] 
372+ 1 .  create config file for logstash
373+ 
374+           $ cat <<EOF > /etc/logstash/config 
375+           input{ 
376+               http{ 
377+                   #TODO, figure out a way to use kubeconfig file to authenticate to logstash 
378+                   #https://www.elastic.co/guide/en/logstash/current/plugins-inputs-http.html#plugins-inputs-http-ssl 
379+                   port=>8888 
380+               } 
381+           } 
382+           filter{ 
383+               split{ 
384+                   # Webhook audit backend sends several events together with EventList 
385+                   # split each event here. 
386+                   field=>[items] 
387+                   # We only need event subelement, remove others. 
388+                   remove_field=>[headers, metadata, apiVersion, "@timestamp", kind, "@version", host] 
389+               } 
390+               mutate{ 
391+                   rename => {items=>event} 
392+               } 
393+           } 
394+           output{ 
395+               file{ 
396+                   # Audit events from different users will be saved into different files. 
397+                   path=>"/var/log/kube-audit-%{[event][user][username]}/audit" 
398+               } 
399+           } 
400+ 1 .  start logstash
401+ 
402+           $ bin/logstash -f /etc/logstash/config --path.settings /etc/logstash/ 
403+ 1 .  create a [ kubeconfig file] ( /docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/ )  for kube-apiserver webhook audit backend
404+ 
405+           $ cat <<EOF > /etc/kubernetes/audit-webhook-kubeconfig 
406+           apiVersion: v1 
407+           clusters: 
408+           - cluster: 
409+               server: http://<ip_of_logstash>:8888 
410+             name: logstash 
411+           contexts: 
412+           - context: 
413+               cluster: logstash 
414+               user: "" 
415+             name: default-context 
416+           current-context: default-context 
417+           kind: Config 
418+           preferences: {} 
419+           users: [] 
420+           EOF 
421+ 1 .  start kube-apiserver with the following options:
422+ 
423+           --audit-policy-file=/etc/kubernetes/audit-policy.yaml --audit-webhook-config-file=/etc/kubernetes/audit-webhook-kubeconfig 
424+ 1 .  check audits in logstash node's directories /var/log/kube-audit-* /audit
425+ 
426+ Note that in addition to file output plugin, logstash has a variety of outputs that
427+ let users route data where they want. For example, users can emit audit events to elasticsearch
428+ plugin which supports full-text search and analytics.
429+ 
287430[ audit-api ] : https://github.com/kubernetes/kubernetes/blob/v1.7.0-rc.1/staging/src/k8s.io/apiserver/pkg/apis/audit/v1alpha1/types.go 
288431[ kube-apiserver ] : /docs/admin/kube-apiserver 
289432[ gce-audit-profile ] : https://github.com/kubernetes/kubernetes/blob/v1.7.0/cluster/gce/gci/configure-helper.sh#L490 
433+ [ fluentd ] : http://www.fluentd.org/ 
434+ [ fluentd_install_doc ] : http://docs.fluentd.org/v0.12/articles/quickstart#step1-installing-fluentd 
435+ [ logstash ] : https://www.elastic.co/products/logstash 
436+ [ logstash_install_doc ] : https://www.elastic.co/guide/en/logstash/current/installing-logstash.html 
0 commit comments