This file is indexed.

/usr/lib/python3/dist-packages/custodia/plugin.py is in python3-custodia 0.5.0-3.

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
# Copyright (C) 2016  Custodia Project Contributors - see LICENSE file
from __future__ import absolute_import

import abc
import grp
import inspect
import json
import pwd
import re
import sys

from jwcrypto.common import json_encode

import six

from .compat import configparser
from .log import CustodiaLoggingAdapter, auditlog, getLogger


logger = getLogger(__name__)


class _Required(object):
    __slots__ = ()

    def __repr__(self):
        return 'REQUIRED'


class INHERIT_GLOBAL(object):  # noqa: N801
    __slots__ = ('default',)

    def __init__(self, default):
        self.default = default

    def __repr__(self):
        return 'INHERIT_GLOBAL({})'.format(self.default)


REQUIRED = _Required()


class CustodiaException(Exception):
    pass


class HTTPError(CustodiaException):
    def __init__(self, code=None, message=None):
        self.code = code if code is not None else 500
        self.mesg = message
        errstring = '%d: %s' % (self.code, self.mesg)
        super(HTTPError, self).__init__(errstring)


class CSStoreError(CustodiaException):
    pass


class CSStoreExists(CustodiaException):
    pass


class CSStoreUnsupported(CustodiaException):
    pass


class CSStoreDenied(CustodiaException):
    pass


class OptionHandler(object):
    """Handler and parser for plugin options
    """
    def __init__(self, parser, section):
        self.parser = parser
        self.section = section
        # handler is reserved to look up the plugin class
        self.seen = {'handler'}

    def get(self, po):
        """Lookup value for a PluginOption instance

        Args:
            po: PluginOption

        Returns: converted value
        """
        name = po.name
        typ = po.typ
        default = po.default

        handler = getattr(self, '_get_{}'.format(typ), None)
        if handler is None:
            raise ValueError(typ)
        self.seen.add(name)

        # pylint: disable=not-callable
        if not self.parser.has_option(self.section, name):
            if default is REQUIRED:
                raise NameError(self.section, name)
            if isinstance(default, INHERIT_GLOBAL):
                return handler('global', name, default.default)
            # don't return default here, give the handler a chance to modify
            # the default, e.g. pw_uid with default='root' returns 0.

        return handler(self.section, name, default)
        # pylint: enable=not-callable

    def check_surplus(self):
        surplus = []
        for name, _ in self.parser.items(self.section):
            if (name not in self.seen and not
                    self.parser.has_option(configparser.DEFAULTSECT, name)):
                surplus.append(name)
        return surplus

    def _get_int(self, section, name, default):
        return self.parser.getint(section, name, fallback=default)

    def _get_oct(self, section, name, default):
        value = self.parser.get(section, name, fallback=default)
        return int(value, 8)

    def _get_hex(self, section, name, default):
        value = self.parser.get(section, name, fallback=default)
        return int(value, 16)

    def _get_float(self, section, name, default):
        return self.parser.getfloat(section, name, fallback=default)

    def _get_bool(self, section, name, default):
        return self.parser.getboolean(section, name, fallback=default)

    def _get_regex(self, section, name, default):
        value = self.parser.get(section, name, fallback=default)
        if not value:
            return None
        else:
            return re.compile(value)

    def _get_str(self, section, name, default):
        return self.parser.get(section, name, fallback=default)

    def _get_str_set(self, section, name, default):
        try:
            value = self.parser.get(section, name)
        except configparser.NoOptionError:
            return default
        if not value or not value.strip():
            return None
        else:
            return set(v.strip() for v in value.split(' '))

    def _get_str_list(self, section, name, default):
        try:
            value = self.parser.get(section, name)
        except configparser.NoOptionError:
            return default
        if not value or not value.strip():
            return None
        else:
            return list(v.strip() for v in value.split(' ') if v.strip())

    def _get_store(self, section, name, default):
        return self.parser.get(section, name, fallback=default)

    def _get_pwd_uid(self, section, name, default):
        value = self.parser.get(section, name, fallback=default)
        try:
            return int(value)
        except ValueError:
            return pwd.getpwnam(value).pw_uid

    def _get_grp_gid(self, section, name, default):
        value = self.parser.get(section, name, fallback=default)
        try:
            return int(value)
        except ValueError:
            return grp.getgrnam(value).gr_gid

    def _get_json(self, section, name, default):
        value = self.parser.get(section, name, fallback=default)
        return json.loads(value)


