build: improve build output.

This commit is contained in:
Alexandre Pujol 2023-12-15 19:14:32 +00:00
parent d2fc3c3325
commit 6fa2c8ec3a
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
3 changed files with 106 additions and 57 deletions

View file

@ -14,9 +14,18 @@ import (
) )
// Build the profiles with the following build tasks // Build the profiles with the following build tasks
var Builds = []BuildFunc{ var (
Builds = []BuildFunc{
BuildUserspace, BuildUserspace,
} }
BuildMsg = map[string]string{
"BuildComplain": "Set complain flag on all profiles",
"BuildEnforce": "All profiles have been enforced",
"BuildUserspace": "Bypass userspace tools restriction",
"BuildABI3": "Build all profiles for abi 3.0 compatibility",
"BuildFullSystemPolicy": "Build all profiles for full system policy mode",
}
)
var ( var (
regAttachments = regexp.MustCompile(`(profile .* @{exec_path})`) regAttachments = regexp.MustCompile(`(profile .* @{exec_path})`)

View file

@ -5,7 +5,12 @@
package prebuild package prebuild
import ( import (
"reflect"
"runtime"
"strings"
"github.com/arduino/go-paths-helper" "github.com/arduino/go-paths-helper"
"github.com/roddhjav/apparmor.d/pkg/logging"
) )
var ( var (
@ -24,11 +29,35 @@ func init() {
Distribution = getSupportedDistribution() Distribution = getSupportedDistribution()
} }
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])
}
}
func Prepare() error { func Prepare() error {
for _, fct := range Prepares { for _, fct := range Prepares {
if err := fct(); err != nil { msg, err := fct()
if err != nil {
return err return err
} }
printPrepareMessage(getFctName(fct), msg)
} }
return nil return nil
} }
@ -48,5 +77,6 @@ func Build() error {
panic(err) panic(err)
} }
} }
printBuildMessage()
return nil return nil
} }

View file

