Home | History | Annotate | Download | only in setools
      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 import logging
     20 
     21 from . import compquery
     22 from . import mixins
     23 from .descriptors import CriteriaDescriptor
     24 
     25 
     26 class SensitivityQuery(mixins.MatchAlias, compquery.ComponentQuery):
     27 
     28     """
     29     Query MLS Sensitivities
     30 
     31     Parameter:
     32     policy       The policy to query.
     33 
     34     Keyword Parameters/Class attributes:
     35     name         The name of the category to match.
     36     name_regex   If true, regular expression matching will
     37                  be used for matching the name.
     38     alias        The alias name to match.
     39     alias_regex  If true, regular expression matching
     40                  will be used on the alias names.
     41     sens         The criteria to match the sensitivity by dominance.
     42     sens_dom     If true, the criteria will match if it dominates
     43                  the sensitivity.
     44     sens_domby   If true, the criteria will match if it is dominated
     45                  by the sensitivity.
     46     """
     47 
     48     sens = CriteriaDescriptor(lookup_function="lookup_sensitivity")
     49     sens_dom = False
     50     sens_domby = False
     51 
     52     def results(self):
     53         """Generator which yields all matching sensitivities."""
     54         self.log.info("Generating results from {0.policy}".format(self))
     55         self.log.debug("Name: {0.name!r}, regex: {0.name_regex}".format(self))
     56         self.log.debug("Alias: {0.alias}, regex: {0.alias_regex}".format(self))
     57         self.log.debug("Sens: {0.sens!r}, dom: {0.sens_dom}, domby: {0.sens_domby}".format(self))
     58 
     59         for s in self.policy.sensitivities():
     60             if not self._match_name(s):
     61                 continue
     62 
     63             if not self._match_alias(s):
     64                 continue
     65 
     66             if self.sens and not self._match_level(
     67                     s,
     68                     self.sens,
     69                     self.sens_dom,
     70                     self.sens_domby,
     71                     False):
     72                 continue
     73 
     74             yield s
     75