Repository: Azure/WALinuxAgent
Pull request #849 (2017-08-21 18:09:44)

Emit Firewall events
Emit firewall-related events
Set version to 2.2.14.3

Signed-off-by: Brendan Dixon <brendand@microsoft.com>


Patch:
diff --git a/azurelinuxagent/common/event.py b/azurelinuxagent/common/event.py
index 6869c22a..e62a925b 100644
--- a/azurelinuxagent/common/event.py
+++ b/azurelinuxagent/common/event.py
@@ -48,6 +48,7 @@ class WALAEventOperation:
     Disable = "Disable"
     Download = "Download"
     Enable = "Enable"
+    Firewall = "Firewall"
     HealthCheck = "HealthCheck"
     HeartBeat = "HeartBeat"
     HostPlugin = "HostPlugin"
@@ -163,11 +164,11 @@ class EventLogger(object):
             raise EventError("Failed to write events to file:{0}", e)
 
     def reset_periodic(self):
-        self.periodic_messages = {}
+        self.periodic_events = {}
 
     def is_period_elapsed(self, delta, h):
-        return h not in self.periodic_messages or \
-            (self.periodic_messages[h] + delta) <= datetime.now()
+        return h not in self.periodic_events or \
+            (self.periodic_events[h] + delta) <= datetime.now()
 
     def add_periodic(self,
         delta, name, op="", is_success=True, duration=0,
@@ -181,7 +182,7 @@ class EventLogger(object):
                 op=op, is_success=is_success, duration=duration,
                 version=version, message=message, evt_type=evt_type,
                 is_internal=is_internal, log_event=log_event)
