Skip to content

Commit f8cdcc5

Browse files
sxllwxliggitt
authored andcommitted
UPSTREAM: 129472: Fix API server crash on concurrent map iteration and write
Improve audit context handling by encapsulating event data and operations behind a structured API. Make the Audit system more robust in concurrent environments by properly isolating mutable state. The cleaner API simplifies interaction with audit events, improving maintainability. Encapsulation reduces bugs by preventing direct manipulation of audit events. Signed-off-by: Davanum Srinivas <[email protected]> Co-Authored-By: Jordan Liggitt <[email protected]> Co-Authored-By: sxllwx <[email protected]>
1 parent e2e5d62 commit f8cdcc5

File tree

18 files changed

+663
-302
lines changed

18 files changed

+663
-302
lines changed

pkg/registry/core/serviceaccount/storage/storage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestCreate_Token_SetsCredentialIDAuditAnnotation(t *testing.T) {
138138
}
139139

140140
auditContext := audit.AuditContextFrom(ctx)
141-
issuedCredentialID, ok := auditContext.Event.Annotations["authentication.kubernetes.io/issued-credential-id"]
141+
issuedCredentialID, ok := auditContext.GetEventAnnotation("authentication.kubernetes.io/issued-credential-id")
142142
if !ok || len(issuedCredentialID) == 0 {
143143
t.Errorf("did not find issued-credential-id in audit event annotations")
144144
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ func ensureAnnotationGetter(a Attributes) error {
8383
}
8484

8585
func (handler *auditHandler) logAnnotations(ctx context.Context, a Attributes) {
86-
ae := audit.AuditEventFrom(ctx)
86+
ae := audit.AuditContextFrom(ctx)
8787
if ae == nil {
8888
return
8989
}
9090

9191
var annotations map[string]string
9292
switch a := a.(type) {
9393
case privateAnnotationsGetter:
94-
annotations = a.getAnnotations(ae.Level)
94+
annotations = a.getAnnotations(ae.GetEventLevel())
9595
case AnnotationsGetter:
96-
annotations = a.GetAnnotations(ae.Level)
96+
annotations = a.GetAnnotations(ae.GetEventLevel())
9797
default:
9898
// this will never happen, because we have already checked it in ensureAnnotationGetter
9999
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ 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-
ae := &ac.Event
148-
ae.Level = auditinternal.LevelMetadata
147+
ac.SetEventLevel(auditinternal.LevelMetadata)
149148
auditHandler := WithAudit(handler)
150149
a := attributes()
151150

@@ -171,9 +170,9 @@ func TestWithAudit(t *testing.T) {
171170
annotations[k] = v
172171
}
173172
if len(annotations) == 0 {
174-
assert.Nil(t, ae.Annotations, tcName+": unexptected annotations set in audit event")
173+
assert.Nil(t, ac.GetEventAnnotations(), tcName+": unexptected annotations set in audit event")
175174
} else {
176-
assert.Equal(t, annotations, ae.Annotations, tcName+": unexptected annotations set in audit event")
175+
assert.Equal(t, annotations, ac.GetEventAnnotations(), tcName+": unexptected annotations set in audit event")
177176
}
178177
}
179178
}
@@ -188,7 +187,7 @@ func TestWithAuditConcurrency(t *testing.T) {
188187
var handler Interface = fakeHandler{admitAnnotations: admitAnnotations, handles: true}
189188
ctx := audit.WithAuditContext(context.Background())
190189
ac := audit.AuditContextFrom(ctx)
191-
ac.Event.Level = auditinternal.LevelMetadata
190+
ac.SetEventLevel(auditinternal.LevelMetadata)
192191
auditHandler := WithAudit(handler)
193192
a := attributes()
194193

0 commit comments

Comments
 (0)