Skip to content

Commit 855bc1d

Browse files
authored
Merge pull request #23 from BoredHF/fix/add-non-root-defaults-to-uid-and-gid
Ensure non-zero defaults for system user ids
2 parents 541c7c3 + d18e444 commit 855bc1d

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/config/config.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import (
2323
"github.com/apex/log"
2424
"github.com/creasty/defaults"
2525
"github.com/gbrlsnchs/jwt/v3"
26+
"github.com/pyrohost/elytra/src/system"
2627
"golang.org/x/sys/unix"
2728
"gopkg.in/yaml.v2"
28-
"github.com/pyrohost/elytra/src/system"
29-
3029
)
3130

3231
const DefaultLocation = "/etc/elytra/config.yml"
@@ -56,6 +55,7 @@ var (
5655
_config *Configuration
5756
_jwtAlgo *jwt.HMACSHA
5857
_debugViaFlag bool
58+
defaultUserID = 8888
5959
)
6060

6161
// Locker specific to writing the configuration to the disk, this happens
@@ -170,8 +170,8 @@ type SystemConfiguration struct {
170170
ContainerGID int `yaml:"container_gid" default:"0"`
171171
} `yaml:"rootless"`
172172

173-
Uid int `yaml:"uid"`
174-
Gid int `yaml:"gid"`
173+
Uid int `yaml:"uid" default:"8888"`
174+
Gid int `yaml:"gid" default:"8888"`
175175
} `yaml:"user"`
176176

177177
// Passwd controls the mounting of a generated passwd files into containers started by Elytra.
@@ -542,11 +542,29 @@ func EnsurePterodactylUser() error {
542542
return err
543543
}
544544

545+
ensureDefaultUserIDs := func() {
546+
if _config.System.User.Uid == 0 {
547+
_config.System.User.Uid = defaultUserID
548+
}
549+
if _config.System.User.Gid == 0 {
550+
_config.System.User.Gid = defaultUserID
551+
}
552+
}
553+
554+
logFinalIDs := func() {
555+
log.WithFields(log.Fields{
556+
"uid": _config.System.User.Uid,
557+
"gid": _config.System.User.Gid,
558+
}).Info("using system user identifiers")
559+
}
560+
545561
// Our way of detecting if elytra is running inside of Docker.
546562
if sysName == "distroless" {
547563
_config.System.Username = system.FirstNotEmpty(os.Getenv("ELYTRA_USERNAME"), "pyrodactyl")
548564
_config.System.User.Uid = system.MustInt(system.FirstNotEmpty(os.Getenv("ELYTRA_UID"), "8888"))
549565
_config.System.User.Gid = system.MustInt(system.FirstNotEmpty(os.Getenv("ELYTRA_GID"), "8888"))
566+
ensureDefaultUserIDs()
567+
logFinalIDs()
550568
return nil
551569
}
552570

@@ -559,6 +577,8 @@ func EnsurePterodactylUser() error {
559577
_config.System.Username = u.Username
560578
_config.System.User.Uid = system.MustInt(u.Uid)
561579
_config.System.User.Gid = system.MustInt(u.Gid)
580+
ensureDefaultUserIDs()
581+
logFinalIDs()
562582
return nil
563583
}
564584

@@ -573,11 +593,14 @@ func EnsurePterodactylUser() error {
573593
} else {
574594
_config.System.User.Uid = system.MustInt(u.Uid)
575595
_config.System.User.Gid = system.MustInt(u.Gid)
596+
ensureDefaultUserIDs()
597+
logFinalIDs()
576598
return nil
577599
}
578600

579601
// Alpine Linux is the only OS we currently support that doesn't work with the useradd
580602
// command, so in those cases we just modify the command a bit to work as expected.
603+
ensureDefaultUserIDs()
581604
gidStr := strconv.Itoa(_config.System.User.Gid)
582605
uidStr := strconv.Itoa(_config.System.User.Uid)
583606

@@ -611,6 +634,8 @@ func EnsurePterodactylUser() error {
611634
}
612635
_config.System.User.Uid = system.MustInt(u.Uid)
613636
_config.System.User.Gid = system.MustInt(u.Gid)
637+
ensureDefaultUserIDs()
638+
logFinalIDs()
614639
return nil
615640
}
616641

0 commit comments

Comments
 (0)