Skip to content

Commit ba98e1b

Browse files
alexlarssonachilleas-k
authored andcommitted
bib: Extract what SELinux policy to us from container
Currently we are always hardcoding "targeted", which is not working for the centos automotive sig that use a custom policy.
1 parent c89561a commit ba98e1b

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

bib/cmd/bootc-image-builder/image.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
344344
img := image.NewBootcDiskImage(containerSource)
345345
img.Users = users.UsersFromBP(customizations.GetUsers())
346346
img.Groups = users.GroupsFromBP(customizations.GetGroups())
347-
// TODO: get from the bootc container instead of hardcoding it
348-
img.SELinux = "targeted"
347+
img.SELinux = c.SourceInfo.SELinuxPolicy
349348

350349
img.KernelOptionsAppend = []string{
351350
"rw",

bib/internal/source/source.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package source
22

33
import (
4+
"bufio"
5+
"errors"
46
"fmt"
57
"os"
68
"path"
@@ -27,6 +29,7 @@ type OSRelease struct {
2729
type Info struct {
2830
OSRelease OSRelease
2931
UEFIVendor string
32+
SELinuxPolicy string
3033
ImageCustomization *blueprint.Customizations
3134
}
3235

@@ -63,6 +66,39 @@ func uefiVendor(root string) (string, error) {
6366
return "", fmt.Errorf("cannot find UEFI vendor in %s", bootupdEfiDir)
6467
}
6568

69+
func readSelinuxPolicy(root string) (string, error) {
70+
configPath := "etc/selinux/config"
71+
f, err := os.Open(path.Join(root, configPath))
72+
if err != nil {
73+
return "", fmt.Errorf("cannot read selinux config %s: %w", configPath, err)
74+
}
75+
// nolint:errcheck
76+
defer f.Close()
77+
78+
policy := ""
79+
scanner := bufio.NewScanner(f)
80+
for scanner.Scan() {
81+
line := strings.TrimSpace(scanner.Text())
82+
if len(line) == 0 {
83+
continue
84+
}
85+
if strings.HasPrefix(line, "#") {
86+
continue
87+
}
88+
89+
parts := strings.SplitN(line, "=", 2)
90+
if len(parts) != 2 {
91+
return "", errors.New("selinux config: invalid input")
92+
}
93+
key := strings.TrimSpace(parts[0])
94+
if key == "SELINUXTYPE" {
95+
policy = strings.TrimSpace(parts[1])
96+
}
97+
}
98+
99+
return policy, nil
100+
}
101+
66102
func readImageCustomization(root string) (*blueprint.Customizations, error) {
67103
prefix := path.Join(root, bibPathPrefix)
68104
config, err := buildconfig.LoadConfig(path.Join(prefix, "config.json"))
@@ -102,6 +138,11 @@ func LoadInfo(root string) (*Info, error) {
102138
return nil, err
103139
}
104140

141+
selinuxPolicy, err := readSelinuxPolicy(root)
142+
if err != nil {
143+
logrus.Debugf("cannot read selinux policy: %v, setting it to none", err)
144+
}
145+
105146
var idLike []string
106147
if osrelease["ID_LIKE"] != "" {
107148
idLike = strings.Split(osrelease["ID_LIKE"], " ")
@@ -118,6 +159,7 @@ func LoadInfo(root string) (*Info, error) {
118159
},
119160

120161
UEFIVendor: vendor,
162+
SELinuxPolicy: selinuxPolicy,
121163
ImageCustomization: customization,
122164
}, nil
123165
}

0 commit comments

Comments
 (0)