Skip to content
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

Add a script for finding unused rules #12110

Merged
merged 4 commits into from
Jul 10, 2024

Conversation

Mab879
Copy link
Member

@Mab879 Mab879 commented Jul 1, 2024

Description:

Add a script for finding unused rules

Rationale:

To help find outdated rules when removing a product.

Review Hints:

Remove or adjusting profiles is out of scope of this PR. This needs to be addressed on a case-by-case basis.

@Mab879 Mab879 added the Infrastructure Our content build system label Jul 1, 2024
@Mab879 Mab879 added this to the 0.1.74 milestone Jul 1, 2024
Copy link

github-actions bot commented Jul 1, 2024

Start a new ephemeral environment with changes proposed in this pull request:

Fedora Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

Copy link

github-actions bot commented Jul 1, 2024

🤖 A k8s content image for this PR is available at:
ghcr.io/complianceascode/k8scontent:12110
This image was built from commit: 329047c

Click here to see how to deploy it

If you alread have Compliance Operator deployed:
utils/build_ds_container.py -i ghcr.io/complianceascode/k8scontent:12110

Otherwise deploy the content and operator together by checking out ComplianceAsCode/compliance-operator and:
CONTENT_IMAGE=ghcr.io/complianceascode/k8scontent:12110 make deploy-local

@Mab879 Mab879 force-pushed the add_script_for_unused_rules branch from 19ec07d to 5d14c90 Compare July 1, 2024 17:41
@Mab879 Mab879 mentioned this pull request Jul 1, 2024
@jan-cerny jan-cerny self-assigned this Jul 2, 2024
Copy link
Collaborator

@jan-cerny jan-cerny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script prints for example rule ensure_amazon_gpgkey_installed which isn't used in a data stream but it is linked as a related rule in a control in the cis_al2023 control file. This means we need to be careful when removing rules, we can't remove all of the rules produced by this script.

docs/manual/developer/05_tools_and_utilities.md Outdated Show resolved Hide resolved
docs/manual/developer/05_tools_and_utilities.md Outdated Show resolved Hide resolved
utils/find_unused_rules.py Outdated Show resolved Hide resolved
docs/manual/developer/05_tools_and_utilities.md Outdated Show resolved Hide resolved
Mab879 added a commit to Mab879/content that referenced this pull request Jul 8, 2024
Mab879 added a commit to Mab879/content that referenced this pull request Jul 8, 2024
@Mab879 Mab879 force-pushed the add_script_for_unused_rules branch from 2fbb3fd to d8274c9 Compare July 8, 2024 14:11
@Mab879
Copy link
Member Author

Mab879 commented Jul 8, 2024

/packit retest-failed

@Mab879 Mab879 requested a review from jan-cerny July 8, 2024 21:20
Copy link
Collaborator

@jan-cerny jan-cerny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have run the script and it works great.

But, you still have some typos left.

Example useage:

```bash
$ ./build_products --derivatives
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better than build_project, but the correct name is build_product.

@@ -743,3 +743,24 @@ An example of how to execute the script to generate roles locally:
$ ./build_product rhel9
$ ./utils/ansible_playbook_to_role.py --dry-run output
```

### `utils/find_unused_rules.py` – List Rules That Are Not Used In Any Datastream
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data Stream

### `utils/find_unused_rules.py` – List Rules That Are Not Used In Any Datastream

This script will output rules are not in any data streams.
To prevent false positives the script will not run if the number of build datas treams less than the total number of products in the project.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

built data streams

It requires that all products (and derivatives) are built.
To do this run ./build_product --derivatives
The script has the following return codes:
0 - All rules are used in the datastreams,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data streams

To do this run ./build_product --derivatives
The script has the following return codes:
0 - All rules are used in the datastreams,
1 - Some rules are not used in the datastreams,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data streams

The script works by adding off the rule ids from the data streams to a `set`.
Then the script converts the keys of `rule_dirs.json` to a set.
The set of rules in the data stream is subtracted to from the set of rules in `rule_dirs.json`.
The differnce is then output to the user.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

difference

The set of rules in the data stream is subtracted to from the set of rules in `rule_dirs.json`.
The differnce is then output to the user.

Example useage:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usage

BUILD_DIR = SSG_ROOT.joinpath("build")
RULE_DIR_JSON = BUILD_DIR.joinpath("rule_dirs.json")
EPILOG = """
This script lists rules that are not used in any datastreams.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data streams

ds_products.add(ds.name.split("-")[1])
if products_count > len(datastream_files):
print("Not all products are built, cowardly refusing to continue.", file=sys.stderr)
print(f"Products: {products_count}, Datastreams: {len(datastream_files)}", file=sys.stderr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data streams:


This script will output rules are not in any data streams.
To prevent false positives the script will not run if the number of build datas treams less than the total number of products in the project.
The script assumes that `./build_project --derivatives` will be ran before the script is used.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run

@Mab879 Mab879 force-pushed the add_script_for_unused_rules branch from d8274c9 to 329047c Compare July 9, 2024 14:12
Copy link

codeclimate bot commented Jul 9, 2024

Code Climate has analyzed commit 329047c and detected 1 issue on this pull request.

Here's the issue category breakdown:

Category Count
Style 1

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 59.4% (0.0% change).

View more on Code Climate.

Copy link
Collaborator

@jan-cerny jan-cerny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have followed the steps in the Example usage doc text. I have reviewed the generated output. This is a great enhancement.

@jan-cerny
Copy link
Collaborator

The CI fail on OL 7 isn't caused by the contents of this PR. I am fine with the line exceeding its length in this case.

@jan-cerny jan-cerny merged commit d690091 into ComplianceAsCode:master Jul 10, 2024
92 of 94 checks passed
@Mab879 Mab879 deleted the add_script_for_unused_rules branch July 10, 2024 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Infrastructure Our content build system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants