File: utils.py

package info (click to toggle)
python-os-brick 6.13.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,240 kB
  • sloc: python: 20,500; sh: 92; makefile: 23
file content (510 lines) | stat: -rw-r--r-- 17,898 bytes parent folder | download | duplicates (2)
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
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
"""Utilities and helper functions."""

from __future__ import annotations

import binascii
import functools
import inspect
import logging as py_logging
import os
import time
from typing import Any, Callable, Optional, Type, Union
import uuid as uuid_lib

from oslo_concurrency import processutils
from oslo_log import log as logging
from oslo_utils import strutils

from os_brick import executor
from os_brick.i18n import _
from os_brick.privileged import nvmeof as priv_nvme
from os_brick.privileged import rootwrap as priv_rootwrap


CUSTOM_LINK_PREFIX = '/dev/disk/by-id/os-brick'

_time_sleep = time.sleep


def _sleep(secs: float) -> None:
    """Helper class to make it easier to work around tenacity's sleep calls.

    Apparently we are all idiots for wanting to test our code here [0], so this
    is a hack to be able to get retries to not actually sleep.

    [0] https://github.com/jd/tenacity/issues/25
    """
    _time_sleep(secs)


time.sleep = _sleep

import tenacity  # noqa


LOG = logging.getLogger(__name__)


class retry_if_exit_code(tenacity.retry_if_exception):
    """Retry on ProcessExecutionError specific exit codes."""
    def __init__(self, codes: Union[int, tuple[int, ...]]):
        self.codes = (codes,) if isinstance(codes, int) else codes
        super(retry_if_exit_code, self).__init__(self._check_exit_code)

    def _check_exit_code(self, exc: Type[Exception]) -> bool:
        return (bool(exc) and
                isinstance(exc, processutils.ProcessExecutionError) and
                exc.exit_code in self.codes)


def retry(retry_param: Union[None,
                             Type[Exception],
                             tuple[Type[Exception], ...],
                             int,
                             tuple[int, ...]],
          interval: float = 1,
          retries: int = 3,
          backoff_rate: float = 2,
          retry: Callable = tenacity.retry_if_exception_type) -> Callable:

    if retries < 1:
        raise ValueError(_('Retries must be greater than or '
                         'equal to 1 (received: %s). ') % retries)

    def _decorator(f):

        @functools.wraps(f)
        def _wrapper(*args, **kwargs):
            r = tenacity.Retrying(
                before_sleep=tenacity.before_sleep_log(LOG, logging.DEBUG),
                after=tenacity.after_log(LOG, logging.DEBUG),
                stop=tenacity.stop_after_attempt(retries),
                reraise=True,
                retry=retry(retry_param),
                wait=tenacity.wait_exponential(
                    multiplier=interval, min=0, exp_base=backoff_rate))
            return r(f, *args, **kwargs)

        return _wrapper

    return _decorator


def platform_matches(current_platform: str, connector_platform: str) -> bool:
    curr_p = current_platform.upper()
    conn_p = connector_platform.upper()
    if conn_p == 'ALL':
        return True

    # Add tests against families of platforms
    if curr_p == conn_p:
        return True

    return False


def os_matches(current_os: str, connector_os: str) -> bool:
    curr_os = current_os.upper()
    conn_os = connector_os.upper()
    if conn_os == 'ALL':
        return True

    # add tests against OSs
    if (conn_os == curr_os or
       conn_os in curr_os):
        return True

    return False


def merge_dict(dict1: dict, dict2: dict) -> dict:
    """Try to safely merge 2 dictionaries."""
    if type(dict1) is not dict:
        raise Exception("dict1 is not a dictionary")
    if type(dict2) is not dict:
        raise Exception("dict2 is not a dictionary")

    dict3 = dict1.copy()
    dict3.update(dict2)
    return dict3


def trace(f: Callable) -> Callable:
    """Trace calls to the decorated function.

    This decorator should always be defined as the outermost decorator so it
    is defined last. This is important so it does not interfere
    with other decorators.

    Using this decorator on a function will cause its execution to be logged at
    `DEBUG` level with arguments, return values, and exceptions.

    :returns: a function decorator
    """

    func_name = f.__name__

    @functools.wraps(f)
    def trace_logging_wrapper(*args, **kwargs):
        if len(args) > 0:
            maybe_self = args[0]
        else:
            maybe_self = kwargs.get('self', None)

        if maybe_self and hasattr(maybe_self, '__module__'):
            logger = logging.getLogger(maybe_self.__module__)
        else:
            logger = LOG

        # NOTE(ameade): Don't bother going any further if DEBUG log level
        # is not enabled for the logger.
        if not logger.isEnabledFor(py_logging.DEBUG):
            return f(*args, **kwargs)

        all_args = inspect.getcallargs(f, *args, **kwargs)
        logger.debug('==> %(func)s: call %(all_args)r',
                     {'func': func_name,
                      # NOTE(mriedem): We have to stringify the dict first
                      # and don't use mask_dict_password because it results in
                      # an infinite recursion failure.
                      'all_args': strutils.mask_password(
                          str(all_args))})

        start_time = time.time() * 1000
        try:
            result = f(*args, **kwargs)
        except Exception as exc:
            total_time = int(round(time.time() * 1000)) - start_time
            logger.debug('<== %(func)s: exception (%(time)dms) %(exc)r',
                         {'func': func_name,
                          'time': total_time,
                          'exc': exc})
            raise
        total_time = int(round(time.time() * 1000)) - start_time

        if isinstance(result, dict):
            mask_result = strutils.mask_dict_password(result)
        elif isinstance(result, str):
            mask_result = strutils.mask_password(result)
        else:
            mask_result = result

        logger.debug('<== %(func)s: return (%(time)dms) %(result)r',
                     {'func': func_name,
                      'time': total_time,
                      'result': mask_result})
        return result
    return trace_logging_wrapper