class PluginOption(object):
    """Plugin option

    code::

        class MyPlugin(CustodiaPlugin):
            number = PluginOption(int, REQUIRED, 'my value')
            values = PluginOption('str_list', 'foo bar', 'a list of strings')


    config::

        [myplugin]
        handler = MyPlugin
        number = 1
        values = egg spam python


    **Supported value types**

    *str*
      plain string
    *str_set*
      set of space-separated strings
    *str_list*
      ordered list of space-separated strings
    *int*
      number (converted from base 10)
    *hex*
      number (converted from base 16)
    *oct*
      number (converted from base 8)
    *float*
      floating point number
    *bool*
      boolean (true: on, true, yes, 1; false: off, false, no, 0)
    *regex*
      regular expression string
    *store*
      special value for refer to a store plugin
    *pwd_uid*
      numeric user id or user name
    *grp_gid*
      numeric group id or group name
    *json*
      JSON string
    """
    __slots__ = ('name', 'typ', 'default', 'doc')

    def __init__(self, typ, default, doc):
        self.name = None
        if typ in {str, int, float, bool, oct, hex}:
            self.typ = typ.__name__
        else:
            self.typ = typ
        self.default = default
        self.doc = doc

    def __repr__(self):
        if self.default is REQUIRED:
            msg = "<Required option {0.name} ({0.typ}): {0.doc}>"
        else:
            msg = ("<Option {0.name} ({0.typ}, default: '{0.default}'): "
                   "{0.doc}>")
        return msg.format(self)


class CustodiaPluginMeta(abc.ABCMeta):
    def __new__(cls, name, bases, namespace):
        ncls = super(CustodiaPluginMeta, cls).__new__(
            cls, name, bases, namespace)

        if sys.version_info < (3, 0):
            args = inspect.getargspec(ncls.__init__).args
        else:
            sig = inspect.signature(ncls.__init__)  # pylint: disable=no-member
            args = list(sig.parameters)

        if args[1:3] != ['config', 'section']:
            # old-style plugin class
            ncls._options = None  # pylint: disable=protected-access
            return ncls

        # new-style plugin class
        # every plugin has a debug option. In case it is not set, the debug
        # flag from [global] is inherited.
        if not hasattr(ncls, 'debug'):
            ncls.debug = PluginOption(bool, INHERIT_GLOBAL(False), '')
        # get options
        options = []
        for name, value in inspect.getmembers(ncls):
            if not isinstance(value, PluginOption):
                continue
            value.name = name
            options.append(value)

        ncls._options = tuple(options)  # pylint: disable=protected-access
        return ncls


