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

rpc,security: simplify and enhance the client cert validation #96207

Merged
merged 3 commits into from
Feb 1, 2023
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
26 changes: 18 additions & 8 deletions pkg/roachpb/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,34 @@ type tenantKey struct{}

// ContextWithClientTenant creates a new context with information about the
// tenant that's the client of an RPC. The tenant ID can be retrieved later with
// ClientTenantFromContext.
// ClientTenantFromContext. Use ContextWithoutClientTenant to clear the
// tenant client information from the context.
//
// An empty tenID clears the respective key from the context.
func ContextWithClientTenant(ctx context.Context, tenID TenantID) context.Context {
var val any
if tenID.IsSet() {
val = tenID
} else {
val = nil
if !tenID.IsSet() {
panic("programming error: missing tenant ID")
}

ctxTenantID, _ := ClientTenantFromContext(ctx)
if tenID == ctxTenantID {
// The context already has the right tenant, or no tenant at all.
// The context already has the right tenant.
return ctx
}

return context.WithValue(ctx, tenantKey{}, val)
return context.WithValue(ctx, tenantKey{}, tenID)
}

// ContextWithoutClientTenant removes the tenant information
// from the context.
func ContextWithoutClientTenant(ctx context.Context) context.Context {
_, ok := ClientTenantFromContext(ctx)
if !ok {
// The context already has no tenant.
return ctx
}

return context.WithValue(ctx, tenantKey{}, nil)
}

// ClientTenantFromContext returns the ID of the tenant that's the client of the
Expand Down
Loading