-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fcos/v1_6_exp: Add new sugar for Selinux Modules.
- Loading branch information
1 parent
0a1b18e
commit 310a1f1
Showing
6 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,13 @@ func (c Config) ToIgn3_5Unvalidated(options common.TranslateOptions) (types.Conf | |
retConfig, ts := baseutil.MergeTranslatedConfigs(retp, tsp, ret, ts) | ||
ret = retConfig.(types.Config) | ||
r.Merge(rp) | ||
|
||
// Clean this as it needs to not be so confusing | ||
retr, trs, rr := c.handleSelinux(options) | ||
returnConfig, ts := baseutil.MergeTranslatedConfigs(retr, trs, ret, ts) | ||
ret = returnConfig.(types.Config) | ||
r.Merge(rr) | ||
|
||
return ret, ts, r | ||
} | ||
|
||
|
@@ -367,3 +374,79 @@ func buildGrubConfig(gb Grub) string { | |
superUserCmd := fmt.Sprintf("set superusers=\"%s\"\n", strings.Join(allUsers, " ")) | ||
return "# Generated by Butane\n\n" + superUserCmd + strings.Join(cmds, "\n") + "\n" | ||
} | ||
|
||
func (c Config) handleSelinux(options common.TranslateOptions) (types.Config, translate.TranslationSet, report.Report) { | ||
rendered := types.Config{} | ||
ts := translate.NewTranslationSet("yaml", "json") | ||
var r report.Report | ||
|
||
for i, module := range c.Selinux.Module { | ||
yamlPath := path.New("yaml", "selinux", "module", i) | ||
if module.Name != "" && module.Content != "" { | ||
rendered = processModule(rendered, module, options, ts, r, yamlPath) | ||
} else { | ||
r.AddOnWarn(yamlPath, common.ErrFieldInvalid) | ||
} | ||
} | ||
|
||
if len(rendered.Storage.Files) != 0 { | ||
newFilesystem := types.Filesystem{ | ||
Device: "/dev/disk/by-label/boot", | ||
Format: util.StrToPtr("ext4"), | ||
Path: util.StrToPtr("/boot"), | ||
} | ||
|
||
rendered.Storage.Filesystems = append(rendered.Storage.Filesystems, newFilesystem) | ||
ts.AddFromCommonSource(path.New("yaml", "selinux", "module", 0), path.New("json", "storage"), newFilesystem) | ||
|
||
} | ||
|
||
return rendered, ts, r | ||
} | ||
|
||
func processModule(rendered types.Config, module Module, options common.TranslateOptions, ts translate.TranslationSet, r report.Report, yamlPath path.ContextPath) types.Config { | ||
src, compression, err := baseutil.MakeDataURL([]byte(module.Content), nil, !options.NoResourceAutoCompression) | ||
if err != nil { | ||
r.AddOnError(yamlPath, err) | ||
return rendered | ||
} | ||
|
||
// Create module file | ||
modulePath := fmt.Sprintf("/etc/selinux/targeted/modules/active/extra/%s.cil", module.Name) | ||
newFile := types.File{ | ||
Node: types.Node{ | ||
Path: modulePath, | ||
}, | ||
FileEmbedded1: types.FileEmbedded1{ | ||
Append: []types.Resource{ | ||
{ | ||
Source: util.StrToPtr(src), | ||
Compression: compression, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
filePath := path.New("json", "storage", "files", len(rendered.Storage.Files), newFile) | ||
rendered.Storage.Files = append(rendered.Storage.Files, newFile) | ||
ts.AddFromCommonSource(yamlPath, filePath, newFile) | ||
|
||
// Create systemd unit to import module | ||
cmdToExecute := "/usr/sbin/semodule -i" + modulePath | ||
newUnit := types.Unit{ | ||
Name: "[email protected]", | ||
Enabled: util.BoolToPtr(true), | ||
Dropins: []types.Dropin{ | ||
{ | ||
Name: module.Name + ".conf", | ||
Contents: util.StrToPtr(" [Unit]\n" + "Description=Import SELinux module\n" + "[Service]\n" + "Type=oneshot\n" + "RemainAfterExit=no\n" + "ExecStart=" + cmdToExecute + "\n" + "[Install]\n" + "WantedBy=multi-user.target\n"), | ||
}, | ||
}, | ||
} | ||
|
||
unitPath := path.New("json", "systemd", "units", len(rendered.Systemd.Units)) | ||
rendered.Systemd.Units = append(rendered.Systemd.Units, newUnit) | ||
ts.AddFromCommonSource(yamlPath, unitPath, newUnit) | ||
|
||
return rendered | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1637,3 +1637,104 @@ func TestTranslateGrub(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestTranslateSelinux(t *testing.T) { | ||
cmdToExecute := "/usr/sbin/semodule -i" + "/etc/selinux/targeted/modules/active/extra/some_name.cil" | ||
translations := []translate.Translation{ | ||
{From: path.New("yaml", "version"), To: path.New("json", "ignition", "version")}, | ||
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems")}, | ||
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems", 0)}, | ||
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems", 0, "path")}, | ||
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems", 0, "device")}, | ||
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage", "filesystems", 0, "format")}, | ||
{From: path.New("yaml", "selinux", "module"), To: path.New("json", "storage")}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "systemd", "units", 0)}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "systemd", "units", 0, "name")}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "systemd", "units", 0, "dropins")}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "systemd", "units", 0, "dropins", 0)}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files")}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0)}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "path")}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "append")}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "append", 0)}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "append", 0, "source")}, | ||
{From: path.New("yaml", "selinux", "module", 0), To: path.New("json", "storage", "files", 0, "append", 0, "compression")}, | ||
} | ||
tests := []struct { | ||
in Config | ||
out types.Config | ||
exceptions []translate.Translation | ||
report report.Report | ||
}{ | ||
// config with one module | ||
{ | ||
Config{ | ||
Selinux: Selinux{ | ||
Module: []Module{ | ||
{ | ||
Name: "some_name", | ||
Content: "some content here", | ||
}, | ||
}, | ||
}, | ||
}, | ||
types.Config{ | ||
Ignition: types.Ignition{ | ||
Version: "3.5.0-experimental", | ||
}, | ||
Systemd: types.Systemd{ | ||
Units: []types.Unit{ | ||
{ | ||
Name: "[email protected]", | ||
Enabled: util.BoolToPtr(true), | ||
Dropins: []types.Dropin{ | ||
{ | ||
Name: "some_name.conf", | ||
Contents: util.StrToPtr(" [Unit]\n" + "Description=Import SELinux module\n" + "[Service]\n" + "Type=oneshot\n" + "RemainAfterExit=no\n" + "ExecStart=" + cmdToExecute + "\n" + "[Install]\n" + "WantedBy=multi-user.target\n"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Storage: types.Storage{ | ||
Filesystems: []types.Filesystem{ | ||
{ | ||
Device: "/dev/disk/by-label/boot", | ||
Format: util.StrToPtr("ext4"), | ||
Path: util.StrToPtr("/boot"), | ||
}, | ||
}, | ||
Files: []types.File{ | ||
{ | ||
Node: types.Node{ | ||
Path: "/etc/selinux/targeted/modules/active/extra/some_name.cil", | ||
}, | ||
FileEmbedded1: types.FileEmbedded1{ | ||
Append: []types.Resource{ | ||
{ | ||
Source: util.StrToPtr("data:,some%20content%20here"), | ||
Compression: util.StrToPtr(""), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
translations, | ||
report.Report{}, | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
t.Run(fmt.Sprintf("translate %d", i), func(t *testing.T) { | ||
actual, translations, r := test.in.ToIgn3_5Unvalidated(common.TranslateOptions{}) | ||
r = confutil.TranslateReportPaths(r, translations) | ||
baseutil.VerifyReport(t, test.in, r) | ||
assert.Equal(t, test.out, actual, "translation mismatch") | ||
assert.Equal(t, test.report, r, "report mismatch") | ||
baseutil.VerifyTranslations(t, translations, test.exceptions) | ||
assert.NoError(t, translations.DebugVerifyCoverage(actual), "incomplete TranslationSet coverage") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters