|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | +from ..exceptions import DashboardException |
| 8 | +from ..security import Scope |
| 9 | +from ..settings import Settings |
| 10 | +from ..tools import configure_cors |
| 11 | +from . import APIDoc, APIRouter, CreatePermission, Endpoint, EndpointDoc, \ |
| 12 | + ReadPermission, RESTController, UIRouter, UpdatePermission |
| 13 | + |
| 14 | + |
| 15 | +@APIRouter('/multi-cluster', Scope.CONFIG_OPT) |
| 16 | +@APIDoc('Multi-cluster Management API', 'Multi-cluster') |
| 17 | +class MultiCluster(RESTController): |
| 18 | + def _proxy(self, method, base_url, path, params=None, payload=None, verify=False, |
| 19 | + token=None): |
| 20 | + try: |
| 21 | + if token: |
| 22 | + headers = { |
| 23 | + 'Accept': 'application/vnd.ceph.api.v1.0+json', |
| 24 | + 'Authorization': 'Bearer ' + token, |
| 25 | + } |
| 26 | + else: |
| 27 | + headers = { |
| 28 | + 'Accept': 'application/vnd.ceph.api.v1.0+json', |
| 29 | + 'Content-Type': 'application/json', |
| 30 | + } |
| 31 | + response = requests.request(method, base_url + path, params=params, |
| 32 | + json=payload, verify=verify, headers=headers) |
| 33 | + except Exception as e: |
| 34 | + raise DashboardException( |
| 35 | + "Could not reach {}, {}".format(base_url+path, e), |
| 36 | + http_status_code=404, |
| 37 | + component='dashboard') |
| 38 | + |
| 39 | + try: |
| 40 | + content = json.loads(response.content, strict=False) |
| 41 | + except json.JSONDecodeError as e: |
| 42 | + raise DashboardException( |
| 43 | + "Error parsing Dashboard API response: {}".format(e.msg), |
| 44 | + component='dashboard') |
| 45 | + return content |
| 46 | + |
| 47 | + @Endpoint('POST') |
| 48 | + @CreatePermission |
| 49 | + @EndpointDoc("Authenticate to a remote cluster") |
| 50 | + def auth(self, url: str, cluster_alias: str, username=None, |
| 51 | + password=None, token=None, hub_url=None): |
| 52 | + |
| 53 | + multi_cluster_config = self.load_multi_cluster_config() |
| 54 | + |
| 55 | + if not url.endswith('/'): |
| 56 | + url = url + '/' |
| 57 | + |
| 58 | + if username and password: |
| 59 | + payload = { |
| 60 | + 'username': username, |
| 61 | + 'password': password |
| 62 | + } |
| 63 | + content = self._proxy('POST', url, 'api/auth', payload=payload) |
| 64 | + if 'token' not in content: |
| 65 | + raise DashboardException( |
| 66 | + "Could not authenticate to remote cluster", |
| 67 | + http_status_code=400, |
| 68 | + component='dashboard') |
| 69 | + |
| 70 | + token = content['token'] |
| 71 | + |
| 72 | + if token: |
| 73 | + self._proxy('PUT', url, 'ui-api/multi-cluster/set_cors_endpoint', |
| 74 | + payload={'url': hub_url}, token=token) |
| 75 | + fsid = self._proxy('GET', url, 'api/health/get_cluster_fsid', token=token) |
| 76 | + content = self._proxy('POST', url, 'api/auth/check', payload={'token': token}, |
| 77 | + token=token) |
| 78 | + if 'username' in content: |
| 79 | + username = content['username'] |
| 80 | + |
| 81 | + if 'config' not in multi_cluster_config: |
| 82 | + multi_cluster_config['config'] = {} |
| 83 | + |
| 84 | + if fsid in multi_cluster_config['config']: |
| 85 | + existing_entries = multi_cluster_config['config'][fsid] |
| 86 | + if not any(entry['user'] == username for entry in existing_entries): |
| 87 | + existing_entries.append({ |
| 88 | + "name": fsid, |
| 89 | + "url": url, |
| 90 | + "cluster_alias": cluster_alias, |
| 91 | + "user": username, |
| 92 | + "token": token, |
| 93 | + }) |
| 94 | + else: |
| 95 | + multi_cluster_config['current_user'] = username |
| 96 | + multi_cluster_config['config'][fsid] = [{ |
| 97 | + "name": fsid, |
| 98 | + "url": url, |
| 99 | + "cluster_alias": cluster_alias, |
| 100 | + "user": username, |
| 101 | + "token": token, |
| 102 | + }] |
| 103 | + |
| 104 | + Settings.MULTICLUSTER_CONFIG = multi_cluster_config |
| 105 | + |
| 106 | + def load_multi_cluster_config(self): |
| 107 | + if isinstance(Settings.MULTICLUSTER_CONFIG, str): |
| 108 | + try: |
| 109 | + itemw_to_dict = json.loads(Settings.MULTICLUSTER_CONFIG) |
| 110 | + except json.JSONDecodeError: |
| 111 | + itemw_to_dict = {} |
| 112 | + multi_cluster_config = itemw_to_dict.copy() |
| 113 | + else: |
| 114 | + multi_cluster_config = Settings.MULTICLUSTER_CONFIG.copy() |
| 115 | + |
| 116 | + return multi_cluster_config |
| 117 | + |
| 118 | + @Endpoint('PUT') |
| 119 | + @UpdatePermission |
| 120 | + def set_config(self, config: object): |
| 121 | + multicluster_config = self.load_multi_cluster_config() |
| 122 | + multicluster_config.update({'current_url': config['url']}) |
| 123 | + multicluster_config.update({'current_user': config['user']}) |
| 124 | + Settings.MULTICLUSTER_CONFIG = multicluster_config |
| 125 | + return Settings.MULTICLUSTER_CONFIG |
| 126 | + |
| 127 | + @Endpoint('POST') |
| 128 | + @CreatePermission |
| 129 | + # pylint: disable=R0911 |
| 130 | + def verify_connection(self, url: str, username=None, password=None, token=None): |
| 131 | + if not url.endswith('/'): |
| 132 | + url = url + '/' |
| 133 | + |
| 134 | + if token: |
| 135 | + try: |
| 136 | + payload = { |
| 137 | + 'token': token |
| 138 | + } |
| 139 | + content = self._proxy('POST', url, 'api/auth/check', payload=payload) |
| 140 | + if 'permissions' not in content: |
| 141 | + return content['detail'] |
| 142 | + user_content = self._proxy('GET', url, f'api/user/{username}', |
| 143 | + token=content['token']) |
| 144 | + if 'status' in user_content and user_content['status'] == '403 Forbidden': |
| 145 | + return 'User is not an administrator' |
| 146 | + except Exception as e: # pylint: disable=broad-except |
| 147 | + if '[Errno 111] Connection refused' in str(e): |
| 148 | + return 'Connection refused' |
| 149 | + return 'Connection failed' |
| 150 | + |
| 151 | + if username and password: |
| 152 | + try: |
| 153 | + payload = { |
| 154 | + 'username': username, |
| 155 | + 'password': password |
| 156 | + } |
| 157 | + content = self._proxy('POST', url, 'api/auth', payload=payload) |
| 158 | + if 'token' not in content: |
| 159 | + return content['detail'] |
| 160 | + user_content = self._proxy('GET', url, f'api/user/{username}', |
| 161 | + token=content['token']) |
| 162 | + if 'status' in user_content and user_content['status'] == '403 Forbidden': |
| 163 | + return 'User is not an administrator' |
| 164 | + except Exception as e: # pylint: disable=broad-except |
| 165 | + if '[Errno 111] Connection refused' in str(e): |
| 166 | + return 'Connection refused' |
| 167 | + return 'Connection failed' |
| 168 | + return 'Connection successful' |
| 169 | + |
| 170 | + @Endpoint() |
| 171 | + @ReadPermission |
| 172 | + def get_config(self): |
| 173 | + return Settings.MULTICLUSTER_CONFIG |
| 174 | + |
| 175 | + |
| 176 | +@UIRouter('/multi-cluster', Scope.CONFIG_OPT) |
| 177 | +class MultiClusterUi(RESTController): |
| 178 | + @Endpoint('PUT') |
| 179 | + @UpdatePermission |
| 180 | + def set_cors_endpoint(self, url: str): |
| 181 | + configure_cors(url) |
0 commit comments