From 6ea2df19ebbdf6441f180d674da12f367a7e3fcf Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Tue, 25 Jul 2023 22:01:07 +0100 Subject: [PATCH] build: simplify profile struct. --- pkg/aa/profile.go | 8 +++----- pkg/aa/profile_test.go | 17 +++++------------ pkg/prebuild/build.go | 4 ++-- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/pkg/aa/profile.go b/pkg/aa/profile.go index 659271ce..73d14d5e 100644 --- a/pkg/aa/profile.go +++ b/pkg/aa/profile.go @@ -29,24 +29,22 @@ var ( ) type AppArmorProfile struct { - Content string Variables map[string][]string Attachments []string } -func NewAppArmorProfile(content string) *AppArmorProfile { +func NewAppArmorProfile() *AppArmorProfile { variables := make(map[string][]string) maps.Copy(variables, Tunables) return &AppArmorProfile{ - Content: content, Variables: variables, Attachments: []string{}, } } // ParseVariables extract all variables from the profile -func (p *AppArmorProfile) ParseVariables() { - matches := regVariablesDef.FindAllStringSubmatch(p.Content, -1) +func (p *AppArmorProfile) ParseVariables(content string) { + matches := regVariablesDef.FindAllStringSubmatch(content, -1) for _, match := range matches { if len(match) > 2 { key := match[1] diff --git a/pkg/aa/profile_test.go b/pkg/aa/profile_test.go index 9a47bc8f..bd47e7a6 100644 --- a/pkg/aa/profile_test.go +++ b/pkg/aa/profile_test.go @@ -11,15 +11,12 @@ import ( func TestNewAppArmorProfile(t *testing.T) { tests := []struct { - name string - content string - want *AppArmorProfile + name string + want *AppArmorProfile }{ { - name: "aa", - content: "", + name: "aa", want: &AppArmorProfile{ - Content: "", Variables: map[string][]string{ "bin": {"/{usr/,}{s,}bin"}, "lib": {"/{usr/,}lib{,exec,32,64}"}, @@ -33,7 +30,7 @@ func TestNewAppArmorProfile(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewAppArmorProfile(tt.content); !reflect.DeepEqual(got, tt.want) { + if got := NewAppArmorProfile(); !reflect.DeepEqual(got, tt.want) { t.Errorf("NewAppArmorProfile() = %v, want %v", got, tt.want) } }) @@ -81,12 +78,11 @@ func TestAppArmorProfile_ParseVariables(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &AppArmorProfile{ - Content: tt.content, Variables: map[string][]string{}, Attachments: []string{}, } - p.ParseVariables() + p.ParseVariables(tt.content) if !reflect.DeepEqual(p.Variables, tt.want) { t.Errorf("AppArmorProfile.ParseVariables() = %v, want %v", p.Variables, tt.want) } @@ -111,7 +107,6 @@ func TestAppArmorProfile_resolve(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &AppArmorProfile{ - Content: "", Variables: tt.variables, Attachments: []string{}, } @@ -179,7 +174,6 @@ func TestAppArmorProfile_ResolveAttachments(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &AppArmorProfile{ - Content: "", Variables: tt.variables, Attachments: []string{}, } @@ -233,7 +227,6 @@ func TestAppArmorProfile_NestAttachments(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { p := &AppArmorProfile{ - Content: "", Variables: map[string][]string{}, Attachments: tt.Attachments, } diff --git a/pkg/prebuild/build.go b/pkg/prebuild/build.go index b8b28a8f..8c0738be 100644 --- a/pkg/prebuild/build.go +++ b/pkg/prebuild/build.go @@ -45,8 +45,8 @@ func BuildComplain(profile string) string { // Bypass userspace tools restriction func BuildUserspace(profile string) string { - p := aa.NewAppArmorProfile(profile) - p.ParseVariables() + p := aa.NewAppArmorProfile() + p.ParseVariables(profile) p.ResolveAttachments() att := p.NestAttachments() matches := regAttachments.FindAllString(profile, -1)