From 68f1cf67569167ea484515a4128c900663740ddb Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 11 Nov 2024 11:11:50 -0600 Subject: [PATCH 1/8] Refactor log file handling to standardize zip creation and improve error reporting --- ee/debug/checkups/download_directory.go | 42 +++++++++---------- ee/debug/checkups/init_logs_darwin.go | 15 +------ ee/debug/checkups/init_logs_linux.go | 9 ++-- ee/debug/checkups/init_logs_windows.go | 10 ++--- ee/debug/checkups/install.go | 41 +----------------- ee/debug/checkups/launchd_darwin.go | 48 ++++++--------------- ee/debug/checkups/logs.go | 15 ++----- ee/debug/checkups/power_windows.go | 49 ++++++++-------------- ee/debug/checkups/util.go | 56 +++++++++++++++++++++++-- 9 files changed, 119 insertions(+), 166 deletions(-) diff --git a/ee/debug/checkups/download_directory.go b/ee/debug/checkups/download_directory.go index 5079f9953..4cc698266 100644 --- a/ee/debug/checkups/download_directory.go +++ b/ee/debug/checkups/download_directory.go @@ -1,6 +1,7 @@ package checkups import ( + "archive/zip" "context" "fmt" "io" @@ -28,21 +29,27 @@ func (c *downloadDirectory) Run(_ context.Context, extraFH io.Writer) error { return nil } - for _, downloadDir := range downloadDirs { - pattern := filepath.Join(downloadDir, "kolide-launcher*") - matches, err := filepath.Glob(pattern) + if extraFH != io.Discard { + zipWriter := zip.NewWriter(extraFH) + defer zipWriter.Close() - if err != nil { - fmt.Fprintf(extraFH, "Error listing files in directory (%s): %s\n", downloadDir, err) - continue - } + for _, downloadDir := range downloadDirs { + pattern := filepath.Join(downloadDir, "kolide-launcher*") + matches, err := filepath.Glob(pattern) + if err != nil { + continue + } - for _, match := range matches { - if info, err := os.Stat(match); err == nil { - c.files = append(c.files, fileInfo{ - Name: match, - ModTime: info.ModTime(), - }) + for _, match := range matches { + if info, err := os.Stat(match); err == nil { + c.files = append(c.files, fileInfo{ + Name: match, + ModTime: info.ModTime(), + }) + if err := addFileToZip(zipWriter, match); err != nil { + fmt.Fprintf(extraFH, "Error adding file to zip %s: %v\n", match, err) + } + } } } } @@ -63,18 +70,11 @@ func (c *downloadDirectory) Run(_ context.Context, extraFH io.Writer) error { c.summary = fmt.Sprintf("Found Kolide installer(s) across user download directories: %s", 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-all-users.txt" + return "kolide-installers-all-users.zip" } func (c *downloadDirectory) Status() Status { diff --git a/ee/debug/checkups/init_logs_darwin.go b/ee/debug/checkups/init_logs_darwin.go index 12662a10d..79df56cfa 100644 --- a/ee/debug/checkups/init_logs_darwin.go +++ b/ee/debug/checkups/init_logs_darwin.go @@ -4,8 +4,6 @@ import ( "archive/zip" "context" "fmt" - "io" - "os" "path/filepath" ) @@ -17,20 +15,9 @@ func writeInitLogs(_ context.Context, logZip *zip.Writer) error { var lastErr error for _, f := range stdMatches { - out, err := logZip.Create(filepath.Base(f)) - if err != nil { + if err := addFileToZip(logZip, f); err != nil { lastErr = err - continue } - - in, err := os.Open(f) - if err != nil { - lastErr = err - continue - } - defer in.Close() - - io.Copy(out, in) } return lastErr diff --git a/ee/debug/checkups/init_logs_linux.go b/ee/debug/checkups/init_logs_linux.go index 8efeb249e..065ccc072 100644 --- a/ee/debug/checkups/init_logs_linux.go +++ b/ee/debug/checkups/init_logs_linux.go @@ -2,8 +2,10 @@ package checkups import ( "archive/zip" + "bytes" "context" "fmt" + "time" "github.com/kolide/launcher/ee/allowedcmd" ) @@ -14,13 +16,10 @@ func writeInitLogs(ctx context.Context, logZip *zip.Writer) error { return fmt.Errorf("creating journalctl command: %w", err) } - outFile, err := logZip.Create("linux_journalctl_launcher_logs.json") + output, err := cmd.Output() if err != nil { return fmt.Errorf("creating linux_journalctl_launcher_logs.json: %w", err) } - cmd.Stderr = outFile - cmd.Stdout = outFile - - return cmd.Run() + return addStreamToZip(logZip, "linux_journalctl_launcher_logs.json", time.Now(), bytes.NewReader(output)) } diff --git a/ee/debug/checkups/init_logs_windows.go b/ee/debug/checkups/init_logs_windows.go index a62ca7c1b..bd967d11e 100644 --- a/ee/debug/checkups/init_logs_windows.go +++ b/ee/debug/checkups/init_logs_windows.go @@ -2,8 +2,10 @@ package checkups import ( "archive/zip" + "bytes" "context" "fmt" + "time" "github.com/kolide/launcher/ee/allowedcmd" ) @@ -15,13 +17,11 @@ func writeInitLogs(ctx context.Context, logZip *zip.Writer) error { return fmt.Errorf("creating powershell command: %w", err) } - outFile, err := logZip.Create("windows_launcher_events.json") + // Capture command output + output, err := cmd.Output() if err != nil { return fmt.Errorf("creating windows_launcher_events.json: %w", err) } - cmd.Stderr = outFile - cmd.Stdout = outFile - - return cmd.Run() + return addStreamToZip(logZip, "windows_launcher_events.json", time.Now(), bytes.NewReader(output)) } diff --git a/ee/debug/checkups/install.go b/ee/debug/checkups/install.go index a6b99b820..3c5ed90bd 100644 --- a/ee/debug/checkups/install.go +++ b/ee/debug/checkups/install.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "os" "runtime" "github.com/kolide/launcher/pkg/launcher" @@ -55,52 +54,16 @@ func gatherInstallationLogs(z *zip.Writer) error { return nil } - out, err := z.Create("macos-var-log-install.log") - if err != nil { - return fmt.Errorf("creating macos-var-log-install.log in zip: %w", err) - } - - installLog, err := os.Open("/var/log/install.log") - if err != nil { - return fmt.Errorf("opening /var/log/install.log: %w", err) - } - defer installLog.Close() - - if _, err := io.Copy(out, installLog); err != nil { - return fmt.Errorf("writing /var/log/install.log contents to zip: %w", err) - } - - return nil + return addFileToZip(z, "/var/log/install.log") } func gatherInstallerInfo(z *zip.Writer) error { if runtime.GOOS == "windows" { - // Installer info is not available on Windows return nil } configDir := launcher.DefaultPath(launcher.EtcDirectory) installerInfoPath := fmt.Sprintf("%s/installer-info.json", configDir) - installerInfoFile, err := os.Open(installerInfoPath) - if err != nil { - // If the file doesn't exist, you might want to skip without error - if os.IsNotExist(err) { - return nil - } - return fmt.Errorf("opening %s: %w", installerInfoPath, err) - } - defer installerInfoFile.Close() - - installerInfoZipPath := "installer-info.json" - out, err := z.Create(installerInfoZipPath) - if err != nil { - return fmt.Errorf("creating %s in zip: %w", installerInfoZipPath, err) - } - - if _, err := io.Copy(out, installerInfoFile); err != nil { - return fmt.Errorf("writing %s contents to zip: %w", installerInfoZipPath, err) - } - - return nil + return addFileToZip(z, installerInfoPath) } diff --git a/ee/debug/checkups/launchd_darwin.go b/ee/debug/checkups/launchd_darwin.go index f99db85bd..bab53a506 100644 --- a/ee/debug/checkups/launchd_darwin.go +++ b/ee/debug/checkups/launchd_darwin.go @@ -10,8 +10,8 @@ import ( "fmt" "io" "os" - "path/filepath" "strings" + "time" "github.com/kolide/launcher/ee/allowedcmd" ) @@ -31,38 +31,24 @@ func (c *launchdCheckup) Name() string { } func (c *launchdCheckup) Run(ctx context.Context, extraWriter io.Writer) error { - // Check that the plist exists (this uses open not stat, because we also want to copy it) - launchdPlist, err := os.Open(launchdPlistPath) - if os.IsNotExist(err) { + // Check that the plist exists + if _, err := os.Stat(launchdPlistPath); os.IsNotExist(err) { c.status = Failing c.summary = "plist does not exist" return nil - } else if err != nil { - c.status = Failing - c.summary = fmt.Sprintf("error reading %s: %s", launchdPlistPath, err) - return nil } - defer launchdPlist.Close() extraZip := zip.NewWriter(extraWriter) defer extraZip.Close() - zippedPlist, err := extraZip.Create(filepath.Base(launchdPlistPath)) - if err != nil { - c.status = Erroring - c.summary = fmt.Sprintf("unable to create extra information: %s", err) - return nil - } - - if _, err := io.Copy(zippedPlist, launchdPlist); err != nil { + // Add plist file using our utility + if err := addFileToZip(extraZip, launchdPlistPath); err != nil { c.status = Erroring - c.summary = fmt.Sprintf("unable to write extra information: %s", err) + c.summary = fmt.Sprintf("unable to add plist file: %s", err) return nil } - // run launchctl to check status - var printOut bytes.Buffer - + // Run launchctl to check status cmd, err := allowedcmd.Launchctl(ctx, "print", launchdServiceName) if err != nil { c.status = Erroring @@ -70,28 +56,21 @@ func (c *launchdCheckup) Run(ctx context.Context, extraWriter io.Writer) error { return nil } - cmd.Stdout = &printOut - cmd.Stderr = &printOut - if err := cmd.Run(); err != nil { + output, err := cmd.Output() + if err != nil { c.status = Failing c.summary = fmt.Sprintf("error running launchctl print: %s", err) return nil } - zippedOut, err := extraZip.Create("launchctl-print.txt") - if err != nil { + // Add command output using our streaming utility + if err := addStreamToZip(extraZip, "launchctl-print.txt", time.Now(), bytes.NewReader(output)); err != nil { c.status = Erroring - c.summary = fmt.Sprintf("unable to create launchctl-print.txt: %s", err) + c.summary = fmt.Sprintf("unable to add launchctl-print.txt output: %s", err) return nil } - if _, err := zippedOut.Write(printOut.Bytes()); err != nil { - c.status = Erroring - c.summary = fmt.Sprintf("unable to write launchctl-print.txt: %s", err) - return nil - } - - if !strings.Contains(printOut.String(), "state = running") { + if !strings.Contains(string(output), "state = running") { c.status = Failing c.summary = "state not active" return nil @@ -101,7 +80,6 @@ func (c *launchdCheckup) Run(ctx context.Context, extraWriter io.Writer) error { c.summary = "state is running" return nil } - func (c *launchdCheckup) ExtraFileName() string { return "launchd.zip" } diff --git a/ee/debug/checkups/logs.go b/ee/debug/checkups/logs.go index 80bd81613..817e5339b 100644 --- a/ee/debug/checkups/logs.go +++ b/ee/debug/checkups/logs.go @@ -48,22 +48,13 @@ func (c *Logs) Run(_ context.Context, fullFH io.Writer) error { matches, _ := filepath.Glob(filepath.Join(c.k.RootDirectory(), "debug*")) for _, f := range matches { - out, err := logZip.Create(filepath.Base(f)) - if err != nil { - continue + if err := addFileToZip(logZip, f); err != nil { + return fmt.Errorf("adding %s to zip: %w", f, err) } - - in, err := os.Open(f) - if err != nil { - fmt.Fprintf(out, "error reading file:\n%s", err) - continue - } - defer in.Close() - - io.Copy(out, in) } return nil + } func (c *Logs) Status() Status { diff --git a/ee/debug/checkups/power_windows.go b/ee/debug/checkups/power_windows.go index 3b65e8f9e..e72ebe972 100644 --- a/ee/debug/checkups/power_windows.go +++ b/ee/debug/checkups/power_windows.go @@ -5,10 +5,12 @@ package checkups import ( "archive/zip" + "bytes" "context" "fmt" "io" "os" + "time" "github.com/kolide/launcher/ee/agent" "github.com/kolide/launcher/ee/allowedcmd" @@ -35,67 +37,50 @@ func (p *powerCheckup) Run(ctx context.Context, extraWriter io.Writer) error { return fmt.Errorf("running powercfg.exe: error %w, output %s", err, string(out)) } - sprHandle, err := os.Open(tmpFilePath) - if err != nil { - return fmt.Errorf("opening system power report: %w", err) - } - defer sprHandle.Close() - extraZip := zip.NewWriter(extraWriter) defer extraZip.Close() - zippedPowerReport, err := extraZip.Create("power.html") - if err != nil { - return fmt.Errorf("creating power report zip file: %w", err) - } - - if _, err := io.Copy(zippedPowerReport, sprHandle); err != nil { - return fmt.Errorf("copying system power report: %w", err) + // Add the power report using addFileToZip + if err := addFileToZip(extraZip, tmpFilePath); err != nil { + return fmt.Errorf("adding power report to zip: %w", err) } + // Get available sleep states powerCfgSleepStatesCmd, err := allowedcmd.Powercfg(ctx, "/availablesleepstates") if err != nil { return fmt.Errorf("creating powercfg sleep states command: %w", err) } hideWindow(powerCfgSleepStatesCmd) - availableSleepStatesOutput, err := powerCfgSleepStatesCmd.CombinedOutput() + sleepStatesOutput, err := powerCfgSleepStatesCmd.Output() if err != nil { - return fmt.Errorf("running powercfg.exe for sleep states: error %w, output %s", err, string(availableSleepStatesOutput)) + return fmt.Errorf("running powercfg.exe for sleep states: error %w", err) } - zippedSleepStates, err := extraZip.Create("available_sleep_states.txt") - if err != nil { - return fmt.Errorf("creating available sleep states output file: %w", err) - } - - if _, err := zippedSleepStates.Write(availableSleepStatesOutput); err != nil { - return fmt.Errorf("writing available sleep states output file: %w", err) + // Add sleep states using addStreamToZip + if err := addStreamToZip(extraZip, "available_sleep_states.txt", time.Now(), bytes.NewReader(sleepStatesOutput)); err != nil { + return fmt.Errorf("adding sleep states to zip: %w", err) } + // Get power events eventFilter := `Get-Winevent -FilterHashtable @{LogName='System'; ProviderName='Microsoft-Windows-Power-Troubleshooter','Microsoft-Windows-Kernel-Power'} -MaxEvents 500 | Select-Object @{name='Time'; expression={$_.TimeCreated.ToString("O")}},Id,LogName,ProviderName,Message,TimeCreated | ConvertTo-Json` getWinEventCmd, err := allowedcmd.Powershell(ctx, eventFilter) if err != nil { return fmt.Errorf("creating powershell get-winevent command: %w", err) } - powerEventFile, err := extraZip.Create("windows_power_events.json") - if err != nil { - return fmt.Errorf("creating windows_power_events.json: %w", err) - } - - powerEventsOut, err := getWinEventCmd.CombinedOutput() + powerEventsOutput, err := getWinEventCmd.Output() if err != nil { - return fmt.Errorf("running get-winevent command: %w, output %s", err, string(powerEventsOut)) + return fmt.Errorf("running get-winevent command: %w", err) } - if _, err := powerEventFile.Write(powerEventsOut); err != nil { - return fmt.Errorf("writing power events to output file: %w", err) + // Add power events using addStreamToZip + if err := addStreamToZip(extraZip, "windows_power_events.json", time.Now(), bytes.NewReader(powerEventsOutput)); err != nil { + return fmt.Errorf("adding power events to zip: %w", err) } return nil } - func (p *powerCheckup) ExtraFileName() string { return "power.zip" } diff --git a/ee/debug/checkups/util.go b/ee/debug/checkups/util.go index 02c7b7681..e37106b5c 100644 --- a/ee/debug/checkups/util.go +++ b/ee/debug/checkups/util.go @@ -52,18 +52,20 @@ type fileInfo struct { // addFileToZip takes a file path, and a zip writer, and adds the file and some metadata. func addFileToZip(z *zip.Writer, location string) error { + // Create metadata file first, keeping existing pattern metaout, err := z.Create(filepath.Join(".", location+".flaremeta")) if err != nil { return fmt.Errorf("creating %s in zip: %w", location+".flaremeta", err) } - // Not totally clear if we should use Lstat or Stat here. + // Get file info first as we do in original fi, err := os.Stat(location) if os.IsNotExist(err) || os.IsPermission(err) { fmt.Fprintf(metaout, `{ "error stating file": "%s" }`, err) return nil } + // Marshal metadata b, err := json.Marshal(fileInfo{ Name: fi.Name(), Size: fi.Size(), @@ -80,7 +82,6 @@ func addFileToZip(z *zip.Writer, location string) error { if err := json.Indent(&buf, b, "", " "); err != nil { // Structural error. Abort return fmt.Errorf("indenting json: %w", err) - } metaout.Write(buf.Bytes()) @@ -95,7 +96,15 @@ func addFileToZip(z *zip.Writer, location string) error { } defer fh.Close() - dataout, err := z.Create(filepath.Join(".", location)) + // Create zip header with metadata + header, err := zip.FileInfoHeader(fi) + if err != nil { + return fmt.Errorf("creating file header: %w", err) + } + header.Name = filepath.Join(".", location) + + // Create file in zip with metadata + dataout, err := z.CreateHeader(header) if err != nil { return fmt.Errorf("creating %s in zip: %w", location, err) } @@ -107,6 +116,47 @@ func addFileToZip(z *zip.Writer, location string) error { return nil } +func addStreamToZip(z *zip.Writer, name string, modTime time.Time, reader io.Reader) error { + // Create metadata file first + metaout, err := z.Create(name + ".flaremeta") + if err != nil { + return fmt.Errorf("creating %s in zip: %w", name+".flaremeta", err) + } + + // Marshal metadata + b, err := json.Marshal(fileInfo{ + Name: filepath.Base(name), + ModTime: modTime, + }) + if err != nil { + return fmt.Errorf("marshalling json: %w", err) + } + + var buf bytes.Buffer + if err := json.Indent(&buf, b, "", " "); err != nil { + return fmt.Errorf("indenting json: %w", err) + } + metaout.Write(buf.Bytes()) + + // Create the main file in zip + header := &zip.FileHeader{ + Name: name, + Method: zip.Deflate, + Modified: modTime, + } + + out, err := z.CreateHeader(header) + if err != nil { + return fmt.Errorf("creating %s in zip: %w", name, err) + } + + if _, err := io.Copy(out, reader); err != nil { + return fmt.Errorf("copying data to zip: %w", err) + } + + return nil +} + var ignoredEnvPrefixes = []string{ "LESS", "LS_COLORS", From d27eda2b7c080e8ec037c0baf31d3e3333ee661a Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 11 Nov 2024 12:45:11 -0600 Subject: [PATCH 2/8] Update power checkup to use CombinedOutput for sleep states command --- ee/debug/checkups/power_windows.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ee/debug/checkups/power_windows.go b/ee/debug/checkups/power_windows.go index e72ebe972..bccd6c68c 100644 --- a/ee/debug/checkups/power_windows.go +++ b/ee/debug/checkups/power_windows.go @@ -52,13 +52,13 @@ func (p *powerCheckup) Run(ctx context.Context, extraWriter io.Writer) error { } hideWindow(powerCfgSleepStatesCmd) - sleepStatesOutput, err := powerCfgSleepStatesCmd.Output() + availableSleepStatesOutput, err := powerCfgSleepStatesCmd.CombinedOutput() if err != nil { return fmt.Errorf("running powercfg.exe for sleep states: error %w", err) } // Add sleep states using addStreamToZip - if err := addStreamToZip(extraZip, "available_sleep_states.txt", time.Now(), bytes.NewReader(sleepStatesOutput)); err != nil { + if err := addStreamToZip(extraZip, "available_sleep_states.txt", time.Now(), bytes.NewReader(availableSleepStatesOutput)); err != nil { return fmt.Errorf("adding sleep states to zip: %w", err) } From a22edad753db3339fabf6ac46f828983479e00b4 Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 11 Nov 2024 12:46:41 -0600 Subject: [PATCH 3/8] reverted download directory --- ee/debug/checkups/download_directory.go | 42 ++++++++++++------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/ee/debug/checkups/download_directory.go b/ee/debug/checkups/download_directory.go index 4cc698266..5079f9953 100644 --- a/ee/debug/checkups/download_directory.go +++ b/ee/debug/checkups/download_directory.go @@ -1,7 +1,6 @@ package checkups import ( - "archive/zip" "context" "fmt" "io" @@ -29,27 +28,21 @@ func (c *downloadDirectory) Run(_ context.Context, extraFH io.Writer) error { return nil } - if extraFH != io.Discard { - zipWriter := zip.NewWriter(extraFH) - defer zipWriter.Close() + for _, downloadDir := range downloadDirs { + pattern := filepath.Join(downloadDir, "kolide-launcher*") + matches, err := filepath.Glob(pattern) - for _, downloadDir := range downloadDirs { - pattern := filepath.Join(downloadDir, "kolide-launcher*") - matches, err := filepath.Glob(pattern) - if err != nil { - continue - } + if err != nil { + fmt.Fprintf(extraFH, "Error listing files in directory (%s): %s\n", downloadDir, err) + continue + } - for _, match := range matches { - if info, err := os.Stat(match); err == nil { - c.files = append(c.files, fileInfo{ - Name: match, - ModTime: info.ModTime(), - }) - if err := addFileToZip(zipWriter, match); err != nil { - fmt.Fprintf(extraFH, "Error adding file to zip %s: %v\n", match, err) - } - } + for _, match := range matches { + if info, err := os.Stat(match); err == nil { + c.files = append(c.files, fileInfo{ + Name: match, + ModTime: info.ModTime(), + }) } } } @@ -70,11 +63,18 @@ func (c *downloadDirectory) Run(_ context.Context, extraFH io.Writer) error { c.summary = fmt.Sprintf("Found Kolide installer(s) across user download directories: %s", 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-all-users.zip" + return "kolide-installers-all-users.txt" } func (c *downloadDirectory) Status() Status { From 4b6aa0139631315b7def7dadb1b9269543630565 Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 11 Nov 2024 12:47:42 -0600 Subject: [PATCH 4/8] bringing back err message --- ee/debug/checkups/power_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ee/debug/checkups/power_windows.go b/ee/debug/checkups/power_windows.go index bccd6c68c..e3ffcd665 100644 --- a/ee/debug/checkups/power_windows.go +++ b/ee/debug/checkups/power_windows.go @@ -59,7 +59,7 @@ func (p *powerCheckup) Run(ctx context.Context, extraWriter io.Writer) error { // Add sleep states using addStreamToZip if err := addStreamToZip(extraZip, "available_sleep_states.txt", time.Now(), bytes.NewReader(availableSleepStatesOutput)); err != nil { - return fmt.Errorf("adding sleep states to zip: %w", err) + return fmt.Errorf("running powercfg.exe for sleep states: error %w, output %s", err, string(availableSleepStatesOutput)) } // Get power events From f5f3ec9c404fb29a338748619d2a18a68e881f25 Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 11 Nov 2024 12:50:35 -0600 Subject: [PATCH 5/8] added back outputs --- ee/debug/checkups/power_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ee/debug/checkups/power_windows.go b/ee/debug/checkups/power_windows.go index e3ffcd665..840c703f6 100644 --- a/ee/debug/checkups/power_windows.go +++ b/ee/debug/checkups/power_windows.go @@ -71,7 +71,7 @@ func (p *powerCheckup) Run(ctx context.Context, extraWriter io.Writer) error { powerEventsOutput, err := getWinEventCmd.Output() if err != nil { - return fmt.Errorf("running get-winevent command: %w", err) + return fmt.Errorf("running get-winevent command: %w, output %s", err, string(powerEventsOutput)) } // Add power events using addStreamToZip From 64ac2de5fbbebd23b284bdfffa72195881861991 Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 11 Nov 2024 12:51:48 -0600 Subject: [PATCH 6/8] added back blank line --- ee/debug/checkups/power_windows.go | 1 + 1 file changed, 1 insertion(+) diff --git a/ee/debug/checkups/power_windows.go b/ee/debug/checkups/power_windows.go index 840c703f6..6fd2eda86 100644 --- a/ee/debug/checkups/power_windows.go +++ b/ee/debug/checkups/power_windows.go @@ -81,6 +81,7 @@ func (p *powerCheckup) Run(ctx context.Context, extraWriter io.Writer) error { return nil } + func (p *powerCheckup) ExtraFileName() string { return "power.zip" } From ab1667303a3292f8c62f63eb71d3687f3e2a2d2a Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 11 Nov 2024 12:54:47 -0600 Subject: [PATCH 7/8] fixed comment and added error handling for writing metada --- ee/debug/checkups/util.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ee/debug/checkups/util.go b/ee/debug/checkups/util.go index e37106b5c..edb2df120 100644 --- a/ee/debug/checkups/util.go +++ b/ee/debug/checkups/util.go @@ -58,7 +58,7 @@ func addFileToZip(z *zip.Writer, location string) error { return fmt.Errorf("creating %s in zip: %w", location+".flaremeta", err) } - // Get file info first as we do in original + // Get file info fi, err := os.Stat(location) if os.IsNotExist(err) || os.IsPermission(err) { fmt.Fprintf(metaout, `{ "error stating file": "%s" }`, err) @@ -83,7 +83,10 @@ func addFileToZip(z *zip.Writer, location string) error { // Structural error. Abort return fmt.Errorf("indenting json: %w", err) } - metaout.Write(buf.Bytes()) + + if _, err := metaout.Write(buf.Bytes()); err != nil { + return fmt.Errorf("writing metadata: %w", err) + } // // Done with metadata, and we know the file exists, and that we have permission to it. @@ -136,7 +139,10 @@ func addStreamToZip(z *zip.Writer, name string, modTime time.Time, reader io.Rea if err := json.Indent(&buf, b, "", " "); err != nil { return fmt.Errorf("indenting json: %w", err) } - metaout.Write(buf.Bytes()) + + if _, err := metaout.Write(buf.Bytes()); err != nil { + return fmt.Errorf("writing metadata: %w", err) + } // Create the main file in zip header := &zip.FileHeader{ From e2ffbfd3b973bdf90271c00f8e7af1cee61e718a Mon Sep 17 00:00:00 2001 From: Cesar Araujo Date: Mon, 11 Nov 2024 12:55:30 -0600 Subject: [PATCH 8/8] added missing blank line --- ee/debug/checkups/launchd_darwin.go | 1 + 1 file changed, 1 insertion(+) diff --git a/ee/debug/checkups/launchd_darwin.go b/ee/debug/checkups/launchd_darwin.go index bab53a506..a20c0e619 100644 --- a/ee/debug/checkups/launchd_darwin.go +++ b/ee/debug/checkups/launchd_darwin.go @@ -80,6 +80,7 @@ func (c *launchdCheckup) Run(ctx context.Context, extraWriter io.Writer) error { c.summary = "state is running" return nil } + func (c *launchdCheckup) ExtraFileName() string { return "launchd.zip" }