Skip to content

Instantly share code, notes, and snippets.

@robbmanes
Last active February 20, 2023 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbmanes/133b2768d6e53ab44e4a00e638a84c27 to your computer and use it in GitHub Desktop.
Save robbmanes/133b2768d6e53ab44e4a00e638a84c27 to your computer and use it in GitHub Desktop.
Go program that tests the container labeling capabilites of `go-selinux/label`.
package main
import (
"fmt"
"github.com/opencontainers/selinux/go-selinux/label"
)
// Mirroring the behavior found here:
// https://github.com/containers/podman/blob/8432ed7488e9c62738f9308fcc03f2ae9c2cd615/libpod/util_linux.go#L110-L129
func main() {
path := "/home/rmanes/Downloads"
_, mountLabel, err := label.InitLabels([]string{})
if err != nil {
fmt.Errorf("Error getting default labels: %w", err)
}
// Deprecated
// https://github.com/opencontainers/selinux/blob/main/go-selinux/label/label.go#L61
if err := label.ReleaseLabel(mountLabel); err != nil {
fmt.Errorf("Error releasing label: %q, %w", mountLabel, err)
}
if err := label.Relabel(path, mountLabel, true); err != nil {
fmt.Errorf("Error relabeling path %s with label %q: %w", path, mountLabel, err)
}
fmt.Printf("Completed path \"%s\" relabeling with label %q.", path, mountLabel)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment