Skip to content

Commit 48ed3ac

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

File tree

6 files changed

+233
-0
lines changed

6 files changed

+233
-0
lines changed

config/common/errors.go

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ var (
9393

9494
// Kernel arguments
9595
ErrGeneralKernelArgumentSupport = errors.New("kernel argument customization is not supported in this spec version")
96+
97+
// Selinux Module
98+
ErrContentInvalid = errors.New("Content is empty, please provide content.")
99+
ErrNameInvalid = errors.New("Name is empty, please provide a valid name.")
100+
ErrFieldInvalid = errors.New("Please, provide valid information.")
96101
)
97102

98103
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

+73
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package v1_6_exp
1616

1717
import (
1818
"fmt"
19+
"os/exec"
1920
"strings"
2021

2122
baseutil "github.com/coreos/butane/base/util"
@@ -89,6 +90,13 @@ func (c Config) ToIgn3_5Unvalidated(options common.TranslateOptions) (types.Conf
8990
retConfig, ts := baseutil.MergeTranslatedConfigs(retp, tsp, ret, ts)
9091
ret = retConfig.(types.Config)
9192
r.Merge(rp)
93+
94+
// Clean this as it needs to not be so confusing
95+
retr, trs, rr := c.handleSelinux(options)
96+
returnConfig, ts := baseutil.MergeTranslatedConfigs(retr, trs, ret, ts)
97+
ret = returnConfig.(types.Config)
98+
r.Merge(rr)
99+
92100
return ret, ts, r
93101
}
94102

@@ -367,3 +375,68 @@ func buildGrubConfig(gb Grub) string {
367375
superUserCmd := fmt.Sprintf("set superusers=\"%s\"\n", strings.Join(allUsers, " "))
368376
return "# Generated by Butane\n\n" + superUserCmd + strings.Join(cmds, "\n") + "\n"
369377
}
378+
379+
func (c Config) handleSelinux(options common.TranslateOptions) (types.Config, translate.TranslationSet, report.Report) {
380+
rendered := types.Config{}
381+
ts := translate.NewTranslationSet("yaml", "json")
382+
var r report.Report
383+
384+
for i, module := range c.Selinux.Module {
385+
yamlPath := path.New("yaml", "selinux", "module", i)
386+
if module.Name != "" && module.Content != "" {
387+
rendered = processModule(rendered, module, options, ts, r, yamlPath)
388+
} else {
389+
r.AddOnWarn(path.New("yaml", "selinux", "module", len(rendered.Storage.Files)), common.ErrFieldInvalid)
390+
}
391+
}
392+
393+
if len(rendered.Storage.Files) != 0 {
394+
rendered.Storage.Filesystems = append(rendered.Storage.Filesystems,
395+
types.Filesystem{
396+
Device: "/dev/disk/by-label/boot",
397+
Format: util.StrToPtr("ext4"),
398+
Path: util.StrToPtr("/boot"),
399+
})
400+
ts.AddFromCommonSource(path.New("yaml", "selinux", "module"), path.New("json", "storage"), rendered.Storage)
401+
402+
}
403+
404+
return rendered, ts, r
405+
}
406+
407+
func processModule(rendered types.Config, module Module, options common.TranslateOptions, ts translate.TranslationSet, r report.Report, yamlPath path.ContextPath) types.Config {
408+
src, compression, err := baseutil.MakeDataURL([]byte(module.Content), nil, !options.NoResourceAutoCompression)
409+
if err != nil {
410+
r.AddOnError(yamlPath, err)
411+
return rendered
412+
}
413+
filePath := fmt.Sprintf("/etc/selinux/targeted/modules/active/extra/%s.cil", module.Name)
414+
415+
rendered.Storage.Files = append(rendered.Storage.Files,
416+
types.File{
417+
Node: types.Node{
418+
Path: filePath,
419+
},
420+
FileEmbedded1: types.FileEmbedded1{
421+
Append: []types.Resource{
422+
{
423+
Source: util.StrToPtr(src),
424+
Compression: compression,
425+
},
426+
},
427+
},
428+
})
429+
430+
commandToExecute := "semodule -i"
431+
cmd := exec.Command(commandToExecute, filePath)
432+
err = cmd.Run()
433+
if err != nil {
434+
fmt.Printf("Error running semodule %v", module.Name)
435+
}
436+
437+
fmt.Printf("SELinux module file imported successfully\n")
438+
439+
ts.AddFromCommonSource(yamlPath, path.New("json", "storage"), rendered.Storage)
440+
441+
return rendered
442+
}

config/fcos/v1_6_exp/translate_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -1637,3 +1637,85 @@ func TestTranslateGrub(t *testing.T) {
16371637
})
16381638
}
16391639
}
1640+
1641+
func TestTranslateSelinux(t *testing.T) {
1642+
translations := []translate.Translation{
1643+
{From: path.New("yaml", "version"), To: path.New("json", "ignition", "version")},
1644+
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems")},
1645+
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems", 0)},
1646+
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems", 0, "path")},
1647+
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems", 0, "device")},
1648+
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems", 0, "format")},
1649+
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage")},
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+
},
1676+
types.Config{
1677+
Ignition: types.Ignition{
1678+
Version: "3.5.0-experimental",
1679+
},
1680+
Storage: types.Storage{
1681+
Filesystems: []types.Filesystem{
1682+
{
1683+
Device: "/dev/disk/by-label/boot",
1684+
Format: util.StrToPtr("ext4"),
1685+
Path: util.StrToPtr("/boot"),
1686+
},
1687+
},
1688+
Files: []types.File{
1689+
{
1690+
Node: types.Node{
1691+
Path: "/etc/selinux/targeted/modules/active/extra/some_name.cil",
1692+
},
1693+
FileEmbedded1: types.FileEmbedded1{
1694+
Append: []types.Resource{
1695+
{
1696+
Source: util.StrToPtr("data:,some%20content%20here"),
1697+
Compression: util.StrToPtr(""),
1698+
},
1699+
},
1700+
},
1701+
},
1702+
},
1703+
},
1704+
},
1705+
translations,
1706+
report.Report{},
1707+
},
1708+
}
1709+
1710+
for i, test := range tests {
1711+
t.Run(fmt.Sprintf("translate %d", i), func(t *testing.T) {
1712+
actual, translations, r := test.in.ToIgn3_5Unvalidated(common.TranslateOptions{})
1713+
r = confutil.TranslateReportPaths(r, translations)
1714+
baseutil.VerifyReport(t, test.in, r)
1715+
assert.Equal(t, test.out, actual, "translation mismatch")
1716+
assert.Equal(t, test.report, r, "report mismatch")
1717+
baseutil.VerifyTranslations(t, translations, test.exceptions)
1718+
assert.NoError(t, translations.DebugVerifyCoverage(actual), "incomplete TranslationSet coverage")
1719+
})
1720+
}
1721+
}

