Skip to content

Commit 2680444

Browse files
committed
fix: actually create the user with a set uid & gid
1 parent 981f0c0 commit 2680444

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/config/config.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path"
1212
"path/filepath"
1313
"regexp"
14+
"strconv"
1415
"strings"
1516
"sync"
1617
"sync/atomic"
@@ -543,9 +544,9 @@ func EnsurePterodactylUser() error {
543544

544545
// Our way of detecting if elytra is running inside of Docker.
545546
if sysName == "distroless" {
546-
_config.System.Username = system.FirstNotEmpty(os.Getenv("ELYTRA_USERNAME"), "pterodactyl")
547-
_config.System.User.Uid = system.MustInt(system.FirstNotEmpty(os.Getenv("ELYTRA_UID"), "988"))
548-
_config.System.User.Gid = system.MustInt(system.FirstNotEmpty(os.Getenv("ELYTRA_GID"), "988"))
547+
_config.System.Username = system.FirstNotEmpty(os.Getenv("ELYTRA_USERNAME"), "pyrodactyl")
548+
_config.System.User.Uid = system.MustInt(system.FirstNotEmpty(os.Getenv("ELYTRA_UID"), "8888"))
549+
_config.System.User.Gid = system.MustInt(system.FirstNotEmpty(os.Getenv("ELYTRA_GID"), "8888"))
549550
return nil
550551
}
551552

@@ -575,18 +576,31 @@ func EnsurePterodactylUser() error {
575576
return nil
576577
}
577578

578-
command := fmt.Sprintf("useradd --system --no-create-home --shell /usr/sbin/nologin %s", _config.System.Username)
579579
// Alpine Linux is the only OS we currently support that doesn't work with the useradd
580580
// command, so in those cases we just modify the command a bit to work as expected.
581+
gidStr := strconv.Itoa(_config.System.User.Gid)
582+
uidStr := strconv.Itoa(_config.System.User.Uid)
583+
581584
if strings.HasPrefix(sysName, "alpine") {
582-
command = fmt.Sprintf("adduser -S -D -H -G %[1]s -s /sbin/nologin %[1]s", _config.System.Username)
583585
// We have to create the group first on Alpine, so do that here before continuing on
584586
// to the user creation process.
585-
if _, err := exec.Command("addgroup", "-S", _config.System.Username).Output(); err != nil {
587+
if _, err := exec.Command("addgroup", "-S", "-g", gidStr, _config.System.Username).Output(); err != nil {
588+
return err
589+
}
590+
} else {
591+
if _, err := exec.Command("groupadd", "--system", "--gid", gidStr, _config.System.Username).Output(); err != nil {
586592
return err
587593
}
588594
}
589595

596+
// Create user with UID/GID from configuration
597+
var command string
598+
if strings.HasPrefix(sysName, "alpine") {
599+
command = fmt.Sprintf("adduser -S -D -H -G %[1]s -u %[2]s -s /sbin/nologin %[1]s", _config.System.Username, uidStr)
600+
} else {
601+
command = fmt.Sprintf("useradd --system --no-create-home --shell /usr/sbin/nologin --uid %[2]s --gid %[1]s %[1]s", _config.System.Username, uidStr)
602+
}
603+
590604
split := strings.Split(command, " ")
591605
if _, err := exec.Command(split[0], split[1:]...).Output(); err != nil {
592606
return err

0 commit comments

Comments
 (0)