Check root permissions in commands that need it

Otherwise the error shown is not helpful (it fails too late with
confusing errors).

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis
2023-09-14 15:35:44 +03:00
parent 274701a3ac
commit fddbf3f657
7 changed files with 47 additions and 10 deletions

2
go.sum
View File

@@ -360,6 +360,8 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kairos-io/kairos-sdk v0.0.14 h1:yVKaay70cjmAUY6m4uLoV9UguBv3Dx3JxAh2faRlUZo= github.com/kairos-io/kairos-sdk v0.0.14 h1:yVKaay70cjmAUY6m4uLoV9UguBv3Dx3JxAh2faRlUZo=
github.com/kairos-io/kairos-sdk v0.0.14/go.mod h1:+fhVoCEA2hD7OFTmhDmy4pP3uC9q84OYhDa7irkEVWQ= github.com/kairos-io/kairos-sdk v0.0.14/go.mod h1:+fhVoCEA2hD7OFTmhDmy4pP3uC9q84OYhDa7irkEVWQ=
github.com/kairos-io/kairos-sdk v0.0.12 h1:+uibTjjsAh8klupXHWpWse2AKww6p8ROu8Ii28t3k48=
github.com/kairos-io/kairos-sdk v0.0.12/go.mod h1:AK9poWisuhnzri04diXnTG8L7EkOSUArSeeDn2LYFU0=
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329 h1:qq2nCpSrXrmvDGRxW0ruW9BVEV1CN2a9YDOExdt+U0o= github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329 h1:qq2nCpSrXrmvDGRxW0ruW9BVEV1CN2a9YDOExdt+U0o=
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329/go.mod h1:2VPVQDR4wO7KXHwP+DAypEy67rXf+okUx2zjgpCxZw4= github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329/go.mod h1:2VPVQDR4wO7KXHwP+DAypEy67rXf+okUx2zjgpCxZw4=
github.com/kendru/darwin/go/depgraph v0.0.0-20221105232959-877d6a81060c h1:eKb4PqwAMhlqwXw0W3atpKaYaPGlXE/Fwh+xpCEYaPk= github.com/kendru/darwin/go/depgraph v0.0.0-20221105232959-877d6a81060c h1:eKb4PqwAMhlqwXw0W3atpKaYaPGlXE/Fwh+xpCEYaPk=

View File

@@ -1,11 +1,13 @@
package agent package agent
import ( import (
"errors"
"fmt" "fmt"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"os" "os"
"path/filepath" "path/filepath"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks" hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/internal/bus" "github.com/kairos-io/kairos-agent/v2/internal/bus"
config "github.com/kairos-io/kairos-agent/v2/pkg/config" config "github.com/kairos-io/kairos-agent/v2/pkg/config"
@@ -94,3 +96,11 @@ func Run(opts ...Option) error {
} }
return err return err
} }
func checkRoot() error {
if os.Geteuid() != 0 {
return errors.New("this command requires root privileges")
}
return nil
}

View File

@@ -5,8 +5,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
"github.com/sanity-io/litter"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@@ -14,6 +12,9 @@ import (
"syscall" "syscall"
"time" "time"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
"github.com/sanity-io/litter"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks" hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/internal/bus" "github.com/kairos-io/kairos-agent/v2/internal/bus"
"github.com/kairos-io/kairos-agent/v2/internal/cmd" "github.com/kairos-io/kairos-agent/v2/internal/cmd"
@@ -54,6 +55,10 @@ func displayInfo(agentConfig *Config) {
} }
func ManualInstall(c, device string, reboot, poweroff, strictValidations bool) error { func ManualInstall(c, device string, reboot, poweroff, strictValidations bool) error {
if err := checkRoot(); err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@@ -84,6 +89,10 @@ func ManualInstall(c, device string, reboot, poweroff, strictValidations bool) e
} }
func Install(dir ...string) error { func Install(dir ...string) error {
if err := checkRoot(); err != nil {
return err
}
var cc *config.Config var cc *config.Config
var err error var err error

View File

@@ -130,6 +130,10 @@ func detectDevice() string {
} }
func InteractiveInstall(debug, spawnShell bool) error { func InteractiveInstall(debug, spawnShell bool) error {
if err := checkRoot(); err != nil {
return err
}
var sshUsers []string var sshUsers []string
bus.Manager.Initialize() bus.Manager.Initialize()

View File

@@ -3,6 +3,10 @@ package agent
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"sync"
"time"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks" hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/internal/bus" "github.com/kairos-io/kairos-agent/v2/internal/bus"
"github.com/kairos-io/kairos-agent/v2/internal/cmd" "github.com/kairos-io/kairos-agent/v2/internal/cmd"
@@ -12,14 +16,15 @@ import (
"github.com/kairos-io/kairos-sdk/collector" "github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/machine" "github.com/kairos-io/kairos-sdk/machine"
"github.com/kairos-io/kairos-sdk/utils" "github.com/kairos-io/kairos-sdk/utils"
"os"
"sync"
"time"
"github.com/mudler/go-pluggable" "github.com/mudler/go-pluggable"
) )
func Reset(reboot, unattended bool, dir ...string) error { func Reset(reboot, unattended bool, dir ...string) error {
if err := checkRoot(); err != nil {
return err
}
bus.Manager.Initialize() bus.Manager.Initialize()
// This config is only for reset branding. // This config is only for reset branding.

View File

@@ -4,10 +4,12 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/spf13/viper"
"sort" "sort"
"github.com/spf13/viper"
hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/kairos-io/kairos-agent/v2/internal/bus" "github.com/kairos-io/kairos-agent/v2/internal/bus"
"github.com/kairos-io/kairos-agent/v2/pkg/action" "github.com/kairos-io/kairos-agent/v2/pkg/action"
@@ -51,6 +53,10 @@ func ListReleases(includePrereleases bool) semver.Collection {
func Upgrade( func Upgrade(
version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) error { version, source string, force, strictValidations bool, dirs []string, preReleases, upgradeRecovery bool) error {
if err := checkRoot(); err != nil {
return err
}
bus.Manager.Initialize() bus.Manager.Initialize()
if version == "" && source == "" { if version == "" && source == "" {

View File

@@ -4,13 +4,14 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/kairos-io/kairos-agent/v2/pkg/utils"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
"github.com/kairos-io/kairos-agent/v2/pkg/utils"
"github.com/kairos-io/kairos-agent/v2/internal/agent" "github.com/kairos-io/kairos-agent/v2/internal/agent"
"github.com/kairos-io/kairos-agent/v2/internal/bus" "github.com/kairos-io/kairos-agent/v2/internal/bus"
"github.com/kairos-io/kairos-agent/v2/internal/common" "github.com/kairos-io/kairos-agent/v2/internal/common"