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

Improve unnecessarily over-complicated regex #11813

Merged
merged 2 commits into from
Apr 16, 2024

Conversation

marcusburghardt
Copy link
Member

Description:

By any reason the regex in this rule was unnecessarily super over-complicated.

This PR updates it from:
'\s*=\s*("(?:\\"|\\\\|[^"\\\n])*"\B|[^"](?:(?:\\,|\\"|\\ |\\\\|[^", \\\n])*)\b)'

to:
'\s*=\s*(?:"?([^",\s]+)"?)'

with the same effect and very likely better performance.

Rationale:

Review Hints:

Automatus tests should be enough, but it would also be nice to play with regex101.com

@marcusburghardt marcusburghardt added bugfix Fixes to reported bugs. enhancement General enhancements to the project. labels Apr 12, 2024
@marcusburghardt marcusburghardt added this to the 0.1.73 milestone Apr 12, 2024
Copy link

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

rhel8 (from CTF) Environment (using Fedora as testing environment)
Open in Gitpod

Fedora Testing Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

Copy link

This datastream diff is auto generated by the check Compare DS/Generate Diff

Click here to see the full diff
bash remediation for rule 'xccdf_org.ssgproject.content_rule_sudo_custom_logfile' differs.
--- xccdf_org.ssgproject.content_rule_sudo_custom_logfile
+++ xccdf_org.ssgproject.content_rule_sudo_custom_logfile
@@ -6,7 +6,7 @@
 
 if /usr/sbin/visudo -qcf /etc/sudoers; then
     cp /etc/sudoers /etc/sudoers.bak
-    if ! grep -P '^[\s]*Defaults[\s]*\blogfile\s*=\s*("(?:\\"|\\\\|[^"\\\n])*"\B|[^"](?:(?:\\,|\\"|\\ |\\\\|[^", \\\n])*)\b)\b.*$' /etc/sudoers; then
+    if ! grep -P '^[\s]*Defaults[\s]*\blogfile\s*=\s*(?:"?([^",\s]+)"?)\b.*$' /etc/sudoers; then
         # sudoers file doesn't define Option logfile
         echo "Defaults logfile=${var_sudo_logfile}" >> /etc/sudoers
     else

Copy link

github-actions bot commented Apr 12, 2024

🤖 A k8s content image for this PR is available at:
ghcr.io/complianceascode/k8scontent:11813
This image was built from commit: 1c28e47

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:11813

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

@marcusburghardt
Copy link
Member Author

Testing farms are failing now because we still need to fix some minor issues with #11796.

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 checkd the regex101.com and I have reviewed the regex. I have executed the test scenarios.

The PR looks good to me.

Please rebase this on the top of the latest upstream master branch which should bring in the sssd_enable_pam_service which should make the TF tests green.

By any reason the regex in this rule was unnecessarily super
overcomplicated. It was updated from:
'\s*=\s*("(?:\\"|\\\\|[^"\\\n])*"\B|[^"](?:(?:\\,|\\"|\\ |\\\\|[^", \\\n])*)\b)'
to:
'\s*=\s*(?:"?([^",\s]+)"?)'
with the same effect and very likely better performance.
@marcusburghardt
Copy link
Member Author

I have checkd the regex101.com and I have reviewed the regex. I have executed the test scenarios.

The PR looks good to me.

Please rebase this on the top of the latest upstream master branch which should bring in the sssd_enable_pam_service which should make the TF tests green.

Done

Copy link

codeclimate bot commented Apr 16, 2024

Code Climate has analyzed commit 1c28e47 and detected 0 issues on this pull request.

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.2% (0.0% change).

View more on Code Climate.

@vojtapolasek
Copy link
Collaborator

vojtapolasek commented Apr 16, 2024

It looks good. @jan-cerny can you approve and merge please?

@jan-cerny jan-cerny merged commit 18c11e4 into ComplianceAsCode:master Apr 16, 2024
45 checks passed
@marcusburghardt marcusburghardt deleted the quoted_logfile branch April 17, 2024 06:43
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Fixes to reported bugs. enhancement General enhancements to the project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants