Home | History | Annotate | Download | only in diff
      1 # Copyright 2015, 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 .descriptors import DiffResultDescriptor
     22 from .difference import Difference, SymbolWrapper
     23 
     24 
     25 modified_commons_record = namedtuple("modified_common", ["added_perms",
     26                                                          "removed_perms",
     27                                                          "matched_perms"])
     28 
     29 
     30 class CommonDifference(Difference):
     31 
     32     """
     33     Determine the difference in common permission sets
     34     between two policies.
     35     """
     36 
     37     added_commons = DiffResultDescriptor("diff_commons")
     38     removed_commons = DiffResultDescriptor("diff_commons")
     39     modified_commons = DiffResultDescriptor("diff_commons")
     40 
     41     def diff_commons(self):
     42         """Generate the difference in commons between the policies."""
     43 
     44         self.log.info(
     45             "Generating common differences from {0.left_policy} to {0.right_policy}".format(self))
     46 
     47         self.added_commons, self.removed_commons, matched_commons = self._set_diff(
     48             (SymbolWrapper(c) for c in self.left_policy.commons()),
     49             (SymbolWrapper(c) for c in self.right_policy.commons()))
     50 
     51         self.modified_commons = dict()
     52 
     53         for left_common, right_common in matched_commons:
     54             # Criteria for modified commons
     55             # 1. change to permissions
     56             added_perms, removed_perms, matched_perms = self._set_diff(left_common.perms,
     57                                                                        right_common.perms)
     58 
     59             if added_perms or removed_perms:
     60                 self.modified_commons[left_common] = modified_commons_record(added_perms,
     61                                                                              removed_perms,
     62                                                                              matched_perms)
     63 
     64     #
     65     # Internal functions
     66     #
     67     def _reset_diff(self):
     68         """Reset diff results on policy changes."""
     69         self.log.debug("Resetting common differences")
     70         self.added_commons = None
     71         self.removed_commons = None
     72         self.modified_commons = None
     73