Skip to content

fix: handle new tenant api key #3929

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

Merged
merged 1 commit into from
Jul 29, 2025
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
15 changes: 5 additions & 10 deletions internal/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,11 @@ var (

func checkHTTPHead(ctx context.Context, path string) error {
healthOnce.Do(func() {
server := utils.Config.Api.ExternalUrl
header := func(req *http.Request) {
req.Header.Add("apikey", utils.Config.Auth.AnonKey.Value)
}
client := NewKongClient()
healthClient = fetcher.NewFetcher(
server,
fetcher.WithHTTPClient(client),
fetcher.WithRequestEditor(header),
fetcher.WithExpectedStatus(http.StatusOK),
healthClient = fetcher.NewServiceGateway(
utils.Config.Api.ExternalUrl,
utils.Config.Auth.AnonKey.Value,
fetcher.WithHTTPClient(NewKongClient()),
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
)
})
// HEAD method does not return response body
Expand Down
14 changes: 6 additions & 8 deletions internal/storage/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ func NewStorageAPI(ctx context.Context, projectRef string) (storage.StorageAPI,
}

func newLocalClient() *fetcher.Fetcher {
client := status.NewKongClient()
return fetcher.NewFetcher(
return fetcher.NewServiceGateway(
utils.Config.Api.ExternalUrl,
fetcher.WithHTTPClient(client),
fetcher.WithBearerToken(utils.Config.Auth.ServiceRoleKey.Value),
utils.Config.Auth.ServiceRoleKey.Value,
fetcher.WithHTTPClient(status.NewKongClient()),
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
fetcher.WithExpectedStatus(http.StatusOK),
)
}

func newRemoteClient(projectRef, token string) *fetcher.Fetcher {
return fetcher.NewFetcher(
return fetcher.NewServiceGateway(
"https://"+utils.GetSupabaseHost(projectRef),
fetcher.WithBearerToken(token),
token,
fetcher.WithHTTPClient(http.DefaultClient),
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
fetcher.WithExpectedStatus(http.StatusOK),
)
}
48 changes: 31 additions & 17 deletions internal/utils/tenant/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package tenant

import (
"context"
"net/http"
"time"
"strings"

"github.com/go-errors/errors"
"github.com/supabase/cli/internal/utils"
Expand Down Expand Up @@ -32,16 +31,41 @@ func NewApiKey(resp []api.ApiKeyResponse) ApiKey {
if err != nil {
continue
}
if t, err := key.Type.Get(); err == nil {
switch t {
case api.ApiKeyResponseTypePublishable:
result.Anon = value
continue
case api.ApiKeyResponseTypeSecret:
if isServiceRole(key) {
result.ServiceRole = value
}
continue
}
}
switch key.Name {
case "anon":
result.Anon = value
if len(result.Anon) == 0 {
result.Anon = value
}
case "service_role":
result.ServiceRole = value
if len(result.ServiceRole) == 0 {
result.ServiceRole = value
}
}
}
return result
}

func isServiceRole(key api.ApiKeyResponse) bool {
if tmpl, err := key.SecretJwtTemplate.Get(); err == nil {
if role, ok := tmpl["role"].(string); ok {
return strings.EqualFold(role, "service_role")
}
}
return false
}

func GetApiKeys(ctx context.Context, projectRef string) (ApiKey, error) {
resp, err := utils.GetSupabase().V1GetProjectApiKeysWithResponse(ctx, projectRef, &api.V1GetProjectApiKeysParams{})
if err != nil {
Expand All @@ -62,19 +86,9 @@ type TenantAPI struct {
}

func NewTenantAPI(ctx context.Context, projectRef, anonKey string) TenantAPI {
server := "https://" + utils.GetSupabaseHost(projectRef)
client := &http.Client{
Timeout: 10 * time.Second,
}
header := func(req *http.Request) {
req.Header.Add("apikey", anonKey)
}
api := TenantAPI{Fetcher: fetcher.NewFetcher(
server,
fetcher.WithHTTPClient(client),
fetcher.WithRequestEditor(header),
return TenantAPI{Fetcher: fetcher.NewServiceGateway(
"https://"+utils.GetSupabaseHost(projectRef),
anonKey,
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
fetcher.WithExpectedStatus(http.StatusOK),
)}
return api
}
29 changes: 13 additions & 16 deletions internal/utils/tenant/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,21 @@ func TestGetApiKeys(t *testing.T) {
}

func TestNewTenantAPI(t *testing.T) {
t.Run("creates tenant api client", func(t *testing.T) {
projectRef := apitest.RandomProjectRef()
anonKey := "test-key"

api := NewTenantAPI(context.Background(), projectRef, anonKey)
projectRef := apitest.RandomProjectRef()
anonKey := "test-key"

assert.NotNil(t, api.Fetcher)
api := NewTenantAPI(context.Background(), projectRef, anonKey)
assert.NotNil(t, api.Fetcher)

defer gock.OffAll()
gock.New("https://"+utils.GetSupabaseHost(projectRef)).
Get("/test").
MatchHeader("apikey", anonKey).
MatchHeader("User-Agent", "SupabaseCLI/"+utils.Version).
Reply(http.StatusOK)
defer gock.OffAll()
gock.New("https://"+utils.GetSupabaseHost(projectRef)).
Get("/test").
MatchHeader("Authorization", "Bearer "+anonKey).
MatchHeader("User-Agent", "SupabaseCLI/"+utils.Version).
Reply(http.StatusOK)

_, err := api.Send(context.Background(), http.MethodGet, "/test", nil)
_, err := api.Send(context.Background(), http.MethodGet, "/test", nil)

assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
}
28 changes: 28 additions & 0 deletions pkg/fetcher/gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fetcher

import (
"net/http"
"strings"
"time"
)

func NewServiceGateway(server, token string, overrides ...FetcherOption) *Fetcher {
opts := append([]FetcherOption{
WithHTTPClient(&http.Client{
Timeout: 10 * time.Second,
}),
withAuthToken(token),
WithExpectedStatus(http.StatusOK),
}, overrides...)
return NewFetcher(server, opts...)
}

func withAuthToken(token string) FetcherOption {
if strings.HasPrefix(token, "sb_") {
header := func(req *http.Request) {
req.Header.Add("apikey", token)
}
return WithRequestEditor(header)
}
return WithBearerToken(token)
}