From 927625dc4f646928a4fde3cf7f928a3fc2c521d5 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Thu, 25 May 2023 01:25:37 +0800 Subject: [PATCH 1/3] fix: use db password everywhere --- internal/db/start/start.go | 2 +- internal/start/start.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/db/start/start.go b/internal/db/start/start.go index cfd877075..4d1b0fea2 100644 --- a/internal/db/start/start.go +++ b/internal/db/start/start.go @@ -48,7 +48,7 @@ func NewContainerConfig() container.Config { config := container.Config{ Image: utils.DbImage, Env: []string{ - "POSTGRES_PASSWORD=postgres", + "POSTGRES_PASSWORD=" + utils.Config.Db.Password, "POSTGRES_HOST=/var/run/postgresql", "POSTGRES_INITDB_ARGS=--lc-ctype=C.UTF-8", }, diff --git a/internal/start/start.go b/internal/start/start.go index 7c2ce84ad..fc26528c8 100644 --- a/internal/start/start.go +++ b/internal/start/start.go @@ -615,7 +615,7 @@ EOF Image: utils.StudioImage, Env: []string{ "STUDIO_PG_META_URL=http://" + utils.PgmetaId + ":8080", - "POSTGRES_PASSWORD=postgres", + "POSTGRES_PASSWORD=" + utils.Config.Db.Password, "SUPABASE_URL=http://" + utils.KongId + ":8000", fmt.Sprintf("SUPABASE_REST_URL=http://localhost:%v/rest/v1/", utils.Config.Api.Port), fmt.Sprintf("SUPABASE_PUBLIC_URL=http://localhost:%v/", utils.Config.Api.Port), From 6bb1d032cce8a574ff7648f3fe814f7e6876664b Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Thu, 25 May 2023 02:15:30 +0800 Subject: [PATCH 2/3] chore: refactor local connection config --- internal/db/branch/switch_/switch_.go | 3 ++- internal/db/diff/migra.go | 4 ++-- internal/db/lint/lint.go | 2 +- internal/db/lint/lint_test.go | 8 ++++---- internal/db/reset/reset_test.go | 4 ++-- internal/db/start/start.go | 4 ++-- internal/migration/apply/apply_test.go | 9 +++++---- internal/migration/up/up.go | 3 ++- internal/migration/up/up_test.go | 9 +++++---- internal/start/start.go | 2 +- internal/utils/connect.go | 24 +++++++++++++++++++++--- internal/utils/connect_test.go | 9 +-------- 12 files changed, 48 insertions(+), 33 deletions(-) diff --git a/internal/db/branch/switch_/switch_.go b/internal/db/branch/switch_/switch_.go index 02d54dfeb..73e980226 100644 --- a/internal/db/branch/switch_/switch_.go +++ b/internal/db/branch/switch_/switch_.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" + "github.com/jackc/pgconn" "github.com/jackc/pgx/v4" "github.com/spf13/afero" "github.com/supabase/cli/internal/db/reset" @@ -57,7 +58,7 @@ func Run(ctx context.Context, target string, fsys afero.Fs, options ...func(*pgx } func switchDatabase(ctx context.Context, source, target string, options ...func(*pgx.ConnConfig)) error { - conn, err := utils.ConnectLocalPostgres(ctx, "localhost", utils.Config.Db.Port, "template1", options...) + conn, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Database: "template1"}, options...) if err != nil { return err } diff --git a/internal/db/diff/migra.go b/internal/db/diff/migra.go index 0aeb11f44..8fed56af1 100644 --- a/internal/db/diff/migra.go +++ b/internal/db/diff/migra.go @@ -67,7 +67,7 @@ func RunMigra(ctx context.Context, schema []string, file string, config pgconn.C func loadSchema(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) (schema []string, err error) { var conn *pgx.Conn if config.Host == "localhost" && config.Port == uint16(utils.Config.Db.Port) { - conn, err = utils.ConnectLocalPostgres(ctx, config.Host, uint(config.Port), config.Database, options...) + conn, err = utils.ConnectLocalPostgres(ctx, config, options...) } else { conn, err = utils.ConnectRemotePostgres(ctx, config, options...) } @@ -113,7 +113,7 @@ func connectShadowDatabase(ctx context.Context, timeout time.Duration, options . defer ticker.Stop() // Retry until connected, cancelled, or timeout for t := now; t.Before(expiry); t = <-ticker.C { - conn, err = utils.ConnectLocalPostgres(ctx, "localhost", utils.Config.Db.ShadowPort, "postgres", options...) + conn, err = utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: uint16(utils.Config.Db.ShadowPort)}, options...) if err == nil || errors.Is(ctx.Err(), context.Canceled) { break } diff --git a/internal/db/lint/lint.go b/internal/db/lint/lint.go index 80440b6c3..a09fb6aca 100644 --- a/internal/db/lint/lint.go +++ b/internal/db/lint/lint.go @@ -69,7 +69,7 @@ func connect(ctx context.Context, config pgconn.Config, fsys afero.Fs, options . if err := utils.AssertSupabaseDbIsRunning(); err != nil { return nil, err } - return utils.ConnectLocalPostgres(ctx, "localhost", utils.Config.Db.Port, "postgres", options...) + return utils.ConnectLocalPostgres(ctx, pgconn.Config{}, options...) } func filterResult(result []Result, minLevel LintLevel) (filtered []Result) { diff --git a/internal/db/lint/lint_test.go b/internal/db/lint/lint_test.go index 9747b8209..48b728a83 100644 --- a/internal/db/lint/lint_test.go +++ b/internal/db/lint/lint_test.go @@ -121,7 +121,7 @@ func TestLintDatabase(t *testing.T) { Query("rollback").Reply("ROLLBACK") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -167,7 +167,7 @@ func TestLintDatabase(t *testing.T) { Query("rollback").Reply("ROLLBACK") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -187,7 +187,7 @@ func TestLintDatabase(t *testing.T) { Query("rollback").Reply("ROLLBACK") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -207,7 +207,7 @@ func TestLintDatabase(t *testing.T) { Query("rollback").Reply("ROLLBACK") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test diff --git a/internal/db/reset/reset_test.go b/internal/db/reset/reset_test.go index b3d0862b3..d155435e8 100644 --- a/internal/db/reset/reset_test.go +++ b/internal/db/reset/reset_test.go @@ -167,7 +167,7 @@ func TestSeedDatabase(t *testing.T) { Reply("INSERT 0 1") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 54322, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -200,7 +200,7 @@ func TestSeedDatabase(t *testing.T) { ReplyError(pgerrcode.NotNullViolation, `null value in column "age" of relation "employees"`) // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 54322, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test diff --git a/internal/db/start/start.go b/internal/db/start/start.go index 4d1b0fea2..967ecff08 100644 --- a/internal/db/start/start.go +++ b/internal/db/start/start.go @@ -136,7 +136,7 @@ func initCurrentBranch(fsys afero.Fs) error { func initDatabase(ctx context.Context, w io.Writer, options ...func(*pgx.ConnConfig)) error { // Initialise globals - conn, err := utils.ConnectLocalPostgres(ctx, "localhost", utils.Config.Db.Port, "postgres", options...) + conn, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{}, options...) if err != nil { return err } @@ -155,7 +155,7 @@ func SetupDatabase(ctx context.Context, dbConfig pgconn.Config, fsys afero.Fs, w if dbConfig.Host != utils.DbId { return nil } - conn, err := utils.ConnectLocalPostgres(ctx, "localhost", utils.Config.Db.Port, "postgres", options...) + conn, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{}, options...) if err != nil { return err } diff --git a/internal/migration/apply/apply_test.go b/internal/migration/apply/apply_test.go index ca3fe8d8a..c29ab4a7f 100644 --- a/internal/migration/apply/apply_test.go +++ b/internal/migration/apply/apply_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "github.com/jackc/pgconn" "github.com/jackc/pgerrcode" "github.com/spf13/afero" "github.com/spf13/viper" @@ -39,7 +40,7 @@ func TestMigrateDatabase(t *testing.T) { Reply("INSERT 1") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -76,7 +77,7 @@ func TestMigrateUp(t *testing.T) { ReplyError(pgerrcode.InsufficientPrivilege, "permission denied for relation supabase_migrations") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -97,7 +98,7 @@ func TestMigrateUp(t *testing.T) { Reply("CREATE TABLE") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -149,7 +150,7 @@ func TestMigrationFile(t *testing.T) { Query(repair.INSERT_MIGRATION_VERSION, "0") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test diff --git a/internal/migration/up/up.go b/internal/migration/up/up.go index 2399cfbfe..f2fadadad 100644 --- a/internal/migration/up/up.go +++ b/internal/migration/up/up.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/jackc/pgconn" "github.com/jackc/pgx/v4" "github.com/spf13/afero" "github.com/supabase/cli/internal/migration/apply" @@ -18,7 +19,7 @@ func Run(ctx context.Context, fsys afero.Fs, options ...func(*pgx.ConnConfig)) e if err := utils.LoadConfigFS(fsys); err != nil { return err } - conn, err := utils.ConnectLocalPostgres(ctx, "localhost", utils.Config.Db.Port, "postgres", options...) + conn, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{}, options...) if err != nil { return err } diff --git a/internal/migration/up/up_test.go b/internal/migration/up/up_test.go index 40d9b6f3c..1d9761b53 100644 --- a/internal/migration/up/up_test.go +++ b/internal/migration/up/up_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "testing" + "github.com/jackc/pgconn" "github.com/spf13/afero" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -34,7 +35,7 @@ func TestPendingMigrations(t *testing.T) { Reply("SELECT 2", []interface{}{"20221201000000"}, []interface{}{"20221201000001"}) // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -54,7 +55,7 @@ func TestPendingMigrations(t *testing.T) { Reply("SELECT 0") // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -73,7 +74,7 @@ func TestPendingMigrations(t *testing.T) { Reply("SELECT 1", []interface{}{"0"}) // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test @@ -94,7 +95,7 @@ func TestPendingMigrations(t *testing.T) { Reply("SELECT 1", []interface{}{"0"}) // Connect to mock ctx := context.Background() - mock, err := utils.ConnectLocalPostgres(ctx, "localhost", 5432, "postgres", conn.Intercept) + mock, err := utils.ConnectLocalPostgres(ctx, pgconn.Config{Port: 5432}, conn.Intercept) require.NoError(t, err) defer mock.Close(ctx) // Run test diff --git a/internal/start/start.go b/internal/start/start.go index fc26528c8..b6b85eba5 100644 --- a/internal/start/start.go +++ b/internal/start/start.go @@ -615,7 +615,7 @@ EOF Image: utils.StudioImage, Env: []string{ "STUDIO_PG_META_URL=http://" + utils.PgmetaId + ":8080", - "POSTGRES_PASSWORD=" + utils.Config.Db.Password, + "POSTGRES_PASSWORD=" + dbConfig.Password, "SUPABASE_URL=http://" + utils.KongId + ":8000", fmt.Sprintf("SUPABASE_REST_URL=http://localhost:%v/rest/v1/", utils.Config.Api.Port), fmt.Sprintf("SUPABASE_PUBLIC_URL=http://localhost:%v/", utils.Config.Api.Port), diff --git a/internal/utils/connect.go b/internal/utils/connect.go index f61cccee5..94ceb37dd 100644 --- a/internal/utils/connect.go +++ b/internal/utils/connect.go @@ -7,6 +7,7 @@ import ( "net" "net/url" "os" + "time" "github.com/jackc/pgconn" "github.com/jackc/pgx/v4" @@ -65,9 +66,26 @@ func ConnectRemotePostgres(ctx context.Context, config pgconn.Config, options .. } // Connnect to local Postgres with optimised settings. The caller is responsible for closing the connection returned. -func ConnectLocalPostgres(ctx context.Context, host string, port uint, database string, options ...func(*pgx.ConnConfig)) (*pgx.Conn, error) { - url := fmt.Sprintf("postgresql://postgres:postgres@%s:%d/%s?connect_timeout=2", host, port, database) - return ConnectByUrl(ctx, url, options...) +func ConnectLocalPostgres(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) (*pgx.Conn, error) { + if len(config.Host) == 0 { + config.Host = "localhost" + } + if config.Port == 0 { + config.Port = uint16(Config.Db.Port) + } + if len(config.User) == 0 { + config.User = "postgres" + } + if len(config.Password) == 0 { + config.Password = Config.Db.Password + } + if len(config.Database) == 0 { + config.Database = "postgres" + } + if config.ConnectTimeout == 0 { + config.ConnectTimeout = 2 * time.Second + } + return ConnectByUrl(ctx, ToPostgresURL(config), options...) } func ConnectByUrl(ctx context.Context, url string, options ...func(*pgx.ConnConfig)) (*pgx.Conn, error) { diff --git a/internal/utils/connect_test.go b/internal/utils/connect_test.go index b4c185e2d..74cd05596 100644 --- a/internal/utils/connect_test.go +++ b/internal/utils/connect_test.go @@ -99,14 +99,7 @@ func TestConnectRemotePostgres(t *testing.T) { func TestConnectLocal(t *testing.T) { t.Run("connects with debug log", func(t *testing.T) { viper.Set("DEBUG", true) - _, err := ConnectLocalPostgres(context.Background(), "0", 5432, "postgres") + _, err := ConnectLocalPostgres(context.Background(), pgconn.Config{Host: "0"}) assert.ErrorContains(t, err, "connect: connection refused") }) - - t.Run("throws error on invalid port", func(t *testing.T) { - _, err := ConnectLocalPostgres(context.Background(), "localhost", 0, "postgres") - assert.ErrorContains(t, err, "invalid port (outside range)") - _, err = ConnectLocalPostgres(context.Background(), "localhost", 65536, "postgres") - assert.ErrorContains(t, err, `invalid port (strconv.ParseUint: parsing "65536": value out of range)`) - }) } From afabb553b0b9be1848b732d97a61aef9a8ea6880 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Thu, 25 May 2023 02:23:52 +0800 Subject: [PATCH 3/3] chore: do not map remote db password --- internal/status/status.go | 2 +- internal/utils/config.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/status/status.go b/internal/status/status.go index 94b761619..922a8075e 100644 --- a/internal/status/status.go +++ b/internal/status/status.go @@ -26,7 +26,7 @@ type CustomName struct { func (c *CustomName) toValues(exclude ...string) map[string]string { values := map[string]string{ - c.DbURL: fmt.Sprintf("postgresql://postgres:postgres@localhost:%d/postgres", utils.Config.Db.Port), + c.DbURL: fmt.Sprintf("postgresql://postgres:%s@localhost:%d/postgres", utils.Config.Db.Password, utils.Config.Db.Port), } if !sliceContains(exclude, utils.RestId) && !sliceContains(exclude, utils.ShortContainerImageName(utils.PostgrestImage)) { values[c.ApiURL] = fmt.Sprintf("http://localhost:%d", utils.Config.Api.Port) diff --git a/internal/utils/config.go b/internal/utils/config.go index 5c11e3e12..8be6892b3 100644 --- a/internal/utils/config.go +++ b/internal/utils/config.go @@ -109,7 +109,7 @@ type ( Port uint `toml:"port"` ShadowPort uint `toml:"shadow_port"` MajorVersion uint `toml:"major_version"` - Password string `toml:"-" mapstructure:"password"` + Password string `toml:"-"` } studio struct {