Skip to content

Exit oscap-im if oscap fails #2222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions utils/oscap-im
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,24 @@ def add_eval_args(args, cmd):


def pre_scan_fix(args):
with tempfile.NamedTemporaryFile(delete=False) as remediation_script:
with tempfile.NamedTemporaryFile() as remediation_script:
gen_fix_cmd = [
"oscap", "xccdf", "generate", "fix", "--fix-type", "bootc",
"--output", remediation_script.name]
add_common_args(args, gen_fix_cmd)
gen_fix_cmd.append(args.data_stream)
subprocess.run(gen_fix_cmd, check=True)
subprocess.run(["bash", remediation_script.name], check=True)
try:
subprocess.run(gen_fix_cmd, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
raise RuntimeError(
f"OpenSCAP generate fix failed with return code "
f"{e.returncode}.\nOutput: {e.stderr.decode()}")
try:
subprocess.run(["bash", remediation_script.name], check=True)
except subprocess.CalledProcessError as e:
raise RuntimeError(
f"Remediation script failed with return code "
f"{e.returncode}.")


def scan_and_remediate(args):
Expand All @@ -125,15 +135,20 @@ def scan_and_remediate(args):
subprocess.run(oscap_cmd, env=env, check=True)
except subprocess.CalledProcessError as e:
if e.returncode not in [0, 2]:
print(e, file=sys.stderr)
raise RuntimeError(
f"OpenSCAP scan failed with return code {e.returncode}.\n")


def main():
args = parse_args()
verify_bootc_build_env()
install_sce_dependencies()
pre_scan_fix(args)
scan_and_remediate(args)
try:
pre_scan_fix(args)
scan_and_remediate(args)
except RuntimeError as e:
print(e, file=sys.stderr)
sys.exit(1)


if __name__ == "__main__":
Expand Down
Loading