def convert_str(text: Union[bytes, str]) -> str:
    """Convert to native string.

    Convert bytes and Unicode strings to native strings:

    * convert to Unicode on Python 3: decode bytes from UTF-8
    """
    if isinstance(text, bytes):
        return text.decode('utf-8')
    else:
        return text


def get_host_nqn(system_uuid: Optional[str] = None) -> Optional[str]:
    """Ensure that hostnqn exists, creating if necessary.

    This method tries to return contents from /etc/nvme/hostnqn and if not
    possible then creates the file calling create_hostnqn and passing provided
    system_uuid and returns the contents of the newly created file.

    Method create_hostnqn gives priority to the provided system_uuid parameter
    for the contents of the file over other alternatives it has.
    """
    try:
        with open('/etc/nvme/hostnqn', 'r') as f:
            host_nqn = f.read().strip()
    except IOError:
        host_nqn = priv_nvme.create_hostnqn(system_uuid)
    except Exception:
        host_nqn = None
    return host_nqn


def get_nvme_host_id(uuid: Optional[str]) -> Optional[str]:
    """Get the nvme host id

    If the hostid file doesn't exist create it either with the passed uuid or
    a random one.
    """
    try:
        with open('/etc/nvme/hostid', 'r') as f:
            host_id = f.read().strip()
    except IOError:
        uuid = uuid or str(uuid_lib.uuid4())
        host_id = priv_nvme.create_hostid(uuid)
    except Exception:
        host_id = None
    return host_id


def _symlink_name_from_device_path(device_path):
    """Generate symlink absolute path for encrypted devices.

    The symlink's basename will contain the original device name so we can
    reconstruct it afterwards on disconnect.

    Being able to restore the original device name may be important for some
    connectors, because the system may have multiple devices for the same
    connection information (for example if a controller came back to life after
    having network issues and an auto scan presented the device) and if we
    reuse an existing symlink created by udev we wouldn't know which one was
    actually used.

    The symlink will be created under the /dev/disk/by-id directory and will
    prefix the name with os-brick- and then continue with the full device path
    that was passed (replacing '/' with '+')
    """
    # Convert / into + that is unlikely used by devices or symlinks (cryptsetup
    # is not happy if we use ยท in the symlink)
    encoded_device = device_path.replace('/', '+')
    return CUSTOM_LINK_PREFIX + encoded_device


def _device_path_from_symlink(symlink):
    """Get the original encrypted device path from the device symlink.

    This is the reverse operation of the one performed by the
    _symlink_name_from_device_path method.
    """
    if (symlink and isinstance(symlink, str)
            and symlink.startswith(CUSTOM_LINK_PREFIX)):
        ending = symlink[len(CUSTOM_LINK_PREFIX):]
        return ending.replace('+', '/')
    return symlink


def connect_volume_prepare_result(
        func: Callable[[Any, dict], dict]) -> Callable[[Any, dict], dict]:
    """Decorator to prepare the result of connect_volume for encrypted volumes.

    WARNING: This decorator must be **before** any connect_volume locking
             because it may call disconnect_volume.

    Encryptor drivers expect a symlink that they "own", so that they can modify
    it as they want.

    The current flow is like this:

    - connect_volume connector call
    - libvirt config is generated by Nova using returned path
    - connect_volume encryptor call  => Replaces the original path

    For encrypted volumes the decorator modifies the "path" value for the
    returned dictionary.

    Unencrypted volumes will be left unchanged.

    There are special connectors that return a file descriptor instead of a
    path depending on the parameters.  In those cases the result will also be
    left untouched.

    If a connector relies on the path that has been used they can use the
    connect_volume_undo_prepare_result decorator to get the value changed back
    the original path.
    """
    @functools.wraps(func)
    def change_encrypted(self, connection_properties):
        res = func(self, connection_properties)
        # Decode if path is bytes, otherwise leave it as it is
        device_path = convert_str(res['path'])
        # There are connectors that sometimes return file descriptors (rbd)
        if (connection_properties.get('encrypted') and
                isinstance(device_path, str)):
            symlink = _symlink_name_from_device_path(device_path)
            try:
                priv_rootwrap.link_root(os.path.realpath(device_path),
                                        symlink,
                                        force=True)
                res['path'] = symlink
            except Exception as exc:
                LOG.debug('Failed to create symlink, cleaning connection: %s',
                          exc)
                self.disconnect_volume(res, force=True, ignore_errors=True)
                raise

        return res
    return change_encrypted


