From fdbec339cacefa45a58e17a31166f3d1037caee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Guid=C3=A9e?= Date: Thu, 28 Sep 2023 00:58:34 -0400 Subject: [PATCH] Forward port and host from kernel to vertex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Quentin Guidée --- cmd/kernel/main.go | 37 +++++++++++++++++++++++++------------ config/config_kernel.go | 16 ++++++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 config/config_kernel.go diff --git a/cmd/kernel/main.go b/cmd/kernel/main.go index c48deefd..04ceb0ac 100644 --- a/cmd/kernel/main.go +++ b/cmd/kernel/main.go @@ -10,6 +10,7 @@ import ( "strconv" "syscall" + "github.com/vertex-center/vertex/config" "github.com/vertex-center/vertex/pkg/log" "github.com/vertex-center/vertex/router" "github.com/vertex-center/vlog" @@ -18,6 +19,8 @@ import ( func main() { ensureRoot() + parseArgs() + shutdownChan := make(chan os.Signal, 1) // Vertex-Kernel @@ -65,43 +68,53 @@ func ensureRoot() { } } -func getUserAndGroupID() (uid uint32, gid uint32, err error) { +func parseArgs() { flagUsername := flag.String("user", "", "username of the unprivileged user") flagUID := flag.Uint("uid", 0, "uid of the unprivileged user") flagGID := flag.Uint("gid", 0, "gid of the unprivileged user") + // Copy/paste from cmd/main/main.go + flagPort := flag.String("port", config.Current.Port, "The Vertex port") + flagHost := flag.String("host", config.Current.Host, "The Vertex access url") + flag.Parse() + config.Current.Host = *flagHost + config.Current.Port = *flagPort + if *flagUsername != "" { u, err := user.Lookup(*flagUsername) if err != nil { - return 0, 0, err + log.Error(err) + os.Exit(1) } uid, err := strconv.ParseInt(u.Uid, 10, 32) if err != nil { - return 0, 0, err + log.Error(err) + os.Exit(1) } gid, err := strconv.ParseInt(u.Gid, 10, 32) if err != nil { - return 0, 0, err + log.Error(err) + os.Exit(1) } - return uint32(uid), uint32(gid), nil + config.KernelCurrent.Uid = uint32(uid) + config.KernelCurrent.Gid = uint32(gid) + return } - return uint32(*flagUID), uint32(*flagGID), nil + config.KernelCurrent.Uid = uint32(*flagUID) + config.KernelCurrent.Gid = uint32(*flagGID) } func runVertex() (*exec.Cmd, error) { - uid, gid, err := getUserAndGroupID() - if err != nil { - return nil, err - } + uid, gid := config.KernelCurrent.Uid, config.KernelCurrent.Gid // If go.mod is there, build vertex first. - _, err = os.Stat("go.mod") + _, err := os.Stat("go.mod") if err == nil { log.Info("init.go found. Building vertex...") buildVertex() @@ -122,7 +135,7 @@ func runVertex() (*exec.Cmd, error) { vlog.Uint32("gid", gid), ) - cmd = exec.Command("./vertex") + cmd = exec.Command("./vertex", "-port", config.Current.Port, "-host", config.Current.Host) cmd.SysProcAttr = &syscall.SysProcAttr{} cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uid, Gid: gid} cmd.Stdout = os.Stdout diff --git a/config/config_kernel.go b/config/config_kernel.go new file mode 100644 index 00000000..bc62f010 --- /dev/null +++ b/config/config_kernel.go @@ -0,0 +1,16 @@ +package config + +var KernelCurrent = NewKernel() + +type Kernel struct { + Config + + Uid uint32 `json:"uid"` + Gid uint32 `json:"gid"` +} + +func NewKernel() Kernel { + return Kernel{ + Config: New(), + } +}