Blame stages/org.osbuild.selinux

Packit a20ca0
#!/usr/bin/python3
Packit a20ca0
"""
Packit a20ca0
Set SELinux file contexts
Packit a20ca0
Packit a20ca0
Sets correct SELinux labels for every file in the tree, according to the
Packit a20ca0
SELinux policy installed inside the tree.
Packit a20ca0
Packit a20ca0
Uses the host's `setfiles` program and the tree's `file_contexts`, usually
Packit a20ca0
    /etc/selinux/<SELINUXTYPE>/contexts/files/file_contexts
Packit a20ca0
where <SELINUXTYPE> is the value set in /etc/selinux/config (usually "targeted"
Packit a20ca0
but may also be "minimum" or "mls").
Packit a20ca0
Packit a20ca0
This stage may set or modify xattrs for any file inside the tree, but should
Packit a20ca0
not need to create files, modify file contents, or read any files other than
Packit a20ca0
`file_contexts`.
Packit a20ca0
Packit a20ca0
This stage should run after all other stages that create (or move) files, since
Packit a20ca0
labels for newly-created files are determined by the host's SELinux policy and
Packit a20ca0
may not match the tree's policy.
Packit a20ca0
"""
Packit a20ca0
Packit a20ca0
Packit a20ca0
import os
Packit a20ca0
import subprocess
Packit a20ca0
import sys
Packit a20ca0
Packit Service 2d981f
import osbuild.api
Packit Service 2d981f
Packit a20ca0
Packit a20ca0
SCHEMA = """
Packit a20ca0
"additionalProperties": false,
Packit a20ca0
"required": ["file_contexts"],
Packit a20ca0
"properties": {
Packit a20ca0
  "file_contexts": {
Packit a20ca0
    "type": "string",
Packit a20ca0
    "description": "Path to the active SELinux policy's `file_contexts`"
Packit a20ca0
  },
Packit a20ca0
  "labels": {
Packit a20ca0
    "type": "object",
Packit a20ca0
    "description": "Labels to set of the specified files or folders",
Packit a20ca0
    "items": {
Packit a20ca0
      "type": "object"
Packit a20ca0
    }
Packit a20ca0
  }
Packit a20ca0
}
Packit a20ca0
"""
Packit a20ca0
Packit a20ca0
Packit a20ca0
def main(tree, options):
Packit a20ca0
    file_contexts = os.path.join(f"{tree}", options["file_contexts"])
Packit a20ca0
    labels = options.get("labels", {})
Packit a20ca0
Packit a20ca0
    subprocess.run(["setfiles", "-F", "-r", f"{tree}", f"{file_contexts}", f"{tree}"], check=True)
Packit a20ca0
Packit a20ca0
    for path, label in labels.items():
Packit a20ca0
        fullpath = os.path.join(tree, path.lstrip("/"))
Packit a20ca0
        subprocess.run(["chcon", "-v", label, fullpath], check=True)
Packit a20ca0
Packit a20ca0
Packit a20ca0
if __name__ == '__main__':
Packit Service 2d981f
    args = osbuild.api.arguments()
Packit a20ca0
    r = main(args["tree"], args["options"])
Packit a20ca0
    sys.exit(r)