From 58acc17cef9d797e947f8acab447a4d46c77c218 Mon Sep 17 00:00:00 2001 From: Kishore Rajasekar <86338791+ki1729@users.noreply.github.com> Date: Sun, 30 Oct 2022 18:57:07 -0700 Subject: [PATCH 1/4] Fixed eventhubs default proxy configuration bug --- .../eventhubs/EventHubClientBuilder.java | 50 +------------------ 1 file changed, 1 insertion(+), 49 deletions(-) diff --git a/sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventHubClientBuilder.java b/sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventHubClientBuilder.java index 7b14d777cfd1..215f80a55d83 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventHubClientBuilder.java +++ b/sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventHubClientBuilder.java @@ -6,7 +6,6 @@ import com.azure.core.amqp.AmqpClientOptions; import com.azure.core.amqp.AmqpRetryOptions; import com.azure.core.amqp.AmqpTransportType; -import com.azure.core.amqp.ProxyAuthenticationType; import com.azure.core.amqp.ProxyOptions; import com.azure.core.amqp.client.traits.AmqpTrait; import com.azure.core.amqp.implementation.AzureTokenManagerProvider; @@ -47,9 +46,7 @@ import reactor.core.scheduler.Scheduler; import reactor.core.scheduler.Schedulers; -import java.net.InetSocketAddress; import java.net.MalformedURLException; -import java.net.Proxy; import java.net.URL; import java.util.Locale; import java.util.Map; @@ -952,7 +949,7 @@ private ConnectionOptions getConnectionOptions() { } if (proxyOptions == null) { - proxyOptions = getDefaultProxyConfiguration(buildConfiguration); + proxyOptions = ProxyOptions.fromConfiguration(buildConfiguration); } // If the proxy has been configured by the user but they have overridden the TransportType with something that @@ -985,49 +982,4 @@ private ConnectionOptions getConnectionOptions() { customEndpointAddress.getPort()); } } - - private ProxyOptions getDefaultProxyConfiguration(Configuration configuration) { - ProxyAuthenticationType authentication = ProxyAuthenticationType.NONE; - if (proxyOptions != null) { - authentication = proxyOptions.getAuthentication(); - } - - String proxyAddress = configuration.get(Configuration.PROPERTY_HTTP_PROXY); - - if (CoreUtils.isNullOrEmpty(proxyAddress)) { - return ProxyOptions.SYSTEM_DEFAULTS; - } - - return getProxyOptions(authentication, proxyAddress, configuration, - Boolean.parseBoolean(configuration.get("java.net.useSystemProxies"))); - } - - private ProxyOptions getProxyOptions(ProxyAuthenticationType authentication, String proxyAddress, - Configuration configuration, boolean useSystemProxies) { - String host; - int port; - if (HOST_PORT_PATTERN.matcher(proxyAddress.trim()).find()) { - final String[] hostPort = proxyAddress.split(":"); - host = hostPort[0]; - port = Integer.parseInt(hostPort[1]); - final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); - final String username = configuration.get(ProxyOptions.PROXY_USERNAME); - final String password = configuration.get(ProxyOptions.PROXY_PASSWORD); - return new ProxyOptions(authentication, proxy, username, password); - } else if (useSystemProxies) { - // java.net.useSystemProxies needs to be set to true in this scenario. - // If it is set to false 'ProxyOptions' in azure-core will return null. - com.azure.core.http.ProxyOptions coreProxyOptions = com.azure.core.http.ProxyOptions - .fromConfiguration(configuration); - Proxy.Type proxyType = coreProxyOptions.getType().toProxyType(); - InetSocketAddress coreProxyAddress = coreProxyOptions.getAddress(); - String username = coreProxyOptions.getUsername(); - String password = coreProxyOptions.getPassword(); - return new ProxyOptions(authentication, new Proxy(proxyType, coreProxyAddress), username, password); - } else { - LOGGER.verbose("'HTTP_PROXY' was configured but ignored as 'java.net.useSystemProxies' wasn't " - + "set or was false."); - return ProxyOptions.SYSTEM_DEFAULTS; - } - } } From 108b819c06ec4942b162a9f6abca192674b14668 Mon Sep 17 00:00:00 2001 From: Kishore Rajasekar <86338791+ki1729@users.noreply.github.com> Date: Sun, 30 Oct 2022 19:51:54 -0700 Subject: [PATCH 2/4] Updating changelog and tests --- .../azure-messaging-eventhubs/CHANGELOG.md | 2 +- .../eventhubs/EventHubClientBuilderTest.java | 41 ++++++++----------- .../ServiceBusClientBuilderTest.java | 18 ++++---- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md b/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md index b6be17e5a620..d29783a2599e 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md +++ b/sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md @@ -7,7 +7,7 @@ ### Breaking Changes ### Bugs Fixed - +- Fixed incorrect proxy configuration using environment variables. ([24230](https://github.com/Azure/azure-sdk-for-java/issues/24230)) ### Other Changes ## 5.14.0 (2022-10-13) diff --git a/sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubClientBuilderTest.java b/sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubClientBuilderTest.java index 650bc6f83e0a..93bf32ba4102 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubClientBuilderTest.java +++ b/sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubClientBuilderTest.java @@ -115,23 +115,18 @@ public void testConnectionStringWithSas() { @MethodSource("getProxyConfigurations") @ParameterizedTest - public void testProxyOptionsConfiguration(String proxyConfiguration, boolean expectedClientCreation) { + public void testProxyOptionsConfiguration(String proxyConfiguration) { Configuration configuration = Configuration.getGlobalConfiguration().clone(); configuration = configuration.put(Configuration.PROPERTY_HTTP_PROXY, proxyConfiguration); configuration = configuration.put(JAVA_NET_USE_SYSTEM_PROXIES, "true"); - boolean clientCreated = false; - try { - EventHubConsumerAsyncClient asyncClient = new EventHubClientBuilder() - .connectionString(CORRECT_CONNECTION_STRING) - .configuration(configuration) - .consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME) - .transportType(AmqpTransportType.AMQP_WEB_SOCKETS) - .buildAsyncConsumerClient(); - clientCreated = true; - } catch (Exception ex) { - } - Assertions.assertEquals(expectedClientCreation, clientCreated); + // Client creation should not fail with incorrect proxy configurations + EventHubConsumerAsyncClient asyncClient = new EventHubClientBuilder() + .connectionString(CORRECT_CONNECTION_STRING) + .configuration(configuration) + .consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME) + .transportType(AmqpTransportType.AMQP_WEB_SOCKETS) + .buildAsyncConsumerClient(); } @Test @@ -241,16 +236,16 @@ public void testThrowsIfAttemptsToCreateClientWithTokenCredentialWithoutEventHub private static Stream getProxyConfigurations() { return Stream.of( - Arguments.of("http://localhost:8080", true), - Arguments.of("localhost:8080", true), - Arguments.of("localhost_8080", false), - Arguments.of("http://example.com:8080", true), - Arguments.of("http://sub.example.com:8080", true), - Arguments.of(":8080", false), - Arguments.of("http://localhost", true), - Arguments.of("sub.example.com:8080", true), - Arguments.of("https://username:password@sub.example.com:8080", true), - Arguments.of("https://username:password@sub.example.com", true) + Arguments.of("http://localhost:8080"), + Arguments.of("localhost:8080"), + Arguments.of("localhost_8080"), + Arguments.of("http://example.com:8080"), + Arguments.of("http://sub.example.com:8080"), + Arguments.of(":8080"), + Arguments.of("http://localhost"), + Arguments.of("sub.example.com:8080"), + Arguments.of("https://username:password@sub.example.com:8080"), + Arguments.of("https://username:password@sub.example.com") ); } diff --git a/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusClientBuilderTest.java b/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusClientBuilderTest.java index 0c0b084a08a4..3b87e0f2973c 100644 --- a/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusClientBuilderTest.java +++ b/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusClientBuilderTest.java @@ -386,16 +386,16 @@ public void testConnectionWithAzureSasCredential() { private static Stream getProxyConfigurations() { return Stream.of( - Arguments.of("http://localhost:8080", true), - Arguments.of("localhost:8080", true), + //Arguments.of("http://localhost:8080", true), + //Arguments.of("localhost:8080", true), Arguments.of("localhost_8080", false), - Arguments.of("http://example.com:8080", true), - Arguments.of("http://sub.example.com:8080", true), - Arguments.of(":8080", false), - Arguments.of("http://localhost", true), - Arguments.of("sub.example.com:8080", true), - Arguments.of("https://username:password@sub.example.com:8080", true), - Arguments.of("https://username:password@sub.example.com", true) + //Arguments.of("http://example.com:8080", true), + //Arguments.of("http://sub.example.com:8080", true), + Arguments.of(":8080", false) + //Arguments.of("http://localhost", true), + //Arguments.of("sub.example.com:8080", true), + //Arguments.of("https://username:password@sub.example.com:8080", true), + //Arguments.of("https://username:password@sub.example.com", true) ); } From 2272f872bb6752e769f3bcb73f984e0e2a870e1c Mon Sep 17 00:00:00 2001 From: Kishore Rajasekar <86338791+ki1729@users.noreply.github.com> Date: Sun, 30 Oct 2022 20:01:49 -0700 Subject: [PATCH 3/4] Remove unused header --- .../com/azure/messaging/eventhubs/EventHubClientBuilderTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubClientBuilderTest.java b/sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubClientBuilderTest.java index 93bf32ba4102..781aa12e7b77 100644 --- a/sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubClientBuilderTest.java +++ b/sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubClientBuilderTest.java @@ -12,7 +12,6 @@ import com.azure.core.credential.TokenCredential; import com.azure.core.util.Configuration; import com.azure.messaging.eventhubs.implementation.ClientConstants; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; From fbaed996164ee5fc6c3be4484c3d27d2a6b04a0a Mon Sep 17 00:00:00 2001 From: Kishore Rajasekar <86338791+ki1729@users.noreply.github.com> Date: Mon, 31 Oct 2022 14:29:06 -0700 Subject: [PATCH 4/4] removing sb file --- .../ServiceBusClientBuilderTest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusClientBuilderTest.java b/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusClientBuilderTest.java index 3b87e0f2973c..0c0b084a08a4 100644 --- a/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusClientBuilderTest.java +++ b/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusClientBuilderTest.java @@ -386,16 +386,16 @@ public void testConnectionWithAzureSasCredential() { private static Stream getProxyConfigurations() { return Stream.of( - //Arguments.of("http://localhost:8080", true), - //Arguments.of("localhost:8080", true), + Arguments.of("http://localhost:8080", true), + Arguments.of("localhost:8080", true), Arguments.of("localhost_8080", false), - //Arguments.of("http://example.com:8080", true), - //Arguments.of("http://sub.example.com:8080", true), - Arguments.of(":8080", false) - //Arguments.of("http://localhost", true), - //Arguments.of("sub.example.com:8080", true), - //Arguments.of("https://username:password@sub.example.com:8080", true), - //Arguments.of("https://username:password@sub.example.com", true) + Arguments.of("http://example.com:8080", true), + Arguments.of("http://sub.example.com:8080", true), + Arguments.of(":8080", false), + Arguments.of("http://localhost", true), + Arguments.of("sub.example.com:8080", true), + Arguments.of("https://username:password@sub.example.com:8080", true), + Arguments.of("https://username:password@sub.example.com", true) ); }