Skip to content

Commit

Permalink
fix(aa-log): ensure aa-log -s return valid result.
Browse files Browse the repository at this point in the history
Fix #268
  • Loading branch information
roddhjav committed Dec 29, 2023
1 parent 62d5488 commit 7cf7adc
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/logs/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,30 @@ func GetAuditLogs(path string) (io.Reader, error) {
func GetJournalctlLogs(path string, useFile bool) (io.Reader, error) {
var logs []systemdLog
var stdout bytes.Buffer
var value string
var unfilteredValue string

if useFile {
content, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
value = string(content)
unfilteredValue = string(content)
} else {
// journalctl -b -o json > systemd.log
cmd := exec.Command("journalctl", "--boot", "--output=json")
// journalctl -b -o json --output-fields=MESSAGE > systemd.log
cmd := exec.Command("journalctl", "--boot", "--output=json", "--output-fields=MESSAGE")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return nil, err
}
value = stdout.String()
unfilteredValue = stdout.String()
}

value := ""
for _, v := range strings.Split(unfilteredValue, "\n") {
if strings.Contains(v, "apparmor") {
value += v + "\n"
}
}
value = strings.Replace(value, "\n", ",\n", -1)
value = strings.TrimSuffix(value, ",\n")
value = `[` + value + `]`
Expand Down

0 comments on commit 7cf7adc

Please sign in to comment.