mirror of
https://github.com/roddhjav/apparmor.d.git
synced 2024-11-14 23:43:56 +01:00
feat(aa-log): add support for multiple logger.
This commit is contained in:
parent
75b25c7e07
commit
f3e2ebfffa
@ -9,6 +9,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -22,9 +23,9 @@ import (
|
|||||||
|
|
||||||
// Command line options
|
// Command line options
|
||||||
var (
|
var (
|
||||||
dbus bool
|
help bool
|
||||||
help bool
|
path string
|
||||||
path string
|
systemd bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogFile is the default path to the file to query
|
// LogFile is the default path to the file to query
|
||||||
@ -94,20 +95,33 @@ func removeDuplicateLog(logs []string) []string {
|
|||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
// getJournalctlDbusSessionLogs return a reader with the logs entries
|
// getAuditLogs return a reader with the logs entries from Auditd
|
||||||
func getJournalctlDbusSessionLogs(file io.Reader, useFile bool) (io.Reader, error) {
|
func getAuditLogs(path string) (io.Reader, error) {
|
||||||
|
file, err := os.Open(filepath.Clean(path))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return file, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// getJournalctlLogs return a reader with the logs entries from Systemd
|
||||||
|
func getJournalctlLogs(path string, user bool, useFile bool) (io.Reader, error) {
|
||||||
var logs []SystemdLog
|
var logs []SystemdLog
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var value string
|
var value string
|
||||||
|
|
||||||
if useFile {
|
if useFile {
|
||||||
content, err := ioutil.ReadAll(file)
|
content, err := ioutil.ReadFile(filepath.Clean(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
value = string(content)
|
value = string(content)
|
||||||
} else {
|
} else {
|
||||||
cmd := exec.Command("journalctl", "--user", "-b", "-u", "dbus.service", "-o", "json")
|
mode := "--system"
|
||||||
|
if user {
|
||||||
|
mode = "--user"
|
||||||
|
}
|
||||||
|
cmd := exec.Command("journalctl", mode, "--boot", "--unit=dbus.service", "--output=json")
|
||||||
cmd.Stdout = &stdout
|
cmd.Stdout = &stdout
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -242,29 +256,23 @@ func (aaLogs AppArmorLogs) String() string {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func aaLog(path string, profile string, dbus bool) error {
|
func aaLog(logger string, path string, profile string) error {
|
||||||
file, err := os.Open(filepath.Clean(path))
|
var err error
|
||||||
|
var file io.Reader
|
||||||
|
|
||||||
|
switch logger {
|
||||||
|
case "auditd":
|
||||||
|
file, err = getAuditLogs(path)
|
||||||
|
case "systemd":
|
||||||
|
file, err = getJournalctlLogs(path, true, path != LogFile)
|
||||||
|
default:
|
||||||
|
err = errors.New("Logger not supported: " + logger)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
/* #nosec G307 */
|
aaLogs := NewApparmorLogs(file, profile)
|
||||||
defer func() {
|
fmt.Print(aaLogs.String())
|
||||||
if err := file.Close(); err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if dbus {
|
|
||||||
file, err := getJournalctlDbusSessionLogs(file, path != LogFile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
aaLogs := NewApparmorLogs(file, profile)
|
|
||||||
fmt.Print(aaLogs.String())
|
|
||||||
} else {
|
|
||||||
aaLogs := NewApparmorLogs(file, profile)
|
|
||||||
fmt.Print(aaLogs.String())
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,7 +280,7 @@ func init() {
|
|||||||
flag.BoolVar(&help, "h", false, "Show this help message and exit.")
|
flag.BoolVar(&help, "h", false, "Show this help message and exit.")
|
||||||
flag.StringVar(&path, "f", LogFile,
|
flag.StringVar(&path, "f", LogFile,
|
||||||
"Set a log`file` or a suffix to the default log file.")
|
"Set a log`file` or a suffix to the default log file.")
|
||||||
flag.BoolVar(&dbus, "d", false, "Show dbus session event.")
|
flag.BoolVar(&systemd, "s", false, "Parse systemd dbus logs.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -293,12 +301,17 @@ func main() {
|
|||||||
profile = flag.Args()[0]
|
profile = flag.Args()[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger := "auditd"
|
||||||
|
if systemd {
|
||||||
|
logger = "systemd"
|
||||||
|
}
|
||||||
|
|
||||||
logfile := filepath.Clean(LogFile + "." + path)
|
logfile := filepath.Clean(LogFile + "." + path)
|
||||||
if _, err := os.Stat(logfile); err != nil {
|
if _, err := os.Stat(logfile); err != nil {
|
||||||
logfile = path
|
logfile = path
|
||||||
}
|
}
|
||||||
|
|
||||||
err := aaLog(logfile, profile, dbus)
|
err := aaLog(logger, logfile, profile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -217,15 +217,17 @@ func TestNewApparmorLogs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_getJournalctlDbusSessionLogs(t *testing.T) {
|
func Test_getJournalctlLogs(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
path string
|
path string
|
||||||
|
user bool
|
||||||
useFile bool
|
useFile bool
|
||||||
want AppArmorLogs
|
want AppArmorLogs
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "gsd-xsettings",
|
name: "gsd-xsettings",
|
||||||
|
user: true,
|
||||||
useFile: true,
|
useFile: true,
|
||||||
path: "../../tests/systemd.log",
|
path: "../../tests/systemd.log",
|
||||||
want: AppArmorLogs{
|
want: AppArmorLogs{
|
||||||
@ -253,8 +255,7 @@ func Test_getJournalctlDbusSessionLogs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
file, _ := os.Open(tt.path)
|
reader, _ := getJournalctlLogs(tt.path, tt.user, tt.useFile)
|
||||||
reader, _ := getJournalctlDbusSessionLogs(file, tt.useFile)
|
|
||||||
if got := NewApparmorLogs(reader, tt.name); !reflect.DeepEqual(got, tt.want) {
|
if got := NewApparmorLogs(reader, tt.name); !reflect.DeepEqual(got, tt.want) {
|
||||||
t.Errorf("NewApparmorLogs() = %v, want %v", got, tt.want)
|
t.Errorf("NewApparmorLogs() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
@ -310,36 +311,43 @@ func TestAppArmorLogs_String(t *testing.T) {
|
|||||||
func Test_app(t *testing.T) {
|
func Test_app(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
logger string
|
||||||
path string
|
path string
|
||||||
profile string
|
profile string
|
||||||
dbus bool
|
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test audit.log",
|
name: "Test audit.log",
|
||||||
|
logger: "auditd",
|
||||||
path: "../../tests/audit.log",
|
path: "../../tests/audit.log",
|
||||||
profile: "",
|
profile: "",
|
||||||
dbus: false,
|
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test Dbus Session",
|
name: "Test Dbus Session",
|
||||||
|
logger: "systemd",
|
||||||
path: "../../tests/systemd.log",
|
path: "../../tests/systemd.log",
|
||||||
profile: "",
|
profile: "",
|
||||||
dbus: true,
|
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "No logfile",
|
name: "No logfile",
|
||||||
|
logger: "auditd",
|
||||||
path: "../../tests/log",
|
path: "../../tests/log",
|
||||||
profile: "",
|
profile: "",
|
||||||
dbus: false,
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Logger not supported",
|
||||||
|
logger: "raw",
|
||||||
|
path: "../../tests/audit.log",
|
||||||
|
profile: "",
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if err := aaLog(tt.path, tt.profile, tt.dbus); (err != nil) != tt.wantErr {
|
if err := aaLog(tt.logger, tt.path, tt.profile); (err != nil) != tt.wantErr {
|
||||||
t.Errorf("aaLog() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("aaLog() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user