mirror of
https://github.com/roddhjav/apparmor.d.git
synced 2025-01-12 23:37:51 +01:00
0206e04b3f
Some checks failed
Ubuntu / build (default, ubuntu-22.04) (push) Has been cancelled
Ubuntu / build (default, ubuntu-24.04) (push) Has been cancelled
Ubuntu / build (full-system-policy, ubuntu-22.04) (push) Has been cancelled
Ubuntu / build (full-system-policy, ubuntu-24.04) (push) Has been cancelled
Ubuntu / tests (push) Has been cancelled
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
// apparmor.d - Full set of apparmor profiles
|
|
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
package builder
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/roddhjav/apparmor.d/pkg/paths"
|
|
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
|
)
|
|
|
|
var (
|
|
// Build the profiles with the following directive applied
|
|
Builds = []Builder{}
|
|
|
|
// Available builders
|
|
Builders = map[string]Builder{}
|
|
)
|
|
|
|
// Main directive interface
|
|
type Builder interface {
|
|
prebuild.BaseInterface
|
|
Apply(opt *Option, profile string) (string, error)
|
|
}
|
|
|
|
// Builder options
|
|
type Option struct {
|
|
Name string
|
|
File *paths.Path
|
|
}
|
|
|
|
func NewOption(file *paths.Path) *Option {
|
|
return &Option{
|
|
Name: strings.TrimSuffix(file.Base(), ".apparmor.d"),
|
|
File: file,
|
|
}
|
|
}
|
|
|
|
func Register(names ...string) {
|
|
for _, name := range names {
|
|
if b, present := Builders[name]; present {
|
|
Builds = append(Builds, b)
|
|
} else {
|
|
panic(fmt.Sprintf("Unknown builder: %s", name))
|
|
}
|
|
}
|
|
}
|
|
|
|
func RegisterBuilder(d Builder) {
|
|
Builders[d.Name()] = d
|
|
}
|
|
|
|
func Run(file *paths.Path, profile string) (string, error) {
|
|
var err error
|
|
opt := NewOption(file)
|
|
for _, b := range Builds {
|
|
profile, err = b.Apply(opt, profile)
|
|
if err != nil {
|
|
return "", fmt.Errorf("%s %s: %w", b.Name(), opt.File, err)
|
|
}
|
|
}
|
|
return profile, nil
|
|
}
|