Skip to content

Commit 1b6bae2

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

File tree

6 files changed

+252
-0
lines changed

6 files changed

+252
-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

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Config struct {
2222
base.Config `yaml:",inline"`
2323
BootDevice BootDevice `yaml:"boot_device"`
2424
Grub Grub `yaml:"grub"`
25+
Selinux Selinux `yaml:"selinux"`
2526
}
2627

2728
type BootDevice struct {
@@ -49,3 +50,12 @@ type GrubUser struct {
4950
Name string `yaml:"name"`
5051
PasswordHash *string `yaml:"password_hash"`
5152
}
53+
54+
type Selinux struct {
55+
Module []Module `yaml:"module"`
56+
}
57+
58+
type Module struct {
59+
Name string `yaml:"name"`
60+
Content string `yaml:"content"`
61+
}

config/fcos/v1_6_exp/translate.go

+74
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,21 @@ func (c Config) ToIgn3_5Unvalidated(options common.TranslateOptions) (types.Conf
8585
}
8686
}
8787

88+
return mergeAndHandleOptions(c, ret, ts, r, options)
89+
90+
}
91+
92+
func mergeAndHandleOptions(c Config, ret types.Config, ts translate.TranslationSet, r report.Report, options common.TranslateOptions) (types.Config, translate.TranslationSet, report.Report) {
8893
retp, tsp, rp := c.handleUserGrubCfg(options)
8994
retConfig, ts := baseutil.MergeTranslatedConfigs(retp, tsp, ret, ts)
9095
ret = retConfig.(types.Config)
9196
r.Merge(rp)
97+
98+
retr, trs, rr := c.handleSelinux(options)
99+
returnConfig, ts := baseutil.MergeTranslatedConfigs(retr, trs, ret, ts)
100+
ret = returnConfig.(types.Config)
101+
r.Merge(rr)
102+
92103
return ret, ts, r
93104
}
94105

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

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{
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)