Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: simplify the postgres installation environment setup (#857)

Danny Xufc8d3054a58f
diff --git a/Dockerfile b/Dockerfile
index d466bb57ba..13e53972c8 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -64,14 +64,11 @@ COPY --from=backend /backend-build/bytebase /usr/local/bin/
# - Demo script to launch Bytebase in readonly demo mode
COPY ./scripts /usr/local/bin/
-# Create bb user for running Postgres database and server.
-RUN addgroup -g 113 -S bb && adduser -u 113 -S -G bb bb
+# Create bytebase user for running Postgres database and server.
+RUN addgroup -g 113 -S bytebase && adduser -u 113 -S -G bytebase bytebase
# Directory to store the data, which can be referenced as the mounting point.
RUN mkdir -p /var/opt/bytebase
-RUN chown -R bb:bb /var/opt/bytebase
-
-USER bb
CMD ["--host", "http://localhost", "--port", "80", "--data", "/var/opt/bytebase"]
diff --git a/Dockerfile.render b/Dockerfile.render
index 52f99f34d1..d186bec712 100644
--- a/Dockerfile.render
+++ b/Dockerfile.render
@@ -65,10 +65,7 @@ COPY --from=backend /backend-build/bytebase /usr/local/bin/
COPY ./scripts /usr/local/bin/
# Create bb user for running Postgres database and server.
-RUN addgroup -g 113 -S bb && adduser -u 113 -S -G bb bb
+RUN addgroup -g 113 -S bytebase && adduser -u 113 -S -G bytebase bytebase
# Directory to store the data, which can be referenced as the mounting point.
RUN mkdir -p /var/opt/bytebase
-RUN chown -R bb:bb /var/opt/bytebase
-
-USER bb
diff --git a/resources/postgres/postgres.go b/resources/postgres/postgres.go
index 29aca1d009..39dc2a8d30 100644
--- a/resources/postgres/postgres.go
+++ b/resources/postgres/postgres.go
@@ -6,10 +6,13 @@ import (
"log"
"os"
"os/exec"
+ "os/user"
"path"
"path/filepath"
"runtime"
+ "strconv"
"strings"
+ "syscall"
"github.com/bytebase/bytebase/resources/utils"
)
@@ -42,8 +45,19 @@ func (i *Instance) Start(port int, stdout, stderr io.Writer, waitSec int) (err e
p := exec.Command(pgbin, "start", "-w",
"-D", i.datadir,
"-o", fmt.Sprintf(`"-p %d"`, i.port))
+
p.Stdout = stdout
p.Stderr = stderr
+ uid, _, sameUser, err := shouldSwitchUser()
+ if err != nil {
+ return err
+ }
+ if !sameUser {
+ p.SysProcAttr = &syscall.SysProcAttr{
+ Setpgid: true,
+ Credential: &syscall.Credential{Uid: uint32(uid)},
+ }
+ }
if err := p.Run(); err != nil {
return fmt.Errorf("failed to start postgres %q, error %v", p.String(), err)
@@ -62,8 +76,19 @@ func (i *Instance) Stop(stdout, stderr io.Writer) error {
pgbin := filepath.Join(i.basedir, "bin", "pg_ctl")
p := exec.Command(pgbin, "stop", "-w",
"-D", i.datadir)
+
p.Stderr = stderr
p.Stdout = stdout
+ uid, _, sameUser, err := shouldSwitchUser()
+ if err != nil {
+ return err
+ }
+ if !sameUser {
+ p.SysProcAttr = &syscall.SysProcAttr{
+ Setpgid: true,
+ Credential: &syscall.Credential{Uid: uint32(uid)},
+ }
+ }
if err := p.Run(); err != nil {
return err
@@ -109,6 +134,7 @@ func Install(resourceDir, pgDataDir, pgUser string) (*Instance, error) {
if err := utils.ExtractTarXz(f, tmpDir); err != nil {
return nil, fmt.Errorf("failed to extract txz file, error: %w", err)
}
+
if err := os.Rename(tmpDir, pgBinDir); err != nil {
return nil, fmt.Errorf("failed to rename postgres binary directory from %q to %q, error: %w", tmpDir, pgBinDir, err)
}
@@ -156,6 +182,19 @@ func initDB(pgBinDir, pgDataDir, pgUser string) error {
)
p.Stderr = os.Stderr
p.Stdout = os.Stdout
+ uid, gid, sameUser, err := shouldSwitchUser()
+ if err != nil {
+ return err
+ }
+ if !sameUser {
+ p.SysProcAttr = &syscall.SysProcAttr{
+ Setpgid: true,
+ Credential: &syscall.Credential{Uid: uint32(uid)},
+ }
+ if err := os.Chown(pgDataDir, int(uid), int(gid)); err != nil {
+ return fmt.Errorf("failed to change owner to bytebase of data directory %q, error: %w", pgDataDir, err)
+ }
+ }
if err := p.Run(); err != nil {
return fmt.Errorf("failed to initdb %q, error %v", p.String(), err)
@@ -163,3 +202,30 @@ func initDB(pgBinDir, pgDataDir, pgUser string) error {
return nil
}
+
+func shouldSwitchUser() (int, int, bool, error) {
+ sameUser := true
+ bytebaseUser, err := user.Current()
+ if err != nil {
+ return 0, 0, true, fmt.Errorf("failed to get current user, error: %w", err)
+ }
+ // If user runs bytebase as root user, we will attempt to run as user `bytebase`.
+ // https://www.postgresql.org/docs/14/app-initdb.html
+ if bytebaseUser.Username == "root" {
+ bytebaseUser, err = user.Lookup("bytebase")
+ if err != nil {
+ return 0, 0, false, fmt.Errorf("Please run Bytebase as non-root user. You can use the following command to create a dedicated bytebase user to run the application: addgroup -g 113 -S bytebase && adduser -u 113 -S -G bytebase bytebase")
+ }
+ sameUser = false
+ }
+
+ uid, err := strconv.ParseUint(bytebaseUser.Uid, 10, 32)
+ if err != nil {
+ return 0, 0, false, err
+ }
+ gid, err := strconv.ParseUint(bytebaseUser.Gid, 10, 32)
+ if err != nil {
+ return 0, 0, false, err
+ }
+ return int(uid), int(gid), sameUser, nil
+}