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

Improve logic of unexpected processes check started by agent #2522

Merged
merged 6 commits into from
Mar 3, 2022
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
21 changes: 20 additions & 1 deletion azurelinuxagent/common/cgroupconfigurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ def _check_processes_in_agent_cgroup(self):
current = process
while current != 0 and current not in agent_commands:
current = self._get_parent(current)
if current == 0:
# Process started by agent will have a marker and check if that marker found in process environment.
if current == 0 and not self.__is_process_descendant_of_the_agent(process):
unexpected.append(self.__format_process(process))
if len(unexpected) >= 5: # collect just a small sample
break
Expand Down Expand Up @@ -640,6 +641,24 @@ def __format_process(pid):
pass
return "[PID: {0}] UNKNOWN".format(pid)

@staticmethod
def __is_process_descendant_of_the_agent(pid):
"""
Returns True if the process is descendant of the agent by looking at the env flag(AZURE_GUEST_AGENT_PARENT_PROCESS_NAME)
that we set when the process starts otherwise False.
"""
try:
env = '/proc/{0}/environ'.format(pid)
if os.path.exists(env):
with open(env, "r") as env_file:
environ = env_file.read()
if environ and environ[-1] == '\x00':
environ = environ[:-1]
return "{0}={1}".format(shellutil.PARENT_PROCESS_NAME, shellutil.AZURE_GUEST_AGENT) in environ
except Exception:
pass
return False

@staticmethod
def _check_agent_throttled_time(cgroup_metrics):
for metric in cgroup_metrics:
Expand Down
15 changes: 14 additions & 1 deletion azurelinuxagent/common/utils/shellutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
# Requires Python 2.6+ and Openssl 1.0+
#

import os
import subprocess
import tempfile
import threading
Expand Down Expand Up @@ -345,10 +345,23 @@ def quote(word_list):
#
_running_commands = []
_running_commands_lock = threading.RLock()
PARENT_PROCESS_NAME = "AZURE_GUEST_AGENT_PARENT_PROCESS_NAME"
AZURE_GUEST_AGENT = "AZURE_GUEST_AGENT"


def _popen(*args, **kwargs):
with _running_commands_lock:
# Add the environment variables
env = {}
if 'env' in kwargs:
env.update(kwargs['env'])
else:
env.update(os.environ)

# Set the marker before process start
env[PARENT_PROCESS_NAME] = AZURE_GUEST_AGENT
kwargs['env'] = env
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If caller sends env arg, use the same flags otherwise extend os.enviorn along with new marker flag


process = subprocess.Popen(*args, **kwargs)
_running_commands.append(process.pid)
return process
Expand Down
5 changes: 4 additions & 1 deletion tests/ga/test_multi_config_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,10 @@ def __assert_env_variables(handler_name, handler_version="1.0.0", seq_no="1", ex
self.assertFalse(any(env_var in commands['data'] for env_var in not_expected), "Unwanted env variable found")

def mock_popen(cmd, *_, **kwargs):
if 'env' in kwargs:
# This cgroupsapi Popen mocking all other popen calls which breaking the extension emulator logic.
# The emulator should be used only on extension commands and not on other commands even env flag set.
# So, added ExtensionVersion check to avoid using extension emulator on non extension operations.
if 'env' in kwargs and ExtCommandEnvVariable.ExtensionVersion in kwargs['env']:
narrieta marked this conversation as resolved.
Show resolved Hide resolved
narrieta marked this conversation as resolved.
Show resolved Hide resolved
handler_name, __, command = extract_extension_info_from_command(cmd)
name = handler_name
if ExtCommandEnvVariable.ExtensionName in kwargs['env']:
Expand Down
14 changes: 14 additions & 0 deletions tests/utils/test_shell_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
import os
import signal
import subprocess
import tempfile
import threading
import unittest
Expand Down Expand Up @@ -156,6 +157,19 @@ def test_run_command_should_execute_the_command(self):
ret = shellutil.run_command(command)
self.assertEqual(ret, "A TEST STRING")

def test_run_command_should_use_popen_arg_list(self):
with patch("azurelinuxagent.common.utils.shellutil.subprocess.Popen", wraps=subprocess.Popen) as popen_patch:
command = ["echo", "-n", "A TEST STRING"]
ret = shellutil.run_command(command)

self.assertEqual(ret, "A TEST STRING")
self.assertEqual(popen_patch.call_count, 1)

args, kwargs = popen_patch.call_args
self.assertTrue(any(arg for arg in args[0] if "A TEST STRING" in arg), "command not being used")
self.assertEqual(kwargs['env'].get(shellutil.PARENT_PROCESS_NAME), shellutil.AZURE_GUEST_AGENT,
"Env flag not being used")

def test_run_pipe_should_execute_a_pipe_with_two_commands(self):
# Output the same string 3 times and then remove duplicates
test_string = "A TEST STRING\n"
Expand Down