[Fix-19900] Proxy Url Issue (#20574)

* Fix Proxy Url Issue

* Fix Failing Tests
This commit is contained in:
Mohit Yadav 2025-04-15 21:36:42 +05:30 committed by GitHub
parent fdf081f28c
commit 27cf2e0354
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 80 additions and 21 deletions

View File

@ -220,6 +220,17 @@ public class SettingsCache {
} }
} }
public static <T> T getSettingOrDefault(
SettingsType settingName, T defaultValue, Class<T> clazz) {
try {
String json = JsonUtils.pojoToJson(CACHE.get(settingName.toString()).getConfigValue());
return JsonUtils.readValue(json, clazz);
} catch (Exception ex) {
LOG.error("Failed to fetch Settings . Setting {}", settingName, ex);
return defaultValue;
}
}
public static void cleanUp() { public static void cleanUp() {
CACHE.invalidateAll(); CACHE.invalidateAll();
initialized = false; initialized = false;

View File

@ -26,6 +26,7 @@ import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.jackson.Jackson; import io.dropwizard.jackson.Jackson;
import io.dropwizard.jersey.validation.Validators; import io.dropwizard.jersey.validation.Validators;
import java.io.File; import java.io.File;
import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -200,12 +201,13 @@ public class OpenMetadataOperations implements Callable<Integer> {
required = true) required = true)
String openMetadataUrl) { String openMetadataUrl) {
try { try {
URI uri = URI.create(openMetadataUrl);
parseConfig(); parseConfig();
Settings updatedSettings = Settings updatedSettings =
new Settings() new Settings()
.withConfigType(SettingsType.OPEN_METADATA_BASE_URL_CONFIGURATION) .withConfigType(SettingsType.OPEN_METADATA_BASE_URL_CONFIGURATION)
.withConfigValue( .withConfigValue(
new OpenMetadataBaseUrlConfiguration().withOpenMetadataUrl(openMetadataUrl)); new OpenMetadataBaseUrlConfiguration().withOpenMetadataUrl(uri.toString()));
Entity.getSystemRepository().createOrUpdate(updatedSettings); Entity.getSystemRepository().createOrUpdate(updatedSettings);
LOG.info("Updated OpenMetadata URL to: {}", openMetadataUrl); LOG.info("Updated OpenMetadata URL to: {}", openMetadataUrl);

View File

@ -38,8 +38,11 @@ import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import lombok.Getter; import lombok.Getter;
import org.openmetadata.common.utils.CommonUtil; import org.openmetadata.common.utils.CommonUtil;
import org.openmetadata.schema.api.configuration.OpenMetadataBaseUrlConfiguration;
import org.openmetadata.schema.settings.SettingsType;
import org.openmetadata.schema.type.ChangeEvent; import org.openmetadata.schema.type.ChangeEvent;
import org.openmetadata.schema.type.EventType; import org.openmetadata.schema.type.EventType;
import org.openmetadata.service.resources.settings.SettingsCache;
public final class RestUtil { public final class RestUtil {
public static final String CHANGE_CUSTOM_HEADER = "X-OpenMetadata-Change"; public static final String CHANGE_CUSTOM_HEADER = "X-OpenMetadata-Change";
@ -66,8 +69,13 @@ public final class RestUtil {
public static URI getHref(UriInfo uriInfo, String collectionPath) { public static URI getHref(UriInfo uriInfo, String collectionPath) {
collectionPath = removeSlashes(collectionPath); collectionPath = removeSlashes(collectionPath);
String uriPath = uriInfo.getBaseUri() + collectionPath; OpenMetadataBaseUrlConfiguration urlConfiguration =
return URI.create(uriPath); SettingsCache.getSettingOrDefault(
SettingsType.OPEN_METADATA_BASE_URL_CONFIGURATION,
new OpenMetadataBaseUrlConfiguration()
.withOpenMetadataUrl(uriInfo.getBaseUri().toString()),
OpenMetadataBaseUrlConfiguration.class);
return URI.create(urlConfiguration.getOpenMetadataUrl() + "/" + collectionPath);
} }
public static URI getHref(URI parent, String child) { public static URI getHref(URI parent, String child) {

View File

@ -22,11 +22,20 @@ import javax.ws.rs.core.UriInfo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.openmetadata.schema.api.configuration.OpenMetadataBaseUrlConfiguration;
import org.openmetadata.schema.settings.SettingsType;
import org.openmetadata.service.OpenMetadataApplicationTest;
import org.openmetadata.service.resources.settings.SettingsCache;
@Slf4j @Slf4j
class RestUtilTest { class RestUtilTest extends OpenMetadataApplicationTest {
@Test @Test
void hrefTests() throws URISyntaxException { void hrefTests() throws URISyntaxException {
OpenMetadataBaseUrlConfiguration urlConfiguration =
SettingsCache.getSetting(
SettingsType.OPEN_METADATA_BASE_URL_CONFIGURATION,
OpenMetadataBaseUrlConfiguration.class);
URI baseUri = URI.create("http://base"); URI baseUri = URI.create("http://base");
assertEquals(URI.create("http://base/path"), RestUtil.getHref(baseUri, "path")); assertEquals(URI.create("http://base/path"), RestUtil.getHref(baseUri, "path"));
assertEquals( assertEquals(
@ -37,45 +46,74 @@ class RestUtilTest {
assertEquals( assertEquals(
URI.create("http://base/path"), RestUtil.getHref(baseUri, "/path/")); // Remove both slashes URI.create("http://base/path"), RestUtil.getHref(baseUri, "/path/")); // Remove both slashes
UriInfo uriInfo = mockUriInfo("http://base/"); UriInfo uriInfo = mockUriInfo(urlConfiguration.getOpenMetadataUrl());
assertEquals(URI.create("http://base/collection"), RestUtil.getHref(uriInfo, "collection")); assertEquals(
assertEquals(URI.create("http://base/collection"), RestUtil.getHref(uriInfo, "/collection")); URI.create(String.format("%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection")),
assertEquals(URI.create("http://base/collection"), RestUtil.getHref(uriInfo, "collection/")); RestUtil.getHref(uriInfo, "collection"));
assertEquals(URI.create("http://base/collection"), RestUtil.getHref(uriInfo, "/collection/")); assertEquals(
URI.create(String.format("%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection")),
RestUtil.getHref(uriInfo, "/collection"));
assertEquals(
URI.create(String.format("%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection")),
RestUtil.getHref(uriInfo, "collection/"));
assertEquals(
URI.create(String.format("%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection")),
RestUtil.getHref(uriInfo, "/collection/"));
UUID id = UUID.randomUUID(); UUID id = UUID.randomUUID();
assertEquals( assertEquals(
URI.create("http://base/collection/" + id), RestUtil.getHref(uriInfo, "collection", id)); URI.create(
String.format("%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", id)),
RestUtil.getHref(uriInfo, "collection", id));
assertEquals( assertEquals(
URI.create("http://base/collection/" + id), RestUtil.getHref(uriInfo, "/collection", id)); URI.create(
String.format("%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", id)),
RestUtil.getHref(uriInfo, "/collection", id));
assertEquals( assertEquals(
URI.create("http://base/collection/" + id), RestUtil.getHref(uriInfo, "collection/", id)); URI.create(
String.format("%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", id)),
RestUtil.getHref(uriInfo, "collection/", id));
assertEquals( assertEquals(
URI.create("http://base/collection/" + id), RestUtil.getHref(uriInfo, "/collection/", id)); URI.create(
String.format("%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", id)),
RestUtil.getHref(uriInfo, "/collection/", id));
assertEquals( assertEquals(
URI.create("http://base/collection/path"), RestUtil.getHref(uriInfo, "collection", "path")); URI.create(
String.format("%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", "path")),
RestUtil.getHref(uriInfo, "collection", "path"));
assertEquals( assertEquals(
URI.create("http://base/collection/path"), URI.create(
String.format("%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", "path")),
RestUtil.getHref(uriInfo, "/collection", "/path")); RestUtil.getHref(uriInfo, "/collection", "/path"));
assertEquals( assertEquals(
URI.create("http://base/collection/path"), URI.create(
String.format("%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", "path")),
RestUtil.getHref(uriInfo, "collection/", "path/")); RestUtil.getHref(uriInfo, "collection/", "path/"));
assertEquals( assertEquals(
URI.create("http://base/collection/path"), URI.create(
String.format("%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", "path")),
RestUtil.getHref(uriInfo, "/collection/", "/path/")); RestUtil.getHref(uriInfo, "/collection/", "/path/"));
assertEquals( assertEquals(
URI.create("http://base/collection/path%201"), URI.create(
String.format(
"%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", "path%201")),
RestUtil.getHref(uriInfo, "collection", "path 1")); RestUtil.getHref(uriInfo, "collection", "path 1"));
assertEquals( assertEquals(
URI.create("http://base/collection/path%201"), URI.create(
String.format(
"%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", "path%201")),
RestUtil.getHref(uriInfo, "/collection", "/path 1")); RestUtil.getHref(uriInfo, "/collection", "/path 1"));
assertEquals( assertEquals(
URI.create("http://base/collection/path%201"), URI.create(
String.format(
"%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", "path%201")),
RestUtil.getHref(uriInfo, "collection/", "path 1/")); RestUtil.getHref(uriInfo, "collection/", "path 1/"));
assertEquals( assertEquals(
URI.create("http://base/collection/path%201"), URI.create(
String.format(
"%s/%s/%s", urlConfiguration.getOpenMetadataUrl(), "collection", "path%201")),
RestUtil.getHref(uriInfo, "/collection/", "/path 1/")); RestUtil.getHref(uriInfo, "/collection/", "/path 1/"));
} }