From e1d1d0be3d605d030acf1f098f9d047eb87ff1c3 Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Thu, 21 Mar 2024 18:58:32 +0000 Subject: [PATCH] refractor(build): move os logic to its own module. --- cmd/aa-test/main.go | 4 +- cmd/prebuild/main.go | 3 +- cmd/prebuild/main_test.go | 3 +- pkg/os/os.go | 90 +++++++++++++++++++ pkg/{prebuild/tools_test.go => os/os_test.go} | 63 ++++++++++--- pkg/prebuild/prebuild.go | 8 +- pkg/prebuild/prebuild_test.go | 4 +- pkg/prebuild/prepare.go | 9 +- pkg/prebuild/tools.go | 60 ------------- 9 files changed, 158 insertions(+), 86 deletions(-) create mode 100644 pkg/os/os.go rename pkg/{prebuild/tools_test.go => os/os_test.go} (81%) diff --git a/cmd/aa-test/main.go b/cmd/aa-test/main.go index 680922fa..5ab05ff7 100644 --- a/cmd/aa-test/main.go +++ b/cmd/aa-test/main.go @@ -15,7 +15,7 @@ import ( "github.com/roddhjav/apparmor.d/pkg/aa" "github.com/roddhjav/apparmor.d/pkg/integration" "github.com/roddhjav/apparmor.d/pkg/logging" - "github.com/roddhjav/apparmor.d/pkg/prebuild" + oss "github.com/roddhjav/apparmor.d/pkg/os" ) const usage = `aa-test [-h] [--bootstrap | --run | --list] @@ -123,7 +123,7 @@ func testDeps(dryRun bool) error { } deps := tSuite.GetDependencies() - switch prebuild.Distribution { + switch oss.Distribution { case "arch": arg := []string{"pacman", "-Sy", "--noconfirm"} arg = append(arg, deps...) diff --git a/cmd/prebuild/main.go b/cmd/prebuild/main.go index 91c9e3da..d4f376a2 100644 --- a/cmd/prebuild/main.go +++ b/cmd/prebuild/main.go @@ -10,6 +10,7 @@ import ( "os" "github.com/roddhjav/apparmor.d/pkg/logging" + oss "github.com/roddhjav/apparmor.d/pkg/os" "github.com/roddhjav/apparmor.d/pkg/prebuild" ) @@ -46,7 +47,7 @@ func init() { } func aaPrebuild() error { - logging.Step("Building apparmor.d profiles for %s.", prebuild.Distribution) + logging.Step("Building apparmor.d profiles for %s.", oss.Distribution) if full { prebuild.Prepares = append(prebuild.Prepares, prebuild.SetFullSystemPolicy) diff --git a/cmd/prebuild/main_test.go b/cmd/prebuild/main_test.go index 5f9acff0..89fc52d1 100644 --- a/cmd/prebuild/main_test.go +++ b/cmd/prebuild/main_test.go @@ -9,6 +9,7 @@ import ( "os/exec" "testing" + oss "github.com/roddhjav/apparmor.d/pkg/os" "github.com/roddhjav/apparmor.d/pkg/prebuild" ) @@ -71,7 +72,7 @@ func Test_AAPrebuild(t *testing.T) { chdirGitRoot() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - prebuild.Distribution = tt.dist + oss.Distribution = tt.dist if tt.full { prebuild.Prepares = append(prebuild.Prepares, prebuild.SetFullSystemPolicy) } diff --git a/pkg/os/os.go b/pkg/os/os.go new file mode 100644 index 00000000..942589cb --- /dev/null +++ b/pkg/os/os.go @@ -0,0 +1,90 @@ +// apparmor.d - Full set of apparmor profiles +// Copyright (C) 2023-2024 Alexandre Pujol +// SPDX-License-Identifier: GPL-2.0-only + +package util + +import ( + "os" + "slices" + "strings" + + "github.com/arduino/go-paths-helper" +) + +var ( + Distribution = getDistribution() + Release = getOSRelease() + Family = getFamily() +) + +var ( + osReleaseFile = "/etc/os-release" + supportedDists = map[string][]string{ + "arch": {}, + "debian": {}, + "ubuntu": {}, + "opensuse": {"suse", "opensuse-tumbleweed"}, + "whonix": {}, + } + famillyDists = map[string][]string{ + "apt": {"debian", "ubuntu", "whonix"}, + "pacman": {"arch"}, + "zypper": {"opensuse"}, + } +) + +func getOSRelease() map[string]string { + var lines []string + var err error + for _, name := range []string{osReleaseFile, "/usr/lib/os-release"} { + path := paths.New(name) + if path.Exist() { + lines, err = path.ReadFileAsLines() + if err != nil { + panic(err) + } + break + } + } + os := map[string]string{} + for _, line := range lines { + item := strings.Split(line, "=") + if len(item) == 2 { + os[item[0]] = strings.Trim(item[1], "\"") + } + } + return os +} + +func getDistribution() string { + dist, present := os.LookupEnv("DISTRIBUTION") + if present { + return dist + } + + id := Release["ID"] + if id == "ubuntu" { + return id + } + id_like := Release["ID_LIKE"] + for main, based := range supportedDists { + if main == id || main == id_like { + return main + } else if slices.Contains(based, id) { + return main + } else if slices.Contains(based, id_like) { + return main + } + } + return id +} + +func getFamily() string { + for familly, dist := range famillyDists { + if slices.Contains(dist, Distribution) { + return familly + } + } + return "" +} diff --git a/pkg/prebuild/tools_test.go b/pkg/os/os_test.go similarity index 81% rename from pkg/prebuild/tools_test.go rename to pkg/os/os_test.go index 8a739447..04ba6504 100644 --- a/pkg/prebuild/tools_test.go +++ b/pkg/os/os_test.go @@ -2,7 +2,7 @@ // Copyright (C) 2023-2024 Alexandre Pujol // SPDX-License-Identifier: GPL-2.0-only -package prebuild +package util import ( "reflect" @@ -79,7 +79,7 @@ ANSI_COLOR="0;38;2;60;110;180" LOGO=fedora-logo-icon` ) -func TestNewOSRelease(t *testing.T) { +func Test_getOSRelease(t *testing.T) { tests := []struct { name string osRelease string @@ -128,14 +128,14 @@ func TestNewOSRelease(t *testing.T) { if err != nil { return } - if got := NewOSRelease(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("NewOSRelease() = %v, want %v", got, tt.want) + if got := getOSRelease(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("getOSRelease() = %v, want %v", got, tt.want) } }) } } -func Test_getSupportedDistribution(t *testing.T) { +func Test_getDistribution(t *testing.T) { tests := []struct { name string osRelease string @@ -161,11 +161,11 @@ func Test_getSupportedDistribution(t *testing.T) { osRelease: OpenSUSETumbleweed, want: "opensuse", }, - // { - // name: "Fedora", - // osRelease: Fedora, - // want: "fedora", - // }, + { + name: "Fedora", + osRelease: Fedora, + want: "fedora", + }, } osReleaseFile = "/tmp/os-release" @@ -175,9 +175,48 @@ func Test_getSupportedDistribution(t *testing.T) { if err != nil { return } - got := getSupportedDistribution() + Release = getOSRelease() + got := getDistribution() if got != tt.want { - t.Errorf("getSupportedDistribution() = %v, want %v", got, tt.want) + t.Errorf("getDistribution() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_getFamily(t *testing.T) { + tests := []struct { + name string + dist string + want string + }{ + { + name: "Archlinux", + dist: "arch", + want: "pacman", + }, + { + name: "Ubuntu", + dist: "ubuntu", + want: "apt", + }, + { + name: "Debian", + dist: "debian", + want: "apt", + }, + { + name: "OpenSUSE Tumbleweed", + dist: "opensuse", + want: "zypper", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + Distribution = tt.dist + if got := getFamily(); got != tt.want { + t.Errorf("getFamily() = %v, want %v", got, tt.want) } }) } diff --git a/pkg/prebuild/prebuild.go b/pkg/prebuild/prebuild.go index e87284f5..99d94d03 100644 --- a/pkg/prebuild/prebuild.go +++ b/pkg/prebuild/prebuild.go @@ -11,11 +11,11 @@ import ( "github.com/arduino/go-paths-helper" "github.com/roddhjav/apparmor.d/pkg/logging" + oss "github.com/roddhjav/apparmor.d/pkg/os" ) var ( overwrite bool = false - Distribution string DistDir *paths.Path Root *paths.Path RootApparmord *paths.Path @@ -27,10 +27,8 @@ func init() { Root = paths.New(".build") FlagDir = DistDir.Join("flags") RootApparmord = Root.Join("apparmor.d") - Distribution = getSupportedDistribution() - if Distribution == "ubuntu" { - os := NewOSRelease() - if os["VERSION_CODENAME"] == "noble" { + if oss.Distribution == "ubuntu" { + if oss.Release["VERSION_CODENAME"] == "noble" { Builds = append(Builds, BuildABI3) overwrite = true } diff --git a/pkg/prebuild/prebuild_test.go b/pkg/prebuild/prebuild_test.go index 079a2bac..196bce7e 100644 --- a/pkg/prebuild/prebuild_test.go +++ b/pkg/prebuild/prebuild_test.go @@ -8,6 +8,8 @@ import ( "os" "os/exec" "testing" + + oss "github.com/roddhjav/apparmor.d/pkg/os" ) func chdirGitRoot() { @@ -74,7 +76,7 @@ func Test_PreBuild(t *testing.T) { chdirGitRoot() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - Distribution = tt.dist + oss.Distribution = tt.dist if tt.full { Prepares = append(Prepares, SetFullSystemPolicy) Builds = append(Builds, BuildFullSystemPolicy) diff --git a/pkg/prebuild/prepare.go b/pkg/prebuild/prepare.go index 8f0b9b46..665b29d1 100644 --- a/pkg/prebuild/prepare.go +++ b/pkg/prebuild/prepare.go @@ -12,6 +12,7 @@ import ( "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/util" ) @@ -59,7 +60,7 @@ func Synchronise() ([]string, error) { // Ignore profiles and files as defined in dists/ignore/ func Ignore() ([]string, error) { res := []string{} - for _, name := range []string{"main.ignore", Distribution + ".ignore"} { + for _, name := range []string{"main.ignore", oss.Distribution + ".ignore"} { path := DistDir.Join("ignore", name) if !path.Exist() { continue @@ -130,7 +131,7 @@ func Merge() ([]string, error) { // Set the distribution specificities func Configure() ([]string, error) { res := []string{} - switch Distribution { + switch oss.Distribution { case "arch", "opensuse": case "ubuntu": @@ -152,7 +153,7 @@ func Configure() ([]string, error) { } default: - return []string{}, fmt.Errorf("%s is not a supported distribution", Distribution) + return []string{}, fmt.Errorf("%s is not a supported distribution", oss.Distribution) } return res, nil @@ -161,7 +162,7 @@ func Configure() ([]string, error) { // Set flags on some profiles according to manifest defined in `dists/flags/` func SetFlags() ([]string, error) { res := []string{} - for _, name := range []string{"main.flags", Distribution + ".flags"} { + for _, name := range []string{"main.flags", oss.Distribution + ".flags"} { path := FlagDir.Join(name) if !path.Exist() { continue diff --git a/pkg/prebuild/tools.go b/pkg/prebuild/tools.go index 0844c971..f8d50673 100644 --- a/pkg/prebuild/tools.go +++ b/pkg/prebuild/tools.go @@ -5,71 +5,11 @@ package prebuild import ( - "os" "strings" "github.com/arduino/go-paths-helper" - "golang.org/x/exp/slices" ) -var ( - osReleaseFile = "/etc/os-release" - supportedDists = map[string][]string{ - "arch": {}, - "debian": {}, - "ubuntu": {}, - "opensuse": {"suse", "opensuse-tumbleweed"}, - "whonix": {}, - } -) - -func NewOSRelease() map[string]string { - var lines []string - var err error - for _, name := range []string{osReleaseFile, "/usr/lib/os-release"} { - path := paths.New(name) - if path.Exist() { - lines, err = path.ReadFileAsLines() - if err != nil { - panic(err) - } - break - } - } - os := map[string]string{} - for _, line := range lines { - item := strings.Split(line, "=") - if len(item) == 2 { - os[item[0]] = strings.Trim(item[1], "\"") - } - } - return os -} - -func getSupportedDistribution() string { - dist, present := os.LookupEnv("DISTRIBUTION") - if present { - return dist - } - - os := NewOSRelease() - id := os["ID"] - if id == "ubuntu" { - return id - } - id_like := os["ID_LIKE"] - for main, based := range supportedDists { - if main == id || main == id_like { - return main - } else if slices.Contains(based, id) { - return main - } else if slices.Contains(based, id_like) { - return main - } - } - return id -} - func copyTo(src *paths.Path, dst *paths.Path) error { files, err := src.ReadDirRecursiveFiltered(nil, paths.FilterOutDirectories(), paths.FilterOutNames("README.md")) if err != nil {