diff --git a/pkg/prebuild/builder/abi.go b/pkg/prebuild/builder/abi.go index 3b5183a6..72b3943d 100644 --- a/pkg/prebuild/builder/abi.go +++ b/pkg/prebuild/builder/abi.go @@ -30,6 +30,6 @@ func init() { }) } -func (b ABI3) Apply(profile string) (string, error) { +func (b ABI3) Apply(opt *Option, profile string) (string, error) { return regAbi4To3.Replace(profile), nil } diff --git a/pkg/prebuild/builder/complain.go b/pkg/prebuild/builder/complain.go index ad1249c8..e0f9f26b 100644 --- a/pkg/prebuild/builder/complain.go +++ b/pkg/prebuild/builder/complain.go @@ -30,7 +30,7 @@ func init() { }) } -func (b Complain) Apply(profile string) (string, error) { +func (b Complain) Apply(opt *Option, profile string) (string, error) { flags := []string{} matches := regFlags.FindStringSubmatch(profile) if len(matches) != 0 { diff --git a/pkg/prebuild/builder/core.go b/pkg/prebuild/builder/core.go index 91f07c88..e6512820 100644 --- a/pkg/prebuild/builder/core.go +++ b/pkg/prebuild/builder/core.go @@ -7,6 +7,7 @@ package builder import ( "fmt" + "github.com/arduino/go-paths-helper" "github.com/roddhjav/apparmor.d/pkg/prebuild/cfg" ) @@ -21,7 +22,20 @@ var ( // Main directive interface type Builder interface { cfg.BaseInterface - Apply(profile string) (string, error) + Apply(opt *Option, profile string) (string, error) +} + +// Builder options +type Option struct { + Name string + File *paths.Path +} + +func NewOption(file *paths.Path) *Option { + return &Option{ + Name: file.Base(), + File: file, + } } func Register(names ...string) { @@ -37,3 +51,15 @@ func Register(names ...string) { func RegisterBuilder(d Builder) { Builders[d.Name()] = d } + +func Run(file *paths.Path, profile string) (string, error) { + var err error + opt := NewOption(file) + for _, b := range Builds { + profile, err = b.Apply(opt, profile) + if err != nil { + return "", err + } + } + return profile, nil +} diff --git a/pkg/prebuild/builder/core_test.go b/pkg/prebuild/builder/core_test.go index 3d76e888..838d63df 100644 --- a/pkg/prebuild/builder/core_test.go +++ b/pkg/prebuild/builder/core_test.go @@ -238,7 +238,8 @@ func TestBuilder_Apply(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := tt.b.Apply(tt.profile) + opt := &Option{} + got, err := tt.b.Apply(opt, tt.profile) if (err != nil) != tt.wantErr { t.Errorf("Builder.Apply() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/pkg/prebuild/builder/dev.go b/pkg/prebuild/builder/dev.go index 73020e3b..f8ebdff0 100644 --- a/pkg/prebuild/builder/dev.go +++ b/pkg/prebuild/builder/dev.go @@ -31,6 +31,6 @@ func init() { }) } -func (b Dev) Apply(profile string) (string, error) { +func (b Dev) Apply(opt *Option, profile string) (string, error) { return regDev.Replace(profile), nil } diff --git a/pkg/prebuild/builder/enforce.go b/pkg/prebuild/builder/enforce.go index 676ac1ef..bc25e03d 100644 --- a/pkg/prebuild/builder/enforce.go +++ b/pkg/prebuild/builder/enforce.go @@ -24,7 +24,7 @@ func init() { }) } -func (b Enforce) Apply(profile string) (string, error) { +func (b Enforce) Apply(opt *Option, profile string) (string, error) { matches := regFlags.FindStringSubmatch(profile) if len(matches) == 0 { return profile, nil diff --git a/pkg/prebuild/builder/fsp.go b/pkg/prebuild/builder/fsp.go index f118e149..003f7952 100644 --- a/pkg/prebuild/builder/fsp.go +++ b/pkg/prebuild/builder/fsp.go @@ -28,6 +28,6 @@ func init() { }) } -func (b FullSystemPolicy) Apply(profile string) (string, error) { +func (b FullSystemPolicy) Apply(opt *Option, profile string) (string, error) { return regFullSystemPolicy.Replace(profile), nil } diff --git a/pkg/prebuild/builder/userspace.go b/pkg/prebuild/builder/userspace.go index 8a95de7f..a8bbbf6e 100644 --- a/pkg/prebuild/builder/userspace.go +++ b/pkg/prebuild/builder/userspace.go @@ -29,7 +29,7 @@ func init() { }) } -func (b Userspace) Apply(profile string) (string, error) { +func (b Userspace) Apply(opt *Option, profile string) (string, error) { p := aa.DefaultTunables() p.ParseVariables(profile) p.ResolveAttachments() diff --git a/pkg/prebuild/prebuild.go b/pkg/prebuild/prebuild.go index 2dd57183..aafeb7b4 100644 --- a/pkg/prebuild/prebuild.go +++ b/pkg/prebuild/prebuild.go @@ -83,11 +83,9 @@ func Build() error { if err != nil { return err } - for _, b := range builder.Builds { - profile, err = b.Apply(profile) - if err != nil { - return err - } + profile, err = builder.Run(file, profile) + if err != nil { + return err } profile, err = directive.Run(file, profile) if err != nil {