Skip to content

Commit f7dfbc1

Browse files
author
Antonio Salinas
authored
refactor: Removed line preventing azldev to run as root (#369)
* Removed line preventing azldev to run as root * Added cobra annotation to conditionally allow execution as root * Fixed minor lint issue
1 parent c701672 commit f7dfbc1

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

cmd/azldev/azldev.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package main
55

66
import (
7-
"fmt"
87
"os"
98

109
"github.com/microsoft/azldev/internal/app/azldev"
@@ -18,13 +17,6 @@ import (
1817
)
1918

2019
func main() {
21-
// Make sure we're *not* running as root.
22-
if os.Geteuid() == 0 {
23-
fmt.Fprintln(os.Stderr,
24-
"error: this tool may not be run as root; it will internally invoke sub-commands as sudo as needed")
25-
os.Exit(1)
26-
}
27-
2820
// Instantiate the main CLI app instance.
2921
app := InstantiateApp()
3022

internal/app/azldev/app.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ func NewApp(fsFactory opctx.FileSystemFactory, osEnvFactory opctx.OSEnvFactory)
107107
Long: usageInfo,
108108
Version: displayVersion,
109109
PersistentPreRunE: func(command *cobra.Command, _ []string) error {
110+
slog.Debug("Command annotations", "annotations", command.Annotations)
111+
112+
if _, ok := command.Annotations[CommandAnnotationRootOK]; !ok && os.Geteuid() == 0 {
113+
return errors.New("this command may not be run as root")
114+
}
115+
110116
env, err := GetEnvFromCommand(command)
111117
if err != nil {
112118
return err

internal/app/azldev/cmds/component/preparesources.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func NewPrepareSourcesCmd() *cobra.Command {
3737
return nil, PrepareComponentSources(env, &options)
3838
}),
3939
ValidArgsFunction: components.GenerateComponentNameCompletions,
40+
Annotations: map[string]string{
41+
azldev.CommandAnnotationRootOK: "true",
42+
},
4043
}
4144

4245
components.AddComponentFilterOptionsToCommand(cmd, &options.ComponentFilter)

internal/app/azldev/command.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ import (
1616
"github.com/spf13/cobra"
1717
)
1818

19+
const (
20+
// CommandAnnotationRootOK is a [cobra.Command.Annotations] key used to indicate that a command
21+
// is allowed to be run as root.
22+
CommandAnnotationRootOK = "rootOK"
23+
)
24+
1925
const (
2026
// CommandGroupMeta represents the command group for meta commands like help, completion, and version.
2127
CommandGroupMeta = "meta"

0 commit comments

Comments
 (0)