Skip to content

Commit fe72506

Browse files
committed
feat: optimize multi tenancy evaluation to happen only once per event
1 parent 753fc7a commit fe72506

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

internal/indexer/consumer.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type auditEvent struct {
3333
} `json:"objectRef"`
3434
ResponseObject map[string]any `json:"responseObject"`
3535
// User carries authenticated user information including tenant context in extra fields.
36-
User struct {
36+
User *struct {
3737
Extra map[string][]string `json:"extra,omitempty"`
3838
} `json:"user,omitempty"`
3939
}
@@ -45,7 +45,7 @@ func extractTenantFromAuditEvent(event *auditEvent) (tenantName string, tenantTy
4545
tenantName = "platform"
4646
tenantType = "platform"
4747

48-
if event.User.Extra == nil {
48+
if event.User == nil || event.User.Extra == nil {
4949
return
5050
}
5151

@@ -134,6 +134,14 @@ func (i *Indexer) Start(ctx context.Context) error {
134134
return
135135
}
136136

137+
// In single-tenant mode, skip non-platform
138+
// events entirely so that no policy can accidentally queue them.
139+
tenantName, tenantType := extractTenantFromAuditEvent(&event)
140+
if !i.multiTenant && tenantType != tenantTypePlatform {
141+
msg.Ack()
142+
return
143+
}
144+
137145
queued := false
138146

139147
policies := i.policyCache.GetPolicies()
@@ -155,14 +163,9 @@ func (i *Indexer) Start(ctx context.Context) error {
155163
continue
156164
}
157165

158-
// Always extract tenant context from the audit event's user extra fields.
159-
// In single-tenant mode, skip non-platform resources so that project-tenant
160-
// resources are never incorrectly indexed as platform resources.
161-
evalResult.Tenant, evalResult.TenantType = extractTenantFromAuditEvent(&event)
162-
if !i.multiTenant && evalResult.TenantType != tenantTypePlatform {
163-
msg.Ack()
164-
continue
165-
}
166+
// Attach the already-extracted tenant context to the eval result.
167+
evalResult.Tenant = tenantName
168+
evalResult.TenantType = tenantType
166169

167170
// Transform the matching resource into an indexable document
168171
doc := evalResult.Transform()

internal/indexer/consumer_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,13 @@ func TestIndexer_Start_ConsumeFlow(t *testing.T) {
159159

160160
func TestExtractTenantFromAuditEvent_WithUserExtra(t *testing.T) {
161161
event := &auditEvent{}
162-
event.User.Extra = map[string][]string{
163-
"iam.miloapis.com/parent-type": {"project"},
164-
"iam.miloapis.com/parent-name": {"my-project"},
162+
event.User = &struct {
163+
Extra map[string][]string `json:"extra,omitempty"`
164+
}{
165+
Extra: map[string][]string{
166+
"iam.miloapis.com/parent-type": {"project"},
167+
"iam.miloapis.com/parent-name": {"my-project"},
168+
},
165169
}
166170

167171
name, typ := extractTenantFromAuditEvent(event)
@@ -192,8 +196,12 @@ func TestExtractTenantFromAuditEvent_PartialUserExtra_TypeOnlyNoName(t *testing.
192196
// Only parent-type is set; parent-name is absent.
193197
// Expect: tenantType reflects the extra field, tenantName falls back to "platform".
194198
event := &auditEvent{}
195-
event.User.Extra = map[string][]string{
196-
"iam.miloapis.com/parent-type": {"project"},
199+
event.User = &struct {
200+
Extra map[string][]string `json:"extra,omitempty"`
201+
}{
202+
Extra: map[string][]string{
203+
"iam.miloapis.com/parent-type": {"project"},
204+
},
197205
}
198206

199207
name, typ := extractTenantFromAuditEvent(event)
@@ -209,9 +217,13 @@ func TestExtractTenantFromAuditEvent_PartialUserExtra_TypeOnlyNoName(t *testing.
209217
func TestExtractTenantFromAuditEvent_EmptySliceValues(t *testing.T) {
210218
// Keys present but with empty slices should not override the defaults.
211219
event := &auditEvent{}
212-
event.User.Extra = map[string][]string{
213-
"iam.miloapis.com/parent-type": {},
214-
"iam.miloapis.com/parent-name": {},
220+
event.User = &struct {
221+
Extra map[string][]string `json:"extra,omitempty"`
222+
}{
223+
Extra: map[string][]string{
224+
"iam.miloapis.com/parent-type": {},
225+
"iam.miloapis.com/parent-name": {},
226+
},
215227
}
216228

217229
name, typ := extractTenantFromAuditEvent(event)

0 commit comments

Comments
 (0)