Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(pg): use unix socket for postgres connection without listening on the port (#907)
Danny Xu41cfa8b97fbb
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.goindex 45922ff087..f511149540 100644--- a/bin/server/cmd/root.go+++ b/bin/server/cmd/root.go@@ -278,7 +278,7 @@ func start() {// NewMain creates a main server based on profile.func NewMain(activeProfile Profile, logger *zap.Logger) (*Main, error) {resourceDir := path.Join(activeProfile.dataDir, "resources")- pgDataDir := path.Join(activeProfile.dataDir, "pgdata")+ pgDataDir := common.GetPostgresDataDir(activeProfile.dataDir)fmt.Println("-----Config BEGIN-----")fmt.Printf("mode=%s\n", activeProfile.mode)fmt.Printf("server=%s:%d\n", host, activeProfile.port)@@ -334,10 +334,11 @@ func (m *Main) Run(ctx context.Context) error {}m.pgStarted = true+ // Even when Postgres opens Unix domain socket only for connection, it still requires a port as ID to differentiate different Postgres instances.connCfg := dbdriver.ConnectionConfig{Username: m.profile.pgUser,Password: "",- Host: "localhost",+ Host: common.GetPostgresSocketDir(),Port: fmt.Sprintf("%d", m.profile.datastorePort),}db := store.NewDB(m.l, m.profile.dsn, connCfg, m.profile.seedDir, m.profile.forceResetSeed, readonly, version, m.profile.schemaVersion)diff --git a/common/util.go b/common/util.goindex ec453ae34c..43ddb14bcd 100644--- a/common/util.go+++ b/common/util.go@@ -2,6 +2,7 @@ package commonimport ("math/rand"+ "path""sort""strings")@@ -37,3 +38,13 @@ func HasPrefixes(src string, prefixes ...string) bool {}return false}++// GetPostgresDataDir returns the postgres data directory of Bytebase.+func GetPostgresDataDir(dataDir string) string {+ return path.Join(dataDir, "pgdata")+}++// GetPostgresSocketDir returns the postgres socket directory of Bytebase.+func GetPostgresSocketDir() string {+ return "/tmp"+}diff --git a/resources/postgres/postgres.go b/resources/postgres/postgres.goindex b57689f5b0..9bb29c3a45 100644--- a/resources/postgres/postgres.go+++ b/resources/postgres/postgres.go@@ -14,6 +14,7 @@ import ("strings""syscall"+ "github.com/bytebase/bytebase/common""github.com/bytebase/bytebase/resources/utils")@@ -42,9 +43,11 @@ func (i *Instance) Start(port int, stdout, stderr io.Writer, waitSec int) (err ei.port = port+ // See -p -k -h option definitions in the link below.+ // https://www.postgresql.org/docs/current/app-postgres.htmlp := exec.Command(pgbin, "start", "-w","-D", i.datadir,- "-o", fmt.Sprintf(`"-p %d"`, i.port))+ "-o", fmt.Sprintf(`-p %d -k %s -h ""`, i.port, common.GetPostgresSocketDir()))p.Stdout = stdoutp.Stderr = stderrdiff --git a/server/instance.go b/server/instance.goindex 07cfdc10cf..9d06bfbba9 100644--- a/server/instance.go+++ b/server/instance.go@@ -3,7 +3,6 @@ package serverimport ("context""fmt"- "net""net/http""strconv"@@ -552,26 +551,9 @@ func (s *Server) instanceCountGuard(ctx context.Context) error {// disallowBytebaseStore prevents users adding Bytebase's own Postgres database.// Otherwise, users can take control of the database which is a security issue.func (s *Server) disallowBytebaseStore(engine db.Type, host, port string) error {- if engine != db.Postgres {- return nil- }- if port != fmt.Sprintf("%v", s.datastorePort) {- return nil- }-- existErr := fmt.Errorf("instance doesn't exist for host %q and port %q", host, port)- if host == "localhost" || host == "127.0.0.1" || host == "::1" {- return existErr- }- // Check loopback addresses- ips, err := net.LookupIP(host)- if err != nil {- return nil- }- for _, ip := range ips {- if ip.IsLoopback() {- return existErr- }+ // Even when Postgres opens Unix domain socket only for connection, it still requires a port as socket file extension to differentiate different Postgres instances.+ if engine == db.Postgres && port == fmt.Sprintf("%v", s.datastorePort) && host == common.GetPostgresSocketDir() {+ return fmt.Errorf("instance doesn't exist for host %q and port %q", host, port)}return nil}