From 2e043d4ec89fd329198113cc0d1da71cc6faea6d Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Sun, 26 May 2024 18:05:15 +0100 Subject: [PATCH] feat(aa): add some rules methods. --- pkg/aa/change_profile.go | 2 ++ pkg/aa/mount.go | 1 + pkg/aa/rlimit.go | 2 ++ pkg/aa/rules.go | 57 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/pkg/aa/change_profile.go b/pkg/aa/change_profile.go index cc21322ac..be08692f9 100644 --- a/pkg/aa/change_profile.go +++ b/pkg/aa/change_profile.go @@ -4,6 +4,8 @@ package aa +import "fmt" + const tokCHANGEPROFILE = "change_profile" func init() { diff --git a/pkg/aa/mount.go b/pkg/aa/mount.go index 4b177442c..7d2e03888 100644 --- a/pkg/aa/mount.go +++ b/pkg/aa/mount.go @@ -5,6 +5,7 @@ package aa import ( + "fmt" "slices" ) diff --git a/pkg/aa/rlimit.go b/pkg/aa/rlimit.go index 1f2c484f2..d3a290490 100644 --- a/pkg/aa/rlimit.go +++ b/pkg/aa/rlimit.go @@ -4,6 +4,8 @@ package aa +import "fmt" + const ( tokRLIMIT = "rlimit" tokSET = "set" diff --git a/pkg/aa/rules.go b/pkg/aa/rules.go index 0a971f5d3..6eec8167a 100644 --- a/pkg/aa/rules.go +++ b/pkg/aa/rules.go @@ -51,10 +51,56 @@ func (r Rules) String() string { return renderTemplate("rules", r) } -func (r Rules) Get(filter string) Rules { +func (r Rules) IndexOf(rule Rule) int { + for idx, rr := range r { + if rr.Kind() == rule.Kind() && rr.Equals(rule) { + return idx + } + } + return -1 +} + +func (r Rules) Contains(rule Rule) bool { + return r.IndexOf(rule) != -1 +} + +func (r Rules) Add(rule Rule) Rules { + if r.Contains(rule) { + return r + } + return append(r, rule) +} + +func (r Rules) Remove(rule Rule) Rules { + idx := r.IndexOf(rule) + if idx == -1 { + return r + } + return append(r[:idx], r[idx+1:]...) +} + +func (r Rules) Insert(idx int, rules ...Rule) Rules { + return append(r[:idx], append(rules, r[idx:]...)...) +} + +func (r Rules) Sort() Rules { + return r +} + +func (r Rules) DeleteKind(kind string) Rules { res := make(Rules, 0) for _, rule := range r { - if rule.Kind() == filter { + if rule.Kind() != kind { + res = append(res, rule) + } + } + return res +} + +func (r Rules) Filter(filter string) Rules { + res := make(Rules, 0) + for _, rule := range r { + if rule.Kind() != filter { res = append(res, rule) } } @@ -120,9 +166,10 @@ func toValues(rule string, key string, input string) ([]string, error) { sep = " " } res := strings.Split(input, sep) - for _, access := range res { - if !slices.Contains(req, access) { - return nil, fmt.Errorf("unrecognized %s: %s", key, access) + for idx := range res { + res[idx] = strings.Trim(res[idx], `" `) + if !slices.Contains(req, res[idx]) { + return nil, fmt.Errorf("unrecognized %s: %s", key, res[idx]) } } slices.SortFunc(res, func(i, j string) int {