diff --git a/azurelinuxagent/common/utils/fileutil.py b/azurelinuxagent/common/utils/fileutil.py index 03090a427d..94eb5cf1bf 100644 --- a/azurelinuxagent/common/utils/fileutil.py +++ b/azurelinuxagent/common/utils/fileutil.py @@ -99,13 +99,15 @@ def get_line_startingwith(prefix, filepath): return None -def mkdir(dirpath, mode=None, owner=None): +def mkdir(dirpath, mode=None, owner=None, reset_mode_and_owner=True): if not os.path.isdir(dirpath): os.makedirs(dirpath) - if mode is not None: - chmod(dirpath, mode) - if owner is not None: - chowner(dirpath, owner) + reset_mode_and_owner = True # force setting the mode and owner + if reset_mode_and_owner: + if mode is not None: + chmod(dirpath, mode) + if owner is not None: + chowner(dirpath, owner) def chowner(path, owner): diff --git a/azurelinuxagent/ga/exthandlers.py b/azurelinuxagent/ga/exthandlers.py index a09a81e96c..fcb14d22b9 100644 --- a/azurelinuxagent/ga/exthandlers.py +++ b/azurelinuxagent/ga/exthandlers.py @@ -1051,7 +1051,7 @@ def get_extension_full_name(self, extension=None): def __set_command_execution_log(self, extension, execution_log_max_size): try: - fileutil.mkdir(self.get_log_dir(), mode=0o755) + fileutil.mkdir(self.get_log_dir(), mode=0o755, reset_mode_and_owner=False) except IOError as e: self.logger.error(u"Failed to create extension log dir: {0}", e) else: diff --git a/tests/ga/test_exthandlers.py b/tests/ga/test_exthandlers.py index 2f03396599..f56ebce14b 100644 --- a/tests/ga/test_exthandlers.py +++ b/tests/ga/test_exthandlers.py @@ -287,6 +287,28 @@ def test_command_extension_log_truncates_correctly(self, mock_log_dir): with open(log_file_path) as truncated_log_file: self.assertEqual(truncated_log_file.read(), "{second_line}\n".format(second_line=second_line)) + def test_set_logger_should_not_reset_the_mode_of_the_log_directory(self): + ext_log_dir = os.path.join(self.tmp_dir, "log_directory") + + with patch("azurelinuxagent.common.conf.get_ext_log_dir", return_value=ext_log_dir): + ext_handler = Extension(name='foo') + ext_handler.version = "1.2.3" + ext_handler_instance = ExtHandlerInstance(ext_handler=ext_handler, protocol=None) + ext_handler_log_dir = os.path.join(ext_log_dir, ext_handler.name) + + # Double-check the initial mode + get_mode = lambda f: os.stat(f).st_mode & 0o777 + mode = get_mode(ext_handler_log_dir) + if mode != 0o755: + raise Exception("The initial mode of the log directory should be 0o755, got 0{0:o}".format(mode)) + + new_mode = 0o700 + os.chmod(ext_handler_log_dir, new_mode) + ext_handler_instance.set_logger() + + mode = get_mode(ext_handler_log_dir) + self.assertEqual(new_mode, mode, "The mode of the log directory should not have changed") + def test_it_should_report_the_message_in_the_hearbeat(self): def heartbeat_with_message(): return {'code': 0, 'formattedMessage': {'lang': 'en-US', 'message': 'This is a heartbeat message'},