Skip to content

Commit

Permalink
Merge pull request kubevirt#2665 from cynepco3hahue/selinux_log_messages
Browse files Browse the repository at this point in the history
Log the error under the SELinux package
  • Loading branch information
slintes authored Sep 4, 2019
2 parents 162320b + ff59b53 commit 17935b9
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pkg/virt-handler/selinux/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,33 @@ func copyPolicy(policyName string, dir string) (err error) {
return nil
}

func (se *SELinuxImpl) Label(label string, dir string) (err error) {
// Label sets selinux label on the directory
func (se *SELinuxImpl) Label(label string, dir string) error {
dir = strings.TrimRight(dir, "/") + "(/.*)?"
out, err := se.execute("semanage", se.Paths, "fcontext", "-a", "-t", label, dir)
if err != nil {
return fmt.Errorf("failed to set label for directory %v: %v ", dir, string(out))
if out, err := se.execute("semanage", se.Paths, "fcontext", "-a", "-t", label, dir); err != nil {
return fmt.Errorf("failed to set label for directory %v: out: %q, error: %v", dir, string(out), err)
}
return nil
}

func (se *SELinuxImpl) IsLabeled(dir string) (labeled bool, err error) {
// IsLabeled verifies if the directory already labeled
func (se *SELinuxImpl) IsLabeled(dir string) (bool, error) {
dir = strings.TrimRight(dir, "/") + "(/.*)?"
out, err := se.execute("semanage", se.Paths, "fcontext", "-l")
if err != nil {
return false, fmt.Errorf("failed to list labels: %v ", string(out))
return false, fmt.Errorf("failed to list labels: out: %q, error: %v", string(out), err)
}
if strings.Contains(string(out), dir) {
return true, nil
}
return false, nil
}

func (se *SELinuxImpl) Restore(dir string) (err error) {
// Restore restores selinux labels on the directory
func (se *SELinuxImpl) Restore(dir string) error {
dir = strings.TrimRight(dir, "/") + "/"
out, err := se.execute("restorecon", se.Paths, "-r", "-v", dir)
if err != nil {
return fmt.Errorf("failed to set selinux permissions: %v ", string(out))
if out, err := se.execute("restorecon", se.Paths, "-r", "-v", dir); err != nil {
return fmt.Errorf("failed to set selinux permissions: out: %q, error: %v", string(out), err)
}
return nil
}
Expand Down

0 comments on commit 17935b9

Please sign in to comment.