Skip to content

Commit a47e0b0

Browse files
committed
fcos/v1_6_exp: Add new sugar for Selinux Modules.
1 parent 0a1b18e commit a47e0b0

File tree

6 files changed

+265
-0
lines changed

6 files changed

+265
-0
lines changed

config/common/errors.go

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ var (
9393

9494
// Kernel arguments
9595
ErrGeneralKernelArgumentSupport = errors.New("kernel argument customization is not supported in this spec version")
96+
97+
// Selinux Module
98+
ErrSelinuxContentNotSpecified = errors.New("field \"content\" is required")
99+
ErrSelinuxNameNotSpecified = errors.New("field \"name\" is required")
96100
)
97101

98102
type ErrUnmarshal struct {

config/fcos/v1_6_exp/schema.go

+27
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Config struct {
2222
base.Config `yaml:",inline"`
2323
BootDevice BootDevice `yaml:"boot_device"`
2424
Grub Grub `yaml:"grub"`
25+
Selinux Selinux `yaml:"selinux"`
26+
Systemd Systemd `yaml:"systemd"`
2527
}
2628

2729
type BootDevice struct {
@@ -49,3 +51,28 @@ type GrubUser struct {
4951
Name string `yaml:"name"`
5052
PasswordHash *string `yaml:"password_hash"`
5153
}
54+
55+
type Systemd struct {
56+
Units []Unit `yaml:"units"`
57+
}
58+
59+
type Unit struct {
60+
Contents *string `yaml:"contents"`
61+
Dropins []Dropin `yaml:"dropins"`
62+
Enabled *bool `yaml:"enabled"`
63+
Mask *bool `yaml:"mask"`
64+
Name string `yaml:"name"`
65+
}
66+
67+
type Dropin struct {
68+
Contents *string `yaml:"contents"`
69+
Name string `yaml:"name"`
70+
}
71+
type Selinux struct {
72+
Module []Module `yaml:"module"`
73+
}
74+
75+
type Module struct {
76+
Name string `yaml:"name"`
77+
Content string `yaml:"content"`
78+
}

config/fcos/v1_6_exp/translate.go

+70
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ func (c Config) ToIgn3_5Unvalidated(options common.TranslateOptions) (types.Conf
8989
retConfig, ts := baseutil.MergeTranslatedConfigs(retp, tsp, ret, ts)
9090
ret = retConfig.(types.Config)
9191
r.Merge(rp)
92+
93+
// Clean this as it needs to not be so confusing
94+
retr, trs, rr := c.handleSelinux(options)
95+
returnConfig, ts := baseutil.MergeTranslatedConfigs(retr, trs, ret, ts)
96+
ret = returnConfig.(types.Config)
97+
r.Merge(rr)
98+
9299
return ret, ts, r
93100
}
94101

@@ -367,3 +374,66 @@ func buildGrubConfig(gb Grub) string {
367374
superUserCmd := fmt.Sprintf("set superusers=\"%s\"\n", strings.Join(allUsers, " "))
368375
return "# Generated by Butane\n\n" + superUserCmd + strings.Join(cmds, "\n") + "\n"
369376
}
377+
378+
func (c Config) handleSelinux(options common.TranslateOptions) (types.Config, translate.TranslationSet, report.Report) {
379+
rendered := types.Config{}
380+
ts := translate.NewTranslationSet("yaml", "json")
381+
var r report.Report
382+
383+
for i, module := range c.Selinux.Module {
384+
yamlPath := path.New("yaml", "selinux", "module", i)
385+
rendered = processModule(rendered, module, options, ts, r, yamlPath)
386+
387+
}
388+
return rendered, ts, r
389+
}
390+
391+
func processModule(rendered types.Config, module Module, options common.TranslateOptions, ts translate.TranslationSet, r report.Report, yamlPath path.ContextPath) types.Config {
392+
src, compression, err := baseutil.MakeDataURL([]byte(module.Content), nil, !options.NoResourceAutoCompression)
393+
if err != nil {
394+
r.AddOnError(yamlPath, err)
395+
return rendered
396+
}
397+
398+
// Create module file
399+
modulePath := fmt.Sprintf("/etc/selinux/targeted/modules/active/extra/%s.cil", module.Name)
400+
newFile := types.File{
401+
Node: types.Node{
402+
Path: modulePath,
403+
},
404+
FileEmbedded1: types.FileEmbedded1{
405+
Append: []types.Resource{
406+
{
407+
Source: util.StrToPtr(src),
408+
Compression: compression,
409+
},
410+
},
411+
},
412+
}
413+
414+
filePath := path.New("json", "storage", "files", len(rendered.Storage.Files), newFile)
415+
rendered.Storage.Files = append(rendered.Storage.Files, newFile)
416+
ts.AddFromCommonSource(yamlPath, filePath, newFile)
417+
418+
// Create systemd unit to import module
419+
cmdToExecute := "/usr/sbin/semodule -i" + modulePath
420+
newUnit := types.Unit{
421+
Name: module.Name + ".conf",
422+
Contents: util.StrToPtr(
423+
"[Unit]\n" +
424+
"Description=Import SELinux module\n" +
425+
"[Service]\n" +
426+
"Type=oneshot\n" +
427+
"RemainAfterExit=yes\n" +
428+
"ExecStart=" + cmdToExecute + "\n" +
429+
"[Install]\n" +
430+
"WantedBy=multi-user.target\n"),
431+
Enabled: util.BoolToPtr(true),
432+
}
433+
434+
unitPath := path.New("json", "systemd", "units", len(rendered.Systemd.Units))
435+
rendered.Systemd.Units = append(rendered.Systemd.Units, newUnit)
436+
ts.AddFromCommonSource(yamlPath, unitPath, newUnit)
437+
438+
return rendered
439+
}

config/fcos/v1_6_exp/translate_test.go

+101
Original file line numberDiff line numberDiff line change
@@ -1637,3 +1637,104 @@ func TestTranslateGrub(t *testing.T) {
16371637
})
16381638
}
16391639
}
1640+
1641+
func TestTranslateSelinux(t *testing.T) {
1642+
cmdToExecute := "/usr/sbin/semodule -i" + "/etc/selinux/targeted/modules/active/extra/some_name.cil"
1643+
translations := []translate.Translation{
1644+
{From: path.New("yaml", "version"), To: path.New("json", "ignition", "version")},
1645+
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage")},
1646+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "systemd", "units", 0)},
1647+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "systemd", "units", 0, "name")},
1648+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "systemd", "units", 0, "contents")},
1649+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "systemd", "units", 0, "enabled")},
1650+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files")},
1651+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0)},
1652+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "path")},
1653+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "append")},
1654+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "append", 0)},
1655+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "append", 0, "source")},
1656+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "append", 0, "compression")},
1657+
}
1658+
tests := []struct {
1659+
in Config
1660+
out types.Config
1661+
exceptions []translate.Translation
1662+
report report.Report
1663+
}{
1664+
// config with one module
1665+
{
1666+
Config{
1667+
Selinux: Selinux{
1668+
Module: []Module{
1669+
{
1670+
Name: "some_name",
1671+
Content: "some content here",
1672+
},
1673+
},
1674+
},
1675+
Systemd: Systemd{ //how can i write the systemd here if isnt the right config type?
1676+
Units: []Unit{
1677+
{
1678+
Name: "some_name.conf",
1679+
Enabled: util.BoolToPtr(true),
1680+
},
1681+
},
1682+
},
1683+
},
1684+
1685+
types.Config{
1686+
Ignition: types.Ignition{
1687+
Version: "3.5.0-experimental",
1688+
},
1689+
Storage: types.Storage{
1690+
Files: []types.File{
1691+
{
1692+
Node: types.Node{
1693+
Path: "/etc/selinux/targeted/modules/active/extra/some_name.cil",
1694+
},
1695+
FileEmbedded1: types.FileEmbedded1{
1696+
Append: []types.Resource{
1697+
{
1698+
Source: util.StrToPtr("data:,some%20content%20here"),
1699+
Compression: util.StrToPtr(""),
1700+
},
1701+
},
1702+
},
1703+
},
1704+
},
1705+
},
1706+
Systemd: types.Systemd{
1707+
Units: []types.Unit{
1708+
{
1709+
Name: "some_name" + ".conf",
1710+
Enabled: util.BoolToPtr(true),
1711+
Contents: util.StrToPtr(
1712+
"[Unit]\n" +
1713+
"Description=Import SELinux module\n" +
1714+
"[Service]\n" +
1715+
"Type=oneshot\n" +
1716+
"RemainAfterExit=yes\n" +
1717+
"ExecStart=" + cmdToExecute + "\n" +
1718+
"[Install]\n" +
1719+
"WantedBy=multi-user.target\n"),
1720+
},
1721+
},
1722+
},
1723+
},
1724+
translations,
1725+
report.Report{},
1726+
},
1727+
}
1728+
1729+
for i, test := range tests {
1730+
t.Run(fmt.Sprintf("translate %d", i), func(t *testing.T) {
1731+
actual, translations, r := test.in.ToIgn3_5Unvalidated(common.TranslateOptions{})
1732+
r = confutil.TranslateReportPaths(r, translations)
1733+
baseutil.VerifyReport(t, test.in, r)
1734+
assert.Equal(t, test.out, actual, "translation mismatch")
1735+
assert.Equal(t, test.report, r, "report mismatch")
1736+
baseutil.VerifyTranslations(t, translations, test.exceptions)
1737+
assert.NoError(t, translations.DebugVerifyCoverage(actual), "incomplete TranslationSet coverage")
1738+
})
1739+
}
1740+
}

