apparmor.d/pkg/prebuild/prebuild.go

101 lines
2.1 KiB
Go
Raw Normal View History

// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package prebuild
import (
2023-12-15 20:14:32 +01:00
"reflect"
"runtime"
"strings"
"github.com/arduino/go-paths-helper"
2023-12-15 20:14:32 +01:00
"github.com/roddhjav/apparmor.d/pkg/logging"
)
var (
2024-03-10 15:47:13 +01:00
overwrite bool = false
Distribution string
DistDir *paths.Path
Root *paths.Path
RootApparmord *paths.Path
FlagDir *paths.Path
)
func init() {
DistDir = paths.New("dists")
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" {
Builds = append(Builds, BuildABI3)
2024-03-10 15:47:13 +01:00
overwrite = true
}
}
}
2023-12-15 20:14:32 +01:00
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 _, fct := range Directives {
name := getFctName(fct)
logging.Success("%v", DirectiveMsg[name])
}
2023-12-15 20:14:32 +01:00
}
func Prepare() error {
for _, fct := range Prepares {
2023-12-15 20:14:32 +01:00
msg, err := fct()
if err != nil {
return err
}
2023-12-15 20:14:32 +01:00
printPrepareMessage(getFctName(fct), msg)
}
return nil
}
func Build() error {
files, _ := RootApparmord.ReadDirRecursiveFiltered(nil, paths.FilterOutDirectories())
for _, file := range files {
if !file.Exist() {
continue
}
2024-03-10 15:24:59 +01:00
content, err := file.ReadFile()
if err != nil {
return err
}
profile := string(content)
for _, fct := range Builds {
profile = fct(profile)
}
for _, fct := range Directives {
profile = fct(file, profile)
}
if err := file.WriteFile([]byte(profile)); err != nil {
2024-03-10 15:24:59 +01:00
return err
}
}
2023-12-15 20:14:32 +01:00
printBuildMessage()
return nil
}