1
1
package source
2
2
3
3
import (
4
+ "bufio"
5
+ "errors"
4
6
"fmt"
5
7
"os"
6
8
"path"
@@ -27,6 +29,7 @@ type OSRelease struct {
27
29
type Info struct {
28
30
OSRelease OSRelease
29
31
UEFIVendor string
32
+ SELinuxPolicy string
30
33
ImageCustomization * blueprint.Customizations
31
34
}
32
35
@@ -63,6 +66,39 @@ func uefiVendor(root string) (string, error) {
63
66
return "" , fmt .Errorf ("cannot find UEFI vendor in %s" , bootupdEfiDir )
64
67
}
65
68
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
+
66
102
func readImageCustomization (root string ) (* blueprint.Customizations , error ) {
67
103
prefix := path .Join (root , bibPathPrefix )
68
104
config , err := buildconfig .LoadConfig (path .Join (prefix , "config.json" ))
@@ -102,6 +138,11 @@ func LoadInfo(root string) (*Info, error) {
102
138
return nil , err
103
139
}
104
140
141
+ selinuxPolicy , err := readSelinuxPolicy (root )
142
+ if err != nil {
143
+ logrus .Debugf ("cannot read selinux policy: %v, setting it to none" , err )
144
+ }
145
+
105
146
var idLike []string
106
147
if osrelease ["ID_LIKE" ] != "" {
107
148
idLike = strings .Split (osrelease ["ID_LIKE" ], " " )
@@ -118,6 +159,7 @@ func LoadInfo(root string) (*Info, error) {
118
159
},
119
160
120
161
UEFIVendor : vendor ,
162
+ SELinuxPolicy : selinuxPolicy ,
121
163
ImageCustomization : customization ,
122
164
}, nil
123
165
}
0 commit comments