@six.add_metaclass(CustodiaPluginMeta)
class CustodiaPlugin(object):
    """Abstract base class for all Custodia plugins
    """
    _options = ()

    def __init__(self, config, section=None):
        origin, debug = self._configure(config, section)
        self._auditlog = auditlog
        self.section = section  # plugin loader sets section for old plugins
        self.origin = origin
        self.logger = CustodiaLoggingAdapter(self, debug)

    def audit_key_access(self, *args, **kwargs):
        self._auditlog.key_access(self.origin, *args, **kwargs)

    def audit_svc_access(self, *args, **kwargs):
        self._auditlog.svc_access(self.origin, *args, **kwargs)

    def _configure(self, config, section):
        if section is not None and self._options is not None:
            # new style configuration
            opt = OptionHandler(config, section)
            # pylint: disable=not-an-iterable
            for option in self._options:
                value = opt.get(option)
                # special case for store
                if option.typ == 'store':
                    if option.name != 'store':
                        raise ValueError(option.name)
                    self.store_name = value
                    self.store = None
                else:
                    setattr(self, option.name, value)

            surplus = opt.check_surplus()
            if surplus:
                raise ValueError('Surplus options in {}: {}'.format(
                    section, surplus))

            origin = '%s-[%s]' % (type(self).__name__, section)
            debug = self.debug  # pylint: disable=no-member
        else:
            # old style configuration
            if config is None:
                config = {}
            self.config = config
            # special case for store
            if 'store' in config:
                self.store_name = self.config.get('store')
                self.store = None
            origin = config.get('facility_name', self.__class__.__name__)
            debug = config.get('debug', 'false').lower() == 'true'

        return origin, debug

    def _attach_store(self, config, cfgparser, context):
        """Attach nested store
        """
        if getattr(self, 'store', None) is not None:
            # already attached
            return
        store_plugin = config['stores'].get(self.store_name)
        if store_plugin is None:
            raise ValueError(
                "'{}' references non-existing store '{}'".format(
                    self.section, self.store_name))
        # pylint: disable=attribute-defined-outside-init
        self.store = store_plugin
        # pylint: enable=attribute-defined-outside-init
        store_plugin.finalize_init(config, cfgparser, context=self)

    def finalize_init(self, config, cfgparser, context=None):
        """Two-phase initialization

        Args:
            config: server config dictionary
            cfgparser: configparser instance
            context: initialization context (None for global)
        """
        if getattr(self, 'store_name', None) is not None:
            self._attach_store(config, cfgparser, context)


class CSStore(CustodiaPlugin):
    """Base class for stores
    """
    @abc.abstractmethod
    def get(self, key):
        pass

    @abc.abstractmethod
    def set(self, key, value, replace=False):
        pass

    # relax ABC for now, see https://github.com/latchset/custodia/issues/84

    # @abc.abstractmethod
    def span(self, key):
        raise NotImplementedError

    # @abc.abstractmethod
    def list(self, keyfilter=None):
        raise NotImplementedError

    # @abc.abstractmethod
    def cut(self, key):
        raise NotImplementedError


class HTTPAuthorizer(CustodiaPlugin):
    """Base class for authorizers
    """
    @abc.abstractmethod
    def handle(self, request):
        pass


class HTTPAuthenticator(CustodiaPlugin):
    """Base class for authenticators
    """
    @abc.abstractmethod
    def handle(self, request):
        pass


DEFAULT_CTYPE = 'text/html; charset=utf-8'
SUPPORTED_COMMANDS = ['GET', 'PUT', 'POST', 'DELETE']


class HTTPConsumer(CustodiaPlugin):
    """Base class for consumers
    """
    def __init__(self, config, section=None):
        super(HTTPConsumer, self).__init__(config, section)
        self.subs = dict()
        self.root = self

    def add_sub(self, name, sub):
        self.subs[name] = sub
        if hasattr(sub, 'root'):
            sub.root = self.root

    def _find_handler(self, request):
        base = self
        command = request.get('command', 'GET')
        if command not in SUPPORTED_COMMANDS:
            raise HTTPError(501)
        trail = request.get('trail', None)
        if trail is not None:
            for comp in trail:
                subs = getattr(base, 'subs', {})
                if comp in subs:
                    base = subs[comp]
                    trail.pop(0)
                else:
                    break

        handler = getattr(base, command)
        if handler is None:
            raise HTTPError(400)

        return handler

    def handle(self, request):
        handler = self._find_handler(request)
        response = {'headers': dict()}

        # Handle request
        output = handler(request, response)
        if output is None:
            output = response.get('output')

        ct = response['headers'].get('Content-Type')
        if ct is None:
            ct = response['headers']['Content-Type'] = DEFAULT_CTYPE

        if 'application/json' in ct and isinstance(output, (dict, list)):
            output = json_encode(output).encode('utf-8')
            response['headers']['Content-Length'] = str(len(output))

        response['output'] = output

        if output is not None and not hasattr(output, 'read') \
                and not isinstance(output, six.binary_type):
            msg = "Handler {} returned unsupported type {} ({}):\n{!r}"
            raise TypeError(msg.format(handler, type(output), ct, output))

        if output is not None and 'Content-Length' not in response['headers']:
            if hasattr(output, 'read'):
                # LOG: warning file-type objects should set Content-Length
                pass
            else:
                response['headers']['Content-Length'] = str(len(output))

        return response