Skip to content

Commit 687a0b4

Browse files
committed
Exit oscap-im if oscap fails
This commit will cause that oscap-im will exit with exit code 1 if any of the called oscap calls will fail. This means that building hardened bootable container images will terminate if oscap crashes or doesn't work. Resolves: https://issues.redhat.com/browse/OPENSCAP-5415
1 parent 061e9ca commit 687a0b4

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

utils/oscap-im

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,22 @@ def pre_scan_fix(args):
111111
"--output", remediation_script.name]
112112
add_common_args(args, gen_fix_cmd)
113113
gen_fix_cmd.append(args.data_stream)
114-
subprocess.run(gen_fix_cmd, check=True)
115-
subprocess.run(["bash", remediation_script.name], check=True)
116-
114+
try:
115+
subprocess.run(gen_fix_cmd, check=True, capture_output=True)
116+
except subprocess.CalledProcessError as e:
117+
raise RuntimeError(
118+
f"OpenSCAP generate fix failed with return code {e.returncode}.\n"
119+
f"Output: {e.stderr.decode()}")
120+
try:
121+
subprocess.run(
122+
["bash", remediation_script.name], check=True,
123+
capture_output=True)
124+
except subprocess.CalledProcessError as e:
125+
raise RuntimeError(
126+
f"Remediation script failed with return code {e.returncode}.\n"
127+
f"Output: {e.stderr.decode()}")
128+
finally:
129+
Path(remediation_script.name).unlink()
117130

118131
def scan_and_remediate(args):
119132
oscap_cmd = ["oscap", "xccdf", "eval", "--progress", "--remediate"]
@@ -125,15 +138,20 @@ def scan_and_remediate(args):
125138
subprocess.run(oscap_cmd, env=env, check=True)
126139
except subprocess.CalledProcessError as e:
127140
if e.returncode not in [0, 2]:
128-
print(e, file=sys.stderr)
141+
raise RuntimeError(
142+
f"OpenSCAP scan failed with return code {e.returncode}.\n")
129143

130144

131145
def main():
132146
args = parse_args()
133147
verify_bootc_build_env()
134148
install_sce_dependencies()
135-
pre_scan_fix(args)
136-
scan_and_remediate(args)
149+
try:
150+
pre_scan_fix(args)
151+
scan_and_remediate(args)
152+
except RuntimeError as e:
153+
print(e, file=sys.stderr)
154+
sys.exit(1)
137155

138156

139157
if __name__ == "__main__":

0 commit comments

Comments
 (0)