Fix file.managed omitting mode from changes when creating a file - #70084
Fix file.managed omitting mode from changes when creating a file#70084HarnageaGabriel wants to merge 1 commit into
Conversation
|
Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here's some information that may help as you continue your Salt journey. There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar. |
check_file_meta() had two early-return points that fired when a
target file does not yet exist (lstats falsy). Both skipped the
mode/user/group comparison block entirely, so file.managed only
ever reported {'newfile': name} or a diff on creation — never the
mode (or user/group) being applied. Existing-file mode changes were
already reported correctly.
Report the requested mode/user/group directly on the new-file paths
(guarded the same way as the existing-file case, skipped on
Windows), since there is no prior state to diff against.
Fixes saltstack#69969
cf40ea9 to
b860413
Compare
twangboy
left a comment
There was a problem hiding this comment.
Let's make these changes on the 3008.x branch, please.
| if group is not None: | ||
| changes["group"] = group | ||
| if mode is not None: | ||
| changes["mode"] = mode |
There was a problem hiding this comment.
1. Unhandled Defaults / Inherited Permissions
Relying strictly on if variable is not None: only captures parameters explicitly passed to check_file_meta. If a user relies on Salt defaults or inherited OS permissions (leaving user, group, or mode as None), these details won't be recorded in changes during file creation. Consider resolving default values or falling back to stats data when these variables are None.
2. Code Duplication
This exact non-Windows metadata check is duplicated verbatim at lines 6322–6328. Refactoring this into a local helper function will keep the logic DRY and consistent across both branches.
Suggested Refactor
def _add_file_meta_changes(changes_dict, stats_data=None):
if salt.utils.platform.is_windows():
return
# Fall back to stats/defaults if parameters were omitted
target_user = user or (stats_data.get("user") if stats_data else None)
target_group = group or (stats_data.get("group") if stats_data else None)
target_mode = mode or (stats_data.get("mode") if stats_data else None)
for key, val in [("user", target_user), ("group", target_group), ("mode", target_mode)]:
if val is not None:
changes_dict[key] = valYou can then simplify both conditional blocks in check_file_meta:
# Line 6216
if not lstats and not new_file_diff:
changes["newfile"] = name
_add_file_meta_changes(changes, lstats)# Line 6321
if not lstats:
_add_file_meta_changes(changes, lstats)You'll also need to add some tests for when the default values are used to make sure the user/group/mode values are reported as changes.
Summary
check_file_meta()insalt/modules/file.pyhas two early-return points that fire when the target file does not yet exist (lstatsfalsy). Both skipped the entire mode/user/group comparison block, sofile.managedonly ever reported{'newfile': name}(test=True) or a diff (real run) when creating a file — never the mode being applied. When the file already existed, mode/user/group changes were reported correctly.mode: '0600'vs an unspecified umask-derived mode produce byte-identicalchanges).mode/user/groupdirectly (there's nothing to diff against), guarded the same way as the existing-file case (skipped on Windows). Both early-return points are updated so behavior is consistent regardless ofnew_file_diff.Fixes #69969
Test plan
tests/pytests/unit/modules/file/test_file_check.pycovering a nonexistent target path withmode/user/groupspecified, for bothnew_file_diff=True/False, and for theignore_ordering/tuple-return path.changelog/69969.fixed.md.Note: ran
pytest tests/pytests/unit/modules/file/test_file_check.pyon Windows; the OS-gated tests (skip_on_windows) skip there since mode/user/group aren't applicable on Windows — the fix's logic was additionally verified directly againstcheck_file_meta()withsalt.utils.platform.is_windows()patched toFalse, confirming the new/existing-file paths behave as expected.🤖 Generated with Claude Code