-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2169 from jan-cerny/oscap_bootc_tool
Introduce oscap-bootc
- Loading branch information
Showing
6 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Red Hat Inc., Durham, North Carolina. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
|
||
import argparse | ||
import subprocess | ||
import sys | ||
import tempfile | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description="Use in your Containerfile to build hardened bootable " | ||
"container images. Performs OpenSCAP scan and remediation of the " | ||
"image.") | ||
parser.add_argument( | ||
"--profile", | ||
help="ID of the profile to be evaluated") | ||
parser.add_argument( | ||
"--tailoring-file", | ||
help="Use given XCCDF Tailoring file") | ||
parser.add_argument( | ||
"--tailoring-id", metavar="COMPONENT_ID", | ||
help="Use given DS component as XCCDF Tailoring file") | ||
parser.add_argument( | ||
"--results-arf", | ||
help="Write ARF (result data stream) into file") | ||
parser.add_argument( | ||
"--report", | ||
help="Write HTML report into file") | ||
parser.add_argument( | ||
"data_stream", metavar="DATA_STREAM", | ||
help="Path to a SCAP source data stream, eg. " | ||
"/usr/share/xml/scap/ssg/content/ssg-rhel10-ds.xml") | ||
# Unfortunately, we can't add "--rule", "--skip-rule", or "--reference" | ||
# because the "oscap xccdf generate fix" submodule doesn't support these | ||
# options. | ||
return parser.parse_args() | ||
|
||
|
||
def ensure_sce_installed(): | ||
query_cmd = ["rpm", "-q", "openscap-engine-sce"] | ||
query_process = subprocess.run(query_cmd, capture_output=True) | ||
if query_process.returncode != 0: | ||
raise RuntimeError( | ||
"The script requires to have the openscap-engine-sce package " | ||
"installed.") | ||
|
||
|
||
def add_args(option_args_list, cmd): | ||
for o, a in option_args_list: | ||
if a: | ||
cmd.append(o) | ||
cmd.append(a) | ||
|
||
|
||
def add_common_args(args, cmd): | ||
oal = [ | ||
("--profile", args.profile), | ||
("--tailoring-file", args.tailoring_file), | ||
("--tailoring-id", args.tailoring_id) | ||
] | ||
add_args(oal, cmd) | ||
|
||
|
||
def add_eval_args(args, cmd): | ||
oal = [ | ||
("--results-arf", args.results_arf), | ||
("--report", args.report), | ||
] | ||
add_args(oal, cmd) | ||
|
||
|
||
def pre_scan_fix(args): | ||
with tempfile.NamedTemporaryFile(delete=False) 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) | ||
|
||
|
||
def scan_and_remediate(args): | ||
oscap_cmd = ["oscap", "xccdf", "eval", "--progress", "--remediate"] | ||
add_common_args(args, oscap_cmd) | ||
add_eval_args(args, oscap_cmd) | ||
oscap_cmd.append(args.data_stream) | ||
env = {"OSCAP_PREFERRED_ENGINE": "SCE", "OSCAP_BOOTC_BUILD": "YES"} | ||
try: | ||
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) | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
ensure_sce_installed() | ||
pre_scan_fix(args) | ||
scan_and_remediate(args) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
.TH oscap-bootc "8" "November 2024" "Red Hat, Inc." "System Administration Utilities" | ||
|
||
.SH NAME | ||
oscap-bootc \- Tool for building hardened bootable container images | ||
|
||
.SH DESCRIPTION | ||
The oscap-bootc tool is a convenience script that makes building hardened bootable container images easier. | ||
This tool is designed to be used during the build of the bootable container image. | ||
Include oscap-bootc in your Containerfile that will be used to build your bootable container image. | ||
The oscap-bootc runs oscap tool on a given container image. | ||
|
||
The oscap-bootc tool can't be used anywhere else than in a Containerfile. | ||
|
||
.SH USAGE | ||
|
||
oscap-bootc [OPTION...] DATASTREAM_FILE | ||
|
||
Usage of the tool mimics usage and options of oscap(8) tool. | ||
|
||
.SH OPTIONS | ||
.TP | ||
\fB\-\-profile PROFILE_ID\fR | ||
.RS | ||
ID of the profile to be evaluated. | ||
.RE | ||
.TP | ||
\fB\-\-tailoring-file TAILORING_FILE\fR | ||
.RS | ||
Use given file for XCCDF tailoring. Select profile from tailoring file to apply using --profile. If both --tailoring-file and --tailoring-id are specified, --tailoring-file takes priority. | ||
.RE | ||
.TP | ||
\fB\-\-tailoring-id COMPONENT_REF_ID\fR | ||
.RS | ||
Use tailoring component in input source data stream for XCCDF tailoring. The tailoring component must be specified by its Ref-ID (value of component-ref/@id attribute in input source data stream). Select profile from tailoring component to apply using --profile. If both --tailoring-file and --tailoring-id are specified, --tailoring-file takes priority. | ||
.RE | ||
.TP | ||
\fB\-\-results-arf FILE\fR | ||
.RS | ||
Writes results to a given FILE in Asset Reporting Format. | ||
.RE | ||
.TP | ||
\fB\-\-report FILE\fR | ||
.RS | ||
Write HTML report into FILE. | ||
.RE | ||
|
||
.SH REPORTING BUGS | ||
.nf | ||
Please report bugs using https://github.com/OpenSCAP/openscap/issues | ||
|
||
.SH AUTHORS | ||
.nf | ||
Jan Černý <jcerny@redhat.com> | ||
.fi |