Skip to content
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
22 changes: 11 additions & 11 deletions internal/db/remote/set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("sets the remote database url", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup initial migration
version := "20220727064247"
_, err := fsys.Create("supabase/migrations/" + version + "_init.sql")
Expand All @@ -36,7 +36,7 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("creates migrations table if absent", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand All @@ -61,23 +61,23 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("throws error on invalid postgres url", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Run test
assert.Error(t, Run("invalid", fsys))
})

t.Run("throws error on failture to connect", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Run test
assert.Error(t, Run(postgresUrl, fsys))
})

t.Run("throws error on missing server version", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock postgres
conn := pgtest.NewWithStatus(map[string]string{
"standard_conforming_strings": "on",
Expand All @@ -90,7 +90,7 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("throws error on unsupported server version", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock postgres
conn := pgtest.NewWithStatus(map[string]string{
"standard_conforming_strings": "on",
Expand All @@ -104,7 +104,7 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("throws error on failure to create table", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand All @@ -119,7 +119,7 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("throws error on failure to list migrations", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand All @@ -134,7 +134,7 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("throws error on migration mismatch", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand All @@ -149,7 +149,7 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("throws error on malformed file name", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup initial migration
version := "20220727064247"
_, err := fsys.Create("supabase/migrations/" + version + ".sql")
Expand All @@ -168,7 +168,7 @@ func TestDbRemoteSetCommand(t *testing.T) {
t.Run("throws error on failure to create directory", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, true))
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand Down
3 changes: 0 additions & 3 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ func Run() error {
if err := utils.LoadConfig(); err != nil {
return err
}
if err := utils.InterpolateEnvInConfig(); err != nil {
return err
}
if err := utils.AssertSupabaseStartIsRunning(); err == nil {
return errors.New(utils.Aqua("supabase start") + " is already running. Try running " + utils.Aqua("supabase stop") + " first.")
}
Expand Down
82 changes: 37 additions & 45 deletions internal/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,56 +228,48 @@ func LoadConfigFS(fsys afero.Fs) error {
ClientId: "",
Secret: "",
}
}
}
}

return nil
}

func InterpolateEnvInConfig() error {
maybeLoadEnv := func(s string) (string, error) {
matches := regexp.MustCompile(`^env\((.*)\)$`).FindStringSubmatch(s)
if len(matches) == 0 {
return s, nil
}

envName := matches[1]
value := os.Getenv(envName)
if value == "" {
return "", errors.New(`Error evaluating "env(` + envName + `)": environment variable ` + envName + " is unset.")
}

return value, nil
}
} else if Config.Auth.External[ext].Enabled {
maybeLoadEnv := func(s string) (string, error) {
matches := regexp.MustCompile(`^env\((.*)\)$`).FindStringSubmatch(s)
if len(matches) == 0 {
return s, nil
}

envName := matches[1]
value := os.Getenv(envName)
if value == "" {
return "", errors.New(`Error evaluating "env(` + envName + `)": environment variable ` + envName + " is unset.")
}

return value, nil
}

for _, ext := range authExternalProviders {
if Config.Auth.External[ext].Enabled {
var clientId, secret string
var clientId, secret string

if Config.Auth.External[ext].ClientId == "" {
return fmt.Errorf("Missing required field in config: auth.external.%s.client_id", ext)
} else {
v, err := maybeLoadEnv(Config.Auth.External[ext].ClientId)
if err != nil {
return err
if Config.Auth.External[ext].ClientId == "" {
return fmt.Errorf("Missing required field in config: auth.external.%s.client_id", ext)
} else {
v, err := maybeLoadEnv(Config.Auth.External[ext].ClientId)
if err != nil {
return err
}
clientId = v
}
clientId = v
}
if Config.Auth.External[ext].Secret == "" {
return fmt.Errorf("Missing required field in config: auth.external.%s.secret", ext)
} else {
v, err := maybeLoadEnv(Config.Auth.External[ext].Secret)
if err != nil {
return err
if Config.Auth.External[ext].Secret == "" {
return fmt.Errorf("Missing required field in config: auth.external.%s.secret", ext)
} else {
v, err := maybeLoadEnv(Config.Auth.External[ext].Secret)
if err != nil {
return err
}
secret = v
}
secret = v
}

Config.Auth.External[ext] = provider{
Enabled: true,
ClientId: clientId,
Secret: secret,
Config.Auth.External[ext] = provider{
Enabled: true,
ClientId: clientId,
Secret: secret,
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions internal/utils/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func TestConfigParsing(t *testing.T) {
t.Setenv("AZURE_CLIENT_ID", "hello")
t.Setenv("AZURE_SECRET", "this is cool")
assert.NoError(t, LoadConfigFS(fsys))
assert.NoError(t, InterpolateEnvInConfig())

assert.Equal(t, "hello", Config.Auth.External["azure"].ClientId)
assert.Equal(t, "this is cool", Config.Auth.External["azure"].Secret)
Expand All @@ -30,7 +29,6 @@ func TestConfigParsing(t *testing.T) {
t.Run("config file with environment variables fails when unset", func(t *testing.T) {
fsys := afero.NewMemMapFs()
assert.NoError(t, WriteConfig(fsys, true))
assert.NoError(t, LoadConfigFS(fsys))
assert.Error(t, InterpolateEnvInConfig())
assert.Error(t, LoadConfigFS(fsys))
})
}