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

🐛 use system path on default when root user is used #1809

Merged
merged 1 commit into from
Sep 20, 2023
Merged
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
7 changes: 7 additions & 0 deletions apps/cnquery/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func init() {
logger.CliCompactLogger(logger.LogOutputWriter)
zerolog.SetGlobalLevel(zerolog.InfoLevel)

// TODO harmonize with initLogger, which is called later and attached to the command
// here we set the log level only by environment variable
envLevel, ok := logger.GetEnvLogLevel()
if ok {
logger.Set(envLevel)
}

config.DefaultConfigFile = "mondoo.yml"

rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose output")
Expand Down
10 changes: 9 additions & 1 deletion providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ import (
var (
SystemPath string
HomePath string
// this is the default path for providers, it's either system or home path, if the user is root the system path is used
DefaultPath string
// CachedProviders contains all providers that have been loaded the last time
// ListActive or ListAll have been called
CachedProviders []*Provider
)

func init() {
SystemPath = config.SystemDataPath("providers")
DefaultPath = SystemPath
if os.Geteuid() != 0 {
HomePath, _ = config.HomePath("providers")
DefaultPath = HomePath
}
}

Expand Down Expand Up @@ -198,14 +202,15 @@ func installVersion(name string, version string) (*Provider, error) {
url = strings.ReplaceAll(url, "{OS}", runtime.GOOS)
url = strings.ReplaceAll(url, "{ARCH}", runtime.GOARCH)

log.Debug().Str("url", url).Msg("installing provider from URL")
res, err := http.Get(url)
if err != nil {
log.Debug().Str("url", url).Msg("failed to install form URL (get request)")
return nil, errors.Wrap(err, "failed to install "+name+"-"+version)
}

installed, err := InstallIO(res.Body, InstallConf{
Dst: HomePath,
Dst: DefaultPath,
})
if err != nil {
log.Debug().Str("url", url).Msg("failed to install form URL (download)")
Expand Down Expand Up @@ -298,7 +303,9 @@ func InstallIO(reader io.ReadCloser, conf InstallConf) ([]*Provider, error) {
if conf.Dst == "" {
conf.Dst = HomePath
}

if !config.ProbeDir(conf.Dst) {
log.Debug().Str("path", conf.Dst).Msg("creating providers directory")
if err := os.MkdirAll(conf.Dst, 0o755); err != nil {
return nil, errors.New("failed to create " + conf.Dst)
}
Expand All @@ -307,6 +314,7 @@ func InstallIO(reader io.ReadCloser, conf InstallConf) ([]*Provider, error) {
}
}

log.Debug().Msg("create temp directory to unpack providers")
tmpdir, err := os.MkdirTemp(conf.Dst, ".providers-unpack")
if err != nil {
return nil, errors.Wrap(err, "failed to create temporary directory to unpack files")
Expand Down