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

refactor: split cli methods to easy testing #3

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

fzipi
Copy link
Member

@fzipi fzipi commented Jan 21, 2025

what

  • refactor main to simpler methods

why

  • easy testing and readability

@fzipi fzipi requested a review from airween January 21, 2025 12:44
src/crs_linter/cli.py Outdated Show resolved Hide resolved
if len(flist) == 0:
errmsg("List of files is empty!")
sys.exit(1)
errmsg(" Can't open file for indent check: %s" % (f))

Choose a reason for hiding this comment

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

Suggested change
errmsg(" Can't open file for indent check: %s" % (f))
errmsg("Can't open file for indent check: %s" % (f))

Copy link

Choose a reason for hiding this comment

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

Just my 2 cents: the indentations (spaces before messages here and at other reviews) helped the readability of the output, eg:

rules/REQUEST-931-APPLICATION-ATTACK-RFI.conf
 Ignore case check ok.
 Action order check ok.
 Indentation check ok.
 no 'ctl:auditLogParts' action found.
 no duplicate id's
 paranoia-level tags are correct.
 PL anomaly_scores are correct.
 All TX variables are set.
 No new tags added.
 No t:lowercase and (?i) flag used.
 No rule without OWASP_CRS tag.
 No rule without correct ver action.
 No rule uses TX.N without capture action.
rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf
 Ignore case check ok.
 Action order check ok.
 ...

because the script processes files in order. If there are no indentations, I think it's a bit harder to read the output.

Copy link
Member Author

Choose a reason for hiding this comment

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

All those are more debug than informational. Meaning, they don't provide additional information.

The only ones that do are the failures, IMHO.

So I think these should be moved to logger.debug() instead, and if people wants to see that output just run it with --debug or similar.

for f in flist:
diff = difflib.unified_diff(from_lines, output)
if from_lines == output:
msg(" Indentation check ok.")

Choose a reason for hiding this comment

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

Suggested change
msg(" Indentation check ok.")
msg("Indentation check ok.")

if from_lines == output:
msg(" Indentation check ok.")
else:
errmsg(" Indentation check found error(s)")

Choose a reason for hiding this comment

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

Suggested change
errmsg(" Indentation check found error(s)")
errmsg("Indentation check found error(s)")

src/crs_linter/cli.py Outdated Show resolved Hide resolved

### check case usings
c.check_ignore_case()
if len(c.caseerror) == 0:
msg(" Ignore case check ok.")
logger.info(" Ignore case check ok.")

Choose a reason for hiding this comment

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

Suggested change
logger.info(" Ignore case check ok.")
logger.info("Ignore case check ok.")

if has_unused == False:
msg(" No unused TX variable")
if not has_unused:
logger.info(" No unused TX variable")

Choose a reason for hiding this comment

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

Suggested change
logger.info(" No unused TX variable")
logger.info("No unused TX variable")

src/crs_linter/linter.py Outdated Show resolved Hide resolved
fzipi and others added 8 commits January 22, 2025 09:40
Signed-off-by: Felipe Zipitria <[email protected]>
Signed-off-by: Felipe Zipitria <[email protected]>
Signed-off-by: Felipe Zipitria <[email protected]>
Signed-off-by: Felipe Zipitria <[email protected]>
Signed-off-by: Felipe Zipitria <[email protected]>
Signed-off-by: Felipe Zipitria <[email protected]>
Signed-off-by: Felipe Zipitria <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants