Skip to content

Commit e6aff48

Browse files
authored
feat: Prompt for privileged user if host collectors present in spec (#1513)
* feat: Prompt for privileged user if host collectors present * Prompt preflight checks that have host collectors * Show cursor before prompting
1 parent 76c52d2 commit e6aff48

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ func runTroubleshoot(v *viper.Viper, args []string) error {
106106
})
107107
}
108108

109+
if interactive {
110+
c := color.New()
111+
c.Println(fmt.Sprintf("\r%s\r", cursor.ClearEntireLine()))
112+
}
113+
114+
if interactive {
115+
if len(mainBundle.Spec.HostCollectors) > 0 && !util.IsRunningAsRoot() {
116+
msg := "Some host collectors may require elevated privileges to run.\nDo you want to exit and rerun the command as a privileged user?"
117+
fmt.Print(cursor.Show())
118+
if util.PromptYesNo(msg) {
119+
fmt.Println("Exiting...")
120+
return nil
121+
}
122+
fmt.Print(cursor.Hide())
123+
}
124+
}
125+
109126
var wg sync.WaitGroup
110127
collectorCB := func(c chan interface{}, msg string) { c <- msg }
111128
progressChan := make(chan interface{})
@@ -174,11 +191,6 @@ func runTroubleshoot(v *viper.Viper, args []string) error {
174191

175192
nonInteractiveOutput := analysisOutput{}
176193

177-
if interactive {
178-
c := color.New()
179-
c.Println(fmt.Sprintf("\r%s\r", cursor.ClearEntireLine()))
180-
}
181-
182194
response, err := supportbundle.CollectSupportBundleFromSpec(&mainBundle.Spec, additionalRedactors, createOpts)
183195
if err != nil {
184196
return errors.Wrap(err, "failed to run collect and analyze process")

internal/util/util.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package util
22

33
import (
4+
"bufio"
45
"bytes"
6+
"fmt"
57
"net/url"
68
"os"
9+
"os/user"
710
"strings"
811
"text/template"
912

@@ -105,3 +108,38 @@ func RenderTemplate(tpl string, data interface{}) (string, error) {
105108
// Return the string representation of the buffer
106109
return buf.String(), nil
107110
}
111+
112+
func IsRunningAsRoot() bool {
113+
currentUser, err := user.Current()
114+
if err != nil {
115+
return false
116+
}
117+
118+
// Check if the user ID is 0 (root's UID)
119+
return currentUser.Uid == "0"
120+
}
121+
122+
func PromptYesNo(question string) bool {
123+
reader := bufio.NewReader(os.Stdin)
124+
125+
for {
126+
fmt.Printf("%s (yes/no): ", question)
127+
128+
response, err := reader.ReadString('\n')
129+
if err != nil {
130+
fmt.Println("Error reading response:", err)
131+
continue
132+
}
133+
134+
response = strings.TrimSpace(response)
135+
response = strings.ToLower(response)
136+
137+
if response == "yes" || response == "y" {
138+
return true
139+
} else if response == "no" || response == "n" {
140+
return false
141+
} else {
142+
fmt.Println("Please type 'yes' or 'no'.")
143+
}
144+
}
145+
}

pkg/preflight/run.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
cursor "github.com/ahmetalpbalkan/go-cursor"
1111
"github.com/fatih/color"
1212
"github.com/pkg/errors"
13+
"github.com/replicatedhq/troubleshoot/internal/util"
1314
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
1415
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1516
"github.com/replicatedhq/troubleshoot/pkg/constants"
@@ -46,6 +47,17 @@ func RunPreflights(interactive bool, output string, format string, args []string
4647
return types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, err)
4748
}
4849

50+
if interactive {
51+
if len(specs.HostPreflightsV1Beta2) > 0 && !util.IsRunningAsRoot() {
52+
fmt.Print(cursor.Show())
53+
if util.PromptYesNo("Some host collectors may require elevated privileges to run.\nDo you want to exit and rerun the command as a privileged user?") {
54+
fmt.Println("Exiting...")
55+
return nil
56+
}
57+
fmt.Print(cursor.Hide())
58+
}
59+
}
60+
4961
warning := validatePreflight(specs)
5062
if warning != nil {
5163
fmt.Println(warning.Warning())

0 commit comments

Comments
 (0)