diff --git a/plugin/src/main/java/org/opensearch/sql/plugin/rest/RestPPLQueryAction.java b/plugin/src/main/java/org/opensearch/sql/plugin/rest/RestPPLQueryAction.java index 996ae8c700..30632f2a92 100644 --- a/plugin/src/main/java/org/opensearch/sql/plugin/rest/RestPPLQueryAction.java +++ b/plugin/src/main/java/org/opensearch/sql/plugin/rest/RestPPLQueryAction.java @@ -18,6 +18,7 @@ import java.util.function.Supplier; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.opensearch.OpenSearchSecurityException; import org.opensearch.client.node.NodeClient; import org.opensearch.core.action.ActionListener; import org.opensearch.core.rest.RestStatus; @@ -38,6 +39,7 @@ import org.opensearch.sql.plugin.transport.PPLQueryAction; import org.opensearch.sql.plugin.transport.TransportPPLQueryRequest; import org.opensearch.sql.plugin.transport.TransportPPLQueryResponse; +import org.opensearch.sql.prometheus.exceptions.PrometheusClientException; public class RestPPLQueryAction extends BaseRestHandler { public static final String QUERY_API_ENDPOINT = "/_plugins/_ppl"; @@ -134,6 +136,11 @@ public void onFailure(Exception e) { "Failed to explain the query due to error: " + e.getMessage()); } else if (e instanceof IllegalAccessException) { reportError(channel, e, BAD_REQUEST); + } else if (e instanceof PrometheusClientException) { + reportError(channel, e, BAD_REQUEST); + } else if (e instanceof OpenSearchSecurityException) { + OpenSearchSecurityException exception = (OpenSearchSecurityException) e; + reportError(channel, exception, exception.status()); } else { LOG.error("Error happened during query handling", e); if (isClientError(e)) { diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java index 2bfaaccd47..1e0935eefb 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClientImpl.java @@ -23,6 +23,7 @@ import org.apache.logging.log4j.Logger; import org.json.JSONArray; import org.json.JSONObject; +import org.opensearch.sql.prometheus.exceptions.PrometheusClientException; import org.opensearch.sql.prometheus.request.system.model.MetricMetadata; public class PrometheusClientImpl implements PrometheusClient { @@ -115,10 +116,10 @@ private JSONObject readResponse(Response response) throws IOException { if ("success".equals(jsonObject.getString("status"))) { return jsonObject; } else { - throw new RuntimeException(jsonObject.getString("error")); + throw new PrometheusClientException(jsonObject.getString("error")); } } else { - throw new RuntimeException( + throw new PrometheusClientException( String.format( "Request to Prometheus is Unsuccessful with : %s", Objects.requireNonNull(response.body(), "Response body can't be null").string())); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/exceptions/PrometheusClientException.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/exceptions/PrometheusClientException.java new file mode 100644 index 0000000000..4495849e6b --- /dev/null +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/exceptions/PrometheusClientException.java @@ -0,0 +1,14 @@ +/* + * + * * Copyright OpenSearch Contributors + * * SPDX-License-Identifier: Apache-2.0 + * + */ +package org.opensearch.sql.prometheus.exceptions; + +/** PrometheusClientException */ +public class PrometheusClientException extends RuntimeException { + public PrometheusClientException(String message) { + super(message); + } +} diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java index b6a4e3c49c..a5301b1d77 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/request/system/PrometheusDescribeMetricRequest.java @@ -27,6 +27,7 @@ import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.prometheus.client.PrometheusClient; +import org.opensearch.sql.prometheus.exceptions.PrometheusClientException; import org.opensearch.sql.prometheus.storage.PrometheusMetricDefaultSchema; /** @@ -80,7 +81,7 @@ public Map getFieldTypes() { "Error while fetching labels for {} from prometheus: {}", metricName, e.getMessage()); - throw new RuntimeException( + throw new PrometheusClientException( String.format( "Error while fetching labels " + "for %s from prometheus: %s", metricName, e.getMessage())); diff --git a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java index edae263ce3..9ed9fee221 100644 --- a/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java +++ b/prometheus/src/main/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactory.java @@ -99,6 +99,7 @@ private OkHttpClient getHttpClient(Map config) { OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder(); okHttpClient.callTimeout(1, TimeUnit.MINUTES); okHttpClient.connectTimeout(30, TimeUnit.SECONDS); + okHttpClient.followRedirects(false); if (config.get(AUTH_TYPE) != null) { AuthenticationType authenticationType = AuthenticationType.get(config.get(AUTH_TYPE)); if (AuthenticationType.BASICAUTH.equals(authenticationType)) { @@ -162,8 +163,8 @@ private void validateURI(Map config) throws URISyntaxException { if (!matcher.matches()) { throw new IllegalArgumentException( String.format( - "Disallowed hostname in the uri: %s. Validate with %s config", - config.get(URI), Settings.Key.DATASOURCES_URI_ALLOWHOSTS.getKeyValue())); + "Invalid hostname in the uri. Validate with %s config", + Settings.Key.DATASOURCES_URI_ALLOWHOSTS.getKeyValue())); } } } diff --git a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java index c2e8e5325a..7781b82b28 100644 --- a/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java +++ b/prometheus/src/test/java/org/opensearch/sql/prometheus/storage/PrometheusStorageFactoryTest.java @@ -269,7 +269,7 @@ void createDataSourceWithHostnameNotMatchingWithAllowHostsConfig() { exception .getMessage() .contains( - "Disallowed hostname in the uri: http://localhost.com:9090. " + "Invalid hostname in the uri. " + "Validate with plugins.query.datasources.uri.allowhosts config")); }