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

Refactor log file handling to standardize zip creation and improve er… #1950

Merged
merged 8 commits into from
Nov 11, 2024
42 changes: 21 additions & 21 deletions ee/debug/checkups/download_directory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package checkups

import (
"archive/zip"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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)
}
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand All @@ -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 {
Expand Down
15 changes: 1 addition & 14 deletions ee/debug/checkups/init_logs_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"archive/zip"
"context"
"fmt"
"io"
"os"
"path/filepath"
)

Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions ee/debug/checkups/init_logs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package checkups

import (
"archive/zip"
"bytes"
"context"
"fmt"
"time"

"github.com/kolide/launcher/ee/allowedcmd"
)
Expand All @@ -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))
}
10 changes: 5 additions & 5 deletions ee/debug/checkups/init_logs_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package checkups

import (
"archive/zip"
"bytes"
"context"
"fmt"
"time"

"github.com/kolide/launcher/ee/allowedcmd"
)
Expand All @@ -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))
}
41 changes: 2 additions & 39 deletions ee/debug/checkups/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"os"
"runtime"

"github.com/kolide/launcher/pkg/launcher"
Expand Down Expand Up @@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this has the inadvertent effect that it's going to cause the end files to be named something else, is that correct? I don't think that's inherently wrong, but I want to note it.

I think we might even want it, since I'd thought about the idea of moving all the files into a dedicated file directory, instead of mini zip files for each flare.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct the file will always be install.log instead of varying for macos

}

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)
}
48 changes: 13 additions & 35 deletions ee/debug/checkups/launchd_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/kolide/launcher/ee/allowedcmd"
)
Expand All @@ -31,67 +31,46 @@ 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
c.summary = fmt.Sprintf("unable to create launchctl command: %s", err)
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
Expand All @@ -101,7 +80,6 @@ func (c *launchdCheckup) Run(ctx context.Context, extraWriter io.Writer) error {
c.summary = "state is running"
return nil
}

cesarfda marked this conversation as resolved.
Show resolved Hide resolved
func (c *launchdCheckup) ExtraFileName() string {
return "launchd.zip"
}
Expand Down
15 changes: 3 additions & 12 deletions ee/debug/checkups/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading
Loading