Skip to content

Commit

Permalink
fix: fixing bug and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yasminvalim committed Nov 6, 2023
1 parent 47092de commit 3f7db36
Showing 1 changed file with 52 additions and 42 deletions.
94 changes: 52 additions & 42 deletions config/fcos/v1_6_exp/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,63 +380,73 @@ func (c Config) handleSelinux(options common.TranslateOptions) (types.Config, tr
rendered := types.Config{}
ts := translate.NewTranslationSet("yaml", "json")
var r report.Report
yamlPath := path.New("yaml", "selinux", "module")

hasValidModule := false

for _, module := range c.Selinux.Module {
if module.Name != "" && module.Content == "" || module.Content != "" && module.Name == "" || module.Name == "" && module.Content == "" {
r.AddOnWarn(path.New("yaml", "selinux", "module"), common.ErrFieldInvalid)
} else {
if isValidModule(module) {
hasValidModule = true
rendered = processModule(rendered, module, options, ts, r)
break
} else {
r.AddOnWarn(path.New("yaml", "selinux", "module"), common.ErrFieldInvalid)
}
}

if hasValidModule {
rendered.Storage.Filesystems = append(rendered.Storage.Filesystems,
types.Filesystem{
Device: "/dev/disk/by-label/boot",
Format: util.StrToPtr("ext4"),
Path: util.StrToPtr("/boot"),
})
if hasValidModule {
rendered.Storage.Filesystems = append(rendered.Storage.Filesystems,
types.Filesystem{
Device: "/dev/disk/by-label/boot",
Format: util.StrToPtr("ext4"),
Path: util.StrToPtr("/boot"),
})
}

src, compression, err := baseutil.MakeDataURL([]byte(module.Content), nil, !options.NoResourceAutoCompression)
if err != nil {
r.AddOnError(yamlPath, err)
return rendered, ts, r
}
return rendered, ts, r
}

if module.Name != "" {
filePath := fmt.Sprintf("/etc/selinux/targeted/modules/active/extra/%s.cil", module.Name)
func processModule(rendered types.Config, module Module, options common.TranslateOptions, ts translate.TranslationSet, r report.Report) types.Config {
yamlPath := path.New("yaml", "selinux", "module")

rendered.Storage.Files = append(rendered.Storage.Files,
types.File{
Node: types.Node{
Path: filePath,
},
FileEmbedded1: types.FileEmbedded1{
Append: []types.Resource{
{
Source: util.StrToPtr(src),
Compression: compression,
},
},
},
})
src, compression, err := baseutil.MakeDataURL([]byte(module.Content), nil, !options.NoResourceAutoCompression)
if err != nil {
r.AddOnError(yamlPath, err)
return rendered
}

commandToExecute := "semodule -i"
cmd := exec.Command(commandToExecute, filePath)
err := cmd.Run()
if err != nil {
fmt.Printf("Error running semodule %v", module.Name)
}
if isValidModule(module) {
filePath := fmt.Sprintf("/etc/selinux/targeted/modules/active/extra/%s.cil", module.Name)

fmt.Printf("SELinux module file imported successfully\n")
rendered.Storage.Files = append(rendered.Storage.Files,
types.File{
Node: types.Node{
Path: filePath,
},
FileEmbedded1: types.FileEmbedded1{
Append: []types.Resource{
{
Source: util.StrToPtr(src),
Compression: compression,
},
},
},
})

ts.AddFromCommonSource(yamlPath, path.New("json", "storage"), rendered.Storage)
}
commandToExecute := "semodule -i"
cmd := exec.Command(commandToExecute, filePath)
err := cmd.Run()
if err != nil {
fmt.Printf("Error running semodule %v", module.Name)
}

fmt.Printf("SELinux module file imported successfully\n")
}

return rendered, ts, r
ts.AddFromCommonSource(yamlPath, path.New("json", "storage"), rendered.Storage)

return rendered
}

func isValidModule(module Module) bool {
return module.Name != "" && module.Content != ""
}

0 comments on commit 3f7db36

Please sign in to comment.