Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,23 +114,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
Expand Down Expand Up @@ -241,16 +235,16 @@ public void testThrowsIfAttemptsToCreateClientWithTokenCredentialWithoutEventHub

private static Stream<Arguments> 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")
);
}

Expand Down