1/*
2Copyright 2020 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package audit
18
19import (
20	"context"
21
22	genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
23)
24
25// The key type is unexported to prevent collisions
26type key int
27
28const (
29	// auditAnnotationsKey is the context key for the audit annotations.
30	auditAnnotationsKey key = iota
31)
32
33// annotations = *[]annotation instead of a map to preserve order of insertions
34type annotation struct {
35	key, value string
36}
37
38// WithAuditAnnotations returns a new context that can store audit annotations
39// via the AddAuditAnnotation function.  This function is meant to be called from
40// an early request handler to allow all later layers to set audit annotations.
41// This is required to support flows where handlers that come before WithAudit
42// (such as WithAuthentication) wish to set audit annotations.
43func WithAuditAnnotations(parent context.Context) context.Context {
44	// this should never really happen, but prevent double registration of this slice
45	if _, ok := parent.Value(auditAnnotationsKey).(*[]annotation); ok {
46		return parent
47	}
48
49	var annotations []annotation // avoid allocations until we actually need it
50	return genericapirequest.WithValue(parent, auditAnnotationsKey, &annotations)
51}
52
53// AddAuditAnnotation sets the audit annotation for the given key, value pair.
54// It is safe to call at most parts of request flow that come after WithAuditAnnotations.
55// The notable exception being that this function must not be called via a
56// defer statement (i.e. after ServeHTTP) in a handler that runs before WithAudit
57// as at that point the audit event has already been sent to the audit sink.
58// Handlers that are unaware of their position in the overall request flow should
59// prefer AddAuditAnnotation over LogAnnotation to avoid dropping annotations.
60func AddAuditAnnotation(ctx context.Context, key, value string) {
61	// use the audit event directly if we have it
62	if ae := genericapirequest.AuditEventFrom(ctx); ae != nil {
63		LogAnnotation(ae, key, value)
64		return
65	}
66
67	annotations, ok := ctx.Value(auditAnnotationsKey).(*[]annotation)
68	if !ok {
69		return // adding audit annotation is not supported at this call site
70	}
71
72	*annotations = append(*annotations, annotation{key: key, value: value})
73}
74
75// This is private to prevent reads/write to the slice from outside of this package.
76// The audit event should be directly read to get access to the annotations.
77func auditAnnotationsFrom(ctx context.Context) []annotation {
78	annotations, ok := ctx.Value(auditAnnotationsKey).(*[]annotation)
79	if !ok {
80		return nil // adding audit annotation is not supported at this call site
81	}
82
83	return *annotations
84}
85