aa-log: add -f option to set a log file.
This commit is contained in:
parent
ba0ccc3edc
commit
6876938719
4 changed files with 47 additions and 13 deletions
|
|
@ -12,7 +12,7 @@ profile aa-log @{exec_path} {
|
||||||
|
|
||||||
@{exec_path} mr,
|
@{exec_path} mr,
|
||||||
|
|
||||||
/var/log/audit/audit.log r,
|
/var/log/audit/audit.log* r,
|
||||||
|
|
||||||
@{sys}/kernel/mm/transparent_hugepage/hpage_pmd_size r,
|
@{sys}/kernel/mm/transparent_hugepage/hpage_pmd_size r,
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -13,7 +14,13 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogFile is the path to the file to query
|
// Command line options
|
||||||
|
var (
|
||||||
|
help bool
|
||||||
|
path string
|
||||||
|
)
|
||||||
|
|
||||||
|
// LogFile is the default path to the file to query
|
||||||
const LogFile = "/var/log/audit/audit.log"
|
const LogFile = "/var/log/audit/audit.log"
|
||||||
|
|
||||||
// Colors
|
// Colors
|
||||||
|
|
@ -157,12 +164,7 @@ func (aaLogs AppArmorLogs) String() string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func aaLog(args []string, path string) error {
|
func aaLog(path string, profile string) error {
|
||||||
profile := ""
|
|
||||||
if len(args) >= 2 {
|
|
||||||
profile = args[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Open(filepath.Clean(path))
|
file, err := os.Open(filepath.Clean(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -179,8 +181,36 @@ func aaLog(args []string, path string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.BoolVar(&help, "h", false, "Show this help message and exit.")
|
||||||
|
flag.StringVar(&path, "f", LogFile,
|
||||||
|
"Set a log`file` or a prefix to the default log file.")
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := aaLog(os.Args, LogFile)
|
flag.Parse()
|
||||||
|
if help {
|
||||||
|
fmt.Printf(`aa-log [-h] [-f file] [profile]
|
||||||
|
|
||||||
|
Review AppArmor generated messages in a colorful way.
|
||||||
|
It can be given an optional profile name to filter the output with.
|
||||||
|
|
||||||
|
`)
|
||||||
|
flag.PrintDefaults()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
profile := ""
|
||||||
|
if len(flag.Args()) >= 1 {
|
||||||
|
profile = flag.Args()[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
logfile := filepath.Clean(LogFile + "." + path)
|
||||||
|
if _, err := os.Stat(logfile); err != nil {
|
||||||
|
logfile = path
|
||||||
|
}
|
||||||
|
|
||||||
|
err := aaLog(logfile, profile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
||||||
|
|
@ -131,26 +131,26 @@ 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
|
||||||
args []string
|
|
||||||
path string
|
path string
|
||||||
|
profile string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "OK",
|
name: "OK",
|
||||||
args: []string{"aa-log", ""},
|
|
||||||
path: "../../tests/audit.log",
|
path: "../../tests/audit.log",
|
||||||
|
profile: "",
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "No logfile",
|
name: "No logfile",
|
||||||
args: []string{"aa-log", ""},
|
|
||||||
path: "../../tests/log",
|
path: "../../tests/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.args, tt.path); (err != nil) != tt.wantErr {
|
if err := aaLog(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)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,10 @@
|
||||||
|
|
||||||
_aa-log () {
|
_aa-log () {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
|
_arguments : \
|
||||||
|
-f'[set a logfile or a prefix to the default log file]:_files' \
|
||||||
|
-h'[display help information]'
|
||||||
|
|
||||||
_values -C 'profile names' ${$(__aa_profiles):-""}
|
_values -C 'profile names' ${$(__aa_profiles):-""}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue