From 89cb898ac3d3164ff0891c4e715ada49ede23b98 Mon Sep 17 00:00:00 2001 From: Hans Krijger Date: Mon, 6 Jun 2016 11:48:21 -0700 Subject: [PATCH] check for existence before chmod/chown/chcon (#206) code review feedback --- azurelinuxagent/common/osutil/default.py | 5 ++++- azurelinuxagent/common/utils/fileutil.py | 12 +++++++++--- bin/waagent2.0 | 8 +++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py index 0efd8ee..577e0e5 100644 --- a/azurelinuxagent/common/osutil/default.py +++ b/azurelinuxagent/common/osutil/default.py @@ -194,7 +194,7 @@ class DefaultOSUtil(object): """ path, thumbprint, value = pubkey if path is None: - raise OSUtilError("Publich key path is None") + raise OSUtilError("Public key path is None") crytputil = CryptUtil(conf.get_openssl_cmd()) @@ -261,6 +261,9 @@ class DefaultOSUtil(object): Returns exit result. """ if self.is_selinux_system(): + if not os.path.exists(path): + logger.error("Path does not exist: {0}".format(path)) + return 1 return shellutil.run('chcon ' + con + ' ' + path) def conf_sshd(self, disable_password): diff --git a/azurelinuxagent/common/utils/fileutil.py b/azurelinuxagent/common/utils/fileutil.py index 24842d0..c476129 100644 --- a/azurelinuxagent/common/utils/fileutil.py +++ b/azurelinuxagent/common/utils/fileutil.py @@ -91,11 +91,17 @@ def mkdir(dirpath, mode=None, owner=None): chowner(dirpath, owner) def chowner(path, owner): - owner_info = pwd.getpwnam(owner) - os.chown(path, owner_info[2], owner_info[3]) + if not os.path.exists(path): + logger.error("Path does not exist: {0}".format(path)) + else: + owner_info = pwd.getpwnam(owner) + os.chown(path, owner_info[2], owner_info[3]) def chmod(path, mode): - os.chmod(path, mode) + if not os.path.exists(path): + logger.error("Path does not exist: {0}".format(path)) + else: + os.chmod(path, mode) def rm_files(*args): for path in args: diff --git a/bin/waagent2.0 b/bin/waagent2.0 index f3c9d71..6178d9f 100644 --- a/bin/waagent2.0 +++ b/bin/waagent2.0 @@ -228,6 +228,9 @@ class AbstractDistro(object): Returns exit result. """ if self.isSelinuxSystem(): + if not os.path.exists(path): + Error("Path does not exist: {0}".format(path)) + return 1 return Run('chcon ' + cn + ' ' + path) def setHostname(self,name): @@ -2336,7 +2339,10 @@ def ChangeOwner(filepath, user): except: pass if p != None: - os.chown(filepath, p[2], p[3]) + if not os.path.exists(filepath): + Error("Path does not exist: {0}".format(filepath)) + else: + os.chown(filepath, p[2], p[3]) def CreateDir(dirpath, user, mode): """