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