config/fcos/v1_6_exp/validate.go

+17
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,20 @@ func (user GrubUser) Validate(c path.ContextPath) (r report.Report) {
7777
}
7878
return
7979
}
80+
81+
func (m Module) Validate(c path.ContextPath) (r report.Report) {
82+
if m.Name == "" && m.Content == "" {
83+
r.AddOnError(c.Append("name"), common.ErrSelinuxContentNotSpecified)
84+
r.AddOnError(c.Append("content"), common.ErrSelinuxContentNotSpecified)
85+
} else {
86+
if m.Name == "" {
87+
r.AddOnError(c.Append("name"), common.ErrSelinuxNameNotSpecified)
88+
}
89+
90+
if m.Content == "" {
91+
r.AddOnError(c.Append("content"), common.ErrSelinuxContentNotSpecified)
92+
}
93+
}
94+
95+
return r
96+
}

config/fcos/v1_6_exp/validate_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,49 @@ func TestValidateConfig(t *testing.T) {
479479
})
480480
}
481481
}
482+
483+
func TestValidateModule(t *testing.T) {
484+
tests := []struct {
485+
in Module
486+
out error
487+
errPath path.ContextPath
488+
}{
489+
{
490+
// valid module
491+
in: Module{
492+
Content: "some content",
493+
Name: "some name",
494+
},
495+
out: nil,
496+
errPath: path.New("yaml"),
497+
},
498+
{
499+
// content is not specified
500+
in: Module{
501+
Content: "",
502+
Name: "some name",
503+
},
504+
out: common.ErrSelinuxContentNotSpecified,
505+
errPath: path.New("yaml", "content"),
506+
},
507+
{
508+
// name is not specified
509+
in: Module{
510+
Name: "",
511+
Content: "some content",
512+
},
513+
out: common.ErrSelinuxNameNotSpecified,
514+
errPath: path.New("yaml", "name"),
515+
},
516+
}
517+
518+
for i, test := range tests {
519+
t.Run(fmt.Sprintf("validate %d", i), func(t *testing.T) {
520+
actual := test.in.Validate(path.New("yaml"))
521+
baseutil.VerifyReport(t, test.in, actual)
522+
expected := report.Report{}
523+
expected.AddOnError(test.errPath, test.out)
524+
assert.Equal(t, expected, actual, "bad report")
525+
})
526+
}
527+
}

0 commit comments

Comments
 (0)