diff --git a/staging/src/k8s.io/apiserver/pkg/apis/audit/types.go b/staging/src/k8s.io/apiserver/pkg/apis/audit/types.go index 2b318d5754d..155381b2c64 100644 --- a/staging/src/k8s.io/apiserver/pkg/apis/audit/types.go +++ b/staging/src/k8s.io/apiserver/pkg/apis/audit/types.go @@ -125,6 +125,14 @@ type Event struct { RequestReceivedTimestamp metav1.MicroTime // Time the request reached current audit stage. StageTimestamp metav1.MicroTime + + // Annotations is an unstructured key value map stored with an audit event that may be set by + // plugins invoked in the request serving chain, including authentication, authorization and + // admission plugins. Keys should uniquely identify the informing component to avoid name + // collisions (e.g. podsecuritypolicy.admission.k8s.io/policy). Values should be short. Annotations + // are included in the Metadata level. + // +optional + Annotations map[string]string } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/staging/src/k8s.io/apiserver/pkg/apis/audit/v1alpha1/types.go b/staging/src/k8s.io/apiserver/pkg/apis/audit/v1alpha1/types.go index 21f10c78d48..8bf4a6ecb23 100644 --- a/staging/src/k8s.io/apiserver/pkg/apis/audit/v1alpha1/types.go +++ b/staging/src/k8s.io/apiserver/pkg/apis/audit/v1alpha1/types.go @@ -132,6 +132,14 @@ type Event struct { // Time the request reached current audit stage. // +optional StageTimestamp metav1.MicroTime `json:"stageTimestamp" protobuf:"bytes,16,opt,name=stageTimestamp"` + + // Annotations is an unstructured key value map stored with an audit event that may be set by + // plugins invoked in the request serving chain, including authentication, authorization and + // admission plugins. Keys should uniquely identify the informing component to avoid name + // collisions (e.g. podsecuritypolicy.admission.k8s.io/policy). Values should be short. Annotations + // are included in the Metadata level. + // +optional + Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,17,rep,name=annotations"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/staging/src/k8s.io/apiserver/pkg/apis/audit/v1beta1/types.go b/staging/src/k8s.io/apiserver/pkg/apis/audit/v1beta1/types.go index 259599c050e..cd9612c54e0 100644 --- a/staging/src/k8s.io/apiserver/pkg/apis/audit/v1beta1/types.go +++ b/staging/src/k8s.io/apiserver/pkg/apis/audit/v1beta1/types.go @@ -128,6 +128,14 @@ type Event struct { // Time the request reached current audit stage. // +optional StageTimestamp metav1.MicroTime `json:"stageTimestamp" protobuf:"bytes,16,opt,name=stageTimestamp"` + + // Annotations is an unstructured key value map stored with an audit event that may be set by + // plugins invoked in the request serving chain, including authentication, authorization and + // admission plugins. Keys should uniquely identify the informing component to avoid name + // collisions (e.g. podsecuritypolicy.admission.k8s.io/policy). Values should be short. Annotations + // are included in the Metadata level. + // +optional + Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,17,rep,name=annotations"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/staging/src/k8s.io/apiserver/pkg/audit/request.go b/staging/src/k8s.io/apiserver/pkg/audit/request.go index 79e6fd8af35..d42feccfc8e 100644 --- a/staging/src/k8s.io/apiserver/pkg/audit/request.go +++ b/staging/src/k8s.io/apiserver/pkg/audit/request.go @@ -204,3 +204,28 @@ func encodeObject(obj runtime.Object, gv schema.GroupVersion, serializer runtime } return nil, fmt.Errorf("no json encoder found") } + +// LogAnnotation fills in the Annotations according to the key value pair. +func LogAnnotation(ae *auditinternal.Event, key, value string) { + if ae == nil || ae.Level.Less(auditinternal.LevelMetadata) { + return + } + if ae.Annotations == nil { + ae.Annotations = make(map[string]string) + } + if v, ok := ae.Annotations[key]; ok && v != value { + glog.Warningf("Failed to set annotations[%q] to %q for audit:%q, it has already been set to %q", key, value, ae.AuditID, ae.Annotations[key]) + return + } + ae.Annotations[key] = value +} + +// LogAnnotations fills in the Annotations according to the annotations map. +func LogAnnotations(ae *auditinternal.Event, annotations map[string]string) { + if ae == nil || ae.Level.Less(auditinternal.LevelMetadata) { + return + } + for key, value := range annotations { + LogAnnotation(ae, key, value) + } +} diff --git a/staging/src/k8s.io/apiserver/pkg/audit/request_test.go b/staging/src/k8s.io/apiserver/pkg/audit/request_test.go new file mode 100644 index 00000000000..b8bfacbdacd --- /dev/null +++ b/staging/src/k8s.io/apiserver/pkg/audit/request_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package audit + +import ( + "testing" + + "github.com/stretchr/testify/assert" + auditinternal "k8s.io/apiserver/pkg/apis/audit" +) + +func TestLogAnnotation(t *testing.T) { + ev := &auditinternal.Event{ + Level: auditinternal.LevelMetadata, + AuditID: "fake id", + } + LogAnnotation(ev, "foo", "bar") + LogAnnotation(ev, "foo", "baz") + assert.Equal(t, "bar", ev.Annotations["foo"], "audit annotation should not be overwritten.") + + LogAnnotation(ev, "qux", "") + LogAnnotation(ev, "qux", "baz") + assert.Equal(t, "", ev.Annotations["qux"], "audit annotation should not be overwritten.") +}