Skip to content

Commit

Permalink
Revert "[EIS] Validate EIS Gateway URL if set (elastic#114600)"
Browse files Browse the repository at this point in the history
This reverts commit 39168e1.
  • Loading branch information
mark-vieira committed Oct 15, 2024
1 parent 837c0e8 commit 4cd2acd
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,14 @@

package org.elasticsearch.xpack.inference.services.elastic;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
* Class encapsulating any global setting for the EIS integration.
*/
public class ElasticInferenceServiceSettings {

public static final Setting<String> EIS_GATEWAY_URL = Setting.simpleString(
"xpack.inference.eis.gateway.url",
new EisGatewayURLValidator(),
Setting.Property.NodeScope
);

private static final Logger log = LogManager.getLogger(ElasticInferenceServiceSettings.class);

/**
* Class to validate the EIS Gateway url set via `xpack.inference.eis.gateway.url`.
*/
public static class EisGatewayURLValidator implements Setting.Validator<String> {

private static final Set<String> VALID_EIS_GATEWAY_SCHEMES = Set.of("http", "https");

@Override
public void validate(String value) {
if (Objects.isNull(value) || value.isEmpty()) {
// No validation needed, if eis-gateway URL is not set
log.debug("eis-gateway url not set. Skipping validation");
return;
}

try {
var uri = new URI(value);
var scheme = uri.getScheme();

if (scheme == null || VALID_EIS_GATEWAY_SCHEMES.contains(scheme) == false) {
throw new IllegalArgumentException(
"["
+ scheme
+ "] is not a valid URI scheme for the setting ["
+ ElasticInferenceServiceSettings.EIS_GATEWAY_URL.getKey()
+ "]. Use one of ["
+ String.join(",", VALID_EIS_GATEWAY_SCHEMES)
+ "]"
);
}
} catch (URISyntaxException e) {
throw new IllegalArgumentException("[" + e.getInput() + "] is not a valid URI", e);
}
}
}
static final Setting<String> EIS_GATEWAY_URL = Setting.simpleString("xpack.inference.eis.gateway.url", Setting.Property.NodeScope);

// Adjust this variable to be volatile, if the setting can be updated at some point in time
private final String eisGatewayUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ private URI createUri() throws URISyntaxException {
default -> throw new IllegalArgumentException("Unsupported model for EIS [" + modelId + "]");
}

var uriString = elasticInferenceServiceComponents().eisGatewayUrl() + "/sparse-text-embedding/" + modelIdUriPath;

// We perform the same validation here as when reading the setting to make sure that our extended URI is still valid
// This method throws, if the URI is invalid
new ElasticInferenceServiceSettings.EisGatewayURLValidator().validate(uriString);

return new URI(uriString);
return new URI(elasticInferenceServiceComponents().eisGatewayUrl() + "/sparse-text-embedding/" + modelIdUriPath);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,6 @@

public class ElasticInferenceServiceSparseEmbeddingsModelTests extends ESTestCase {

public void testCreateURI_ThrowError_OnMissingURIScheme() {
expectThrows(IllegalArgumentException.class, () -> createModel("www.missing-scheme-gateway-url.com"));
}

public void testCreateURI_ThrowError_OnWrongURIScheme() {
expectThrows(IllegalArgumentException.class, () -> createModel("file://www.missing-scheme-gateway-url.com"));
}

public void testCreateURI_DoesNotThrowError_ForHTTP() {
var scheme = "http";

try {
createModel(scheme + "://www.valid-gateway-url.com");
} catch (Exception e) {
fail(e, "Should not throw exception for " + "[" + scheme + "]");
}
}

public void testCreateURI_DoesNotThrowError_ForHTTPS() {
var scheme = "https";

try {
createModel(scheme + "://www.valid-gateway-url.com");
} catch (Exception e) {
fail(e, "Should not throw exception for " + "[" + scheme + "]");
}
}

public static ElasticInferenceServiceSparseEmbeddingsModel createModel(String url) {
return createModel(url, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ private ElasticInferenceService createServiceWithMockSender() {
return new ElasticInferenceService(
mock(HttpRequestSender.Factory.class),
createWithEmptySettings(threadPool),
new ElasticInferenceServiceComponents("http://valid-eis-gateway-url.com")
new ElasticInferenceServiceComponents(null)
);
}
}

0 comments on commit 4cd2acd

Please sign in to comment.