|
| 1 | +# |
| 2 | +# Copyright 2023 Logical Clocks AB |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +from furl import furl |
| 18 | + |
| 19 | +from hsfs import client |
| 20 | +from hsfs.client.exceptions import FeatureStoreException |
| 21 | +from hsfs.core.variable_api import VariableApi |
| 22 | + |
| 23 | + |
| 24 | +class OPENSEARCH_CONFIG: |
| 25 | + ELASTIC_ENDPOINT_ENV_VAR = "ELASTIC_ENDPOINT" |
| 26 | + SSL_CONFIG = "es.net.ssl" |
| 27 | + NODES_WAN_ONLY = "es.nodes.wan.only" |
| 28 | + NODES = "es.nodes" |
| 29 | + SSL_KEYSTORE_LOCATION = "es.net.ssl.keystore.location" |
| 30 | + SSL_KEYSTORE_PASSWORD = "es.net.ssl.keystore.pass" |
| 31 | + SSL_TRUSTSTORE_LOCATION = "es.net.ssl.truststore.location" |
| 32 | + SSL_TRUSTSTORE_PASSWORD = "es.net.ssl.truststore.pass" |
| 33 | + HTTP_AUTHORIZATION = "es.net.http.header.Authorization" |
| 34 | + INDEX = "es.resource" |
| 35 | + HOSTS = "hosts" |
| 36 | + HTTP_COMPRESS = "http_compress" |
| 37 | + HEADERS = "headers" |
| 38 | + USE_SSL = "use_ssl" |
| 39 | + VERIFY_CERTS = "verify_certs" |
| 40 | + SSL_ASSERT_HOSTNAME = "ssl_assert_hostname" |
| 41 | + CA_CERTS = "ca_certs" |
| 42 | + |
| 43 | + |
| 44 | +class OpenSearchApi: |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + project_id, |
| 48 | + project_name, |
| 49 | + ): |
| 50 | + self._project_id = project_id |
| 51 | + self._project_name = project_name |
| 52 | + self._variable_api = VariableApi() |
| 53 | + |
| 54 | + def _get_opensearch_url(self): |
| 55 | + if isinstance(client.get_instance(), client.external.Client): |
| 56 | + external_domain = self._variable_api.get_loadbalancer_external_domain() |
| 57 | + if external_domain == "": |
| 58 | + raise FeatureStoreException( |
| 59 | + "External client could not locate loadbalancer_external_domain " |
| 60 | + "in cluster configuration or variable is empty." |
| 61 | + ) |
| 62 | + return f"https://{external_domain}:9200" |
| 63 | + else: |
| 64 | + service_discovery_domain = self._variable_api.get_service_discovery_domain() |
| 65 | + if service_discovery_domain == "": |
| 66 | + raise FeatureStoreException( |
| 67 | + "Client could not locate service_discovery_domain " |
| 68 | + "in cluster configuration or variable is empty." |
| 69 | + ) |
| 70 | + return f"https://rest.elastic.service.{service_discovery_domain}:9200" |
| 71 | + |
| 72 | + def get_project_index(self, index): |
| 73 | + """ |
| 74 | + This helper method prefixes the supplied index name with the project name to avoid index name clashes. |
| 75 | +
|
| 76 | + Args: |
| 77 | + :index: the opensearch index to interact with. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + A valid opensearch index name. |
| 81 | + """ |
| 82 | + return (self._project_name + "_" + index).lower() |
| 83 | + |
| 84 | + def get_default_py_config(self): |
| 85 | + """ |
| 86 | + Get the required opensearch configuration to setup a connection using the *opensearch-py* library. |
| 87 | +
|
| 88 | + ```python |
| 89 | +
|
| 90 | + import hopsworks |
| 91 | + from opensearchpy import OpenSearch |
| 92 | +
|
| 93 | + project = hopsworks.login() |
| 94 | +
|
| 95 | + opensearch_api = project.get_opensearch_api() |
| 96 | +
|
| 97 | + client = OpenSearch(**opensearch_api.get_default_py_config()) |
| 98 | +
|
| 99 | + ``` |
| 100 | + Returns: |
| 101 | + A dictionary with required configuration. |
| 102 | + """ |
| 103 | + url = furl(self._get_opensearch_url()) |
| 104 | + return { |
| 105 | + OPENSEARCH_CONFIG.HOSTS: [{"host": url.host, "port": url.port}], |
| 106 | + OPENSEARCH_CONFIG.HTTP_COMPRESS: False, |
| 107 | + OPENSEARCH_CONFIG.HEADERS: { |
| 108 | + "Authorization": self._get_authorization_token() |
| 109 | + }, |
| 110 | + OPENSEARCH_CONFIG.USE_SSL: True, |
| 111 | + OPENSEARCH_CONFIG.VERIFY_CERTS: True, |
| 112 | + OPENSEARCH_CONFIG.SSL_ASSERT_HOSTNAME: False, |
| 113 | + OPENSEARCH_CONFIG.CA_CERTS: client.get_instance()._get_ca_chain_path(), |
| 114 | + } |
| 115 | + |
| 116 | + def _get_authorization_token(self): |
| 117 | + """Get opensearch jwt token. |
| 118 | +
|
| 119 | + # Returns |
| 120 | + `str`: OpenSearch jwt token |
| 121 | + # Raises |
| 122 | + `RestAPIError`: If unable to get the token |
| 123 | + """ |
| 124 | + |
| 125 | + _client = client.get_instance() |
| 126 | + path_params = ["elastic", "jwt", self._project_id] |
| 127 | + |
| 128 | + headers = {"content-type": "application/json"} |
| 129 | + return _client._send_request("GET", path_params, headers=headers)["token"] |
0 commit comments