mirror of
https://github.com/roddhjav/apparmor.d.git
synced 2025-01-18 00:48:10 +01:00
feat(aa): rewrite the rules Format method.
Automate padding regardless of rule kind.
This commit is contained in:
parent
0e0f87611a
commit
4e1b972ee5
1 changed files with 38 additions and 79 deletions
117
pkg/aa/rules.go
117
pkg/aa/rules.go
|
@ -6,9 +6,6 @@ package aa
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type requirement map[string][]string
|
type requirement map[string][]string
|
||||||
|
@ -144,8 +141,7 @@ func (r Rules) GetIncludes() []*Include {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge merge similar rules together.
|
// Merge merge similar rules together:
|
||||||
// Steps:
|
|
||||||
// - Remove identical rules
|
// - Remove identical rules
|
||||||
// - Merge rule access. Eg: for same path, 'r' and 'w' becomes 'rw'
|
// - Merge rule access. Eg: for same path, 'r' and 'w' becomes 'rw'
|
||||||
//
|
//
|
||||||
|
@ -201,90 +197,53 @@ func (r Rules) Sort() Rules {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format the rules for better readability before printing it.
|
// setPaddings set paddings for each element in each rules
|
||||||
|
func (r *Rules) setPaddings(paddingsIndex map[Kind][]int, paddingsMaxLen map[Kind][]int) {
|
||||||
|
for kind, index := range paddingsIndex {
|
||||||
|
if len(index) <= 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, i := range index {
|
||||||
|
(*r)[i].setPaddings(paddingsMaxLen[kind])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format the rules for better readability before printing it. Format supposes
|
||||||
|
// the rules are merged and sorted.
|
||||||
// Follow: https://apparmor.pujol.io/development/guidelines/#the-file-block
|
// Follow: https://apparmor.pujol.io/development/guidelines/#the-file-block
|
||||||
func (r Rules) Format() Rules {
|
func (r Rules) Format() Rules {
|
||||||
const prefixOwner = " "
|
// Insert new line between rule of different type/subtype.
|
||||||
suffixMaxlen := 36
|
for i := len(r) - 1; i >= 0; i-- {
|
||||||
transitions := append(requirements[FILE]["transition"], "m")
|
j := i - 1
|
||||||
|
if j < 0 || r[j] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if r[i].addLine(r[j]) {
|
||||||
|
r = r.Insert(i, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
paddingIndex := []int{}
|
// Find max paddings for each element in each rules
|
||||||
paddingMaxLenght := 0
|
paddingsIndex := map[Kind][]int{}
|
||||||
|
paddingsMaxLen := map[Kind][]int{}
|
||||||
for i, rule := range r {
|
for i, rule := range r {
|
||||||
if rule == nil {
|
if rule == nil {
|
||||||
|
r.setPaddings(paddingsIndex, paddingsMaxLen)
|
||||||
|
paddingsIndex = map[Kind][]int{}
|
||||||
|
paddingsMaxLen = map[Kind][]int{}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if rule.Kind() == FILE {
|
lengths := rule.Lengths()
|
||||||
rule := r[i].(*File)
|
paddingsIndex[rule.Kind()] = append(paddingsIndex[rule.Kind()], i)
|
||||||
|
for idx, length := range lengths {
|
||||||
// Add padding to align with other transition rule
|
if _, ok := paddingsMaxLen[rule.Kind()]; !ok {
|
||||||
isTransition := util.Intersect(transitions, rule.Access)
|
paddingsMaxLen[rule.Kind()] = make([]int, len(lengths))
|
||||||
if len(isTransition) > 0 {
|
|
||||||
ruleLen := len(rule.Path) + 1
|
|
||||||
paddingMaxLenght = max(ruleLen, paddingMaxLenght)
|
|
||||||
paddingIndex = append(paddingIndex, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add suffix to align comment on udev/data rule
|
|
||||||
if rule.Comment != "" && strings.HasPrefix(rule.Path, "@{run}/udev/data/") {
|
|
||||||
suffixlen := suffixMaxlen - len(rule.Path)
|
|
||||||
if suffixlen < 0 {
|
|
||||||
suffixlen = 0
|
|
||||||
}
|
|
||||||
rule.Suffix = strings.Repeat(" ", suffixlen)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(paddingIndex) > 1 {
|
|
||||||
r.setPadding(paddingIndex, paddingMaxLenght)
|
|
||||||
}
|
|
||||||
|
|
||||||
hasOwnerRule := false
|
|
||||||
for i := len(r) - 1; i >= 0; i-- {
|
|
||||||
if r[i] == nil {
|
|
||||||
hasOwnerRule = false
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// File rule
|
|
||||||
if r[i].Kind() == FILE {
|
|
||||||
rule := r[i].(*File)
|
|
||||||
|
|
||||||
// Add prefix before rule path to align with other rule
|
|
||||||
if rule.Owner {
|
|
||||||
hasOwnerRule = true
|
|
||||||
} else if hasOwnerRule {
|
|
||||||
rule.Prefix = prefixOwner
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not add new line on executable rule
|
|
||||||
isTransition := util.Intersect(transitions, rule.Access)
|
|
||||||
if len(isTransition) > 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a new line between Files rule of different group type
|
|
||||||
j := i - 1
|
|
||||||
if j < 0 || r[j] == nil || r[j].Kind() != FILE {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
letterI := getLetterIn(fileAlphabet, rule.Path)
|
|
||||||
letterJ := getLetterIn(fileAlphabet, r[j].(*File).Path)
|
|
||||||
groupI, ok1 := fileAlphabetGroups[letterI]
|
|
||||||
groupJ, ok2 := fileAlphabetGroups[letterJ]
|
|
||||||
if letterI != letterJ && !(ok1 && ok2 && groupI == groupJ) {
|
|
||||||
hasOwnerRule = false
|
|
||||||
r = r.Insert(i, nil)
|
|
||||||
}
|
}
|
||||||
|
paddingsMaxLen[rule.Kind()][idx] = max(paddingsMaxLen[rule.Kind()][idx], length)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
r.setPaddings(paddingsIndex, paddingsMaxLen)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// setPadding adds padding to the rule path to align with other rules.
|
|
||||||
func (r *Rules) setPadding(paddingIndex []int, paddingMaxLenght int) {
|
|
||||||
for _, i := range paddingIndex {
|
|
||||||
(*r)[i].(*File).Padding = strings.Repeat(" ", paddingMaxLenght-len((*r)[i].(*File).Path))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue