build: ensure @{exec_path} is present in profile att.

This commit is contained in:
Alexandre Pujol 2024-07-15 23:04:35 +01:00
parent 8ef9a18242
commit 9b2470462f
No known key found for this signature in database
GPG Key ID: C5469996F0DF68EC

View File

@ -5,6 +5,7 @@
package builder package builder
import ( import (
"fmt"
"regexp" "regexp"
"strings" "strings"
@ -12,8 +13,10 @@ import (
"github.com/roddhjav/apparmor.d/pkg/prebuild/cfg" "github.com/roddhjav/apparmor.d/pkg/prebuild/cfg"
) )
const tokATTACHMENT = "@{exec_path}"
var ( var (
regAttachments = regexp.MustCompile(`(profile .* @{exec_path})`) regAttachments = regexp.MustCompile(`(profile .* ` + tokATTACHMENT + `)`)
) )
type Userspace struct { type Userspace struct {
@ -41,13 +44,18 @@ func (b Userspace) Apply(opt *Option, profile string) (string, error) {
if _, err := f.Parse(profile); err != nil { if _, err := f.Parse(profile); err != nil {
return "", err return "", err
} }
if len(f.GetDefaultProfile().Attachments) > 0 &&
f.GetDefaultProfile().Attachments[0] != tokATTACHMENT {
return "", fmt.Errorf("missing '%s' attachment", tokATTACHMENT)
}
if err := f.Resolve(); err != nil { if err := f.Resolve(); err != nil {
return "", err return "", err
} }
att := f.GetDefaultProfile().GetAttachments()
matches := regAttachments.FindAllString(profile, -1) matches := regAttachments.FindAllString(profile, -1)
if len(matches) > 0 { if len(matches) > 0 {
strheader := strings.Replace(matches[0], "@{exec_path}", att, -1) att := f.GetDefaultProfile().GetAttachments()
strheader := strings.Replace(matches[0], tokATTACHMENT, att, -1)
return regAttachments.ReplaceAllLiteralString(profile, strheader), nil return regAttachments.ReplaceAllLiteralString(profile, strheader), nil
} }
return profile, nil return profile, nil