From 56a37e223ddf3c37a03b52c906e622bcfead7f11 Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 7 Oct 2024 10:08:14 -0500 Subject: [PATCH 1/4] added download info --- ee/debug/checkups/checkups.go | 1 + ee/debug/checkups/download_directory.go | 117 ++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 ee/debug/checkups/download_directory.go diff --git a/ee/debug/checkups/checkups.go b/ee/debug/checkups/checkups.go index 199ecee87..55fec1751 100644 --- a/ee/debug/checkups/checkups.go +++ b/ee/debug/checkups/checkups.go @@ -125,6 +125,7 @@ func checkupsFor(k types.Knapsack, target targetBits) []checkupInt { {&uninstallHistoryCheckup{k: k}, flareSupported}, {&desktopMenu{k: k}, flareSupported}, {&coredumpCheckup{}, doctorSupported | flareSupported}, + {&DownloadDirectory{}, flareSupported}, } checkupsToRun := make([]checkupInt, 0) diff --git a/ee/debug/checkups/download_directory.go b/ee/debug/checkups/download_directory.go new file mode 100644 index 000000000..fbe47279d --- /dev/null +++ b/ee/debug/checkups/download_directory.go @@ -0,0 +1,117 @@ +package checkups + +import ( + "context" + "fmt" + "io" + "os" + "path/filepath" + "runtime" + "strings" +) + +type DownloadDirectory struct { + status Status + summary string + files []string +} + +func (c *DownloadDirectory) Name() string { + return "Download directory contents" +} + +func (c *DownloadDirectory) Run(_ context.Context, extraFH io.Writer) error { + downloadDir := getDownloadDir() + if downloadDir == "" { + return fmt.Errorf("no default download directory") + } + + err := filepath.Walk(downloadDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() && isKolideInstaller(info.Name()) { + c.files = append(c.files, path) + } + return nil + }) + + switch { + case os.IsNotExist(err): + c.status = Warning + c.summary = fmt.Sprintf("download directory (%s) not present", downloadDir) + case err != nil: + c.status = Erroring + c.summary = fmt.Sprintf("error listing files in directory (%s): %s", downloadDir, err) + case len(c.files) == 0: + c.status = Warning + c.summary = fmt.Sprintf("no Kolide installers found in directory (%s)", downloadDir) + default: + c.status = Passing + fileNames := make([]string, len(c.files)) + for i, file := range c.files { + fileNames[i] = filepath.Base(file) + } + installerList := strings.Join(fileNames, ", ") + c.summary = fmt.Sprintf("Found Kolide installer(s) in directory (%s): %s", downloadDir, installerList) + + } + + if len(c.files) > 0 { + fmt.Fprintln(extraFH, "Kolide installers found:") + for _, file := range c.files { + fmt.Fprintln(extraFH, file) + } + } + + return nil +} + +func (c *DownloadDirectory) ExtraFileName() string { + return "kolide-installers" +} + +func (c *DownloadDirectory) Status() Status { + return c.status +} + +func (c *DownloadDirectory) Summary() string { + return c.summary +} + +func (c *DownloadDirectory) Data() any { + return c.files +} + +func getDownloadDir() string { + homeDir, err := os.UserHomeDir() + if err != nil { + return "" + } + + switch runtime.GOOS { + case "darwin": + return filepath.Join(homeDir, "Downloads") + case "linux": + return filepath.Join(homeDir, "Downloads") + case "windows": + return filepath.Join(homeDir, "Downloads") + } + + return "" +} + +func isKolideInstaller(filename string) bool { + lowerFilename := strings.ToLower(filename) + switch runtime.GOOS { + case "darwin": + return strings.HasSuffix(lowerFilename, "kolide-launcher.pkg") + case "linux": + return strings.HasSuffix(lowerFilename, "kolide-launcher.rpm") || + strings.HasSuffix(lowerFilename, "kolide-launcher.deb") || + strings.HasSuffix(lowerFilename, "kolide-launcher.pacman") + case "windows": + return strings.HasSuffix(lowerFilename, "kolide-launcher.msi") + } + return false +} From 875745d0416532e2a894511b07223785fc0e1f58 Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 7 Oct 2024 15:35:05 -0500 Subject: [PATCH 2/4] addresed feedback --- ee/debug/checkups/checkups.go | 2 +- ee/debug/checkups/download_directory.go | 118 ++++++++++++------------ 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/ee/debug/checkups/checkups.go b/ee/debug/checkups/checkups.go index 55fec1751..42ef6801d 100644 --- a/ee/debug/checkups/checkups.go +++ b/ee/debug/checkups/checkups.go @@ -125,7 +125,7 @@ func checkupsFor(k types.Knapsack, target targetBits) []checkupInt { {&uninstallHistoryCheckup{k: k}, flareSupported}, {&desktopMenu{k: k}, flareSupported}, {&coredumpCheckup{}, doctorSupported | flareSupported}, - {&DownloadDirectory{}, flareSupported}, + {&downloadDirectory{}, flareSupported}, } checkupsToRun := make([]checkupInt, 0) diff --git a/ee/debug/checkups/download_directory.go b/ee/debug/checkups/download_directory.go index fbe47279d..1557fad5f 100644 --- a/ee/debug/checkups/download_directory.go +++ b/ee/debug/checkups/download_directory.go @@ -10,51 +10,57 @@ import ( "strings" ) -type DownloadDirectory struct { +type downloadDirectory struct { status Status summary string - files []string + files []fileInfo } -func (c *DownloadDirectory) Name() string { - return "Download directory contents" +func (c *downloadDirectory) Name() string { + return "Download directory contents for all users" } -func (c *DownloadDirectory) Run(_ context.Context, extraFH io.Writer) error { - downloadDir := getDownloadDir() - if downloadDir == "" { - return fmt.Errorf("no default download directory") +func (c *downloadDirectory) Run(_ context.Context, extraFH io.Writer) error { + downloadDirs := getDownloadDirs() + if len(downloadDirs) == 0 { + c.status = Erroring + c.summary = "No download directories found" + return nil } - err := filepath.Walk(downloadDir, func(path string, info os.FileInfo, err error) error { + for _, downloadDir := range downloadDirs { + pattern := filepath.Join(downloadDir, "kolide-launcher*") + matches, err := filepath.Glob(pattern) + if err != nil { - return err + fmt.Fprintf(extraFH, "Error listing files in directory (%s): %s\n", downloadDir, err) + continue } - if !info.IsDir() && isKolideInstaller(info.Name()) { - c.files = append(c.files, path) + + for _, match := range matches { + if info, err := os.Stat(match); err == nil { + c.files = append(c.files, fileInfo{ + Name: match, + ModTime: info.ModTime(), + }) + } } - return nil - }) + } switch { - case os.IsNotExist(err): - c.status = Warning - c.summary = fmt.Sprintf("download directory (%s) not present", downloadDir) - case err != nil: - c.status = Erroring - c.summary = fmt.Sprintf("error listing files in directory (%s): %s", downloadDir, err) case len(c.files) == 0: - c.status = Warning - c.summary = fmt.Sprintf("no Kolide installers found in directory (%s)", downloadDir) + c.status = Informational + c.summary = "No Kolide installers found in any user's download directory" default: - c.status = Passing - fileNames := make([]string, len(c.files)) + c.status = Informational + fileInfos := make([]string, len(c.files)) for i, file := range c.files { - fileNames[i] = filepath.Base(file) + fileName := filepath.Base(file.Name) + modTime := file.ModTime.Format("Mon, 02 Jan 2006 15:04:05 MST") + fileInfos[i] = fmt.Sprintf("%s (Modified: %s)", fileName, modTime) } - installerList := strings.Join(fileNames, ", ") - c.summary = fmt.Sprintf("Found Kolide installer(s) in directory (%s): %s", downloadDir, installerList) - + installerList := strings.Join(fileInfos, ", ") + c.summary = fmt.Sprintf("Found Kolide installer(s) across user download directories: %s", installerList) } if len(c.files) > 0 { @@ -67,51 +73,45 @@ func (c *DownloadDirectory) Run(_ context.Context, extraFH io.Writer) error { return nil } -func (c *DownloadDirectory) ExtraFileName() string { - return "kolide-installers" +func (c *downloadDirectory) ExtraFileName() string { + return "kolide-installers-all-users.txt" } -func (c *DownloadDirectory) Status() Status { +func (c *downloadDirectory) Status() Status { return c.status } -func (c *DownloadDirectory) Summary() string { +func (c *downloadDirectory) Summary() string { return c.summary } -func (c *DownloadDirectory) Data() any { +func (c *downloadDirectory) Data() any { return c.files } -func getDownloadDir() string { - homeDir, err := os.UserHomeDir() - if err != nil { - return "" - } +func getDownloadDirs() []string { + var userDirs []string + var baseDir string - switch runtime.GOOS { - case "darwin": - return filepath.Join(homeDir, "Downloads") - case "linux": - return filepath.Join(homeDir, "Downloads") - case "windows": - return filepath.Join(homeDir, "Downloads") + if runtime.GOOS == "windows" { + baseDir = "C:\\Users" + } else { + baseDir = "/Users" } - return "" -} + entries, err := os.ReadDir(baseDir) + if err != nil { + return nil + } -func isKolideInstaller(filename string) bool { - lowerFilename := strings.ToLower(filename) - switch runtime.GOOS { - case "darwin": - return strings.HasSuffix(lowerFilename, "kolide-launcher.pkg") - case "linux": - return strings.HasSuffix(lowerFilename, "kolide-launcher.rpm") || - strings.HasSuffix(lowerFilename, "kolide-launcher.deb") || - strings.HasSuffix(lowerFilename, "kolide-launcher.pacman") - case "windows": - return strings.HasSuffix(lowerFilename, "kolide-launcher.msi") + for _, entry := range entries { + if entry.IsDir() { + userDir := filepath.Join(baseDir, entry.Name(), "Downloads") + if _, err := os.Stat(userDir); err == nil { + userDirs = append(userDirs, userDir) + } + } } - return false + + return userDirs } From 7943934667e0221364b72113023bf313a8e4a14b Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 7 Oct 2024 16:35:02 -0500 Subject: [PATCH 3/4] fixing basedir for linux --- ee/debug/checkups/download_directory.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ee/debug/checkups/download_directory.go b/ee/debug/checkups/download_directory.go index 1557fad5f..1cff5f0ef 100644 --- a/ee/debug/checkups/download_directory.go +++ b/ee/debug/checkups/download_directory.go @@ -95,8 +95,10 @@ func getDownloadDirs() []string { if runtime.GOOS == "windows" { baseDir = "C:\\Users" - } else { + } else if runtime.GOOS == "darwin" { baseDir = "/Users" + } else { + baseDir = "/home" } entries, err := os.ReadDir(baseDir) From d32103f6cb15d5c952ffd6903569cbe63645e0ed Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Tue, 8 Oct 2024 11:06:19 -0500 Subject: [PATCH 4/4] addressed comments --- ee/debug/checkups/download_directory.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ee/debug/checkups/download_directory.go b/ee/debug/checkups/download_directory.go index 1cff5f0ef..5079f9953 100644 --- a/ee/debug/checkups/download_directory.go +++ b/ee/debug/checkups/download_directory.go @@ -17,7 +17,7 @@ type downloadDirectory struct { } func (c *downloadDirectory) Name() string { - return "Download directory contents for all users" + return "Kolide Downloads" } func (c *downloadDirectory) Run(_ context.Context, extraFH io.Writer) error { @@ -107,11 +107,12 @@ func getDownloadDirs() []string { } for _, entry := range entries { - if entry.IsDir() { - userDir := filepath.Join(baseDir, entry.Name(), "Downloads") - if _, err := os.Stat(userDir); err == nil { - userDirs = append(userDirs, userDir) - } + if !entry.IsDir() { + continue + } + userDir := filepath.Join(baseDir, entry.Name(), "Downloads") + if _, err := os.Stat(userDir); err == nil { + userDirs = append(userDirs, userDir) } }