Fix iptables state aborting when the OS-default iptables conf file is absent#69763
Open
ggiesen wants to merge 1 commit into
Open
Fix iptables state aborting when the OS-default iptables conf file is absent#69763ggiesen wants to merge 1 commit into
ggiesen wants to merge 1 commit into
Conversation
When get_saved_rules is called without an explicit conf_file, _parse_conf
falls back to the OS-default path from _conf() and opened it
unconditionally. On a minion where that file does not exist (for example
/etc/sysconfig/iptables on RedHat), fopen raised IOError/FileNotFoundError,
which propagated up through get_saved_rules into state.iptables.insert and
state.iptables.append when save=True, aborting the state run.
Catch FileNotFoundError and return an empty dict instead. Returning {} is
safe for every internal caller: the state save-diff no-ops and correctly
triggers the save that writes the missing file, and get_saved_policy falls
through its existing KeyError guard to return None. The stale docstring that
claimed a False return is corrected to describe the empty-dict behaviour.
Fixes saltstack#54459
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
When
iptables.get_saved_rulesis called without an explicitconf_file,_parse_conffalls back to the OS-default path returned by_conf()(for example/etc/sysconfig/iptableson RedHat) and opened it unconditionally. On a minion where that file does not exist,salt.utils.files.fopenraisedIOError/FileNotFoundError, which propagated up throughget_saved_rulesintostate.iptables.insertandstate.iptables.appendwheneversave=Truewas set, aborting the state run.This wraps that read in
try/except FileNotFoundErrorand returns an empty dict instead. Returning{}is safe for every internal caller: the state save-diff sees empty saved rules, no-ops the comparison loop, and correctly triggers the save that writes the missing file (the user's desired outcome);get_saved_policyfalls through its existingKeyErrorguard and returnsNone. The stale docstring that claimed aFalsereturn is corrected to describe the empty-dict behaviour.What issues does this PR fix or reference?
Fixes #54459
Previous Behavior
A state such as
iptables.insertwithsave: Trueon a minion that has no/etc/sysconfig/iptablesfailed with:raised from
_parse_confand surfaced as a state exception.New Behavior
_parse_confreturns an empty dict when the OS-default conf file is absent. The state no longer aborts; the diff against the in-memory rules triggers the save, which creates the missing file. The happy path (file present), thein_mempath, and the explicit-conf_filepath are unchanged.Merge requirements satisfied?
[NOTICE] Bug fixes or features added to Salt require tests.
changelog/54459.fixed.md)tests/pytests/unit/modules/test_iptables.py:test_parse_conf_missing_file_54459- direct-altitude: calls_parse_conf(conf_file=None, family="ipv4")(the exact shapeget_saved_rulespasses from thesave=Truestate path) withfopenraisingFileNotFoundError; asserts{}.test_get_saved_rules_missing_file_54459- end-to-end reproducer through the real callerget_saved_rules(family="ipv4").test_parse_conf_present_file_no_regression_54459- inverse must-not-regress: conf file present viamock_open, parsed dict still yields thefilter/INPUTrules; passes with and without the fix.test_get_saved_policy_missing_file_54459- peripheral:get_saved_policyresolves toNone(itsKeyErrorguard) when the conf file is missing.Commits signed with GPG?
No