|
3 | 3 | import base64 |
4 | 4 | import json |
5 | 5 | import time |
| 6 | +from urllib.parse import urlparse |
6 | 7 |
|
7 | 8 | import requests |
8 | 9 |
|
9 | 10 | from .. import mgr |
10 | 11 | from ..exceptions import DashboardException |
11 | 12 | from ..security import Scope |
| 13 | +from ..services.orchestrator import OrchClient |
12 | 14 | from ..settings import Settings |
13 | 15 | from ..tools import configure_cors |
14 | 16 | from . import APIDoc, APIRouter, CreatePermission, DeletePermission, Endpoint, \ |
@@ -69,9 +71,11 @@ def auth(self, url: str, cluster_alias: str, username: str, |
69 | 71 | cluster_token = self.check_cluster_connection(url, payload, username, |
70 | 72 | ssl_verify, ssl_certificate) |
71 | 73 |
|
| 74 | + cors_endpoints_string = self.get_cors_endpoints_string(hub_url) |
| 75 | + |
72 | 76 | self._proxy('PUT', url, 'ui-api/multi-cluster/set_cors_endpoint', |
73 | | - payload={'url': hub_url}, token=cluster_token, verify=ssl_verify, |
74 | | - cert=ssl_certificate) |
| 77 | + payload={'url': cors_endpoints_string}, token=cluster_token, |
| 78 | + verify=ssl_verify, cert=ssl_certificate) |
75 | 79 |
|
76 | 80 | fsid = self._proxy('GET', url, 'api/health/get_cluster_fsid', token=cluster_token) |
77 | 81 |
|
@@ -101,6 +105,26 @@ def auth(self, url: str, cluster_alias: str, username: str, |
101 | 105 |
|
102 | 106 | return False |
103 | 107 |
|
| 108 | + def get_cors_endpoints_string(self, hub_url): |
| 109 | + parsed_url = urlparse(hub_url) |
| 110 | + hostname = parsed_url.hostname |
| 111 | + cors_endpoints_set = set() |
| 112 | + cors_endpoints_set.add(hub_url) |
| 113 | + |
| 114 | + orch = OrchClient.instance() |
| 115 | + inventory_hosts = [host.to_json() for host in orch.hosts.list()] |
| 116 | + |
| 117 | + for host in inventory_hosts: |
| 118 | + host_addr = host['addr'] |
| 119 | + host_ip_url = hub_url.replace(hostname, host_addr) |
| 120 | + host_hostname_url = hub_url.replace(hostname, host['hostname']) |
| 121 | + |
| 122 | + cors_endpoints_set.add(host_ip_url) |
| 123 | + cors_endpoints_set.add(host_hostname_url) |
| 124 | + |
| 125 | + cors_endpoints_string = ", ".join(cors_endpoints_set) |
| 126 | + return cors_endpoints_string |
| 127 | + |
104 | 128 | def check_cluster_connection(self, url, payload, username, ssl_verify, ssl_certificate): |
105 | 129 | try: |
106 | 130 | content = self._proxy('POST', url, 'api/auth', payload=payload, |
|
0 commit comments