Skip to content

Commit 2411339

Browse files
authored
fix: load config once using viper (#3367)
1 parent 19a7c33 commit 2411339

24 files changed

+154
-135
lines changed

internal/db/start/start.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func NewContainerConfig() container.Config {
6363
env := []string{
6464
"POSTGRES_PASSWORD=" + utils.Config.Db.Password,
6565
"POSTGRES_HOST=/var/run/postgresql",
66-
"JWT_SECRET=" + utils.Config.Auth.JwtSecret,
66+
"JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
6767
fmt.Sprintf("JWT_EXP=%d", utils.Config.Auth.JwtExpiry),
6868
}
6969
if len(utils.Config.Experimental.OrioleDBVersion) > 0 {
@@ -96,7 +96,7 @@ docker-entrypoint.sh postgres -D /etc/postgresql
9696
` + webhookSchema + `
9797
` + _supabaseSchema + `
9898
EOF
99-
` + utils.Config.Db.RootKey + `
99+
` + utils.Config.Db.RootKey.Value + `
100100
EOF
101101
` + utils.Config.Db.Settings.ToPostgresConfig() + `
102102
EOF`},
@@ -157,7 +157,7 @@ docker-entrypoint.sh postgres -D /etc/postgresql
157157
EOF
158158
` + restoreScript + `
159159
EOF
160-
` + utils.Config.Db.RootKey + `
160+
` + utils.Config.Db.RootKey.Value + `
161161
EOF
162162
` + utils.Config.Db.Settings.ToPostgresConfig() + `
163163
EOF`}
@@ -284,8 +284,8 @@ 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-
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
288-
"METRICS_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
287+
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
288+
"METRICS_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
289289
"APP_NAME=realtime",
290290
"SECRET_KEY_BASE=" + utils.Config.Realtime.SecretKeyBase,
291291
"ERL_AFLAGS=" + utils.ToRealtimeEnv(utils.Config.Realtime.IpVersion),
@@ -305,9 +305,9 @@ func initStorageJob(host string) utils.DockerJob {
305305
Image: utils.Config.Storage.Image,
306306
Env: []string{
307307
"DB_INSTALL_ROLES=false",
308-
"ANON_KEY=" + utils.Config.Auth.AnonKey,
309-
"SERVICE_KEY=" + utils.Config.Auth.ServiceRoleKey,
310-
"PGRST_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
308+
"ANON_KEY=" + utils.Config.Auth.AnonKey.Value,
309+
"SERVICE_KEY=" + utils.Config.Auth.ServiceRoleKey.Value,
310+
"PGRST_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
311311
fmt.Sprintf("DATABASE_URL=postgresql://supabase_storage_admin:%s@%s:5432/postgres", utils.Config.Db.Password, host),
312312
fmt.Sprintf("FILE_SIZE_LIMIT=%v", utils.Config.Storage.FileSizeLimit),
313313
"STORAGE_BACKEND=file",
@@ -330,7 +330,7 @@ func initAuthJob(host string) utils.DockerJob {
330330
"GOTRUE_DB_DRIVER=postgres",
331331
fmt.Sprintf("GOTRUE_DB_DATABASE_URL=postgresql://supabase_auth_admin:%s@%s:5432/postgres", utils.Config.Db.Password, host),
332332
"GOTRUE_SITE_URL=" + utils.Config.Auth.SiteUrl,
333-
"GOTRUE_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
333+
"GOTRUE_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
334334
},
335335
Cmd: []string{"gotrue", "migrate"},
336336
}

internal/functions/new/new.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func createEntrypointFile(slug string, fsys afero.Fs) error {
7373
defer f.Close()
7474
if err := indexTemplate.Option("missingkey=error").Execute(f, indexConfig{
7575
URL: utils.GetApiUrl("/functions/v1/" + slug),
76-
Token: utils.Config.Auth.AnonKey,
76+
Token: utils.Config.Auth.AnonKey.Value,
7777
}); err != nil {
7878
return errors.Errorf("failed to write entrypoint: %w", err)
7979
}

internal/functions/serve/serve.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ func ServeFunctions(ctx context.Context, envFilePath string, noVerifyJWT *bool,
104104
}
105105
env = append(env,
106106
fmt.Sprintf("SUPABASE_URL=http://%s:8000", utils.KongAliases[0]),
107-
"SUPABASE_ANON_KEY="+utils.Config.Auth.AnonKey,
108-
"SUPABASE_SERVICE_ROLE_KEY="+utils.Config.Auth.ServiceRoleKey,
107+
"SUPABASE_ANON_KEY="+utils.Config.Auth.AnonKey.Value,
108+
"SUPABASE_SERVICE_ROLE_KEY="+utils.Config.Auth.ServiceRoleKey.Value,
109109
"SUPABASE_DB_URL="+dbUrl,
110-
"SUPABASE_INTERNAL_JWT_SECRET="+utils.Config.Auth.JwtSecret,
110+
"SUPABASE_INTERNAL_JWT_SECRET="+utils.Config.Auth.JwtSecret.Value,
111111
fmt.Sprintf("SUPABASE_INTERNAL_HOST_PORT=%d", utils.Config.Api.Port),
112112
)
113113
if viper.GetBool("DEBUG") {

internal/gen/keys/keys.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func Run(ctx context.Context, projectRef, format string, names CustomName, fsys
3131
return utils.EncodeOutput(format, os.Stdout, map[string]string{
3232
names.DbHost: fmt.Sprintf("%s-%s.fly.dev", projectRef, branch),
3333
names.DbPassword: utils.Config.Db.Password,
34-
names.JWTSecret: utils.Config.Auth.JwtSecret,
35-
names.AnonKey: utils.Config.Auth.AnonKey,
36-
names.ServiceRoleKey: utils.Config.Auth.ServiceRoleKey,
34+
names.JWTSecret: utils.Config.Auth.JwtSecret.Value,
35+
names.AnonKey: utils.Config.Auth.AnonKey.Value,
36+
names.ServiceRoleKey: utils.Config.Auth.ServiceRoleKey.Value,
3737
})
3838
}
3939

@@ -46,11 +46,11 @@ func GenerateSecrets(ctx context.Context, projectRef, branch string, fsys afero.
4646
if resp.JSON200 == nil {
4747
return errors.New("Unexpected error retrieving JWT secret: " + string(resp.Body))
4848
}
49-
utils.Config.Auth.JwtSecret = *resp.JSON200.JwtSecret
49+
utils.Config.Auth.JwtSecret.Value = *resp.JSON200.JwtSecret
5050
// Generate database password
5151
key := strings.Join([]string{
5252
projectRef,
53-
utils.Config.Auth.JwtSecret,
53+
utils.Config.Auth.JwtSecret.Value,
5454
branch,
5555
}, ":")
5656
hash := sha256.Sum256([]byte(key))
@@ -61,15 +61,15 @@ func GenerateSecrets(ctx context.Context, projectRef, branch string, fsys afero.
6161
Ref: projectRef,
6262
Role: "anon",
6363
}.NewToken()
64-
if utils.Config.Auth.AnonKey, err = anonToken.SignedString([]byte(utils.Config.Auth.JwtSecret)); err != nil {
64+
if utils.Config.Auth.AnonKey.Value, err = anonToken.SignedString([]byte(utils.Config.Auth.JwtSecret.Value)); err != nil {
6565
return errors.Errorf("failed to sign anon key: %w", err)
6666
}
6767
serviceToken := config.CustomClaims{
6868
Issuer: "supabase",
6969
Ref: projectRef,
7070
Role: "service_role",
7171
}.NewToken()
72-
if utils.Config.Auth.ServiceRoleKey, err = serviceToken.SignedString([]byte(utils.Config.Auth.JwtSecret)); err != nil {
72+
if utils.Config.Auth.ServiceRoleKey.Value, err = serviceToken.SignedString([]byte(utils.Config.Auth.JwtSecret.Value)); err != nil {
7373
return errors.Errorf("failed to sign service_role key: %w", err)
7474
}
7575
return nil

internal/start/start.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ EOF
464464
"GOTRUE_JWT_AUD=authenticated",
465465
"GOTRUE_JWT_DEFAULT_GROUP_NAME=authenticated",
466466
fmt.Sprintf("GOTRUE_JWT_EXP=%v", utils.Config.Auth.JwtExpiry),
467-
"GOTRUE_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
467+
"GOTRUE_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
468468
"GOTRUE_JWT_ISSUER=" + utils.GetApiUrl("/auth/v1"),
469469

470470
fmt.Sprintf("GOTRUE_EXTERNAL_EMAIL_ENABLED=%v", utils.Config.Auth.Email.EnableSignup),
@@ -755,9 +755,9 @@ EOF
755755
"DB_NAME=" + dbConfig.Database,
756756
"DB_AFTER_CONNECT_QUERY=SET search_path TO _realtime",
757757
"DB_ENC_KEY=" + utils.Config.Realtime.EncryptionKey,
758-
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
758+
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
759759
fmt.Sprintf("API_JWT_JWKS=%s", jwks),
760-
"METRICS_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
760+
"METRICS_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
761761
"APP_NAME=realtime",
762762
"SECRET_KEY_BASE=" + utils.Config.Realtime.SecretKeyBase,
763763
"ERL_AFLAGS=" + utils.ToRealtimeEnv(utils.Config.Realtime.IpVersion),
@@ -838,9 +838,9 @@ EOF
838838
container.Config{
839839
Image: utils.Config.Storage.Image,
840840
Env: []string{
841-
"ANON_KEY=" + utils.Config.Auth.AnonKey,
842-
"SERVICE_KEY=" + utils.Config.Auth.ServiceRoleKey,
843-
"AUTH_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
841+
"ANON_KEY=" + utils.Config.Auth.AnonKey.Value,
842+
"SERVICE_KEY=" + utils.Config.Auth.ServiceRoleKey.Value,
843+
"AUTH_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
844844
fmt.Sprintf("AUTH_JWT_JWKS=%s", jwks),
845845
fmt.Sprintf("DATABASE_URL=postgresql://supabase_storage_admin:%s@%s:%d/%s", dbConfig.Password, dbConfig.Host, dbConfig.Port, dbConfig.Database),
846846
fmt.Sprintf("FILE_SIZE_LIMIT=%v", utils.Config.Storage.FileSizeLimit),
@@ -986,9 +986,9 @@ EOF
986986
"POSTGRES_PASSWORD=" + dbConfig.Password,
987987
"SUPABASE_URL=http://" + utils.KongId + ":8000",
988988
"SUPABASE_PUBLIC_URL=" + utils.Config.Studio.ApiUrl,
989-
"AUTH_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
990-
"SUPABASE_ANON_KEY=" + utils.Config.Auth.AnonKey,
991-
"SUPABASE_SERVICE_KEY=" + utils.Config.Auth.ServiceRoleKey,
989+
"AUTH_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
990+
"SUPABASE_ANON_KEY=" + utils.Config.Auth.AnonKey.Value,
991+
"SUPABASE_SERVICE_KEY=" + utils.Config.Auth.ServiceRoleKey.Value,
992992
"LOGFLARE_API_KEY=" + utils.Config.Analytics.ApiKey,
993993
"OPENAI_API_KEY=" + utils.Config.Studio.OpenaiApiKey.Value,
994994
fmt.Sprintf("LOGFLARE_URL=http://%v:4000", utils.LogflareId),
@@ -1056,8 +1056,8 @@ EOF
10561056
"CLUSTER_POSTGRES=true",
10571057
"SECRET_KEY_BASE=" + utils.Config.Db.Pooler.SecretKeyBase,
10581058
"VAULT_ENC_KEY=" + utils.Config.Db.Pooler.EncryptionKey,
1059-
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
1060-
"METRICS_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
1059+
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
1060+
"METRICS_JWT_SECRET=" + utils.Config.Auth.JwtSecret.Value,
10611061
"REGION=local",
10621062
"RUN_JANITOR=true",
10631063
"ERL_AFLAGS=-proto_dist inet_tcp",

internal/status/status.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func (c *CustomName) toValues(exclude ...string) map[string]string {
5050
values[c.StudioURL] = fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Studio.Port)
5151
}
5252
if utils.Config.Auth.Enabled && !utils.SliceContains(exclude, utils.GotrueId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.Config.Auth.Image)) {
53-
values[c.JWTSecret] = utils.Config.Auth.JwtSecret
54-
values[c.AnonKey] = utils.Config.Auth.AnonKey
55-
values[c.ServiceRoleKey] = utils.Config.Auth.ServiceRoleKey
53+
values[c.JWTSecret] = utils.Config.Auth.JwtSecret.Value
54+
values[c.AnonKey] = utils.Config.Auth.AnonKey.Value
55+
values[c.ServiceRoleKey] = utils.Config.Auth.ServiceRoleKey.Value
5656
}
5757
if utils.Config.Inbucket.Enabled && !utils.SliceContains(exclude, utils.InbucketId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.Config.Inbucket.Image)) {
5858
values[c.InbucketURL] = fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Inbucket.Port)
@@ -178,7 +178,7 @@ func checkHTTPHead(ctx context.Context, path string) error {
178178
healthOnce.Do(func() {
179179
server := utils.Config.Api.ExternalUrl
180180
header := func(req *http.Request) {
181-
req.Header.Add("apikey", utils.Config.Auth.AnonKey)
181+
req.Header.Add("apikey", utils.Config.Auth.AnonKey.Value)
182182
}
183183
client := NewKongClient()
184184
healthClient = fetcher.NewFetcher(

internal/storage/client/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func NewStorageAPI(ctx context.Context, projectRef string) (storage.StorageAPI,
1818
client.Fetcher = newLocalClient()
1919
} else if viper.IsSet("AUTH_SERVICE_ROLE_KEY") {
2020
// Special case for calling storage API without personal access token
21-
client.Fetcher = newRemoteClient(projectRef, utils.Config.Auth.ServiceRoleKey)
21+
client.Fetcher = newRemoteClient(projectRef, utils.Config.Auth.ServiceRoleKey.Value)
2222
} else if apiKey, err := tenant.GetApiKeys(ctx, projectRef); err == nil {
2323
client.Fetcher = newRemoteClient(projectRef, apiKey.ServiceRole)
2424
} else {
@@ -32,7 +32,7 @@ func newLocalClient() *fetcher.Fetcher {
3232
return fetcher.NewFetcher(
3333
utils.Config.Api.ExternalUrl,
3434
fetcher.WithHTTPClient(client),
35-
fetcher.WithBearerToken(utils.Config.Auth.ServiceRoleKey),
35+
fetcher.WithBearerToken(utils.Config.Auth.ServiceRoleKey.Value),
3636
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
3737
fetcher.WithExpectedStatus(http.StatusOK),
3838
)

pkg/config/auth.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type (
7272
Enabled bool `toml:"enabled"`
7373
Image string `toml:"-"`
7474

75-
SiteUrl string `toml:"site_url" mapstructure:"site_url"`
75+
SiteUrl string `toml:"site_url"`
7676
AdditionalRedirectUrls []string `toml:"additional_redirect_urls"`
7777
JwtExpiry uint `toml:"jwt_expiry"`
7878
EnableRefreshTokenRotation bool `toml:"enable_refresh_token_rotation"`
@@ -93,9 +93,9 @@ type (
9393
External external `toml:"external"`
9494

9595
// Custom secrets can be injected from .env file
96-
JwtSecret string `toml:"-" mapstructure:"jwt_secret"`
97-
AnonKey string `toml:"-" mapstructure:"anon_key"`
98-
ServiceRoleKey string `toml:"-" mapstructure:"service_role_key"`
96+
JwtSecret Secret `toml:"jwt_secret"`
97+
AnonKey Secret `toml:"anon_key"`
98+
ServiceRoleKey Secret `toml:"service_role_key"`
9999

100100
ThirdParty thirdParty `toml:"third_party"`
101101
}
@@ -177,11 +177,11 @@ type (
177177
EnableSignup bool `toml:"enable_signup"`
178178
EnableConfirmations bool `toml:"enable_confirmations"`
179179
Template string `toml:"template"`
180-
Twilio twilioConfig `toml:"twilio" mapstructure:"twilio"`
181-
TwilioVerify twilioConfig `toml:"twilio_verify" mapstructure:"twilio_verify"`
182-
Messagebird messagebirdConfig `toml:"messagebird" mapstructure:"messagebird"`
183-
Textlocal textlocalConfig `toml:"textlocal" mapstructure:"textlocal"`
184-
Vonage vonageConfig `toml:"vonage" mapstructure:"vonage"`
180+
Twilio twilioConfig `toml:"twilio"`
181+
TwilioVerify twilioConfig `toml:"twilio_verify"`
182+
Messagebird messagebirdConfig `toml:"messagebird"`
183+
Textlocal textlocalConfig `toml:"textlocal"`
184+
Vonage vonageConfig `toml:"vonage"`
185185
TestOTP map[string]string `toml:"test_otp"`
186186
MaxFrequency time.Duration `toml:"max_frequency"`
187187
}
@@ -234,26 +234,26 @@ type (
234234
Enabled bool `toml:"enabled"`
235235
AccountSid string `toml:"account_sid"`
236236
MessageServiceSid string `toml:"message_service_sid"`
237-
AuthToken Secret `toml:"auth_token" mapstructure:"auth_token"`
237+
AuthToken Secret `toml:"auth_token"`
238238
}
239239

240240
messagebirdConfig struct {
241241
Enabled bool `toml:"enabled"`
242242
Originator string `toml:"originator"`
243-
AccessKey Secret `toml:"access_key" mapstructure:"access_key"`
243+
AccessKey Secret `toml:"access_key"`
244244
}
245245

246246
textlocalConfig struct {
247247
Enabled bool `toml:"enabled"`
248248
Sender string `toml:"sender"`
249-
ApiKey Secret `toml:"api_key" mapstructure:"api_key"`
249+
ApiKey Secret `toml:"api_key"`
250250
}
251251

252252
vonageConfig struct {
253253
Enabled bool `toml:"enabled"`
254254
From string `toml:"from"`
255-
ApiKey string `toml:"api_key" mapstructure:"api_key"`
256-
ApiSecret Secret `toml:"api_secret" mapstructure:"api_secret"`
255+
ApiKey string `toml:"api_key"`
256+
ApiSecret Secret `toml:"api_secret"`
257257
}
258258

259259
provider struct {

0 commit comments

Comments
 (0)