Home | History | Annotate | Download | only in diff
      1 # Copyright 2016, Tresys Technology, LLC
      2 #
      3 # This file is part of SETools.
      4 #
      5 # SETools is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU Lesser General Public License as
      7 # published by the Free Software Foundation, either version 2.1 of
      8 # the License, or (at your option) any later version.
      9 #
     10 # SETools is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU Lesser General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU Lesser General Public
     16 # License along with SETools.  If not, see
     17 # <http://www.gnu.org/licenses/>.
     18 #
     19 from collections import namedtuple
     20 
     21 from .context import ContextWrapper
     22 from .descriptors import DiffResultDescriptor
     23 from .difference import Difference, Wrapper
     24 
     25 
     26 modified_nodecon_record = namedtuple("modified_nodecon", ["rule",
     27                                                           "added_context",
     28                                                           "removed_context"])
     29 
     30 
     31 class NodeconsDifference(Difference):
     32 
     33     """Determine the difference in nodecons between two policies."""
     34 
     35     added_nodecons = DiffResultDescriptor("diff_nodecons")
     36     removed_nodecons = DiffResultDescriptor("diff_nodecons")
     37     modified_nodecons = DiffResultDescriptor("diff_nodecons")
     38 
     39     def diff_nodecons(self):
     40         """Generate the difference in nodecons between the policies."""
     41 
     42         self.log.info("Generating nodecon differences from {0.left_policy} to {0.right_policy}".
     43                       format(self))
     44 
     45         self.added_nodecons, self.removed_nodecons, matched_nodecons = self._set_diff(
     46             (NodeconWrapper(n) for n in self.left_policy.nodecons()),
     47             (NodeconWrapper(n) for n in self.right_policy.nodecons()))
     48 
     49         self.modified_nodecons = []
     50 
     51         for left_nodecon, right_nodecon in matched_nodecons:
     52             # Criteria for modified nodecons
     53             # 1. change to context
     54             if ContextWrapper(left_nodecon.context) != ContextWrapper(right_nodecon.context):
     55                 self.modified_nodecons.append(modified_nodecon_record(left_nodecon,
     56                                                                       right_nodecon.context,
     57                                                                       left_nodecon.context))
     58 
     59     #
     60     # Internal functions
     61     #
     62     def _reset_diff(self):
     63         """Reset diff results on policy changes."""
     64         self.log.debug("Resetting nodecon differences")
     65         self.added_nodecons = None
     66         self.removed_nodecons = None
     67         self.modified_nodecons = None
     68 
     69 
     70 class NodeconWrapper(Wrapper):
     71 
     72     """Wrap nodecon statements for diff purposes."""
     73 
     74     def __init__(self, ocon):
     75         self.origin = ocon
     76         self.ip_version = ocon.ip_version
     77         self.address = ocon.address
     78         self.netmask = ocon.netmask
     79         self.key = hash(ocon)
     80 
     81     def __hash__(self):
     82         return self.key
     83 
     84     def __lt__(self, other):
     85         return self.origin < other.origin
     86 
     87     def __eq__(self, other):
     88         return self.ip_version == other.ip_version and \
     89                self.address == other.address and \
     90                self.netmask == other.netmask
     91