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

Fix incorrect file permissions in file.line #30212

Merged
merged 1 commit into from
Jan 19, 2016

Conversation

abednarik
Copy link
Contributor

Looks like atomic_open() doesn't take care of preserving file permissions. It will create
a new file, edit it and then move this temp file to the original file ignoring file permissions.
Also updated the check to be sure file exist and is a file. Otherwise code will try to update
directory content and will fail badly and throw a python error.

Fixes #30150.

Hope this one don't break anything :(

@bailsman
Copy link
Contributor

bailsman commented Jan 8, 2016

When atomic_open is called from the file module, the file always exists. However, when atomic_open() is called from returners, the file does not exist yet. Should atomic_open() make the assumption that the file exists? If yes, you should fix the calling code in salt/returners/local_cache.py and perhaps other places.

If atomic_open should NOT assume the file exists, I think it's a bit nicer to check if the file exists rather than simply swallowing exceptions. For example:

        if os.path.isfile(self._filename):
            shutil.copymode(self._filename, self._tmp_filename)

After all, you still want to know if something unexpected happens, and not silently continue.

@abednarik
Copy link
Contributor Author

Agree. Thanks @codeHotter

We are not checking tmp file, which is also a good idea I think. What do you think? Otherwise it will fail badly.

@bailsman
Copy link
Contributor

bailsman commented Jan 8, 2016

The temp file should be guaranteed to exist here. It's not necessary to check for it.

@abednarik
Copy link
Contributor Author

Thanks again @codeHotter. I just updated this PR with your suggestion.

@@ -1468,9 +1468,9 @@ def line(path, content, match=None, mode=None, location=None,
salt '*' file.line /etc/nsswitch.conf "networks:\tfiles dns", after="hosts:.*?", mode='ensure'
'''
path = os.path.realpath(os.path.expanduser(path))
if not os.path.exists(path):
if not os.path.isfle(path):
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be isfile

@abednarik
Copy link
Contributor Author

Is the way i send in the PR

@bailsman
Copy link
Contributor

bailsman commented Jan 8, 2016

In your previous version it was fine, but now it says "isfle". Look in your github repository on the fix_file_line_permissions branch:
https://github.com/abednarik/salt/blob/fix_file_line_permissions/salt/modules/file.py#L1471

If it's correct locally, maybe you can try to push the branch again?

Looks like atomic_open() doesn't take care of preserving file permissions.  It will create
a new file, edit it and then move this temp file to the original file ignoring file permissions.
Also updated the check to be sure file exist and is a file. Otherwise code will try to update
directory content and will fail badly and throw a python error.

Fixes saltstack#30150.
@abednarik
Copy link
Contributor Author

You are right. Now should be fixed.

Thanks.

@Grokzen
Copy link

Grokzen commented Jan 12, 2016

@abednarik Would this also fix the following issue? #28320 This issue behaves in the same way as mentioned issue does.

@abednarik
Copy link
Contributor Author

Hi @Grokzen don't' think so, since comment_line is using other function _mkstemp_copy. Anyway, should be fair easy to apply the same I did here.
Will send a PR today, if I can.

cachedout pushed a commit that referenced this pull request Jan 19, 2016
Fix incorrect file permissions in file.line
@cachedout cachedout merged commit 0af5e16 into saltstack:2015.8 Jan 19, 2016
@abednarik abednarik deleted the fix_file_line_permissions branch January 19, 2016 21:18
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.

4 participants