Skip to content

Commit b0653ec

Browse files
dimstjungblu
authored andcommitted
UPSTREAM: 131694: Eliminate AuditContext`s SetEventLevel
Signed-off-by: Davanum Srinivas <[email protected]> Co-Authored-By: Jordan Liggitt <[email protected]> Set event level during context init Signed-off-by: Davanum Srinivas <[email protected]>
1 parent f8cdcc5 commit b0653ec

File tree

8 files changed

+21
-33
lines changed

8 files changed

+21
-33
lines changed

staging/src/k8s.io/apiserver/pkg/admission/audit_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ func TestWithAudit(t *testing.T) {
144144
var handler Interface = fakeHandler{tc.admit, tc.admitAnnotations, tc.validate, tc.validateAnnotations, tc.handles}
145145
ctx := audit.WithAuditContext(context.Background())
146146
ac := audit.AuditContextFrom(ctx)
147-
ac.SetEventLevel(auditinternal.LevelMetadata)
147+
if err := ac.Init(audit.RequestAuditConfig{Level: auditinternal.LevelMetadata}, nil); err != nil {
148+
t.Fatal(err)
149+
}
150+
148151
auditHandler := WithAudit(handler)
149152
a := attributes()
150153

@@ -186,8 +189,6 @@ func TestWithAuditConcurrency(t *testing.T) {
186189
}
187190
var handler Interface = fakeHandler{admitAnnotations: admitAnnotations, handles: true}
188191
ctx := audit.WithAuditContext(context.Background())
189-
ac := audit.AuditContextFrom(ctx)
190-
ac.SetEventLevel(auditinternal.LevelMetadata)
191192
auditHandler := WithAudit(handler)
192193
a := attributes()
193194

staging/src/k8s.io/apiserver/pkg/audit/context.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ type AuditContext struct {
4646
// initialized indicates whether requestAuditConfig and sink have been populated and are safe to read unguarded.
4747
// This should only be set via Init().
4848
initialized atomic.Bool
49-
// initialize wraps setting requestAuditConfig and sink, and is only called via Init().
50-
initialize sync.Once
5149
// requestAuditConfig is the audit configuration that applies to the request.
5250
// This should only be written via Init(RequestAuditConfig, Sink), and only read when initialized.Load() is true.
5351
requestAuditConfig RequestAuditConfig
@@ -81,16 +79,15 @@ func (ac *AuditContext) Enabled() bool {
8179
}
8280

8381
func (ac *AuditContext) Init(requestAuditConfig RequestAuditConfig, sink Sink) error {
84-
initialized := false
85-
ac.initialize.Do(func() {
86-
ac.requestAuditConfig = requestAuditConfig
87-
ac.sink = sink
88-
ac.initialized.Store(true)
89-
initialized = true
90-
})
91-
if !initialized {
82+
ac.lock.Lock()
83+
defer ac.lock.Unlock()
84+
if ac.initialized.Load() {
9285
return errors.New("audit context was already initialized")
9386
}
87+
ac.requestAuditConfig = requestAuditConfig
88+
ac.sink = sink
89+
ac.event.Level = requestAuditConfig.Level
90+
ac.initialized.Store(true)
9491
return nil
9592
}
9693

@@ -198,12 +195,6 @@ func (ac *AuditContext) GetEventLevel() auditinternal.Level {
198195
return level
199196
}
200197

201-
func (ac *AuditContext) SetEventLevel(level auditinternal.Level) {
202-
ac.visitEvent(func(event *auditinternal.Event) {
203-
event.Level = level
204-
})
205-
}
206-
207198
func (ac *AuditContext) SetEventStage(stage auditinternal.Stage) {
208199
ac.visitEvent(func(event *auditinternal.Event) {
209200
event.Stage = stage

staging/src/k8s.io/apiserver/pkg/audit/request.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const (
4040
userAgentTruncateSuffix = "...TRUNCATED"
4141
)
4242

43-
func LogRequestMetadata(ctx context.Context, req *http.Request, requestReceivedTimestamp time.Time, level auditinternal.Level, attribs authorizer.Attributes) {
43+
func LogRequestMetadata(ctx context.Context, req *http.Request, requestReceivedTimestamp time.Time, attribs authorizer.Attributes) {
4444
ac := AuditContextFrom(ctx)
4545
if !ac.Enabled() {
4646
return
@@ -51,7 +51,6 @@ func LogRequestMetadata(ctx context.Context, req *http.Request, requestReceivedT
5151
ev.Verb = attribs.GetVerb()
5252
ev.RequestURI = req.URL.RequestURI()
5353
ev.UserAgent = maybeTruncateUserAgent(req)
54-
ev.Level = level
5554

5655
ips := utilnet.SourceIPs(req)
5756
ev.SourceIPs = make([]string, len(ips))

staging/src/k8s.io/apiserver/pkg/authentication/token/cache/cached_token_authenticator.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"golang.org/x/sync/singleflight"
3434

3535
apierrors "k8s.io/apimachinery/pkg/api/errors"
36-
auditinternal "k8s.io/apiserver/pkg/apis/audit"
3736
"k8s.io/apiserver/pkg/audit"
3837
"k8s.io/apiserver/pkg/authentication/authenticator"
3938
"k8s.io/apiserver/pkg/warning"
@@ -199,9 +198,6 @@ func (a *cachedTokenAuthenticator) doAuthenticateToken(ctx context.Context, toke
199198

200199
ctx = audit.WithAuditContext(ctx)
201200
ac := audit.AuditContextFrom(ctx)
202-
// since this is shared work between multiple requests, we have no way of knowing if any
203-
// particular request supports audit annotations. thus we always attempt to record them.
204-
ac.SetEventLevel(auditinternal.LevelMetadata)
205201

206202
record.resp, record.ok, record.err = a.authenticator.AuthenticateToken(ctx, token)
207203
record.annotations = ac.GetEventAnnotations()

staging/src/k8s.io/apiserver/pkg/authentication/token/cache/cached_token_authenticator_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535

3636
utilrand "k8s.io/apimachinery/pkg/util/rand"
3737
"k8s.io/apimachinery/pkg/util/uuid"
38-
auditinternal "k8s.io/apiserver/pkg/apis/audit"
3938
"k8s.io/apiserver/pkg/audit"
4039
"k8s.io/apiserver/pkg/authentication/authenticator"
4140
"k8s.io/apiserver/pkg/authentication/user"
@@ -546,8 +545,6 @@ func (s *singleBenchmark) bench(b *testing.B) {
546545
// extraction.
547546
func withAudit(ctx context.Context) context.Context {
548547
ctx = audit.WithAuditContext(ctx)
549-
ac := audit.AuditContextFrom(ctx)
550-
ac.SetEventLevel(auditinternal.LevelMetadata)
551548
return ctx
552549
}
553550

staging/src/k8s.io/apiserver/pkg/endpoints/filters/audit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func evaluatePolicyAndCreateAuditEvent(req *http.Request, policy audit.PolicyRul
142142
if !ok {
143143
requestReceivedTimestamp = time.Now()
144144
}
145-
audit.LogRequestMetadata(ctx, req, requestReceivedTimestamp, rac.Level, attribs)
145+
audit.LogRequestMetadata(ctx, req, requestReceivedTimestamp, attribs)
146146

147147
return ac, nil
148148
}

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/delete_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ import (
3131
"k8s.io/apimachinery/pkg/runtime"
3232
"k8s.io/apimachinery/pkg/runtime/schema"
3333
"k8s.io/apimachinery/pkg/runtime/serializer"
34+
<<<<<<< HEAD
3435
auditapis "k8s.io/apiserver/pkg/apis/audit"
36+
=======
37+
"k8s.io/apiserver/pkg/admission"
38+
auditinternal "k8s.io/apiserver/pkg/apis/audit"
39+
>>>>>>> ad91dd2caa0 (UPSTREAM: 131694: Eliminate AuditContext`s SetEventLevel)
3540
"k8s.io/apiserver/pkg/audit"
3641
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation"
3742
"k8s.io/apiserver/pkg/registry/rest"
@@ -66,7 +71,9 @@ func TestDeleteResourceAuditLogRequestObject(t *testing.T) {
6671
6772
ctx := audit.WithAuditContext(context.TODO())
6873
ac := audit.AuditContextFrom(ctx)
69-
ac.SetEventLevel(auditapis.LevelRequestResponse)
74+
if err := ac.Init(audit.RequestAuditConfig{Level: auditinternal.LevelRequestResponse}, nil); err != nil {
75+
t.Fatal(err)
76+
}
7077
7178
policy := metav1.DeletePropagationBackground
7279
deleteOption := &metav1.DeleteOptions{

staging/src/k8s.io/apiserver/pkg/util/x509metrics/server_cert_deprecations_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"testing"
3131

3232
"github.com/stretchr/testify/require"
33-
auditapi "k8s.io/apiserver/pkg/apis/audit"
3433
"k8s.io/apiserver/pkg/audit"
3534
"k8s.io/component-base/metrics"
3635
"k8s.io/component-base/metrics/testutil"
@@ -247,7 +246,6 @@ func TestCheckForHostnameError(t *testing.T) {
247246
}
248247
req = req.WithContext(audit.WithAuditContext(req.Context()))
249248
auditCtx := audit.AuditContextFrom(req.Context())
250-
auditCtx.SetEventLevel(auditapi.LevelMetadata)
251249

252250
_, err = client.Transport.RoundTrip(req)
253251

@@ -390,7 +388,6 @@ func TestCheckForInsecureAlgorithmError(t *testing.T) {
390388
}
391389
req = req.WithContext(audit.WithAuditContext(req.Context()))
392390
auditCtx := audit.AuditContextFrom(req.Context())
393-
auditCtx.SetEventLevel(auditapi.LevelMetadata)
394391

395392
// can't use tlsServer.Client() as it contains the server certificate
396393
// in tls.Config.Certificates. The signatures are, however, only checked

0 commit comments

Comments
 (0)