Skip to content

Commit 4fc698b

Browse files
committed
pipe tenant id to restful search interface
1 parent 685a5d2 commit 4fc698b

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

internal/tenant/grpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
const (
2121
// headerKeyTenantID is the header key for the tenant ID.
22-
headerKeyTenantID = "X-Sourcegraph-Tenant-ID"
22+
headerKeyTenantID = "X-Tenant-ID"
2323

2424
// headerValueNoTenant indicates the request has no tenant.
2525
headerValueNoTenant = "none"

internal/tenant/rest_helper.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tenant
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/sourcegraph/zoekt/internal/tenant/internal/tenanttype"
10+
"google.golang.org/grpc/codes"
11+
"google.golang.org/grpc/status"
12+
)
13+
14+
func InjectTenantFromHeader(ctx context.Context, header http.Header) (context.Context, error) {
15+
log.Printf("header: %v", header)
16+
tenantID := header.Get(headerKeyTenantID)
17+
log.Printf("tenantID: %s", tenantID)
18+
if tenantID != "" {
19+
tenant, err := tenanttype.Unmarshal(tenantID)
20+
if err != nil {
21+
return ctx, status.New(codes.InvalidArgument, fmt.Errorf("bad tenant value in header: %w", err).Error()).Err()
22+
}
23+
24+
return tenanttype.WithTenant(ctx, tenant), nil
25+
}
26+
return ctx, nil
27+
}

json/json.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/sourcegraph/zoekt"
10+
"github.com/sourcegraph/zoekt/internal/tenant"
1011
"github.com/sourcegraph/zoekt/query"
1112
)
1213

@@ -85,6 +86,12 @@ func (s *jsonSearcher) jsonSearch(w http.ResponseWriter, req *http.Request) {
8586
defer cancel()
8687
}
8788

89+
ctx, err = tenant.InjectTenantFromHeader(ctx, req.Header)
90+
if err != nil {
91+
jsonError(w, http.StatusBadRequest, err.Error())
92+
return
93+
}
94+
8895
if err := CalculateDefaultSearchLimits(ctx, q, s.Searcher, searchArgs.Opts); err != nil {
8996
jsonError(w, http.StatusInternalServerError, err.Error())
9097
return

0 commit comments

Comments
 (0)