feat(prebuild): make prebuild available as an external package.

Usefull for downstream repo.
This commit is contained in:
Alexandre Pujol 2023-05-06 13:01:07 +01:00
parent 538da05696
commit 913ac3131c
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
13 changed files with 304 additions and 214 deletions

View file

@ -9,9 +9,9 @@ import (
"fmt"
"os"
"github.com/arduino/go-paths-helper"
"github.com/roddhjav/apparmor.d/pkg/logging"
"github.com/roddhjav/apparmor.d/pkg/util"
"github.com/roddhjav/apparmor.d/pkg/prebuild"
"golang.org/x/exp/slices"
)
const usage = `prebuild [-h] [--full] [--complain]
@ -20,82 +20,53 @@ const usage = `prebuild [-h] [--full] [--complain]
Options:
-h, --help Show this help message and exit.
-d, --dist The target Linux distribution.
-f, --full Set AppArmor for full system policy.
-c, --complain Set complain flag on all profiles.
`
var (
help bool
Full bool
Complain bool
Distribution string
DistDir *paths.Path
Root *paths.Path
RootApparmord *paths.Path
// Prepare the build directory with the following tasks
prepare = []prepareFunc{Synchronise, Ignore, Merge, Configure, SetFlags, SetFullSystemPolicy}
// Build the profiles with the following build tasks
build = []buildFunc{BuildUserspace, BuildComplain, BuildABI}
help bool
full bool
complain bool
)
type prepareFunc func() error
type buildFunc func(string) string
func init() {
DistDir = paths.New("dists")
Root = paths.New(".build")
RootApparmord = Root.Join("apparmor.d")
Distribution, _ = util.GetSupportedDistribution()
flag.BoolVar(&help, "h", false, "Show this help message and exit.")
flag.BoolVar(&help, "help", false, "Show this help message and exit.")
flag.BoolVar(&Full, "f", false, "Set AppArmor for full system policy.")
flag.BoolVar(&Full, "full", false, "Set AppArmor for full system policy.")
flag.BoolVar(&Complain, "c", false, "Set complain flag on all profiles.")
flag.BoolVar(&Complain, "complain", false, "Set complain flag on all profiles.")
}
// Build the profiles.
func buildProfiles() error {
files, _ := RootApparmord.ReadDir(paths.FilterOutDirectories())
for _, file := range files {
if !file.Exist() {
continue
}
content, _ := file.ReadFile()
profile := string(content)
for _, fct := range build {
profile = fct(profile)
}
if err := file.WriteFile([]byte(profile)); err != nil {
panic(err)
}
}
return nil
flag.BoolVar(&full, "f", false, "Set AppArmor for full system policy.")
flag.BoolVar(&full, "full", false, "Set AppArmor for full system policy.")
flag.BoolVar(&complain, "c", false, "Set complain flag on all profiles.")
flag.BoolVar(&complain, "complain", false, "Set complain flag on all profiles.")
}
func aaPrebuild() error {
logging.Step("Building apparmor.d profiles for %s.", Distribution)
logging.Step("Building apparmor.d profiles for %s.", prebuild.Distribution)
for _, fct := range prepare {
if err := fct(); err != nil {
return err
}
if full {
prebuild.Prepares = append(prebuild.Prepares, prebuild.SetFullSystemPolicy)
}
if complain {
prebuild.Builds = append(prebuild.Builds, prebuild.BuildComplain)
}
if slices.Contains([]string{"debian", "whonix"}, prebuild.Distribution) {
prebuild.Builds = append(prebuild.Builds, prebuild.BuildABI)
}
if err := buildProfiles(); err != nil {
if err := prebuild.Prepare(); err != nil {
return err
}
if err := prebuild.Build(); err != nil {
return err
}
logging.Success("Builded profiles with: ")
logging.Bullet("Bypass userspace tools restriction")
if Complain {
if complain {
logging.Bullet("Set complain flag on all profiles")
}
switch Distribution {
case "debian", "whonix":
logging.Bullet("%s does not support abi 3.0 yet", Distribution)
if slices.Contains([]string{"debian", "whonix"}, prebuild.Distribution) {
logging.Bullet("%s does not support abi 3.0 yet", prebuild.Distribution)
}
return nil
}
@ -107,8 +78,7 @@ func main() {
flag.Usage()
os.Exit(0)
}
err := aaPrebuild()
if err != nil {
if err := aaPrebuild(); err != nil {
logging.Fatal(err.Error())
}
}