Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AllowRoot setting to control if a cli is allowed to run as root #192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type App struct {
OnExit OnExit
ContextConfig func(Context, context.Context) context.Context
ContextOptions []ContextOption
AllowRoot bool
}

type Option func(*App)
Expand All @@ -33,9 +34,10 @@ type ContextOption func(*Context)

func NewApp(opts ...Option) *App {
app := &App{
Stdout: os.Stdout,
Stderr: os.Stderr,
OnExit: newOnExit(),
Stdout: os.Stdout,
Stderr: os.Stderr,
OnExit: newOnExit(),
AllowRoot: false,
}
for _, opt := range opts {
opt(app)
Expand Down
1 change: 1 addition & 0 deletions cli/cliviper/cliviper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestCLIViperApp(t *testing.T) {

// set cliviper.App() option
app := cli.NewApp(cliviper.App())
app.AllowRoot = true
app.Flags = []flag.Flag{
flag.StringFlag{Name: msgFlag},
}
Expand Down
1 change: 1 addition & 0 deletions cli/debugapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestNewDebugApp(t *testing.T) {
{err: testExitCoder{error: fmt.Errorf("foo"), exitCode: 2}, wantExitCode: 2, errorStringer: testErrorStringer, wantDebugFalse: "^foo\n$", wantDebugTrue: "^error-stringer\n$"},
} {
app := cli.NewApp(cli.DebugHandler(currCase.errorStringer))
app.AllowRoot = true
app.Action = func(ctx cli.Context) error {
return currCase.err
}
Expand Down
1 change: 1 addition & 0 deletions cli/exitcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

func main() {
app := cli.NewApp()
app.AllowRoot = true
app.Action = func(ctx cli.Context) error {
%v
}
Expand Down
2 changes: 2 additions & 0 deletions cli/flagvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestBindFlagValues(t *testing.T) {
},
} {
app := cli.NewApp()
app.AllowRoot = true
app.Command = cli.Command{
Name: "foo",
Flags: []flag.Flag{
Expand Down Expand Up @@ -111,6 +112,7 @@ func TestBindFlagValuesStringParam(t *testing.T) {
},
} {
app := cli.NewApp()
app.AllowRoot = true
app.Command = cli.Command{
Name: "foo",
Flags: []flag.Flag{
Expand Down
1 change: 1 addition & 0 deletions cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func TestParseFlags(t *testing.T) {
t.Run(currCase.name, func(t *testing.T) {
app := cli.NewApp()
app.Name = "test"
app.AllowRoot = true

output := &bytes.Buffer{}
app.Subcommands = []cli.Command{
Expand Down
7 changes: 7 additions & 0 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func (app *App) Run(args []string) (exitStatus int) {
return 0
}

if !app.AllowRoot {
if syscall.Getuid() == 0 {
ctx.Errorf("%v\n", "Root is not allowed to run this program.")
return 1
}
}

baseContext := context.Background()
if app.ContextConfig != nil {
baseContext = app.ContextConfig(ctx, baseContext)
Expand Down
5 changes: 5 additions & 0 deletions cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestRunErrorOutput(t *testing.T) {

for i, currCase := range cases {
app := cli.NewApp()
app.AllowRoot = true
app.Action = func(ctx cli.Context) error {
return currCase.err
}
Expand Down Expand Up @@ -79,6 +80,7 @@ func TestRunErrorHandler(t *testing.T) {

for i, currCase := range cases {
app := cli.NewApp()
app.AllowRoot = true
app.ErrorHandler = currCase.handler
app.Action = func(ctx cli.Context) error {
return currCase.err
Expand Down Expand Up @@ -118,6 +120,7 @@ func TestRunContext(t *testing.T) {
name: "check that context is propagated to app action",
check: func(t *testing.T) {
app := cli.NewApp()
app.AllowRoot = true

app.Command.Flags = []flag.Flag{
flag.StringFlag{
Expand All @@ -136,6 +139,7 @@ func TestRunContext(t *testing.T) {
name: "check that context is propagated to app error handler",
check: func(t *testing.T) {
app := cli.NewApp()
app.AllowRoot = true

app.Command.Flags = []flag.Flag{
flag.StringFlag{
Expand All @@ -162,6 +166,7 @@ func TestRunContext(t *testing.T) {
name: "check that context is propagated to app subcommand",
check: func(t *testing.T) {
app := cli.NewApp()
app.AllowRoot = true

app.Command.Flags = []flag.Flag{
flag.StringFlag{
Expand Down