From 69f2f46c4661bef5e888f53c341d7388ba18ec2d Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Mon, 25 Mar 2024 23:37:13 +0000 Subject: [PATCH] refractor(build): update prebuild logic to the new interface structure. --- cmd/prebuild/main.go | 31 ++++++----- cmd/prebuild/main_test.go | 12 ++-- pkg/prebuild/prebuild.go | 100 +++++++++++++++++----------------- pkg/prebuild/prebuild_test.go | 21 +++---- 4 files changed, 80 insertions(+), 84 deletions(-) diff --git a/cmd/prebuild/main.go b/cmd/prebuild/main.go index 68840387..c39d4cbb 100644 --- a/cmd/prebuild/main.go +++ b/cmd/prebuild/main.go @@ -10,12 +10,14 @@ import ( "os" "github.com/roddhjav/apparmor.d/pkg/logging" - oss "github.com/roddhjav/apparmor.d/pkg/os" "github.com/roddhjav/apparmor.d/pkg/prebuild" + "github.com/roddhjav/apparmor.d/pkg/prebuild/builder" + "github.com/roddhjav/apparmor.d/pkg/prebuild/cfg" "github.com/roddhjav/apparmor.d/pkg/prebuild/directive" + "github.com/roddhjav/apparmor.d/pkg/prebuild/prepare" ) -const usage = `prebuild [-h] [--full] [--complain | --enforce] [profiles...] +const usage = `prebuild [-h] [--full] [--complain | --enforce] Prebuild apparmor.d profiles for a given distribution and apply internal built-in directives. @@ -27,7 +29,6 @@ Options: -e, --enforce Set enforce flag on all profiles. --abi4 Convert the profiles to Apparmor abi/4.0. -Directives: ` var ( @@ -51,23 +52,23 @@ func init() { } func aaPrebuild() error { - logging.Step("Building apparmor.d profiles for %s.", oss.Distribution) + logging.Step("Building apparmor.d profiles for %s.", cfg.Distribution) if full { - prebuild.Prepares = append(prebuild.Prepares, prebuild.SetFullSystemPolicy) - prebuild.Builds = append(prebuild.Builds, prebuild.BuildFullSystemPolicy) + prepare.Register("fsp") + builder.Register("fsp") } else { - prebuild.Prepares = append(prebuild.Prepares, prebuild.SetEarlySystemd) + prepare.Register("systemd-early") } if complain { - prebuild.Builds = append(prebuild.Builds, prebuild.BuildComplain) + builder.Register("complain") } else if enforce { - prebuild.Builds = append(prebuild.Builds, prebuild.BuildEnforce) + builder.Register("enforce") } if abi4 { - prebuild.Builds = append(prebuild.Builds, prebuild.BuildABI3) + builder.Register("abi3") } if err := prebuild.Prepare(); err != nil { @@ -78,11 +79,11 @@ func aaPrebuild() error { func main() { flag.Usage = func() { - res := usage - for _, d := range directive.Directives { - res += ` ` + d.Usage() + "\n" - } - fmt.Print(res) + fmt.Printf("%s%s\n%s\n%s", usage, + cfg.Help("Prepare", prepare.Tasks), + cfg.Help("Build", builder.Builders), + cfg.Usage("Directives", directive.Directives), + ) } flag.Parse() if help { diff --git a/cmd/prebuild/main_test.go b/cmd/prebuild/main_test.go index 89fc52d1..92af60c2 100644 --- a/cmd/prebuild/main_test.go +++ b/cmd/prebuild/main_test.go @@ -9,8 +9,9 @@ import ( "os/exec" "testing" - oss "github.com/roddhjav/apparmor.d/pkg/os" - "github.com/roddhjav/apparmor.d/pkg/prebuild" + "github.com/roddhjav/apparmor.d/pkg/prebuild/builder" + "github.com/roddhjav/apparmor.d/pkg/prebuild/cfg" + "github.com/roddhjav/apparmor.d/pkg/prebuild/prepare" ) func chdirGitRoot() { @@ -72,12 +73,13 @@ func Test_AAPrebuild(t *testing.T) { chdirGitRoot() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - oss.Distribution = tt.dist + cfg.Distribution = tt.dist if tt.full { - prebuild.Prepares = append(prebuild.Prepares, prebuild.SetFullSystemPolicy) + prepare.Register("fsp") + builder.Register("fsp") } if tt.complain { - prebuild.Builds = append(prebuild.Builds, prebuild.BuildComplain) + builder.Register("complain") } if err := aaPrebuild(); (err != nil) != tt.wantErr { t.Errorf("aaPrebuild() error = %v, wantErr %v", err, tt.wantErr) diff --git a/pkg/prebuild/prebuild.go b/pkg/prebuild/prebuild.go index c7fee908..77ff86ae 100644 --- a/pkg/prebuild/prebuild.go +++ b/pkg/prebuild/prebuild.go @@ -5,75 +5,61 @@ package prebuild import ( - "reflect" - "runtime" "strings" "github.com/arduino/go-paths-helper" "github.com/roddhjav/apparmor.d/pkg/logging" - oss "github.com/roddhjav/apparmor.d/pkg/os" + "github.com/roddhjav/apparmor.d/pkg/prebuild/builder" + "github.com/roddhjav/apparmor.d/pkg/prebuild/cfg" "github.com/roddhjav/apparmor.d/pkg/prebuild/directive" -) - -var ( - overwrite bool = false - DistDir *paths.Path - Root *paths.Path - RootApparmord *paths.Path - FlagDir *paths.Path + "github.com/roddhjav/apparmor.d/pkg/prebuild/prepare" ) func init() { - DistDir = paths.New("dists") - Root = paths.New(".build") - FlagDir = DistDir.Join("flags") - RootApparmord = Root.Join("apparmor.d") - if oss.Distribution == "ubuntu" { - if oss.Release["VERSION_CODENAME"] == "noble" { - Builds = append(Builds, BuildABI3) - overwrite = true + // Define the tasks applied by default + prepare.Register( + "synchronise", + "ignore", + "merge", + "configure", + "setflags", + "systemd-default", + ) + + // Build tasks applied by default + builder.Register("userspace") + + switch cfg.Distribution { + case "ubuntu": + if cfg.Release["VERSION_CODENAME"] == "noble" { + builder.Register("abi3") + cfg.Overwrite = true } } } -func getFctName(i any) string { - tmp := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() - res := strings.Split(tmp, ".") - return res[len(res)-1] -} - -func printPrepareMessage(name string, msg []string) { - logging.Success("%v", PrepareMsg[name]) - logging.Indent = " " - for _, line := range msg { - logging.Bullet("%s", line) - } - logging.Indent = "" -} - -func printBuildMessage() { - for _, fct := range Builds { - name := getFctName(fct) - logging.Success("%v", BuildMsg[name]) - } - for _, dir := range directive.Directives { - logging.Success("%v", dir.Message()) - } -} - func Prepare() error { - for _, fct := range Prepares { - msg, err := fct() + for _, task := range prepare.Prepares { + msg, err := task.Apply() if err != nil { return err } - printPrepareMessage(getFctName(fct), msg) + logging.Success("%s", task.Message()) + logging.Indent = " " + for _, line := range msg { + if strings.Contains(line, "not found") { + logging.Warning("%s", line) + } else { + logging.Bullet("%s", line) + } + } + logging.Indent = "" } return nil } func Build() error { - files, _ := RootApparmord.ReadDirRecursiveFiltered(nil, paths.FilterOutDirectories()) + files, _ := cfg.RootApparmord.ReadDirRecursiveFiltered(nil, paths.FilterOutDirectories()) for _, file := range files { if !file.Exist() { continue @@ -83,14 +69,26 @@ func Build() error { return err } profile := string(content) - for _, fct := range Builds { - profile = fct(profile) + for _, b := range builder.Builds { + profile = b.Apply(profile) } profile = directive.Run(file, profile) if err := file.WriteFile([]byte(profile)); err != nil { return err } } - printBuildMessage() + + logging.Success("Build tasks:") + logging.Indent = " " + for _, task := range builder.Builds { + logging.Bullet("%s", task.Message()) + } + logging.Indent = "" + logging.Success("Directives processed:") + logging.Indent = " " + for _, dir := range directive.Directives { + logging.Bullet("%s", dir.Name()) + } + logging.Indent = "" return nil } diff --git a/pkg/prebuild/prebuild_test.go b/pkg/prebuild/prebuild_test.go index 196bce7e..083dca35 100644 --- a/pkg/prebuild/prebuild_test.go +++ b/pkg/prebuild/prebuild_test.go @@ -9,7 +9,9 @@ import ( "os/exec" "testing" - oss "github.com/roddhjav/apparmor.d/pkg/os" + "github.com/roddhjav/apparmor.d/pkg/prebuild/builder" + "github.com/roddhjav/apparmor.d/pkg/prebuild/cfg" + "github.com/roddhjav/apparmor.d/pkg/prebuild/prepare" ) func chdirGitRoot() { @@ -65,27 +67,20 @@ func Test_PreBuild(t *testing.T) { enforce: false, dist: "opensuse", }, - // { - // name: "Build for Fedora", - // wantErr: true, - // full: false, - // complain: false, - // dist: "fedora", - // }, } chdirGitRoot() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - oss.Distribution = tt.dist + cfg.Distribution = tt.dist if tt.full { - Prepares = append(Prepares, SetFullSystemPolicy) - Builds = append(Builds, BuildFullSystemPolicy) + prepare.Register("fsp") + builder.Register("fsp") } if tt.complain { - Builds = append(Builds, BuildComplain) + builder.Register("complain") } if tt.enforce { - Builds = append(Builds, BuildEnforce) + builder.Register("enforce") } if err := Prepare(); (err != nil) != tt.wantErr { t.Errorf("Prepare() error = %v, wantErr %v", err, tt.wantErr)