Skip to content

Commit 32727c3

Browse files
committed
fix: handle new tenant api key
1 parent 8f8ddb2 commit 32727c3

File tree

4 files changed

+71
-33
lines changed

4 files changed

+71
-33
lines changed

internal/status/status.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,11 @@ var (
176176

177177
func checkHTTPHead(ctx context.Context, path string) error {
178178
healthOnce.Do(func() {
179-
server := utils.Config.Api.ExternalUrl
180-
header := func(req *http.Request) {
181-
req.Header.Add("apikey", utils.Config.Auth.AnonKey.Value)
182-
}
183-
client := NewKongClient()
184-
healthClient = fetcher.NewFetcher(
185-
server,
186-
fetcher.WithHTTPClient(client),
187-
fetcher.WithRequestEditor(header),
188-
fetcher.WithExpectedStatus(http.StatusOK),
179+
healthClient = fetcher.NewServiceGateway(
180+
utils.Config.Api.ExternalUrl,
181+
utils.Config.Auth.AnonKey.Value,
182+
fetcher.WithHTTPClient(NewKongClient()),
183+
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
189184
)
190185
})
191186
// HEAD method does not return response body

internal/storage/client/api.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@ func NewStorageAPI(ctx context.Context, projectRef string) (storage.StorageAPI,
2828
}
2929

3030
func newLocalClient() *fetcher.Fetcher {
31-
client := status.NewKongClient()
32-
return fetcher.NewFetcher(
31+
return fetcher.NewServiceGateway(
3332
utils.Config.Api.ExternalUrl,
34-
fetcher.WithHTTPClient(client),
35-
fetcher.WithBearerToken(utils.Config.Auth.ServiceRoleKey.Value),
33+
utils.Config.Auth.ServiceRoleKey.Value,
34+
fetcher.WithHTTPClient(status.NewKongClient()),
3635
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
37-
fetcher.WithExpectedStatus(http.StatusOK),
3836
)
3937
}
4038

4139
func newRemoteClient(projectRef, token string) *fetcher.Fetcher {
42-
return fetcher.NewFetcher(
40+
return fetcher.NewServiceGateway(
4341
"https://"+utils.GetSupabaseHost(projectRef),
44-
fetcher.WithBearerToken(token),
42+
token,
43+
fetcher.WithHTTPClient(http.DefaultClient),
4544
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
46-
fetcher.WithExpectedStatus(http.StatusOK),
4745
)
4846
}

internal/utils/tenant/client.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package tenant
22

33
import (
44
"context"
5-
"net/http"
6-
"time"
5+
"strings"
76

87
"github.com/go-errors/errors"
98
"github.com/supabase/cli/internal/utils"
@@ -39,9 +38,37 @@ func NewApiKey(resp []api.ApiKeyResponse) ApiKey {
3938
result.ServiceRole = value
4039
}
4140
}
41+
// Apply new keys
42+
for _, key := range resp {
43+
t, err := key.Type.Get()
44+
if err != nil {
45+
continue
46+
}
47+
value, err := key.ApiKey.Get()
48+
if err != nil {
49+
continue
50+
}
51+
switch t {
52+
case api.ApiKeyResponseTypePublishable:
53+
result.Anon = value
54+
case api.ApiKeyResponseTypeSecret:
55+
if isServiceRole(key) {
56+
result.ServiceRole = value
57+
}
58+
}
59+
}
4260
return result
4361
}
4462

63+
func isServiceRole(key api.ApiKeyResponse) bool {
64+
if tmpl, err := key.SecretJwtTemplate.Get(); err == nil {
65+
if role, ok := tmpl["role"].(string); ok {
66+
return strings.EqualFold(role, "service_role")
67+
}
68+
}
69+
return false
70+
}
71+
4572
func GetApiKeys(ctx context.Context, projectRef string) (ApiKey, error) {
4673
resp, err := utils.GetSupabase().V1GetProjectApiKeysWithResponse(ctx, projectRef, &api.V1GetProjectApiKeysParams{})
4774
if err != nil {
@@ -62,19 +89,9 @@ type TenantAPI struct {
6289
}
6390

6491
func NewTenantAPI(ctx context.Context, projectRef, anonKey string) TenantAPI {
65-
server := "https://" + utils.GetSupabaseHost(projectRef)
66-
client := &http.Client{
67-
Timeout: 10 * time.Second,
68-
}
69-
header := func(req *http.Request) {
70-
req.Header.Add("apikey", anonKey)
71-
}
72-
api := TenantAPI{Fetcher: fetcher.NewFetcher(
73-
server,
74-
fetcher.WithHTTPClient(client),
75-
fetcher.WithRequestEditor(header),
92+
return TenantAPI{Fetcher: fetcher.NewServiceGateway(
93+
"https://"+utils.GetSupabaseHost(projectRef),
94+
anonKey,
7695
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
77-
fetcher.WithExpectedStatus(http.StatusOK),
7896
)}
79-
return api
8097
}

pkg/fetcher/gateway.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package fetcher
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
"time"
7+
)
8+
9+
func NewServiceGateway(server, token string, overrides ...FetcherOption) *Fetcher {
10+
opts := append([]FetcherOption{
11+
WithHTTPClient(&http.Client{
12+
Timeout: 10 * time.Second,
13+
}),
14+
withAuthToken(token),
15+
WithExpectedStatus(http.StatusOK),
16+
}, overrides...)
17+
return NewFetcher(server, opts...)
18+
}
19+
20+
func withAuthToken(token string) FetcherOption {
21+
if strings.HasPrefix(token, "sb_") {
22+
header := func(req *http.Request) {
23+
req.Header.Add("apikey", token)
24+
}
25+
return WithRequestEditor(header)
26+
}
27+
return WithBearerToken(token)
28+
}

0 commit comments

Comments
 (0)