From 4442c161c68e3f32370f5c219138a86b0597775b Mon Sep 17 00:00:00 2001 From: Krzysztof Chmielewski Date: Sun, 1 Sep 2024 22:08:24 +0200 Subject: [PATCH] HTTP-119 - Fix bug where default Java TrustStore was not used when no cert-related properties were used. Signed-off-by: Krzysztof Chmielewski --- .../utils/JavaNetHttpClientFactory.java | 24 ++++++++++-- .../ElasticSearchLiteQueryCreatorTest.java | 18 +++++++++ .../GenericJsonQueryCreatorTest.java | 17 +++++++++ .../QueryFormatAwareConfigurationTest.java | 38 +++++++++++++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/QueryFormatAwareConfigurationTest.java diff --git a/src/main/java/com/getindata/connectors/http/internal/utils/JavaNetHttpClientFactory.java b/src/main/java/com/getindata/connectors/http/internal/utils/JavaNetHttpClientFactory.java index 4ffaf4fb..a344897c 100644 --- a/src/main/java/com/getindata/connectors/http/internal/utils/JavaNetHttpClientFactory.java +++ b/src/main/java/com/getindata/connectors/http/internal/utils/JavaNetHttpClientFactory.java @@ -2,6 +2,7 @@ import java.net.http.HttpClient; import java.net.http.HttpClient.Redirect; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -24,8 +25,8 @@ public class JavaNetHttpClientFactory { /** - * Creates Java's {@link HttpClient} instance that will be using default, JVM shared {@link - * java.util.concurrent.ForkJoinPool} for async calls. + * Creates Java's {@link HttpClient} instance that will be using default, JVM shared + * {@link java.util.concurrent.ForkJoinPool} for async calls. * * @param properties properties used to build {@link SSLContext} * @return new {@link HttpClient} instance. @@ -73,7 +74,9 @@ public static HttpClient createClient(Properties properties, Executor executor) * @return new {@link SSLContext} instance. */ private static SSLContext getSslContext(Properties properties) { - SecurityContext securityContext = createSecurityContext(properties); + + String keyStorePath = + properties.getProperty(HttpConnectorConfigConstants.KEY_STORE_PATH, ""); boolean selfSignedCert = Boolean.parseBoolean( properties.getProperty(HttpConnectorConfigConstants.ALLOW_SELF_SIGNED, "false")); @@ -88,6 +91,21 @@ private static SSLContext getSslContext(Properties properties) { String clientPrivateKey = properties .getProperty(HttpConnectorConfigConstants.CLIENT_PRIVATE_KEY, ""); + if (StringUtils.isNullOrWhitespaceOnly(keyStorePath) + && !selfSignedCert + && serverTrustedCerts.length == 0 + && StringUtils.isNullOrWhitespaceOnly(clientCert) + && StringUtils.isNullOrWhitespaceOnly(clientPrivateKey)) { + + try { + return SSLContext.getDefault(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + + SecurityContext securityContext = createSecurityContext(properties); + for (String cert : serverTrustedCerts) { if (!StringUtils.isNullOrWhitespaceOnly(cert)) { securityContext.addCertToTrustStore(cert); diff --git a/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/ElasticSearchLiteQueryCreatorTest.java b/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/ElasticSearchLiteQueryCreatorTest.java index 37c15a74..1b70ec66 100644 --- a/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/ElasticSearchLiteQueryCreatorTest.java +++ b/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/ElasticSearchLiteQueryCreatorTest.java @@ -18,6 +18,24 @@ public class ElasticSearchLiteQueryCreatorTest { + @Test + public void testWithEmptyLookup() { + + // GIVEN + LookupRow lookupRow = new LookupRow(); + lookupRow.setLookupPhysicalRowDataType(DataTypes.STRING()); + + GenericRowData lookupDataRow = GenericRowData.of(StringData.fromString("val1")); + + // WHEN + var queryCreator = new ElasticSearchLiteQueryCreator(lookupRow); + var createdQuery = queryCreator.createLookupQuery(lookupDataRow); + + // THEN + assertThat(createdQuery.getLookupQuery()).isEqualTo(""); + assertThat(createdQuery.getBodyBasedUrlQueryParameters()).isEmpty(); + } + @Test public void testQueryCreationForSingleQueryStringParam() { diff --git a/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/GenericJsonQueryCreatorTest.java b/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/GenericJsonQueryCreatorTest.java index 3fcfe00d..bb951926 100644 --- a/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/GenericJsonQueryCreatorTest.java +++ b/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/GenericJsonQueryCreatorTest.java @@ -52,6 +52,23 @@ public void shouldSerializeToJson() { row.setField(0, 11); row.setField(1, StringData.fromString("myUuid")); + this.jsonQueryCreator.createLookupQuery(row); + LookupQueryInfo lookupQuery = this.jsonQueryCreator.createLookupQuery(row); + assertThat(lookupQuery.getBodyBasedUrlQueryParameters().isEmpty()); + assertThat(lookupQuery.getLookupQuery()).isEqualTo("{\"id\":11,\"uuid\":\"myUuid\"}"); + } + + @Test + public void shouldSerializeToJsonTwice() { + GenericRowData row = new GenericRowData(2); + row.setField(0, 11); + row.setField(1, StringData.fromString("myUuid")); + + this.jsonQueryCreator.createLookupQuery(row); + + // Call createLookupQuery two times + // to check that serialization schema is not opened Two times. + this.jsonQueryCreator.createLookupQuery(row); LookupQueryInfo lookupQuery = this.jsonQueryCreator.createLookupQuery(row); assertThat(lookupQuery.getBodyBasedUrlQueryParameters().isEmpty()); assertThat(lookupQuery.getLookupQuery()).isEqualTo("{\"id\":11,\"uuid\":\"myUuid\"}"); diff --git a/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/QueryFormatAwareConfigurationTest.java b/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/QueryFormatAwareConfigurationTest.java new file mode 100644 index 00000000..9daaecb5 --- /dev/null +++ b/src/test/java/com/getindata/connectors/http/internal/table/lookup/querycreators/QueryFormatAwareConfigurationTest.java @@ -0,0 +1,38 @@ +package com.getindata.connectors.http.internal.table.lookup.querycreators; + +import java.util.Collections; +import java.util.Optional; + +import org.apache.flink.configuration.ConfigOption; +import org.apache.flink.configuration.ConfigOptions; +import org.apache.flink.configuration.Configuration; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; + +class QueryFormatAwareConfigurationTest { + + private static final ConfigOption configOption = ConfigOptions.key("key") + .stringType() + .noDefaultValue(); + + @Test + public void testWithDot() { + QueryFormatAwareConfiguration queryConfig = new QueryFormatAwareConfiguration( + "prefix.", Configuration.fromMap(Collections.singletonMap("prefix.key", "val")) + ); + + Optional optional = queryConfig.getOptional(configOption); + assertThat(optional.get()).isEqualTo("val"); + } + + @Test + public void testWithoutDot() { + QueryFormatAwareConfiguration queryConfig = new QueryFormatAwareConfiguration( + "prefix", Configuration.fromMap(Collections.singletonMap("prefix.key", "val")) + ); + + Optional optional = queryConfig.getOptional(configOption); + assertThat(optional.get()).isEqualTo("val"); + } + +}