firewall: T9205: fix ruff findings in conf_mode/op_mode firewall.py - #5398
firewall: T9205: fix ruff findings in conf_mode/op_mode firewall.py#5398ruben-herold wants to merge 3 commits into
Conversation
Bare except:, extraneous f-string prefixes on ConfigError()/text calls without placeholders, and 3 unused test locals in test_firewall.py. No behavior change. Split out of PR vyos#5374 (T9160) at sever-sever's request: that PR bundled this cleanup with the apply-path feature because it happened to touch the same files, but it isn't related to apply-path itself.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository YAML (base), Central YAML (inherited), Organization UI (inherited) Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🔗 Linked repositories identifiedCodeRabbit considers these linked repositories for cross-repo context during reviews:
🚧 Files skipped from review as they are similar to previous changes (1)
📜 Recent review details⏰ Context from checks skipped due to timeout. (2)
🧰 Additional context used🔍 Remote MCP vyos.devRelevant review context
📝 WalkthroughSummary by CodeRabbit
WalkthroughChangesFirewall maintenance
Mergeability Score: ⚪ Minimal · up to This PR makes localized firewall cleanup changes and narrows exception handling to documented failure modes; no actionable merge-blocking risk remains after normal checks and review. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR targets ruff-driven cleanup in VyOS firewall conf-mode and op-mode scripts, primarily adjusting exception handling and string formatting, plus minor smoketest refactoring.
Changes:
- Replace bare
except:blocks insrc/op_mode/firewall.pywith explicit exception handling. - Remove unnecessary
f-string prefixes / reformat someConfigErrorraises insrc/conf_mode/firewall.py. - Remove unused local variables in
smoketest/scripts/cli/test_firewall.py.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/op_mode/firewall.py | Reworks exception handling around nft queries and JSON parsing to satisfy linting. |
| src/conf_mode/firewall.py | Cleans up string formatting in ConfigError paths and removes an unnecessary f-string. |
| smoketest/scripts/cli/test_firewall.py | Removes unused locals in a few firewall smoketests. |
Suppressed comments (5)
src/op_mode/firewall.py:116
- The PR description says "No behavior change", but changing a bare
except:toexcept Exception:does change behavior (e.g.,KeyboardInterrupt/SystemExitwill no longer be swallowed and will now propagate). If this is intended (it usually is), please update the PR description to reflect that it’s a small behavior change in exception handling semantics.
command = ['nft', 'list', 'chain', suffix, 'vyos_filter', f'VYOS_STATE_{name_suffix}']
try:
results = cmdl(command)
except Exception:
return {}
src/op_mode/firewall.py:139
- Catching
Exceptionhere can hide JSON parsing bugs or other unexpected errors. Since the expected failure modes arecmdl()failing (raisesOSError) or invalid JSON (json.JSONDecodeError), catch those explicitly and let everything else raise.
try:
results_str = cmdl(['nft', '-j', 'list', 'set', prefix, table, name])
results = json.loads(results_str)
except Exception:
return out
src/op_mode/firewall.py:167
- Same as above:
except Exceptionis broader than needed and can mask unexpected errors. Consider catching the expectedOSError/json.JSONDecodeErrorfailure modes only.
try:
results_str = cmdl(['nft', '-j', 'list', 'set', prefix, table, name])
results = json.loads(results_str)
except Exception:
return out
smoketest/scripts/cli/test_firewall.py:417
- The PR description mentions restoring unused-but-harmless locals, but this change removes an unused
interfacelocal in this test. Please update the PR description to match the code (or re-add it if that was the intent).
def test_ipv4_mask(self):
name = 'smoketest-mask'
self.cli_set(['firewall', 'group', 'address-group', 'mask_group', 'address', '1.1.1.1'])
smoketest/scripts/cli/test_firewall.py:645
- The PR description mentions restoring unused-but-harmless locals, but this change removes an unused
interfacelocal in this test. Please update the PR description to match the code (or re-add it if that was the intent).
def test_ipv6_mask(self):
name = 'v6-smoketest-mask'
self.cli_set(['firewall', 'group', 'ipv6-address-group', 'mask_group', 'address', '::beef'])
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| try: | ||
| results = cmdl(command) | ||
| except: | ||
| except Exception: | ||
| return {} |
| def test_ipv4_advanced(self): | ||
| name = 'smoketest-adv' | ||
| name2 = 'smoketest-adv2' | ||
| interface = 'eth0' | ||
|
|
||
| self.cli_set(['firewall', 'ipv4', 'name', name, 'default-action', 'drop']) |
Address copilot-pull-request-reviewer feedback on PR vyos#5398: cmdl() only raises OSError and json.loads() only raises json.JSONDecodeError here, so catching bare Exception hid unrelated bugs. Narrowed all four call sites accordingly (three flagged, one identical pattern found by inspection).
|
Thanks for the review, addressed:
|
cmdl() decodes subprocess output as utf-8 and can raise UnicodeDecodeError (not an OSError subclass) if that fails, in addition to OSError on command failure. Catch both, closing a gap left by the exact-exception narrowing in the previous commit.
|
One more refinement while double-checking: |
|
CI integration 👍 passed! Details
|
Summary
except:, extraneous f-string prefixes onConfigError()/text calls without placeholders insrc/conf_mode/firewall.py, the sameexcept:pattern insrc/op_mode/firewall.py, and removes 4 unused-but-harmless locals insmoketest/scripts/cli/test_firewall.py(this is the ruff cleanup itself; firewall: T9160: add apply-path for deriving group membership from config #5374 separately re-added the same locals to undo the unrelated cleanup that had snuck into that branch).except Exception:insrc/op_mode/firewall.pyfurther to the specific exceptions each call site can actually raise (OSErrorfromcmdl(),json.JSONDecodeErrorfromjson.loads()), per review feedback.except:blocks silently swallowed everything, includingKeyboardInterrupt/SystemExit/unrelated bugs. They now only catch the documented failure modes of the calls inside thetry, so unexpected errors propagate instead of being hidden.Task: https://vyos.dev/T9205
Test plan
ruff check src/conf_mode/firewall.py src/op_mode/firewall.pypasses cleanpython3 -m py_compileon all three touched files