def get_dev_path(connection_properties, device_info):
    """Return the device that was returned when connecting a volume."""
    if device_info and device_info.get('path'):
        res = device_info['path']
    else:
        res = connection_properties.get('device_path') or ''

    # Decode if path is bytes, otherwise leave it as it is
    return convert_str(res)


def connect_volume_undo_prepare_result(
        f: Optional[Callable] = None,
        unlink_after: bool = False) -> Callable:
    """Decorator that returns the device path to how it was originally.

    WARNING: This decorator must be **the first** decorator of the method to
             get the actual method signature during introspection.

    Undo changes made to the device path of encrypted volumes done by the
    connect_volume_prepare_result decorator.

    That way the connector will always get back the same device path that it
    returned.

    Examples of connector methods that may want to use this are
    disconnect_volume and extend_volume.

    It can optionally delete the symlink on successful completion, required for
    disconnect_volume method.

    @connect_volume_undo_prepare_result(unlink_after=True)
    def disconnect_volume(...):

    @connect_volume_undo_prepare_result
    def extend_volume(...):

    """
    def decorator(func):
        @functools.wraps(func)
        def change_encrypted(*args, **kwargs):
            # May receive only connection_properties or also device_info params
            call_args = inspect.getcallargs(func, *args, **kwargs)
            conn_props = call_args['connection_properties']

            custom_symlink = False
            if conn_props.get('encrypted'):
                dev_info = call_args.get('device_info')
                symlink = get_dev_path(conn_props, dev_info)
                devpath = _device_path_from_symlink(symlink)

                # Symlink can be a file descriptor, which we don't touch, same
                # for old symlinks where the path is the same
                if isinstance(symlink, str) and symlink != devpath:
                    custom_symlink = True
                    # Don't modify the caller's dictionaries
                    call_args['connection_properties'] = conn_props.copy()
                    call_args['connection_properties']['device_path'] = devpath

                    # Same for the device info dictionary
                    if dev_info:
                        dev_info = call_args['device_info'] = dev_info.copy()
                        dev_info['path'] = devpath

            res = func(**call_args)

            # Clean symlink only when asked (usually on disconnect)
            if custom_symlink and unlink_after:
                try:
                    priv_rootwrap.unlink_root(symlink)
                except Exception:
                    LOG.warning('Failed to remove encrypted custom symlink %s',
                                symlink)
            return res
        return change_encrypted

    if f:
        return decorator(f)
    return decorator


def get_device_size(executor: executor.Executor, device: str) -> Optional[int]:
    """Get the size in bytes of a volume."""
    (out, _err) = executor._execute('blockdev', '--getsize64',
                                    device, run_as_root=True,
                                    root_helper=executor._root_helper)
    var = str(out.strip())
    if var.isnumeric():
        return int(var)
    else:
        return None


def check_valid_device(executor: executor.Executor, path: str) -> bool:
    cmd = ('dd', 'if=%(path)s' % {"path": path},
           'of=/dev/null', 'count=1')
    out, info = None, None
    try:
        out, info = executor._execute(*cmd, run_as_root=True,
                                      root_helper=executor._root_helper)
    except processutils.ProcessExecutionError as e:
        LOG.error("Failed to access the device on the path "
                  "%(path)s: %(error)s.",
                  {"path": path, "error": e.stderr})
        return False
    # If the info is none, the path does not exist.
    return info is not None


def get_passphrase_from_secret(key) -> str:
    """Convert encryption key retrieved from the Key Manager into a passphrase.

    If the secret type is 'passphrase', assume that the key is already in
    a suitable string format and simply return it.
    In any other case, assume a binary key that needs to be converted into
    an ASCII representation using binascii.hexlify().

    Cinder uses 'symmetric' in conjunction with binascii.hexlify() to
    handle encryption keys for its own volumes and resulting volume images.
    Nova uses the 'passphrase' type instead for its qcow2+LUKS images which
    are directly passed to LUKS as passphrase input. User-defined Glance
    images may reference secrets of any type (defaulting to 'opaque') which
    we optimistically assume to represent binary keys too (unless their
    type is 'passphrase' explicitly).

    :param key: Key Manager Secret containing the encryption key
    :type key: castellan.common.objects.managed_object.ManagedObject
    :return: passphrase
    :rtype: str
    """
    if key.managed_type() == 'passphrase':
        LOG.debug(
            "os_brick.utils.get_passphrase_from_secret: the secret is of type "
            "passphrase and will be used without conversion"
        )
        return key.get_encoded().decode('utf-8')
    else:
        LOG.debug(
            "os_brick.utils.get_passphrase_from_secret: the secret is not of "
            "type passphrase and will be converted using hex representation"
        )
        return binascii.hexlify(key.get_encoded()).decode('utf-8')


class Anything(object):
    """Object equal to everything."""
    def __eq__(self, other):
        return True

    def __ne__(self, other):
        return False

    def __str__(self):
        return '<Anything>'

    __lt__ = __gt__ = __le__ = __ge__ = __ne__
    __repr__ = __str__


ANY = Anything()