/usr/lib/python3/dist-packages/plainbox/impl/secure/qualifiers.py is in python3-plainbox 0.5.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | # This file is part of Checkbox.
#
# Copyright 2013, 2014 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
:mod:`plainbox.impl.secure.qualifiers` -- Job Qualifiers
========================================================
Qualifiers are callable objects that can be used to 'match' a job definition to
some set of rules.
"""
import abc
import itertools
import logging
import os
import re
from plainbox.abc import IJobQualifier
from plainbox.impl.secure.rfc822 import FileTextSource
from plainbox.impl.secure.rfc822 import Origin
from plainbox.impl.secure.rfc822 import UnknownTextSource
_logger = logging.getLogger("plainbox.secure.qualifiers")
class SimpleQualifier(IJobQualifier):
"""
Abstract base class that implements common features of simple (non
composite) qualifiers. This allows two concrete subclasses below to
have share some code.
"""
def __init__(self, inclusive=True):
self._inclusive = inclusive
@property
def inclusive(self):
return self._inclusive
@property
def is_primitive(self):
return True
def designates(self, job):
return self.get_vote(job) == self.VOTE_INCLUDE
@abc.abstractmethod
def get_simple_match(self, job):
"""
Get a simple yes-or-no boolean answer if the given job matches the
simple aspect of this qualifier. This method should be overridden by
concrete subclasses.
"""
def get_vote(self, job):
"""
Get one of the ``VOTE_IGNORE``, ``VOTE_INCLUDE``, ``VOTE_EXCLUDE``
votes that this qualifier associated with the specified job.
:param job:
A IJobDefinition instance that is to be visited
:returns:
* ``VOTE_INCLUDE`` if the job matches the simple qualifier concept
embedded into this qualifier and this qualifier is **inclusive**.
* ``VOTE_EXCLUDE`` if the job matches the simple qualifier concept
embedded into this qualifier and this qualifier is **not
inclusive**.
* ``VOTE_IGNORE`` otherwise.
.. versionadded: 0.5
"""
if self.get_simple_match(job):
if self.inclusive:
return self.VOTE_INCLUDE
else:
return self.VOTE_EXCLUDE
else:
return self.VOTE_IGNORE
def get_primitive_qualifiers(self):
"""
Return a list of primitives that constitute this qualifier.
:returns:
A list of IJobQualifier objects that each is the smallest,
indivisible entity. Here it just returns a list of one element,
itself.
.. versionadded: 0.5
"""
return [self]
class RegExpJobQualifier(SimpleQualifier):
"""
A JobQualifier that designates jobs by matching their id to a regular
expression
"""
def __init__(self, pattern, inclusive=True):
"""
Initialize a new RegExpJobQualifier with the specified pattern.
"""
super().__init__(inclusive)
self._pattern = re.compile(pattern)
self._pattern_text = pattern
def get_simple_match(self, job):
"""
Check if the given job matches this qualifier.
This method should not be called directly, it is an implementation
detail of SimpleQualifier class.
"""
return self._pattern.match(job.id) is not None
@property
def pattern_text(self):
"""
text of the regular expression embedded in this qualifier
"""
return self._pattern_text
def __repr__(self):
return "{0}({1!r}, inclusive={2})".format(
self.__class__.__name__, self._pattern_text, self._inclusive)
class JobIdQualifier(SimpleQualifier):
"""
A JobQualifier that designates a single job with a particular id
"""
def __init__(self, id, inclusive=True):
super().__init__(inclusive)
self._id = id
def get_simple_match(self, job):
"""
Check if the given job matches this qualifier.
This method should not be called directly, it is an implementation
detail of SimpleQualifier class.
"""
return self._id == job.id
def __repr__(self):
return "{0}({1!r}, inclusive={2})".format(
self.__class__.__name__, self._id, self._inclusive)
class NonLocalJobQualifier(SimpleQualifier):
"""
A JobQualifier that designates only non local jobs
"""
def __init__(self, inclusive=True):
super().__init__(inclusive)
def get_simple_match(self, job):
"""
Check if the given job matches this qualifier.
This method should not be called directly, it is an implementation
detail of SimpleQualifier class.
"""
return job.plugin != 'local'
def __repr__(self):
return "{0}(inclusive={1})".format(
self.__class__.__name__, self._inclusive)
class IMatcher(metaclass=abc.ABCMeta):
"""
Interface for objects that perform some kind of comparison on a value
"""
@abc.abstractmethod
def match(self, value):
"""
Match (or not) specified value
:param value:
value to match
:returns:
True if it matched, False otherwise
"""
class OperatorMatcher(IMatcher):
"""
A matcher that applies a binary operator to the value
"""
def __init__(self, op, value):
self._op = op
self._value = value
def match(self, value):
return self._op(self._value, value)
def __repr__(self):
return "{0}({1!r}, {2!r})".format(
self.__class__.__name__, self._op, self._value)
class PatternMatcher(IMatcher):
"""
A matcher that compares values by regular expression pattern
"""
def __init__(self, pattern):
self._pattern_text = pattern
self._pattern = re.compile(pattern)
def match(self, value):
return self._pattern.match(value) is not None
def __repr__(self):
return "{0}({1!r})".format(
self.__class__.__name__, self._pattern_text)
class FieldQualifier(SimpleQualifier):
"""
A SimpleQualifer that uses matchers to compare particular fields
"""
def __init__(self, field, matcher, inclusive=True):
"""
Initialize a new FieldQualifier with the specified field, matcher and
inclusive flag
:param field:
Name of the JobDefinition field to use
:param matcher:
A IMatcher object
:param inclusive:
Inclusive selection flag (default: True)
"""
super().__init__(inclusive)
self._field = field
self._matcher = matcher
def get_simple_match(self, job):
"""
Check if the given job matches this qualifier.
This method should not be called directly, it is an implementation
detail of SimpleQualifier class.
"""
field_value = getattr(job, str(self._field))
return self._matcher.match(field_value)
def __repr__(self):
return "{0}({1!r}, {2!r}, inclusive={3})".format(
self.__class__.__name__, self._field, self._matcher,
self._inclusive)
class CompositeQualifier(IJobQualifier):
"""
A JobQualifier that has qualifies jobs matching any inclusive qualifiers
while not matching all of the exclusive qualifiers
"""
def __init__(self, qualifier_list):
self.qualifier_list = qualifier_list
@property
def is_primitive(self):
return False
def designates(self, job):
return self.get_vote(job) == self.VOTE_INCLUDE
def get_vote(self, job):
"""
Get one of the ``VOTE_IGNORE``, ``VOTE_INCLUDE``, ``VOTE_EXCLUDE``
votes that this qualifier associated with the specified job.
:param job:
A IJobDefinition instance that is to be visited
:returns:
* ``VOTE_INCLUDE`` if the job matches at least one qualifier voted
to select it and no qualifiers voted to deselect it.
* ``VOTE_EXCLUDE`` if at least one qualifier voted to deselect it
* ``VOTE_IGNORE`` otherwise or if the list of qualifiers is empty.
.. versionadded: 0.5
"""
if self.qualifier_list:
return min([
qualifier.get_vote(job)
for qualifier in self.qualifier_list])
else:
return self.VOTE_IGNORE
def get_primitive_qualifiers(self):
return get_flat_primitive_qualifier_list(self.qualifier_list)
# NOTE: using CompositeQualifier seems strange but it's a tested proven
# component so all we have to ensure is that we read the whitelist files
# correctly.
class WhiteList(CompositeQualifier):
"""
A qualifier that understands checkbox whitelist files.
A whitelist file is a plain text, line oriented file. Each line represents
a regular expression pattern that can be matched against the id of a job.
The file can contain simple shell-style comments that begin with the pound
or hash key (#). Those are ignored. Comments can span both a fraction of a
line as well as the whole line.
For historical reasons each pattern has an implicit '^' and '$' prepended
and appended (respectively) to the actual pattern specified in the file.
"""
def __init__(self, pattern_list, name=None, origin=None,
implicit_namespace=None):
"""
Initialize a WhiteList object with the specified list of patterns.
The patterns must be already mangled with '^' and '$'.
"""
self._name = name
self._origin = origin
self._implicit_namespace = implicit_namespace
if implicit_namespace is not None:
# If we have an implicit namespace then transform all the patterns
# without the namespace operator ('::')
namespace_pattern = implicit_namespace.replace('.', '\\.')
def transform_pattern(maybe_partial_id_pattern):
if "::" not in maybe_partial_id_pattern:
return "^{}::{}$".format(
namespace_pattern, maybe_partial_id_pattern[1:-1])
else:
return maybe_partial_id_pattern
qualifier_list = [
RegExpJobQualifier(transform_pattern(pattern), inclusive=True)
for pattern in pattern_list]
else:
# Otherwise just use the patterns directly
qualifier_list = [
RegExpJobQualifier(pattern, inclusive=True)
for pattern in pattern_list]
super().__init__(qualifier_list)
def __repr__(self):
return "<{} name:{!r}>".format(self.__class__.__name__, self.name)
@property
def name(self):
"""
name of this WhiteList (might be None)
"""
return self._name
@name.setter
def name(self, value):
"""
set a new name for a WhiteList
"""
self._name = value
@property
def origin(self):
"""
origin object associated with this WhiteList (might be None)
"""
return self._origin
@property
def implicit_namespace(self):
"""
namespace used to qualify patterns without explicit namespace
"""
return self._implicit_namespace
@classmethod
def from_file(cls, pathname, implicit_namespace=None):
"""
Load and initialize the WhiteList object from the specified file.
:param pathname:
file to load
:param implicit_namespace:
(optional) implicit namespace for jobs that are using partial
identifiers (all jobs)
:returns:
a fresh WhiteList object
"""
pattern_list, max_lineno = cls._load_patterns(pathname)
name = os.path.splitext(os.path.basename(pathname))[0]
origin = Origin(FileTextSource(pathname), 1, max_lineno)
return cls(pattern_list, name, origin, implicit_namespace)
@classmethod
def from_string(cls, text, *, filename=None, name=None, origin=None,
implicit_namespace=None):
"""
Load and initialize the WhiteList object from the specified string.
:param text:
full text of the whitelist
:param filename:
(optional, keyword-only) filename from which text was read from.
This simulates a call to :meth:`from_file()` which properly
computes the name and origin of the whitelist.
:param name:
(optional) name of the whitelist, only used if filename is not
specified.
:param origin:
(optional) origin of the whitelist, only used if a filename is not
specified. If omitted a default origin value will be constructed
out of UnknownTextSource instance
:param implicit_namespace:
(optional) implicit namespace for jobs that are using partial
identifiers (all jobs)
:returns:
a fresh WhiteList object
The optional filename or a pair of name and origin arguments may be
provided in order to have additional meta-data. This is typically
needed when the :meth:`from_file()` method cannot be used as the caller
already has the full text of the intended file available.
"""
_logger.debug("Loaded whitelist from %r", filename)
pattern_list, max_lineno = cls._parse_patterns(text)
# generate name and origin if filename is provided
if filename is not None:
name = WhiteList.name_from_filename(filename)
origin = Origin(FileTextSource(filename), 1, max_lineno)
else:
# otherwise generate origin if it's not specified
if origin is None:
origin = Origin(UnknownTextSource(), 1, max_lineno)
return cls(pattern_list, name, origin, implicit_namespace)
@classmethod
def name_from_filename(cls, filename):
"""
Compute the name of a whitelist based on the name
of the file it is stored in.
"""
return os.path.splitext(os.path.basename(filename))[0]
@classmethod
def _parse_patterns(cls, text):
"""
Load whitelist patterns from the specified text
:param text:
string of text, including newlines, to parse
:returns:
(pattern_list, lineno) where lineno is the final line number
(1-based) and pattern_list is a list of regular expression strings
parsed from the whitelist.
"""
pattern_list = []
lineno = 0
# Load the file
for lineno, line in enumerate(text.splitlines(), 1):
# Strip shell-style comments if there are any
try:
index = line.index("#")
except ValueError:
pass
else:
line = line[:index]
# Strip whitespace
line = line.strip()
# Skip empty lines (especially after stripping comments)
if line == "":
continue
# Surround the pattern with ^ and $
# so that it wont just match a part of the job name.
regexp_pattern = r"^{pattern}$".format(pattern=line)
# Accumulate patterns into the list
pattern_list.append(regexp_pattern)
return pattern_list, lineno
@classmethod
def _load_patterns(cls, pathname):
"""
Load whitelist patterns from the specified file
:param pathname:
pathname of the file to load and parse
:returns:
(pattern_list, lineno) where lineno is the final line number
(1-based) and pattern_list is a list of regular expression strings
parsed from the whitelist.
"""
with open(pathname, "rt", encoding="UTF-8") as stream:
return cls._parse_patterns(stream.read())
def get_flat_primitive_qualifier_list(qualifier_list):
return list(itertools.chain(*[
qual.get_primitive_qualifiers()
for qual in qualifier_list]))
def select_jobs(job_list, qualifier_list):
"""
Select desired jobs.
:param job_list:
A list of JobDefinition objects
:param qualifier_list:
A list of IJobQualifier objects.
:returns:
A sub-list of JobDefinition objects, selected from job_list.
"""
# Flatten the qualifier list, so that we can see the fine structure of
# composite objects, such as whitelists.
flat_qualifier_list = get_flat_primitive_qualifier_list(qualifier_list)
# Short-circuit if there are no jobs to select. Min is used later and this
# will allow us to assume that the matrix is not empty.
if not flat_qualifier_list:
return []
# Vote matrix, encodes the vote cast by a particular qualifier for a
# particular job. Visually it's a two-dimensional array like this:
#
# ^
# q |
# u | X
# a |
# l | ........
# i |
# f | .
# i | .
# e | .
# r |
# ------------------->
# job
#
# The vertical axis represents qualifiers from the flattened qualifier
# list. The horizontal axis represents jobs from job list. Dots represent
# inclusion, X represents exclusion.
#
# The result of the select_job() function is a list of jobs that have at
# least one inclusion and no exclusions. The resulting list is ordered by
# increasing qualifier index.
#
# The algorithm implemented below is composed of two steps.
#
# The first step iterates over the vote matrix (row-major, meaning that we
# visit all columns for each visit of one row) and constructs two
# structures: a set of jobs that got VOTE_INCLUDE and a list of those jobs,
# in the order of discovery. All VOTE_EXCLUDE votes are collected in
# another set.
#
# The second step filters-out all items from the excluded job set from the
# selected job list. For extra efficiency the algorithm operates on
# integers representing the index of a particular job in job_list.
#
# The final complexity is O(N x M) + O(M), where N is the number of
# qualifiers (flattened) and M is the number of jobs. The algorithm assumes
# that set lookup is a O(1) operation which is true enough for python.
#
# A possible optimization would differentiate qualifiers that may select
# more than one job and fall-back to the current implementation while
# short-circuiting qualifiers that may select at most one job with a
# separate set lookup. That would make the algorithm "mostly" linear in the
# common case.
#
# As a separate feature, we might return a list of qualifiers that never
# matched anything. That may be helpful for debugging.
included_list = []
included_set = set()
excluded_set = set()
for qualifier in flat_qualifier_list:
for j_index, job in enumerate(job_list):
vote = qualifier.get_vote(job)
if vote == IJobQualifier.VOTE_INCLUDE:
if j_index in included_set:
continue
included_set.add(j_index)
included_list.append(j_index)
elif vote == IJobQualifier.VOTE_EXCLUDE:
excluded_set.add(j_index)
elif vote == IJobQualifier.VOTE_IGNORE:
pass
return [job_list[index] for index in included_list
if index not in excluded_set]
|