Skip to content

Commit

Permalink
feature: check the invoker user, make possible to run as root if its …
Browse files Browse the repository at this point in the history
…really necessary
  • Loading branch information
janosmiko committed Feb 2, 2021
1 parent f0df945 commit 0685d7a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.4-beta
0.1.5-beta
18 changes: 18 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ var rootCmd = &cobra.Command{
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return CheckInvokerUser(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
return RootCmd(cmd)
},
Expand Down Expand Up @@ -225,3 +228,18 @@ func RootCmd(cmd *cobra.Command) error {

return nil
}

// CheckInvokerUser returns an error if the invoker user is root.
func CheckInvokerUser(cmd *cobra.Command) error {
// If the REWARD_ALLOW_SUPERUSER=1 is set or the Distro is Windows then we can skip this.
if IsAllowedSuperuser() || GetOSDistro() == "windows" {
return nil
}

// Most of the commands should run by normal users except `self-update`.
if cmd.Name() != "self-update" && IsAdmin() {
return ErrInvokedAsRootUser
}

return nil
}
3 changes: 2 additions & 1 deletion cmd/selfUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var selfUpdateCmd = &cobra.Command{
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
},
Args: cobra.ExactArgs(0),
Aliases: []string{"selfpudate"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return SelfUpdateCmd(cmd)
},
Expand Down
11 changes: 11 additions & 0 deletions internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ var (
AppName)
ErrCannotFindContainer = errors.New("container cannot be found")
ErrArgumentRequired = errors.New("argument required")
ErrInvokedAsRootUser = errors.New("In most cases, you should not run " +
AppName + " as root user except for `self-update`. " + "If you are sure you want to do this, use " +
strings.ToUpper(AppName) + "_ALLOW_SUPERUSER=1.")
)

func FileNotFoundError(op string) error {
Expand Down Expand Up @@ -278,6 +281,14 @@ func IsContainerRunning(name string) bool {
return err == nil
}

func IsAllowedSuperuser() bool {
if viper.IsSet(AppName + "_allow_superuser") {
return viper.GetBool(AppName + "_allow_superuser")
}

return false
}

// ContainsString checks if a slice of string contains a string.
func ContainsString(slice []string, val string) bool {
for _, item := range slice {
Expand Down
20 changes: 11 additions & 9 deletions internal/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ debug: false
#reward_mailhog: 0
#reward_phpmyadmin: 0
#reward_elastichq: 0
#reward_allow_superuser: 0
`

func InstallCmd() error {
Expand Down Expand Up @@ -75,6 +77,15 @@ func uninstall() error {
}

func install() error {
// On windows this command should run in elevated command prompt
osDistro := GetOSDistro()
if osDistro == "windows" {
if !IsAdmin() {
log.Printf("Running %v in an Elevated command prompt...", AppName)
RunMeElevated()
}
}

appHomeDir := GetAppHomeDir()

err := CreateDir(appHomeDir, getInstallModeFlag())
Expand All @@ -101,15 +112,6 @@ func install() error {
}
}

// On windows this command should run in elevated command prompt
osDistro := GetOSDistro()
if osDistro == "windows" {
if !IsAdmin() {
log.Printf("Running %v in an Elevated command prompt...", AppName)
RunMeElevated()
}
}

// CA Cert
if !getInstallDNSFlag() && !getInstallSSHKeyFlag() && !getInstallSSHConfigFlag() {
sslDir := filepath.Join(appHomeDir, "ssl")
Expand Down

0 comments on commit 0685d7a

Please sign in to comment.