Oleksiy Petrenko | 95664c0 | 2018-04-19 17:05:16 +0300 | [diff] [blame] | 1 | import logging |
| 2 | import os_client_config |
| 3 | from uuid import UUID |
| 4 | try: |
| 5 | from urllib.parse import urlsplit |
| 6 | except ImportError: |
| 7 | from urlparse import urlsplit |
| 8 | |
| 9 | log = logging.getLogger(__name__) |
| 10 | |
| 11 | |
| 12 | class BarbicanException(Exception): |
| 13 | |
| 14 | _msg = "Barbican module exception occured." |
| 15 | |
| 16 | def __init__(self, message=None, **kwargs): |
| 17 | super(BarbicanException, self).__init__(message or self._msg) |
| 18 | |
| 19 | |
| 20 | class NoBarbicanEndpoint(BarbicanException): |
| 21 | _msg = "Barbican endpoint not found in keystone catalog." |
| 22 | |
| 23 | |
| 24 | class NoAuthPluginConfigured(BarbicanException): |
| 25 | _msg = ("You are using keystoneauth auth plugin that does not support " |
| 26 | "fetching endpoint list from token (noauth or admin_token).") |
| 27 | |
| 28 | |
| 29 | class NoCredentials(BarbicanException): |
| 30 | _msg = "Please provide cloud name present in clouds.yaml." |
| 31 | |
| 32 | |
| 33 | class ResourceNotFound(BarbicanException): |
| 34 | _msg = "Uniq resource: {resource} with name: {name} not found." |
| 35 | |
| 36 | def __init__(self, resource, name, **kwargs): |
| 37 | super(BarbicanException, self).__init__( |
| 38 | self._msg.format(resource=resource, name=name)) |
| 39 | |
| 40 | |
| 41 | class MultipleResourcesFound(BarbicanException): |
| 42 | _msg = "Multiple resource: {resource} with name: {name} found." |
| 43 | |
| 44 | def __init__(self, resource, name, **kwargs): |
| 45 | super(BarbicanException, self).__init__( |
| 46 | self._msg.format(resource=resource, name=name)) |
| 47 | |
| 48 | |
| 49 | def _get_raw_client(cloud_name): |
| 50 | service_type = 'key-manager' |
| 51 | adapter = os_client_config.make_rest_client(service_type, |
| 52 | cloud=cloud_name) |
| 53 | try: |
| 54 | access_info = adapter.session.auth.get_access(adapter.session) |
| 55 | endpoints = access_info.service_catalog.get_endpoints() |
| 56 | except (AttributeError, ValueError): |
| 57 | e = NoAuthPluginConfigured() |
| 58 | log.exception('%s' % e) |
| 59 | raise e |
| 60 | if service_type not in endpoints: |
| 61 | if not service_type: |
| 62 | e = NoBarbicanEndpoint() |
| 63 | log.error('%s' % e) |
| 64 | raise e |
| 65 | return adapter |
| 66 | |
| 67 | |
| 68 | def send(method): |
| 69 | def wrap(func): |
| 70 | def wrapped_f(*args, **kwargs): |
| 71 | cloud_name = kwargs.pop('cloud_name') |
| 72 | if not cloud_name: |
| 73 | e = NoCredentials() |
| 74 | log.error('%s' % e) |
| 75 | raise e |
| 76 | adapter = _get_raw_client(cloud_name) |
| 77 | # Remove salt internal kwargs |
| 78 | kwarg_keys = list(kwargs.keys()) |
| 79 | for k in kwarg_keys: |
| 80 | if k.startswith('__'): |
| 81 | kwargs.pop(k) |
| 82 | url, request_kwargs = func(*args, **kwargs) |
| 83 | response = getattr(adapter, method)(url, **request_kwargs) |
| 84 | if not response.content: |
| 85 | return {} |
| 86 | try: |
| 87 | resp = response.json() |
| 88 | except: |
| 89 | resp = response.content |
| 90 | return resp |
| 91 | return wrapped_f |
| 92 | return wrap |
| 93 | |
| 94 | |
| 95 | def _check_uuid(val): |
| 96 | try: |
| 97 | return str(UUID(val)).replace('-', '') == val |
| 98 | except (TypeError, ValueError, AttributeError): |
| 99 | return False |
| 100 | |
| 101 | |
| 102 | def _parse_secret_href(href): |
| 103 | return urlsplit(href).path.split('/')[-1] |
| 104 | |
| 105 | |
| 106 | def get_by_name_or_uuid(resource_list, resp_key): |
| 107 | def wrap(func): |
| 108 | def wrapped_f(*args, **kwargs): |
| 109 | if 'name' in kwargs: |
| 110 | ref = kwargs.pop('name', None) |
| 111 | start_arg = 0 |
| 112 | else: |
| 113 | start_arg = 1 |
| 114 | ref = args[0] |
| 115 | cloud_name = kwargs['cloud_name'] |
| 116 | if _check_uuid(ref): |
| 117 | uuid = ref |
| 118 | else: |
| 119 | # Then we have name not uuid |
| 120 | resp = resource_list( |
| 121 | name=ref, cloud_name=cloud_name)[resp_key] |
| 122 | if len(resp) == 0: |
| 123 | raise ResourceNotFound(resp_key, ref) |
| 124 | elif len(resp) > 1: |
| 125 | raise MultipleResourcesFound(resp_key, ref) |
| 126 | href = resp[0]['secret_ref'] |
| 127 | uuid = _parse_secret_href(href) |
| 128 | return func(uuid, *args[start_arg:], **kwargs) |
| 129 | return wrapped_f |
| 130 | return wrap |