@ -15,35 +15,47 @@ import (
) )
// Prepare the build directory with the following tasks // Prepare the build directory with the following tasks
var Prepares = []PrepareFunc{ var (
Prepares = []PrepareFunc{
Synchronise, Synchronise,
Ignore, Ignore,
Merge, Merge,
Configure, Configure,
SetFlags, SetFlags,
} }
PrepareMsg = map[string]string{
"Synchronise": "Initialize a new clean apparmor.d build directory",
"Ignore": "Ignore profiles and files from:",
"Merge": "Merge all profiles",
"Configure": "Set distribution specificities",
"SetFlags": "Set flags on some profiles",
"SetDefaultSystemd": "Set systemd unit drop in files to ensure some service start after apparmor",
"SetFullSystemPolicy": "Configure AppArmor for full system policy",
}
)
type PrepareFunc func() error type PrepareFunc func() ([]string, error)
// Initialize a new clean apparmor.d build directory // Initialize a new clean apparmor.d build directory
func Synchronise() error { func Synchronise() ([]string, error) {
res := []string{}
dirs := paths.PathList{RootApparmord, Root.Join("root"), Root.Join("systemd")} dirs := paths.PathList{RootApparmord, Root.Join("root"), Root.Join("systemd")}
for _, dir := range dirs { for _, dir := range dirs {
if err := dir.RemoveAll(); err != nil { if err := dir.RemoveAll(); err != nil {
return err return res, err
} }
} }
for _, name := range []string{"apparmor.d", "root"} { for _, name := range []string{"apparmor.d", "root"} {
if err := copyTo(paths.New(name), Root.Join(name)); err != nil { if err := copyTo(paths.New(name), Root.Join(name)); err != nil {
return err return res, err
} }
} }
logging.Success("Initialize a new clean apparmor.d build directory") return res, nil
return nil
} }
// Ignore profiles and files as defined in dists/ignore/ // Ignore profiles and files as defined in dists/ignore/
func Ignore() error { func Ignore() ([]string, error) {
res := []string{}
for _, name := range []string{"main.ignore", Distribution + ".ignore"} { for _, name := range []string{"main.ignore", Distribution + ".ignore"} {
path := DistDir.Join("ignore", name) path := DistDir.Join("ignore", name)
if !path.Exist() { if !path.Exist() {
@ -58,27 +70,28 @@ func Ignore() error {
if profile.NotExist() { if profile.NotExist() {
files, err := RootApparmord.ReadDirRecursiveFiltered(nil, paths.FilterNames(line)) files, err := RootApparmord.ReadDirRecursiveFiltered(nil, paths.FilterNames(line))
if err != nil { if err != nil {
return err return res, err
} }
for _, path := range files { for _, path := range files {
if err := path.RemoveAll(); err != nil { if err := path.RemoveAll(); err != nil {
return err return res, err
} }
} }
} else { } else {
if err := profile.RemoveAll(); err != nil { if err := profile.RemoveAll(); err != nil {
return err return res, err
} }
} }
} }
logging.Success("Ignore profiles/files in %s", path) res = append(res, path.String())
} }
return nil return res, nil
} }
// Merge all profiles in a new apparmor.d directory // Merge all profiles in a new apparmor.d directory
func Merge() error { func Merge() ([]string, error) {
var dirToMerge = []string{ res := []string{}
dirToMerge := []string{
"groups/*/*", "groups", "groups/*/*", "groups",
"profiles-*-*/*", "profiles-*", "profiles-*-*/*", "profiles-*",
} }
@ -88,50 +101,51 @@ func Merge() error {
dirMoved, dirRemoved := dirToMerge[idx], dirToMerge[idx+1] dirMoved, dirRemoved := dirToMerge[idx], dirToMerge[idx+1]
files, err := filepath.Glob(RootApparmord.Join(dirMoved).String()) files, err := filepath.Glob(RootApparmord.Join(dirMoved).String())
if err != nil { if err != nil {
return err return res, err
} }
for _, file := range files { for _, file := range files {
err := os.Rename(file, RootApparmord.Join(filepath.Base(file)).String()) err := os.Rename(file, RootApparmord.Join(filepath.Base(file)).String())
if err != nil { if err != nil {
return err return res, err
} }
} }
files, err = filepath.Glob(RootApparmord.Join(dirRemoved).String()) files, err = filepath.Glob(RootApparmord.Join(dirRemoved).String())
if err != nil { if err != nil {
return err return []string{}, err
} }
for _, file := range files { for _, file := range files {
if err := paths.New(file).RemoveAll(); err != nil { if err := paths.New(file).RemoveAll(); err != nil {
return err return res, err
} }
} }
idx = idx + 2 idx = idx + 2
} }
logging.Success("Merge all profiles") return res, nil
return nil
} }
// Set the distribution specificities // Set the distribution specificities
func Configure() (err error) { func Configure() ([]string, error) {
res := []string{}
switch Distribution { switch Distribution {
case "arch", "opensuse": case "arch", "opensuse":
case "debian", "ubuntu", "whonix": case "debian", "ubuntu", "whonix":
// Copy Ubuntu specific profiles // Copy Ubuntu specific profiles
if err := copyTo(DistDir.Join("ubuntu"), RootApparmord); err != nil { if err := copyTo(DistDir.Join("ubuntu"), RootApparmord); err != nil {
return err return res, err
} }
default: default:
return fmt.Errorf("%s is not a supported distribution", Distribution) return []string{}, fmt.Errorf("%s is not a supported distribution", Distribution)
} }
return err return res, nil
} }
// Set flags on some profiles according to manifest defined in `dists/flags/` // Set flags on some profiles according to manifest defined in `dists/flags/`
func SetFlags() error { func SetFlags() ([]string, error) {
res := []string{}
for _, name := range []string{"main.flags", Distribution + ".flags"} { for _, name := range []string{"main.flags", Distribution + ".flags"} {
path := FlagDir.Join(name) path := FlagDir.Join(name)
if !path.Exist() { if !path.Exist() {
@ -155,51 +169,47 @@ func SetFlags() error {
flags := " flags=(" + manifest[1] + ") {" flags := " flags=(" + manifest[1] + ") {"
content, err := file.ReadFile() content, err := file.ReadFile()
if err != nil { if err != nil {
return err return res, err
} }
// Remove all flags definition, then set manifest' flags // Remove all flags definition, then set manifest' flags
res := regFlags.ReplaceAllLiteralString(string(content), "") out := regFlags.ReplaceAllLiteralString(string(content), "")
res = regProfileHeader.ReplaceAllLiteralString(res, flags) out = regProfileHeader.ReplaceAllLiteralString(out, flags)
if err := file.WriteFile([]byte(res)); err != nil { if err := file.WriteFile([]byte(out)); err != nil {
return err return res, err
} }
} }
} }
logging.Success("Set profile flags from %s", path) res = append(res, path.String())
} }
return nil return res, nil
} }
// Set systemd unit drop in files to ensure some service start after apparmor // Set systemd unit drop in files to ensure some service start after apparmor
func SetDefaultSystemd() error { func SetDefaultSystemd() ([]string, error) {
return copyTo(paths.New("systemd/default/"), Root.Join("systemd")) return []string{}, copyTo(paths.New("systemd/default/"), Root.Join("systemd"))
} }
// Set AppArmor for (experimental) full system policy. // Set AppArmor for (experimental) full system policy.
// See https://apparmor.pujol.io/development/structure/#full-system-policy // See https://apparmor.pujol.io/development/structure/#full-system-policy
func SetFullSystemPolicy() error { func SetFullSystemPolicy() ([]string, error) {
res := []string{}
// Install full system policy profiles // Install full system policy profiles
if err := copyTo(paths.New("apparmor.d/groups/_full/"), Root.Join("apparmor.d")); err != nil { if err := copyTo(paths.New("apparmor.d/groups/_full/"), Root.Join("apparmor.d")); err != nil {
return err return res, err
} }
// Set systemd profile name // Set systemd profile name
path := RootApparmord.Join("tunables/multiarch.d/system") path := RootApparmord.Join("tunables/multiarch.d/system")
content, err := path.ReadFile() content, err := path.ReadFile()
if err != nil { if err != nil {
return err return res, err
} }
res := strings.Replace(string(content), "@{systemd}=unconfined", "@{systemd}=systemd", -1) out := strings.Replace(string(content), "@{systemd}=unconfined", "@{systemd}=systemd", -1)
if err := path.WriteFile([]byte(res)); err != nil { if err := path.WriteFile([]byte(out)); err != nil {
return err return res, err
} }
// Set systemd unit drop-in files // Set systemd unit drop-in files
if err := copyTo(paths.New("systemd/full/"), Root.Join("systemd")); err != nil { return res, copyTo(paths.New("systemd/full/"), Root.Join("systemd"))
return err
}
logging.Success("Configure AppArmor for full system policy")
return nil
} }