Skip to content

Commit

Permalink
Add option to Automatus to remove platforms from benchmark/profiles.
Browse files Browse the repository at this point in the history
This new option --remove-platforms should be used instead of
add-platform option because it makes the content applicable in any
target. Previously the CPE injection relied on OpenSCAP's CPE
dictionaries that were not being updated anymore.
  • Loading branch information
ggbecker committed Nov 24, 2022
1 parent 19d9bd9 commit 68dd0df
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .gitpod.launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
"--remove-ocp4-only",
"--add-product-to-fips-certified",
"fedora",
"--add-platform",
"&&CPE&&",
"--remove-platforms",
"${command:content-navigator.getRuleId}"
],
"env": {
Expand Down
4 changes: 1 addition & 3 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ tasks:
git+https://github.com/ggbecker/sphinxcontrib.jinjadomain.git#egg=sphinxcontrib-jinjadomain
[ -z "$PRODUCT" ] && PRODUCT="fedora"
[ -z "$CONTAINER" ] && CONTAINER=$PRODUCT
[ -z "$CONTAINER_VERSION" ] && CONTAINER_VERSION="$CONTAINER:34"
[ -z "$CPE" ] && CPE="cpe:/o:fedoraproject:fedora:34"
[ -z "$CONTAINER_VERSION" ] && CONTAINER_VERSION="$CONTAINER"
mkdir -p .vscode && cp .gitpod.launch.json .vscode/launch.json
CONTAINER_NAME=${CONTAINER}_container
sed -i "s/&&CONTAINER_NAME&&/$CONTAINER_NAME/g" .vscode/launch.json
sed -i "s/&&DEFAULT_PRODUCT&&/$PRODUCT/g" .vscode/launch.json
sed -i "s,&&CPE&&,$CPE,g" .vscode/launch.json
PRIVATE_KEY_FOLDER=.ssh
PRIVATE_KEY_FILEPATH=$PRIVATE_KEY_FOLDER/id_rsa
sed -i "s,&&PRIVATE_KEY_FILEPATH&&,$PRIVATE_KEY_FILEPATH,g" .vscode/launch.json
Expand Down
3 changes: 1 addition & 2 deletions docs/workshop/lab3_profiles.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

// Variables
:container_name: fedora_container
:cpe_identifier: cpe:/o:fedoraproject:fedora:34

== Introduction

Expand Down Expand Up @@ -647,7 +646,7 @@ You also add arguments to turn on full reporting, which generates XML and HTML r
--datastream build/ssg-rhel8-ds.xml \
--remediate-using bash \
--remove-machine-only \
--add-platform "{cpe_identifier}" \
--remove-platforms \
--logdir profile-log \
travel
----
Expand Down
17 changes: 8 additions & 9 deletions docs/workshop/lab5_oval.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

// Variables
:container_name: fedora_container
:cpe_identifier: cpe:/o:fedoraproject:fedora:34


== Introduction
Expand Down Expand Up @@ -277,7 +276,7 @@ With that in mind, execute the test suite:
--datastream build/ssg-rhel8-ds.xml \
--remediate-using bash \
--remove-machine-only \
--add-platform "{cpe_identifier}" \
--remove-platforms \
accounts_tmout
----
+
Expand Down Expand Up @@ -307,7 +306,7 @@ The alternative mode is `profile` mode, which is simpler--there are no test scen
* Finally, you specify what to test--a rule regular expression: `accounts_tmout` or `^accounts_tmout$`.
* Other parameters are also used for supporting running tests in this kind of environment. For example, you use `--add-platform "{cpe_identifier}"` to make the RHEL8 content applicable to Fedora images.
* Other parameters are also used for supporting running tests in this kind of environment. For example, you use `--remove-platforms` to make the RHEL8 content applicable to Fedora images.
====

The output tells you the following:
Expand Down Expand Up @@ -433,7 +432,7 @@ TIP: You can use the `Up` arrow key to browse the command history so you do not
--datastream build/ssg-rhel8-ds.xml \
--remediate-using bash \
--remove-machine-only \
--add-platform "{cpe_identifier}" \
--remove-platforms \
accounts_tmout
----
+
Expand Down Expand Up @@ -573,7 +572,7 @@ This way, you do not have to worry about possibly introducing those link:https:/
--docker {container_name} \
--remediate-using bash \
--remove-machine-only \
--add-platform "{cpe_identifier}" \
--remove-platforms \
accounts_tmout
----
+
Expand Down Expand Up @@ -656,7 +655,7 @@ fi
--docker {container_name} \
--remediate-using bash \
--remove-machine-only \
--add-platform "{cpe_identifier}" \
--remove-platforms \
accounts_tmout
----
+
Expand Down Expand Up @@ -705,7 +704,7 @@ This time, when rebuilt and executed again, the tests pass:
--docker {container_name} \
--remediate-using bash \
--remove-machine-only \
--add-platform "{cpe_identifier}" \
--remove-platforms \
accounts_tmout
----
+
Expand Down Expand Up @@ -757,7 +756,7 @@ Execute the test suite again and expect the `ERROR - Script correct_value.pass.s
--docker {container_name} \
--remediate-using bash \
--remove-machine-only \
--add-platform "{cpe_identifier}" \
--remove-platforms \
accounts_tmout
----
+
Expand Down Expand Up @@ -804,7 +803,7 @@ The non-capturing group that consists of `export` followed by at least one white
--docker {container_name} \
--remediate-using bash \
--remove-machine-only \
--add-platform "{cpe_identifier}" \
--remove-platforms \
accounts_tmout
----
+
Expand Down
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ Using Podman:
```

or just call the `test_rule_in_container.sh` script that passes the backend options for you
in addition to `--remove-machine-only` and `--add-platform`
in addition to `--remove-machine-only` and `--remove-platforms`
that remove some testing limitations of the container backend.

Using Docker:
Expand Down
13 changes: 12 additions & 1 deletion tests/automatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def parse_args():
"--add-platform",
metavar="<CPE REGEX>",
default=None,
help="Find all CPEs that are present in local OpenSCAP's CPE dictionary "
help="DEPRECATED: Use --remove-platforms instead; "
"Find all CPEs that are present in local OpenSCAP's CPE dictionary "
"that match the provided regex, "
"and add them as platforms to all datastream benchmarks. "
"If the regex doesn't match anything, it will be treated "
Expand All @@ -81,6 +82,14 @@ def parse_args():
default=None,
help="Add installed_OS_is_$product extend_definition to the "
"installed_OS_is_FIPS_certified OVAL criteria definition.")
common_parser.add_argument(
"--remove-platforms",
default=False,
action="store_true",
help="Remove any platforms from the Benchmark XML elements and Profile "
"XML elements, essentially making the content applicable to any platform. "
"Although more low level platforms such as packages or container/machine "
"CPE are still applicable.")
common_parser.add_argument(
"--remove-machine-only",
default=False,
Expand Down Expand Up @@ -484,6 +493,8 @@ def main():
options.datastream = stashed_datastream

with xml_operations.datastream_root(stashed_datastream, stashed_datastream) as root:
if options.remove_platforms:
xml_operations.remove_platforms(root)
if options.remove_machine_only:
xml_operations.remove_machine_platform(root)
xml_operations.remove_machine_remediation_condition(root)
Expand Down
19 changes: 15 additions & 4 deletions tests/ssg_test_suite/xml_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,19 @@ def datastream_root(ds_location, save_location=None):
tree.write(save_location)


def remove_platforms_from_element(root, element_spec, platforms):
query = BENCHMARK_QUERY + "//{0}".format(element_spec)
elements = root.findall(query, PREFIX_TO_NS)
def find_elements(root, element_spec=None):
query = BENCHMARK_QUERY
if element_spec is not None:
query = query + "//{0}".format(element_spec)
return root.findall(query, PREFIX_TO_NS)


def remove_platforms_from_element(root, element_spec=None, platforms=None):
elements = find_elements(root, element_spec)
for el in elements:
platforms_xml = el.findall("./xccdf-1.2:platform", PREFIX_TO_NS)
for p in platforms_xml:
if instance_in_platforms(p, platforms):
if platforms is None or instance_in_platforms(p, platforms):
el.remove(p)


Expand All @@ -103,6 +109,11 @@ def remove_machine_platform(root):
remove_platforms_from_element(root, "xccdf-1.2:Group", "#machine")


def remove_platforms(root):
remove_platforms_from_element(root)
remove_platforms_from_element(root, "xccdf-1.2:Profile")


def remove_ocp4_platforms(root):
remove_platforms_from_element(
root, "xccdf-1.2:Rule",
Expand Down
4 changes: 1 addition & 3 deletions tests/test_rule_in_container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ if test "$_arg_docker" = on; then
fi

$CONTAINER_BACKEND images | grep -q "$_arg_name" || die "Couldn't find the $CONTAINER_BACKEND image '$_arg_name'"
test_image_cpe_product=$($CONTAINER_BACKEND run --rm "$_arg_name" sh -c '. /etc/os-release && echo "$CPE_NAME"')
test -n "$test_image_cpe_product" || die "Unable to deduce the product CPE from the container's /etc/os-release file."

additional_args=()
test "$_arg_dontclean" = on && additional_args+=(--dontclean)
Expand All @@ -248,7 +246,7 @@ if test -n "$ADDITIONAL_SSGTS_OPTIONS"; then
ADDITIONAL_TEST_OPTIONS="$ADDITIONAL_SSGTS_OPTIONS"
fi

command=(python3 "${script_dir}/automatus.py" rule ${ADDITIONAL_TEST_OPTIONS} --remove-machine-only --remove-ocp4-only "${additional_args[@]}" --add-platform "$test_image_cpe_product" "$CONTAINER_OPT" "$_arg_name" -- "${_arg_rule[@]}")
command=(python3 "${script_dir}/automatus.py" rule ${ADDITIONAL_TEST_OPTIONS} --remove-platforms --remove-machine-only --remove-ocp4-only "${additional_args[@]}" "$CONTAINER_OPT" "$_arg_name" -- "${_arg_rule[@]}")
if test "$_arg_dry_run" = on; then
printf '%s\n' "${command[*]}"
else
Expand Down

0 comments on commit 68dd0df

Please sign in to comment.