Move selinux labeling support from pkg/util to pkg/selinux

The goal here is to make the package less heavy and not overload
the pkg/util.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2020-04-22 08:56:37 -04:00
parent 703fd50553
commit ede8380d37
3 changed files with 43 additions and 39 deletions

40
pkg/selinux/selinux.go Normal file
View File

@ -0,0 +1,40 @@
package selinux
import (
"github.com/opencontainers/selinux/go-selinux"
)
// SELinuxKVMLabel returns labels for running kvm isolated containers
func SELinuxKVMLabel(cLabel string) (string, error) {
if cLabel == "" {
// selinux is disabled
return "", nil
}
processLabel, _ := selinux.KVMContainerLabels()
selinux.ReleaseLabel(processLabel)
return swapSELinuxLabel(cLabel, processLabel)
}
// SELinuxInitLabel returns labels for running systemd based containers
func SELinuxInitLabel(cLabel string) (string, error) {
if cLabel == "" {
// selinux is disabled
return "", nil
}
processLabel, _ := selinux.InitContainerLabels()
selinux.ReleaseLabel(processLabel)
return swapSELinuxLabel(cLabel, processLabel)
}
func swapSELinuxLabel(cLabel, processLabel string) (string, error) {
dcon, err := selinux.NewContext(cLabel)
if err != nil {
return "", err
}
scon, err := selinux.NewContext(processLabel)
if err != nil {
return "", err
}
dcon["type"] = scon["type"]
return dcon.Get(), nil
}