Skip to content

Commit bb968eb

Browse files
authored
fix: pass down apikey as query param to realtime (#4255)
* fix: pass down apikey as query param to realtime * fix: resolve jwks when initialising realtime tenant
1 parent 4da30e7 commit bb968eb

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

internal/db/start/start.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func InitSchema14(ctx context.Context, conn *pgx.Conn) error {
272272
return file.ExecBatch(ctx, conn)
273273
}
274274

275-
func initRealtimeJob(host string) utils.DockerJob {
275+
func initRealtimeJob(host, jwks string) utils.DockerJob {
276276
return utils.DockerJob{
277277
Image: utils.Config.Realtime.Image,
278278
Env: []string{
@@ -284,6 +284,7 @@ func initRealtimeJob(host string) utils.DockerJob {
284284
"DB_NAME=postgres",
285285
"DB_AFTER_CONNECT_QUERY=SET search_path TO _realtime",
286286
"DB_ENC_KEY=" + utils.Config.Realtime.EncryptionKey,
287+
fmt.Sprintf("API_JWT_JWKS=%s", jwks),
287288
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
288289
"METRICS_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
289290
"APP_NAME=realtime",
@@ -341,7 +342,11 @@ func initSchema15(ctx context.Context, host string) error {
341342
// Apply service migrations
342343
var initJobs []utils.DockerJob
343344
if utils.Config.Realtime.Enabled {
344-
initJobs = append(initJobs, initRealtimeJob(host))
345+
jwks, err := utils.Config.Auth.ResolveJWKS(context.Background())
346+
if err != nil {
347+
return err
348+
}
349+
initJobs = append(initJobs, initRealtimeJob(host, jwks))
345350
}
346351
if utils.Config.Storage.Enabled {
347352
initJobs = append(initJobs, initStorageJob(host))

internal/start/start.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type kongConfig struct {
8787
ApiHost string
8888
ApiPort uint16
8989
BearerToken string
90+
QueryToken string
9091
}
9192

9293
var (
@@ -145,7 +146,7 @@ func run(ctx context.Context, fsys afero.Fs, excludedContainers []string, dbConf
145146
excluded[name] = true
146147
}
147148

148-
jwks, err := utils.Config.Auth.ResolveJWKS(ctx, fsys)
149+
jwks, err := utils.Config.Auth.ResolveJWKS(ctx)
149150
if err != nil {
150151
return err
151152
}
@@ -362,6 +363,13 @@ EOF
362363
utils.Config.Auth.PublishableKey.Value,
363364
utils.Config.Auth.AnonKey.Value,
364365
),
366+
QueryToken: fmt.Sprintf(
367+
`$((function() return (query_params.apikey == '%s' and '%s') or (query_params.apikey == '%s' and '%s') or query_params.apikey end)())`,
368+
utils.Config.Auth.SecretKey.Value,
369+
utils.Config.Auth.ServiceRoleKey.Value,
370+
utils.Config.Auth.PublishableKey.Value,
371+
utils.Config.Auth.AnonKey.Value,
372+
),
365373
}); err != nil {
366374
return errors.Errorf("failed to exec template: %w", err)
367375
}

internal/start/templates/kong.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ services:
132132
- name: request-transformer
133133
config:
134134
replace:
135-
headers:
136-
- "Authorization: {{ .BearerToken }}"
137-
- name: realtime-v1-longpoll
135+
querystring:
136+
- "apikey:{{ .QueryToken }}"
137+
- name: realtime-v1-longpoll-not-working
138138
_comment: "Realtime: /realtime/v1/* -> ws://realtime:4000/socket/longpoll"
139139
url: http://{{ .RealtimeId }}:4000/socket
140140
protocol: http

pkg/config/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/go-errors/errors"
2929
"github.com/go-viper/mapstructure/v2"
3030
"github.com/joho/godotenv"
31-
"github.com/spf13/afero"
3231
"github.com/spf13/viper"
3332
"github.com/supabase/cli/pkg/cast"
3433
"github.com/supabase/cli/pkg/fetcher"
@@ -1428,7 +1427,7 @@ type (
14281427
// ResolveJWKS creates the JWKS from the JWT secret and Third-Party Auth
14291428
// configs by resolving the JWKS via the OIDC discovery URL.
14301429
// It always returns a JWKS string, except when there's an error fetching.
1431-
func (a *auth) ResolveJWKS(ctx context.Context, fsys afero.Fs) (string, error) {
1430+
func (a *auth) ResolveJWKS(ctx context.Context) (string, error) {
14321431
var jwks remoteJWKS
14331432

14341433
if issuerURL := a.ThirdParty.IssuerURL(); issuerURL != "" {

0 commit comments

Comments
 (0)