refractor(aa-log): merge identical function together.

This commit is contained in:
Alexandre Pujol 2023-09-30 15:34:30 +01:00
parent 95c322d62a
commit cd80a7d919
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
3 changed files with 30 additions and 54 deletions

View file

@ -30,6 +30,33 @@ type systemdLog struct {
Message string `json:"MESSAGE"`
}
// GetApparmorLogs return a list of cleaned apparmor logs from a file
func GetApparmorLogs(file io.Reader, profile string) []string {
res := ""
isAppArmorLog := isAppArmorLogTemplate.Copy()
if profile != "" {
exp := `apparmor=("DENIED"|"ALLOWED"|"AUDIT")`
exp = fmt.Sprintf(exp+`.* (profile="%s.*"|label="%s.*")`, profile, profile)
isAppArmorLog = regexp.MustCompile(exp)
}
// Select Apparmor logs
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if isAppArmorLog.MatchString(line) {
res += line + "\n"
}
}
// Clean & remove doublon in logs
for _, aa := range regCleanLogs {
res = aa.Regex.ReplaceAllLiteralString(res, aa.Repl)
}
logs := strings.Split(res, "\n")
return util.RemoveDuplicate(logs)
}
// GetAuditLogs return a reader with the logs entries from Auditd
func GetAuditLogs(path string) (io.Reader, error) {
file, err := os.Open(filepath.Clean(path))
@ -92,31 +119,3 @@ func SelectLogFile(path string) string {
}
return ""
}
func Raw(file io.Reader, profile string) string {
res := ""
isAppArmorLog := isAppArmorLogTemplate.Copy()
if profile != "" {
exp := `apparmor=("DENIED"|"ALLOWED"|"AUDIT")`
exp = fmt.Sprintf(exp+`.* (profile="%s.*"|label="%s.*")`, profile, profile)
isAppArmorLog = regexp.MustCompile(exp)
}
// Select Apparmor logs
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if isAppArmorLog.MatchString(line) {
res += line + "\n"
}
}
// Clean & remove doublon in logs
for _, aa := range regCleanLogs {
res = aa.Regex.ReplaceAllLiteralString(res, aa.Repl)
}
logs := strings.Split(res, "\n")
logs = util.RemoveDuplicate(logs)
return strings.Join(logs, "\n")
}