Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tenant: introduce systemtenant #863

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ nextFileMatch:

// 🚨 SECURITY: Skip documents that don't belong to the tenant. This check is
// necessary to prevent leaking data across tenants.
if !tenant.EqualsID(ctx, repoMetadata.TenantID) {
if !tenant.HasAccess(ctx, repoMetadata.TenantID) {
continue
}

Expand Down Expand Up @@ -624,7 +624,7 @@ func (d *indexData) List(ctx context.Context, q query.Q, opts *ListOptions) (rl
}
// 🚨 SECURITY: Skip documents that don't belong to the tenant. This check is
// necessary to prevent leaking data across tenants.
if !tenant.EqualsID(ctx, d.repoMetaData[i].TenantID) {
if !tenant.HasAccess(ctx, d.repoMetaData[i].TenantID) {
continue
}
rle := &d.repoListEntry[i]
Expand Down
9 changes: 7 additions & 2 deletions internal/tenant/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package tenant

import (
"context"

"github.com/sourcegraph/zoekt/internal/tenant/systemtenant"
)

// EqualsID returns true if the tenant ID in the context matches the
// HasAccess returns true if the tenant ID in the context matches the
// given ID. If tenant enforcement is disabled, it always returns true.
func EqualsID(ctx context.Context, id int) bool {
func HasAccess(ctx context.Context, id int) bool {
if !EnforceTenant() {
return true
}
if systemtenant.Is(ctx) {
return true
}
t, err := FromContext(ctx)
if err != nil {
return false
Expand Down
22 changes: 22 additions & 0 deletions internal/tenant/systemtenant/systemtenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Package systemtenant exports UnsafeCtx which allows to access shards across
// all tenants. This must only be used for tasks that are not request specific.
package systemtenant

import (
"context"
)

type contextKey int

const systemTenantKey contextKey = iota

// UnsafeCtx is a context that allows queries across all tenants. Don't use this
// for user requests.
var UnsafeCtx = context.WithValue(context.Background(), systemTenantKey, systemTenantKey)

// Is returns true if the context has been marked to allow queries across all
// tenants.
func Is(ctx context.Context) bool {
_, ok := ctx.Value(systemTenantKey).(contextKey)
return ok
}
15 changes: 15 additions & 0 deletions internal/tenant/systemtenant/systemtenant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package systemtenant

import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

func TestSystemtenantRoundtrip(t *testing.T) {
if Is(context.Background()) {
t.Fatal()
}
require.True(t, Is(UnsafeCtx))
}
6 changes: 5 additions & 1 deletion shards/shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"go.uber.org/atomic"

"github.com/sourcegraph/zoekt"
"github.com/sourcegraph/zoekt/internal/tenant/systemtenant"
"github.com/sourcegraph/zoekt/query"
"github.com/sourcegraph/zoekt/trace"
)
Expand Down Expand Up @@ -1064,7 +1065,10 @@ func (s *shardedSearcher) getLoaded() loaded {

func mkRankedShard(s zoekt.Searcher) *rankedShard {
q := query.Const{Value: true}
result, err := s.List(context.Background(), &q, nil)
// We need to use UnsafeCtx here, otherwise we cannot return a proper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be a way to bypass List and load the repo metadata directly? So this just obviously becomes a low-level, privileged operation. This would introducing systemtenant just for this single privileged call. Or maybe it's not possible to refactor things to support this 🤔

I'm not an expert on this code and totally lack context, so just ignore me if this not helpful :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial thoughts was around this, but I couldn't think of a nice way. EG we want to take into account tombstones/etc and anything else we may add to List in the future. So using List feels like the cleanest approach.

// rankedShard. On the user request path we use selectRepoSet which relies on
// rankedShard.repos being set.
result, err := s.List(systemtenant.UnsafeCtx, &q, nil)
stefanhengl marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return &rankedShard{Searcher: s}
}
Expand Down
Loading