-            self.periodic_messages[h] = datetime.now()
+            self.periodic_events[h] = datetime.now()
 
     def add_event(self,
                   name,
diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py
index b5f750b1..9a79ff79 100644
--- a/azurelinuxagent/common/osutil/default.py
+++ b/azurelinuxagent/common/osutil/default.py
@@ -79,21 +79,23 @@ class DefaultOSUtil(object):
             if shellutil.run(accept_rule, chk_err=False) == 0 or \
                 shellutil.run(drop_rule, chk_err=False) == 0:
                 logger.verbose("Firewall appears established")
-                return
+                return True
 
             # Neither rule exists, append both rules
             accept_rule = FIREWALL_ACCEPT.format("A", dst_ip, uid)
             drop_rule = FIREWALL_DROP.format("A", dst_ip)
 
             if shellutil.run(accept_rule) != 0:
-                logger.warn("Unable to add ACCEPT firewall rule '{0}'".format(
-                    accept_rule))
-                return
+                msg = "Unable to add ACCEPT firewall rule '{0}'".format(
+                    accept_rule)
+                logger.warn(msg)
+                raise Exception(msg)
 
             if shellutil.run(drop_rule) != 0:
-                logger.warn("Unable to add DROP firewall rule '{0}'".format(
-                    drop_rule))
-                return
+                msg = "Unable to add DROP firewall rule '{0}'".format(
+                    drop_rule)
+                logger.warn(msg)
+                raise Exception(msg)
 
             logger.info("Successfully added Azure fabric firewall rules")
 
@@ -103,8 +105,11 @@ class DefaultOSUtil(object):
             else:
                 logger.warn("Listing firewall rules failed: {0}".format(output))
 
+            return True
+
         except Exception as e:
             logger.info("Unable to establish firewall: {0}".format(ustr(e)))
+            return False
 
     def _correct_instance_id(self, id):
         '''
diff --git a/azurelinuxagent/common/version.py b/azurelinuxagent/common/version.py
index 9447d40a..de451f11 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.2'
+AGENT_VERSION = '2.2.14.3'
 AGENT_LONG_VERSION = "{0}-{1}".format(AGENT_NAME, AGENT_VERSION)
 AGENT_DESCRIPTION = """
 The Azure Linux Agent supports the provisioning and running of Linux
diff --git a/azurelinuxagent/ga/env.py b/azurelinuxagent/ga/env.py
index 7273319c..0456cb06 100644
--- a/azurelinuxagent/ga/env.py
+++ b/azurelinuxagent/ga/env.py
@@ -26,8 +26,10 @@ import azurelinuxagent.common.conf as conf
 import azurelinuxagent.common.logger as logger
 
 from azurelinuxagent.common.dhcp import get_dhcp_handler
+from azurelinuxagent.common.event import add_periodic, WALAEventOperation
 from azurelinuxagent.common.osutil import get_osutil
 from azurelinuxagent.common.protocol import get_protocol_util
+from azurelinuxagent.common.version import AGENT_NAME, CURRENT_VERSION
 
 def get_env_handler():
     return EnvHandler()
@@ -75,9 +77,16 @@ class EnvHandler(object):
             self.osutil.remove_rules_files()
 
             if conf.enable_firewall():
-                self.osutil.enable_firewall(
-                    dst_ip=protocol.endpoint,
-                    uid=os.getuid())
+                success = self.osutil.enable_firewall(
+                                dst_ip=protocol.endpoint,
+                                uid=os.getuid())
+                add_periodic(
+                    logger.EVERY_HOUR,
+                    AGENT_NAME,
+                    version=CURRENT_VERSION,
+                    op=WALAEventOperation.Firewall,
+                    is_success=success,
+                    log_event=True)
 
             timeout = conf.get_root_device_scsi_timeout()
             if timeout is not None:


Test patch:
diff --git a/tests/common/osutil/test_default.py b/tests/common/osutil/test_default.py
index 3013f65f..5bba0e9e 100644
--- a/tests/common/osutil/test_default.py
+++ b/tests/common/osutil/test_default.py
@@ -498,8 +498,8 @@ Match host 192.168.1.2\n\
         uid = 42
 
         mock_run.side_effect = [1, 1, 0, 0]
-        mock_output.return_value = 0
-        util.enable_firewall(dst_ip=dst, uid=uid)
+        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), chk_err=False),
@@ -519,8 +519,7 @@ Match host 192.168.1.2\n\
         uid = 42
 
         mock_run.side_effect = [0, 1, 0, 0]
-        mock_output.return_value = 0
-        util.enable_firewall(dst_ip=dst, uid=uid)
+        self.assertTrue(util.enable_firewall(dst_ip=dst, uid=uid))
 
         mock_run.assert_has_calls([
             call(osutil.FIREWALL_ACCEPT.format("C", dst, uid), chk_err=False)
@@ -537,8 +536,7 @@ Match host 192.168.1.2\n\
         uid = 42
 
         mock_run.side_effect = [1, 0, 0, 0]
-        mock_output.return_value = 0
-        util.enable_firewall(dst_ip=dst, uid=uid)
+        self.assertTrue(util.enable_firewall(dst_ip=dst, uid=uid))
 
         mock_run.assert_has_calls([
             call(osutil.FIREWALL_ACCEPT.format("C", dst, uid), chk_err=False),
@@ -556,7 +554,7 @@ Match host 192.168.1.2\n\
         uid = 42
 
         mock_run.side_effect = [1, 1, Exception]
-        util.enable_firewall(dst_ip=dst, uid=uid)
+        self.assertFalse(util.enable_firewall(dst_ip=dst, uid=uid))
 
         mock_run.assert_has_calls([
             call(osutil.FIREWALL_ACCEPT.format("C", dst, uid), chk_err=False),
diff --git a/tests/common/test_event.py b/tests/common/test_event.py
index a5bb050d..55a99c49 100644
--- a/tests/common/test_event.py
+++ b/tests/common/test_event.py
@@ -145,7 +145,7 @@ class TestEvent(AgentTestCase):
         self.assertEqual(1, mock_event.call_count)
 
         h = hash("FauxEvent"+""+ustr(True)+"")
-        event.__event_logger__.periodic_messages[h] = \
+        event.__event_logger__.periodic_events[h] = \
             datetime.now() - logger.EVERY_DAY - logger.EVERY_HOUR
         event.add_periodic(logger.EVERY_DAY, "FauxEvent")
         self.assertEqual(2, mock_event.call_count)