charm-helpers
charm-helpers copied to clipboard
templating.render doesn't work in functional tests not run as root
When using charmhelpers.core.templating.render() to create some templated files in a functional test, I ran across an issue where the default owner/group is root/root when creating the target files.
It appears that os.fchown is called from charmhelpers.core.host.write_file(), and just assumes that the user has permissions to chown files to another user/group without any exception handling.
It would be beneficial for adaptability of the templating code (and other functions that rely on write_file) to have write_file check the euid == root and if not, skip the os.fchown() call, or call it only with current euid as owner, as you cannot change file user-ownership as non-root.
The workaround for this is to call templating.render() with owner=pwd.getpwuid(os.geteuid()).pw_name, group=grp.getgrgid(os.getegid()).gr_name.
def write_file(path, content, owner='root', group='root', perms=0o444):
snip with open(path, 'wb') as target:
os.fchown(target.fileno(), uid, gid)
E PermissionError: [Errno 1] Operation not permitted
.tox/functional/lib/python3.6/site-packages/charmhelpers/core/host.py:557: PermissionError