From 6726c4dafe3ab6c8143034595b909a264af0d494 Mon Sep 17 00:00:00 2001 From: Bryce Kahle Date: Thu, 28 Sep 2023 12:20:37 -0700 Subject: [PATCH] only use sudo when necessary --- .gitignore | 4 +++- pkg/pkg/utils.go | 5 ++--- pkg/repo/rhel.go | 3 ++- pkg/repo/utils.go | 6 ++++-- pkg/utils/btf.go | 8 ++++++++ 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d888caa2..8b813b6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ +# binary btfhub # arvhices archive/* custom-archive/* # makefile leftovers .check* -# binary +# JetBrains +.idea/ diff --git a/pkg/pkg/utils.go b/pkg/pkg/utils.go index 6760300c..0f3eb566 100644 --- a/pkg/pkg/utils.go +++ b/pkg/pkg/utils.go @@ -31,9 +31,8 @@ func yumDownload(ctx context.Context, pkg string, destdir string) error { destDirParam := fmt.Sprintf("--downloaddir=%s", destdir) - cmd := exec.CommandContext(ctx, - "sudo", "yum", "install", "-y", "--downloadonly", destDirParam, pkg, - ) + binary, args := utils.SudoCMD("yum", "install", "-y", "--downloadonly", destDirParam, pkg) + cmd := exec.CommandContext(ctx, binary, args...) cmd.Stdout = os.Stdout cmd.Stderr = stderr diff --git a/pkg/repo/rhel.go b/pkg/repo/rhel.go index 9b6833df..098af1fd 100644 --- a/pkg/repo/rhel.go +++ b/pkg/repo/rhel.go @@ -45,7 +45,8 @@ func (d *RHELRepo) GetKernelPackages( ) error { altArch := d.archs[arch] rver := d.releaseVersions[release+":"+altArch] - if err := utils.RunCMD(ctx, "", "sudo", "subscription-manager", "release", fmt.Sprintf("--set=%s", rver)); err != nil { + binary, args := utils.SudoCMD("subscription-manager", "release", fmt.Sprintf("--set=%s", rver)) + if err := utils.RunCMD(ctx, "", binary, args...); err != nil { return err } diff --git a/pkg/repo/utils.go b/pkg/repo/utils.go index 25149fe9..8c679705 100644 --- a/pkg/repo/utils.go +++ b/pkg/repo/utils.go @@ -12,11 +12,12 @@ import ( "path/filepath" "strings" + "golang.org/x/exp/maps" + "github.com/aquasecurity/btfhub/pkg/job" "github.com/aquasecurity/btfhub/pkg/kernel" "github.com/aquasecurity/btfhub/pkg/pkg" "github.com/aquasecurity/btfhub/pkg/utils" - "golang.org/x/exp/maps" ) func parseYumPackages(rdr io.Reader, minVersion kernel.Version) ([]pkg.Package, error) { @@ -62,7 +63,8 @@ func parseYumPackages(rdr io.Reader, minVersion kernel.Version) ([]pkg.Package, func yumSearch(ctx context.Context, pkg string) (*bytes.Buffer, error) { stdout := &bytes.Buffer{} stderr := &bytes.Buffer{} - cmd := exec.CommandContext(ctx, "sudo", "yum", "search", "--showduplicates", pkg) + binary, args := utils.SudoCMD("yum", "search", "--showduplicates", pkg) + cmd := exec.CommandContext(ctx, binary, args...) cmd.Stdout = stdout cmd.Stderr = stderr if err := cmd.Run(); err != nil { diff --git a/pkg/utils/btf.go b/pkg/utils/btf.go index 07dd7801..56b9223f 100644 --- a/pkg/utils/btf.go +++ b/pkg/utils/btf.go @@ -5,6 +5,7 @@ import ( "context" "debug/elf" "fmt" + "os" "os/exec" "strings" ) @@ -34,3 +35,10 @@ func RunCMD(ctx context.Context, cwd string, binary string, args ...string) erro return nil } + +func SudoCMD(binary string, args ...string) (string, []string) { + if os.Getuid() != 0 { + return "sudo", append([]string{binary}, args...) + } + return binary, args +}