feat(aa-log): rewrite the profile template.

This commit is contained in:
Alexandre Pujol 2023-09-25 00:15:51 +01:00
parent 422418e0e2
commit 88f275ef43
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
2 changed files with 210 additions and 251 deletions

View file

@ -6,21 +6,81 @@ package aa
import (
_ "embed"
"reflect"
"strings"
"text/template"
)
// Default indentation for apparmor profile (2 spaces)
const indentation = " "
//go:embed template.j2
var tmplFileAppArmorProfile string
var (
//go:embed template.j2
tmplFileAppArmorProfile string
var tmplFunctionMap = template.FuncMap{
"indent": indent,
"overindent": indentDbus,
// tmplFunctionMap is the list of function available in the template
tmplFunctionMap = template.FuncMap{
"typeof": typeOf,
"join": join,
"indent": indent,
"overindent": indentDbus,
}
// The apparmor profile template
tmplAppArmorProfile = template.Must(template.New("profile").
Funcs(tmplFunctionMap).Parse(tmplFileAppArmorProfile))
// convert apparmor requested mask to apparmor access mode
// TODO: Should be a map of slice, not exhausive yet
maskToAccess = map[string]string{
"a": "w",
"c": "w",
"d": "w",
"k": "rk",
"l": "l",
"m": "rm",
"r": "r",
"ra": "rw",
"read write": "read write",
"read": "read",
"readby": "readby",
"receive": "receive",
"rm": "rm",
"rw": "rw",
"send receive": "send receive",
"send": "send",
"w": "w",
"wc": "w",
"wd": "w",
"wk": "wk",
"wr": "rw",
"wrc": "rw",
"wrd": "rw",
"write": "write",
"x": "rix",
}
)
func join(i any) string {
switch reflect.TypeOf(i).Kind() {
case reflect.Slice:
return strings.Join(i.([]string), " ")
case reflect.Map:
res := []string{}
for k, v := range i.(map[string]string) {
res = append(res, k+"="+v)
}
return strings.Join(res, " ")
default:
return i.(string)
}
}
var tmplAppArmorProfile = template.Must(template.New("profile").
Funcs(tmplFunctionMap).Parse(tmplFileAppArmorProfile))
func typeOf(i any) string {
return strings.TrimPrefix(reflect.TypeOf(i).String(), "*aa.")
}
func indent(s string) string {
return indentation + s
@ -29,31 +89,3 @@ func indent(s string) string {
func indentDbus(s string) string {
return indentation + " " + s
}
// TODO: Should be a map of slice, not exhausive yet
var maskToAccess = map[string]string{
"a": "w",
"c": "w",
"d": "w",
"k": "rk",
"l": "l",
"m": "rm",
"r": "r",
"ra": "rw",
"read write": "read write",
"read": "read",
"readby": "readby",
"receive": "receive",
"rm": "rm",
"rw": "rw",
"send receive": "send receive",
"send": "send",
"w": "w",
"wc": "w",
"wr": "rw",
"wrc": "rw",
"wrd": "rw",
"write": "write",
"x": "rix",
}