config/fcos/v1_6_exp/validate.go

+11
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,14 @@ 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("content"), common.ErrContentInvalid)
84+
}
85+
86+
if m.Content != "" && m.Name == "" {
87+
r.AddOnError(c.Append("name"), common.ErrNameInvalid)
88+
}
89+
return r
90+
}

config/fcos/v1_6_exp/validate_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,55 @@ 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+
// content is empty, path is specified
491+
in: Module{
492+
Content: "",
493+
Name: "some name",
494+
},
495+
out: common.ErrContentInvalid,
496+
errPath: path.New("yaml", "content"),
497+
},
498+
{
499+
// name is empty, content is specified
500+
in: Module{
501+
Name: "",
502+
Content: "some content",
503+
},
504+
out: common.ErrNameInvalid,
505+
errPath: path.New("yaml", "name"),
506+
},
507+
{
508+
// name and content are empty
509+
in: Module{},
510+
out: nil,
511+
errPath: path.New("yaml"),
512+
},
513+
{
514+
// name and content are specified
515+
in: Module{
516+
Content: "some content",
517+
Name: "some name",
518+
},
519+
out: nil,
520+
errPath: path.New("yaml"),
521+
},
522+
}
523+
524+
for i, test := range tests {
525+
t.Run(fmt.Sprintf("validate %d", i), func(t *testing.T) {
526+
actual := test.in.Validate(path.New("yaml"))
527+
baseutil.VerifyReport(t, test.in, actual)
528+
expected := report.Report{}
529+
expected.AddOnError(test.errPath, test.out)
530+
assert.Equal(t, expected, actual, "bad report")
531+
})
532+
}
533+
}

0 commit comments

Comments
 (0)