From bc8f4ffaa611b638fc821bbcd553d6f99c8fa3ab Mon Sep 17 00:00:00 2001 From: Keegan Carruthers-Smith Date: Mon, 19 May 2025 20:09:35 +0200 Subject: [PATCH] tenant: unexport EnforceTenant (#957) The only place this was called outside of the tenant package was for deciding if we should call tenant.Log. However, we can inline that check and both simplify the tenant exported API as well as how you call tenant.Log. Test Plan: CI --- internal/tenant/context.go | 4 ++++ internal/tenant/enforcement.go | 2 +- internal/tenant/query.go | 2 +- search/eval.go | 12 +++--------- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/internal/tenant/context.go b/internal/tenant/context.go index 26fb778..9ddace6 100644 --- a/internal/tenant/context.go +++ b/internal/tenant/context.go @@ -27,6 +27,10 @@ func FromContext(ctx context.Context) (*tenanttype.Tenant, error) { // Log logs the tenant ID to the trace. If tenant logging is enabled, it also // logs a stack trace to a pprof profile. func Log(ctx context.Context, tr *trace.Trace) { + if !enforceTenant() { + return + } + if systemtenant.Is(ctx) { tr.LazyPrintf("tenant: system") return diff --git a/internal/tenant/enforcement.go b/internal/tenant/enforcement.go index 1540cd9..3e65b77 100644 --- a/internal/tenant/enforcement.go +++ b/internal/tenant/enforcement.go @@ -6,7 +6,7 @@ import ( "github.com/sourcegraph/zoekt/internal/tenant/internal/enforcement" ) -func EnforceTenant() bool { +func enforceTenant() bool { switch enforcement.EnforcementMode.Load() { case "strict": return true diff --git a/internal/tenant/query.go b/internal/tenant/query.go index 3f56cb7..bc8a386 100644 --- a/internal/tenant/query.go +++ b/internal/tenant/query.go @@ -9,7 +9,7 @@ import ( // HasAccess returns true if the tenant ID in the context matches the // given ID. If tenant enforcement is disabled, it always returns true. func HasAccess(ctx context.Context, id int) bool { - if !EnforceTenant() { + if !enforceTenant() { return true } if systemtenant.Is(ctx) { diff --git a/search/eval.go b/search/eval.go index 26220b8..25ebe2c 100644 --- a/search/eval.go +++ b/search/eval.go @@ -20,9 +20,7 @@ func (s *typeRepoSearcher) Search(ctx context.Context, q query.Q, opts *zoekt.Se tr, ctx := trace.New(ctx, "typeRepoSearcher.Search", "") tr.LazyLog(q, true) tr.LazyPrintf("opts: %+v", opts) - if tenant.EnforceTenant() { - tenant.Log(ctx, tr) - } + tenant.Log(ctx, tr) defer func() { if sr != nil { tr.LazyPrintf("num files: %d", len(sr.Files)) @@ -47,9 +45,7 @@ func (s *typeRepoSearcher) StreamSearch(ctx context.Context, q query.Q, opts *zo tr, ctx := trace.New(ctx, "typeRepoSearcher.StreamSearch", "") tr.LazyLog(q, true) tr.LazyPrintf("opts: %+v", opts) - if tenant.EnforceTenant() { - tenant.Log(ctx, tr) - } + tenant.Log(ctx, tr) var stats zoekt.Stats defer func() { tr.LazyPrintf("stats: %+v", stats) @@ -75,9 +71,7 @@ func (s *typeRepoSearcher) List(ctx context.Context, q query.Q, opts *zoekt.List tr, ctx := trace.New(ctx, "typeRepoSearcher.List", "") tr.LazyLog(q, true) tr.LazyPrintf("opts: %s", opts) - if tenant.EnforceTenant() { - tenant.Log(ctx, tr) - } + tenant.Log(ctx, tr) defer func() { if rl != nil { tr.LazyPrintf("repos.size=%d reposmap.size=%d crashes=%d stats=%+v", len(rl.Repos), len(rl.ReposMap), rl.Crashes, rl.Stats) -- 2.51.2