Repository: Azure/WALinuxAgent
Pull request #853 (2017-08-22 23:12:27)

Improve firewall handling and make GoalState retrieval more robust
Signed-off-by: Brendan Dixon <brendandixon@me.com>


Patch:
diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py
index d3c553e9..27ef20a0 100644
--- a/azurelinuxagent/common/osutil/default.py
+++ b/azurelinuxagent/common/osutil/default.py
@@ -73,16 +73,14 @@ class DefaultOSUtil(object):
             if dst_ip is None or uid is None:
                 raise Exception("Missing arguments to enable_firewall")
 
-            # If either firewall rule exists, make no changes
-            accept_rule = FIREWALL_ACCEPT.format("C", dst_ip, uid, FIREWALL_WAIT_IN_SECONDS)
+            # If the DROP rule exists, make no changes
             drop_rule = FIREWALL_DROP.format("C", dst_ip, FIREWALL_WAIT_IN_SECONDS)
 
-            if shellutil.run(accept_rule, chk_err=False) == 0 or \
-                shellutil.run(drop_rule, chk_err=False) == 0:
+            if  shellutil.run(drop_rule, chk_err=False) == 0:
                 logger.verbose("Firewall appears established")
                 return True
 
-            # Neither rule exists, append both rules
+            # Otherwise, append both rules
             accept_rule = FIREWALL_ACCEPT.format("A", dst_ip, uid, FIREWALL_WAIT_IN_SECONDS)
             drop_rule = FIREWALL_DROP.format("A", dst_ip, FIREWALL_WAIT_IN_SECONDS)
 
diff --git a/azurelinuxagent/common/protocol/wire.py b/azurelinuxagent/common/protocol/wire.py
index 0142c89a..0794f82c 100644
--- a/azurelinuxagent/common/protocol/wire.py
+++ b/azurelinuxagent/common/protocol/wire.py
@@ -731,6 +731,12 @@ class WireClient(object):
                     self.host_plugin.container_id = goal_state.container_id
                     self.host_plugin.role_config_name = goal_state.role_config_name
                 return
+
+            except ProtocolError:
+                if retry < max_retry-1:
+                    continue
+                raise
+
             except WireProtocolResourceGone:
                 logger.info("Incarnation is out of date. Update goalstate.")
                 xml_text = self.fetch_config(uri, self.get_header())
diff --git a/azurelinuxagent/common/version.py b/azurelinuxagent/common/version.py
index de451f11..a43930b1 100644
--- a/azurelinuxagent/common/version.py
+++ b/azurelinuxagent/common/version.py
@@ -113,7 +113,7 @@ def get_distro():
 
 AGENT_NAME = "WALinuxAgent"
 AGENT_LONG_NAME = "Azure Linux Agent"
-AGENT_VERSION = '2.2.14.3'
+AGENT_VERSION = '2.2.14.4'
 AGENT_LONG_VERSION = "{0}-{1}".format(AGENT_NAME, AGENT_VERSION)
 AGENT_DESCRIPTION = """
 The Azure Linux Agent supports the provisioning and running of Linux


Test patch:
diff --git a/tests/common/osutil/test_default.py b/tests/common/osutil/test_default.py
index 291189bc..2a584d85 100644
--- a/tests/common/osutil/test_default.py
+++ b/tests/common/osutil/test_default.py
@@ -498,36 +498,17 @@ Match host 192.168.1.2\n\
         uid = 42
         wait = 30
 
-        mock_run.side_effect = [1, 1, 0, 0]
+        mock_run.side_effect = [1, 0, 0]
         mock_output.return_value = (0, "Output")
         self.assertTrue(util.enable_firewall(dst_ip=dst, uid=uid))
 
         mock_run.assert_has_calls([
-            call(osutil.FIREWALL_ACCEPT.format("C", dst, uid, wait), chk_err=False),
             call(osutil.FIREWALL_DROP.format("C", dst, wait), chk_err=False),
             call(osutil.FIREWALL_ACCEPT.format("A", dst, uid, wait)),
             call(osutil.FIREWALL_DROP.format("A", dst, wait))
         ])
         mock_output.assert_called_with(osutil.FIREWALL_LIST)
 
-    @patch('os.getuid', return_value=42)
-    @patch('azurelinuxagent.common.utils.shellutil.run_get_output')
-    @patch('azurelinuxagent.common.utils.shellutil.run')
-    def test_enable_firewall_skips_if_accept_exists(self, mock_run, mock_output, mock_uid):
-        util = osutil.DefaultOSUtil()
-
-        dst = '1.2.3.4'
-        uid = 42
-        wait = 30
-
-        mock_run.side_effect = [0, 1, 0, 0]
-        self.assertTrue(util.enable_firewall(dst_ip=dst, uid=uid))
-
-        mock_run.assert_has_calls([
-            call(osutil.FIREWALL_ACCEPT.format("C", dst, uid, wait), chk_err=False)
-        ])
-        mock_output.assert_not_called()
-
     @patch('os.getuid', return_value=42)
     @patch('azurelinuxagent.common.utils.shellutil.run_get_output')
     @patch('azurelinuxagent.common.utils.shellutil.run')
@@ -538,11 +519,10 @@ Match host 192.168.1.2\n\
         uid = 42
         wait = 30
 
-        mock_run.side_effect = [1, 0, 0, 0]
+        mock_run.side_effect = [0, 0, 0]
         self.assertTrue(util.enable_firewall(dst_ip=dst, uid=uid))
 
         mock_run.assert_has_calls([
-            call(osutil.FIREWALL_ACCEPT.format("C", dst, uid, wait), chk_err=False),
             call(osutil.FIREWALL_DROP.format("C", dst, wait), chk_err=False),
         ])
         mock_output.assert_not_called()
@@ -557,11 +537,10 @@ Match host 192.168.1.2\n\
         uid = 42
         wait = 30
 
-        mock_run.side_effect = [1, 1, Exception]
+        mock_run.side_effect = [1, Exception]
         self.assertFalse(util.enable_firewall(dst_ip=dst, uid=uid))
 
         mock_run.assert_has_calls([
-            call(osutil.FIREWALL_ACCEPT.format("C", dst, uid, wait), chk_err=False),
             call(osutil.FIREWALL_DROP.format("C", dst, wait), chk_err=False),
             call(osutil.FIREWALL_ACCEPT.format("A", dst, uid, wait))
         ])