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

Do not reset the mode of a extension's log directory #3014

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions azurelinuxagent/common/utils/fileutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion azurelinuxagent/ga/exthandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions tests/ga/test_exthandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand Down
Loading