From bf40c48812fbafd77ec292d052af1d85b1a7f74c Mon Sep 17 00:00:00 2001 From: Dimitris Karakasilis Date: Thu, 14 Sep 2023 15:53:27 +0300 Subject: [PATCH] Move checkRoot to main Signed-off-by: Dimitris Karakasilis --- internal/agent/agent.go | 9 ------- internal/agent/install.go | 8 ------ internal/agent/interactive_install.go | 4 --- internal/agent/reset.go | 4 --- internal/agent/upgrade.go | 4 --- main.go | 36 +++++++++++++++++++++------ 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/internal/agent/agent.go b/internal/agent/agent.go index f64796d..4d4bb37 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -1,7 +1,6 @@ package agent import ( - "errors" "fmt" "os" "path/filepath" @@ -96,11 +95,3 @@ func Run(opts ...Option) error { } return err } - -func checkRoot() error { - if os.Geteuid() != 0 { - return errors.New("this command requires root privileges") - } - - return nil -} diff --git a/internal/agent/install.go b/internal/agent/install.go index ea9dfae..48ec687 100644 --- a/internal/agent/install.go +++ b/internal/agent/install.go @@ -55,10 +55,6 @@ func displayInfo(agentConfig *Config) { } func ManualInstall(c, device string, reboot, poweroff, strictValidations bool) error { - if err := checkRoot(); err != nil { - return err - } - ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -89,10 +85,6 @@ func ManualInstall(c, device string, reboot, poweroff, strictValidations bool) e } func Install(dir ...string) error { - if err := checkRoot(); err != nil { - return err - } - var cc *config.Config var err error diff --git a/internal/agent/interactive_install.go b/internal/agent/interactive_install.go index 0ab6b05..7279300 100644 --- a/internal/agent/interactive_install.go +++ b/internal/agent/interactive_install.go @@ -130,10 +130,6 @@ func detectDevice() string { } func InteractiveInstall(debug, spawnShell bool) error { - if err := checkRoot(); err != nil { - return err - } - var sshUsers []string bus.Manager.Initialize() diff --git a/internal/agent/reset.go b/internal/agent/reset.go index ca09cad..bbc9b0c 100644 --- a/internal/agent/reset.go +++ b/internal/agent/reset.go @@ -21,10 +21,6 @@ import ( ) func Reset(reboot, unattended bool, dir ...string) error { - if err := checkRoot(); err != nil { - return err - } - bus.Manager.Initialize() // This config is only for reset branding. diff --git a/internal/agent/upgrade.go b/internal/agent/upgrade.go index a6f310c..8231a34 100644 --- a/internal/agent/upgrade.go +++ b/internal/agent/upgrade.go @@ -53,10 +53,6 @@ func ListReleases(includePrereleases bool) semver.Collection { func Upgrade( version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) error { - if err := checkRoot(); err != nil { - return err - } - bus.Manager.Initialize() if version == "" && source == "" { diff --git a/main.go b/main.go index ec9799d..3d34a36 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "errors" "fmt" "os" "path/filepath" @@ -119,7 +120,8 @@ See https://kairos.io/docs/upgrade/manual/ for documentation. return fmt.Errorf("source %s does not match any of oci:, dir: or file: ", source) } } - return nil + + return checkRoot() }, Action: func(c *cli.Context) error { var v string @@ -230,6 +232,9 @@ E.g. kairos-agent install-bundle container:quay.io/kairos/kairos... }, }, UsageText: "Install a bundle manually in the node", + Before: func(c *cli.Context) error { + return checkRoot() + }, Action: func(c *cli.Context) error { if c.Args().Len() != 1 { return fmt.Errorf("bundle name required") @@ -382,6 +387,9 @@ This command is meant to be used from the boot GRUB menu, but can be also starte }, }, Usage: "Starts interactive installation", + Before: func(c *cli.Context) error { + return checkRoot() + }, Action: func(c *cli.Context) error { return agent.InteractiveInstall(c.Bool("debug"), c.Bool("shell")) }, @@ -403,6 +411,9 @@ This command is meant to be used from the boot GRUB menu, but can be also starte Name: "reboot", }, }, + Before: func(c *cli.Context) error { + return checkRoot() + }, Action: func(c *cli.Context) error { if c.NArg() == 0 { return fmt.Errorf("expect one argument. the config file - if you don't have it, use the interactive-install") @@ -424,6 +435,9 @@ See also https://kairos.io/docs/installation/qrcode/ for documentation. This command is meant to be used from the boot GRUB menu, but can be started manually`, Aliases: []string{"i"}, + Before: func(c *cli.Context) error { + return checkRoot() + }, Action: func(c *cli.Context) error { return agent.Install(configScanDir...) }, @@ -457,6 +471,9 @@ This command is meant to be used from the boot GRUB menu, but can likely be used Usage: "Do not wait for user input and provide ttys after reset. Also sets the fast mode (do not wait 60 seconds before reset)", }, }, + Before: func(c *cli.Context) error { + return checkRoot() + }, Action: func(c *cli.Context) error { reboot := c.Bool("reboot") unattended := c.Bool("unattended") @@ -535,7 +552,8 @@ The validate command expects a configuration file as its only argument. Local fi _ = cli.ShowSubcommandHelp(c) return fmt.Errorf("") } - return nil + + return checkRoot() }, Action: func(c *cli.Context) error { stage := c.Args().First() @@ -574,11 +592,7 @@ The validate command expects a configuration file as its only argument. Local fi return fmt.Errorf("") } - if os.Geteuid() != 0 { - return fmt.Errorf("this command requires root privileges") - } - - return nil + return checkRoot() }, Action: func(c *cli.Context) error { image := c.Args().Get(0) @@ -664,3 +678,11 @@ The kairos agent is a component to abstract away node ops, providing a common fe os.Exit(1) } } + +func checkRoot() error { + if os.Geteuid() != 0 { + return errors.New("this command requires root privileges") + } + + return nil +}