From 2e3712357fc57d7cb36c8bbf727c61a9ab8a2718 Mon Sep 17 00:00:00 2001 From: Tadgh Date: Tue, 9 Feb 2021 18:59:11 -0500 Subject: [PATCH] Throw configueration error when somebody tries to put a protocol in the rest URL --- ...asticsearchHibernatePropertiesBuilder.java | 5 +- ...csearchHibernatePropertiesBuilderTest.java | 60 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/search/elastic/ElasticsearchHibernatePropertiesBuilderTest.java diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/elastic/ElasticsearchHibernatePropertiesBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/elastic/ElasticsearchHibernatePropertiesBuilder.java index 778ea971d47..c0c75adac98 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/elastic/ElasticsearchHibernatePropertiesBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/elastic/ElasticsearchHibernatePropertiesBuilder.java @@ -109,6 +109,9 @@ public class ElasticsearchHibernatePropertiesBuilder { } public ElasticsearchHibernatePropertiesBuilder setRestUrl(String theRestUrl) { + if (theRestUrl.contains("://")) { + throw new ConfigurationException("Elasticsearch URL cannot include a protocol, that is a separate property. Remove http:// or https:// from this URL."); + } myRestUrl = theRestUrl; return this; } @@ -144,7 +147,7 @@ public class ElasticsearchHibernatePropertiesBuilder { * TODO GGG HS: In HS6.1, we should have a native way of performing index settings manipulation at bootstrap time, so this should * eventually be removed in favour of whatever solution they come up with. */ - private void injectStartupTemplate(String theProtocol, String theHostAndPort, String theUsername, String thePassword) { + void injectStartupTemplate(String theProtocol, String theHostAndPort, String theUsername, String thePassword) { PutIndexTemplateRequest ngramTemplate = new PutIndexTemplateRequest("ngram-template") .patterns(Arrays.asList("resourcetable-*", "termconcept-*")) .settings(Settings.builder().put("index.max_ngram_diff", 50)); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/search/elastic/ElasticsearchHibernatePropertiesBuilderTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/search/elastic/ElasticsearchHibernatePropertiesBuilderTest.java new file mode 100644 index 00000000000..e99b73d632a --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/search/elastic/ElasticsearchHibernatePropertiesBuilderTest.java @@ -0,0 +1,60 @@ +package ca.uhn.fhir.jpa.search.elastic; + +import ca.uhn.fhir.context.ConfigurationException; +import org.hibernate.search.backend.elasticsearch.cfg.ElasticsearchBackendSettings; +import org.hibernate.search.engine.cfg.BackendSettings; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Properties; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.spy; + +@ExtendWith(MockitoExtension.class) +class ElasticsearchHibernatePropertiesBuilderTest { + + ElasticsearchHibernatePropertiesBuilder myPropertiesBuilder = spy(ElasticsearchHibernatePropertiesBuilder.class); + + @BeforeEach + public void prepMocks() { + //ensures we don't try to reach out to a real ES server on apply. + doNothing().when(myPropertiesBuilder).injectStartupTemplate(any(), any(), any(), any()); + } + + @Test + public void testRestUrlCannotContainProtocol() { + String host = "localhost:9200"; + String protocolHost = "http://" + host; + String failureMessage = "Elasticsearch URL cannot include a protocol, that is a separate property. Remove http:// or https:// from this URL."; + + myPropertiesBuilder + .setProtocol("http") + .setUsername("whatever") + .setPassword("whatever"); + + //SUT + try { + myPropertiesBuilder.setRestUrl(protocolHost); + fail(); + } catch (ConfigurationException e ) { + assertThat(e.getMessage(), is(equalTo(failureMessage))); + } + + Properties properties = new Properties(); + myPropertiesBuilder + .setRestUrl(host) + .apply(properties); + + assertThat(properties.getProperty(BackendSettings.backendKey(ElasticsearchBackendSettings.HOSTS)), is(equalTo(host))); + + } + +}