From 4fc698b0e06a5159b775fa4672c7c1e4ea9131e4 Mon Sep 17 00:00:00 2001 From: msukkari Date: Tue, 14 Jan 2025 13:31:48 -0800 Subject: [PATCH 1/5] pipe tenant id to restful search interface --- internal/tenant/grpc.go | 2 +- internal/tenant/rest_helper.go | 27 +++++++++++++++++++++++++++ json/json.go | 7 +++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 internal/tenant/rest_helper.go diff --git a/internal/tenant/grpc.go b/internal/tenant/grpc.go index 774c216dd..d5c4f4086 100644 --- a/internal/tenant/grpc.go +++ b/internal/tenant/grpc.go @@ -19,7 +19,7 @@ import ( const ( // headerKeyTenantID is the header key for the tenant ID. - headerKeyTenantID = "X-Sourcegraph-Tenant-ID" + headerKeyTenantID = "X-Tenant-ID" // headerValueNoTenant indicates the request has no tenant. headerValueNoTenant = "none" diff --git a/internal/tenant/rest_helper.go b/internal/tenant/rest_helper.go new file mode 100644 index 000000000..9166168f2 --- /dev/null +++ b/internal/tenant/rest_helper.go @@ -0,0 +1,27 @@ +package tenant + +import ( + "context" + "fmt" + "log" + "net/http" + + "github.com/sourcegraph/zoekt/internal/tenant/internal/tenanttype" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func InjectTenantFromHeader(ctx context.Context, header http.Header) (context.Context, error) { + log.Printf("header: %v", header) + tenantID := header.Get(headerKeyTenantID) + log.Printf("tenantID: %s", tenantID) + if tenantID != "" { + tenant, err := tenanttype.Unmarshal(tenantID) + if err != nil { + return ctx, status.New(codes.InvalidArgument, fmt.Errorf("bad tenant value in header: %w", err).Error()).Err() + } + + return tenanttype.WithTenant(ctx, tenant), nil + } + return ctx, nil +} diff --git a/json/json.go b/json/json.go index 80b47348d..7936ddee4 100644 --- a/json/json.go +++ b/json/json.go @@ -7,6 +7,7 @@ import ( "time" "github.com/sourcegraph/zoekt" + "github.com/sourcegraph/zoekt/internal/tenant" "github.com/sourcegraph/zoekt/query" ) @@ -85,6 +86,12 @@ func (s *jsonSearcher) jsonSearch(w http.ResponseWriter, req *http.Request) { defer cancel() } + ctx, err = tenant.InjectTenantFromHeader(ctx, req.Header) + if err != nil { + jsonError(w, http.StatusBadRequest, err.Error()) + return + } + if err := CalculateDefaultSearchLimits(ctx, q, s.Searcher, searchArgs.Opts); err != nil { jsonError(w, http.StatusInternalServerError, err.Error()) return From 01ad7a76fdc5f03ab3fd43553460c01d0782cb8f Mon Sep 17 00:00:00 2001 From: msukkari Date: Tue, 14 Jan 2025 15:15:53 -0800 Subject: [PATCH 2/5] change tenant header convention --- internal/tenant/grpc.go | 2 +- internal/tenant/rest_helper.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/tenant/grpc.go b/internal/tenant/grpc.go index d5c4f4086..774c216dd 100644 --- a/internal/tenant/grpc.go +++ b/internal/tenant/grpc.go @@ -19,7 +19,7 @@ import ( const ( // headerKeyTenantID is the header key for the tenant ID. - headerKeyTenantID = "X-Tenant-ID" + headerKeyTenantID = "X-Sourcegraph-Tenant-ID" // headerValueNoTenant indicates the request has no tenant. headerValueNoTenant = "none" diff --git a/internal/tenant/rest_helper.go b/internal/tenant/rest_helper.go index 9166168f2..ad4076d07 100644 --- a/internal/tenant/rest_helper.go +++ b/internal/tenant/rest_helper.go @@ -13,7 +13,7 @@ import ( func InjectTenantFromHeader(ctx context.Context, header http.Header) (context.Context, error) { log.Printf("header: %v", header) - tenantID := header.Get(headerKeyTenantID) + tenantID := header.Get("X-TENANT-ID") // TODO: we don't use headerKeyTenantID here so we don't need to change it and potentially break future grpc changes log.Printf("tenantID: %s", tenantID) if tenantID != "" { tenant, err := tenanttype.Unmarshal(tenantID) From bef774983ff59f3e24b3e515c3a93683ac2d799f Mon Sep 17 00:00:00 2001 From: msukkari Date: Tue, 14 Jan 2025 15:44:37 -0800 Subject: [PATCH 3/5] remove unneeeded logs --- internal/tenant/rest_helper.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/internal/tenant/rest_helper.go b/internal/tenant/rest_helper.go index ad4076d07..e5809a8cb 100644 --- a/internal/tenant/rest_helper.go +++ b/internal/tenant/rest_helper.go @@ -3,7 +3,6 @@ package tenant import ( "context" "fmt" - "log" "net/http" "github.com/sourcegraph/zoekt/internal/tenant/internal/tenanttype" @@ -12,9 +11,7 @@ import ( ) func InjectTenantFromHeader(ctx context.Context, header http.Header) (context.Context, error) { - log.Printf("header: %v", header) tenantID := header.Get("X-TENANT-ID") // TODO: we don't use headerKeyTenantID here so we don't need to change it and potentially break future grpc changes - log.Printf("tenantID: %s", tenantID) if tenantID != "" { tenant, err := tenanttype.Unmarshal(tenantID) if err != nil { From 6cb8ef57efdb175095668bf392b45c0e78e15457 Mon Sep 17 00:00:00 2001 From: msukkari Date: Wed, 15 Jan 2025 14:30:22 -0800 Subject: [PATCH 4/5] add DS_STORE to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 054270c28..0627a45b2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ bazel-bin bazel-out bazel-testlogs bazel-zoekt + +.DS_STORE From 123cf926812a0284f7bf1056160b9e7e4c2c7a2f Mon Sep 17 00:00:00 2001 From: msukkari Date: Mon, 27 Jan 2025 17:01:00 -0800 Subject: [PATCH 5/5] pipe tenant id in list repo endpoint --- json/json.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/json/json.go b/json/json.go index 7936ddee4..c4a0d1cf6 100644 --- a/json/json.go +++ b/json/json.go @@ -153,6 +153,7 @@ func CalculateDefaultSearchLimits(ctx context.Context, } func (s *jsonSearcher) jsonList(w http.ResponseWriter, req *http.Request) { + ctx := req.Context() w.Header().Add("Content-Type", "application/json") if req.Method != "POST" { @@ -173,7 +174,13 @@ func (s *jsonSearcher) jsonList(w http.ResponseWriter, req *http.Request) { return } - listResult, err := s.Searcher.List(req.Context(), query, listArgs.Opts) + ctx, err = tenant.InjectTenantFromHeader(ctx, req.Header) + if err != nil { + jsonError(w, http.StatusBadRequest, err.Error()) + return + } + + listResult, err := s.Searcher.List(ctx, query, listArgs.Opts) if err != nil { jsonError(w, http.StatusInternalServerError, err.Error()) return