refractor(aa): move profile specific method to the profile struct.

This commit is contained in:
Alexandre Pujol 2024-04-23 21:32:58 +01:00
parent 120db93396
commit 2923df2a73
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
3 changed files with 89 additions and 63 deletions

View file

@ -5,6 +5,8 @@
package aa
import (
"reflect"
"sort"
"strings"
)
@ -28,8 +30,29 @@ func (r Rules) String() string {
return renderTemplate("rules", r)
}
// Sort the rules in a profile.
// Follow: https://apparmor.pujol.io/development/guidelines/#guidelines
func (r Rules) Sort() {
sort.Slice(r, func(i, j int) bool {
typeOfI := reflect.TypeOf(r[i])
typeOfJ := reflect.TypeOf(r[j])
if typeOfI != typeOfJ {
valueOfI := typeToValue(typeOfI)
valueOfJ := typeToValue(typeOfJ)
if typeOfI == reflect.TypeOf((*Include)(nil)) && r[i].(*Include).IfExists {
valueOfI = "include_if_exists"
}
if typeOfJ == reflect.TypeOf((*Include)(nil)) && r[j].(*Include).IfExists {
valueOfJ = "include_if_exists"
}
return ruleWeights[valueOfI] < ruleWeights[valueOfJ]
}
return r[i].Less(r[j])
})
}
type RuleBase struct {
IsLineRule bool
Comment string
NoNewPrivs bool
FileInherit bool
@ -67,6 +90,7 @@ func newRuleFromLog(log map[string]string) RuleBase {
}
return RuleBase{
IsLineRule: false,
Comment: msg,
NoNewPrivs: noNewPrivs,
FileInherit: fileInherit,