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, SymbolWrapper
     24 
     25 
     26 modified_initsids_record = namedtuple("modified_initsid", ["added_context", "removed_context"])
     27 
     28 
     29 class InitialSIDsDifference(Difference):
     30 
     31     """Determine the difference in initsids between two policies."""
     32 
     33     added_initialsids = DiffResultDescriptor("diff_initialsids")
     34     removed_initialsids = DiffResultDescriptor("diff_initialsids")
     35     modified_initialsids = DiffResultDescriptor("diff_initialsids")
     36 
     37     def diff_initialsids(self):
     38         """Generate the difference in initial SIDs between the policies."""
     39 
     40         self.log.info("Generating initial SID differences from {0.left_policy} to {0.right_policy}".
     41                       format(self))
     42 
     43         self.added_initialsids, self.removed_initialsids, matched_initialsids = self._set_diff(
     44             (SymbolWrapper(i) for i in self.left_policy.initialsids()),
     45             (SymbolWrapper(i) for i in self.right_policy.initialsids()))
     46 
     47         self.modified_initialsids = dict()
     48 
     49         for left_initialsid, right_initialsid in matched_initialsids:
     50             # Criteria for modified initialsids
     51             # 1. change to context
     52             if ContextWrapper(left_initialsid.context) != ContextWrapper(right_initialsid.context):
     53                 self.modified_initialsids[left_initialsid] = modified_initsids_record(
     54                     right_initialsid.context, left_initialsid.context)
     55 
     56     #
     57     # Internal functions
     58     #
     59     def _reset_diff(self):
     60         """Reset diff results on policy changes."""
     61         self.log.debug("Resetting initialsid differences")
     62         self.added_initialsids = None
     63         self.removed_initialsids = None
     64         self.modified_initialsids = None
     65