-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Adds support for proxies in Event Hubs #5621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e3a89a3
Add WebSocketsProxyConnectionHandler.
conniey c79d40e
Update ReactorHandlerProvider to connect proxy.
conniey 465fab5
Add Objects.requiresNotNull check.
conniey 702e33b
Integration web proxy handler in ReactorConnection.
conniey f919758
Fix API for MockReactorHandlerProvider.
conniey e5e4a5f
Adding tests for WebSocketsProxyConnectionHandler.
conniey 2389f8a
Use logger.throw
conniey 1e474f4
Adding tests for ReactorHandlerProvider.
conniey 0a9f868
Adding documentation to WebSocketsProxyConnectionHandler. Using Syste…
conniey cc6bbc0
Asserting UserAgent string is in connection properties.
conniey f6aee42
Adding simple proxy for integration tests.
conniey d245b86
Update IntegrationTestBase to getProxyConfiguration from configuration.
conniey b776efb
Add ProxyIntegrationTest.
conniey 41b8c1d
Add ProxyReceiveTest.
conniey b26dc64
Handling PROTON_IO errors.
conniey 972ef14
Add integration tests for proxy configuration.
conniey 5bdfef8
Cleaning up simple proxy.
conniey 170ffc1
Fix checkstyle issues
srnagar f3862bd
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
conniey abc5ce9
Fix import types in proxy connection handler.
conniey 6e43678
Ignore tests until simple proxy is fixed.
conniey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
...ain/java/com/azure/core/amqp/implementation/handler/WebSocketsProxyConnectionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.core.amqp.implementation.handler; | ||
|
|
||
| import com.azure.core.amqp.implementation.AmqpErrorCode; | ||
| import com.azure.core.amqp.models.ProxyConfiguration; | ||
| import com.azure.core.implementation.util.ImplUtils; | ||
| import com.azure.core.util.logging.ClientLogger; | ||
| import com.microsoft.azure.proton.transport.proxy.ProxyAuthenticationType; | ||
| import com.microsoft.azure.proton.transport.proxy.ProxyHandler; | ||
| import com.microsoft.azure.proton.transport.proxy.impl.ProxyHandlerImpl; | ||
| import com.microsoft.azure.proton.transport.proxy.impl.ProxyImpl; | ||
| import org.apache.qpid.proton.amqp.transport.ConnectionError; | ||
| import org.apache.qpid.proton.amqp.transport.ErrorCondition; | ||
| import org.apache.qpid.proton.engine.Connection; | ||
| import org.apache.qpid.proton.engine.Event; | ||
| import org.apache.qpid.proton.engine.Transport; | ||
| import org.apache.qpid.proton.engine.impl.TransportInternal; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.Proxy; | ||
| import java.net.ProxySelector; | ||
| import java.net.URI; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Creates an AMQP connection using web sockets (port 443) and connects through a proxy. | ||
| */ | ||
| public class WebSocketsProxyConnectionHandler extends WebSocketsConnectionHandler { | ||
| private static final String HTTPS_URI_FORMAT = "https://%s:%s"; | ||
| private static final String PROXY_SELECTOR_HAS_BEEN_MODIFIED = "ProxySelector has been modified."; | ||
|
|
||
| private final ClientLogger logger = new ClientLogger(WebSocketsProxyConnectionHandler.class); | ||
| private final String amqpHostname; | ||
| private final ProxyConfiguration proxyConfiguration; | ||
|
|
||
| /** | ||
| * Creates a handler that handles proton-j's connection through a proxy using web sockets. | ||
| * | ||
| * @param connectionId Identifier for this connection. | ||
| * @param amqpHostname Hostname of the AMQP message broker. The hostname of the proxy is exposed in {@link | ||
| * #getHostname()}. | ||
| * @throws NullPointerException if {@code amqpHostname} or {@code proxyConfiguration} is null. | ||
| */ | ||
| public WebSocketsProxyConnectionHandler(String connectionId, String amqpHostname, | ||
| ProxyConfiguration proxyConfiguration) { | ||
| super(connectionId, amqpHostname); | ||
| this.amqpHostname = Objects.requireNonNull(amqpHostname, "'amqpHostname' cannot be null."); | ||
| this.proxyConfiguration = Objects.requireNonNull(proxyConfiguration, "'proxyConfiguration' is required."); | ||
|
conniey marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Looks through system defined proxies to see if one should be used for connecting to the message broker. | ||
| * | ||
| * @param hostname Hostname for the AMQP message broker. | ||
| * | ||
| * @return {@code true} if a proxy should be used to connect to the AMQP message broker and null otherwise. | ||
| */ | ||
| public static boolean shouldUseProxy(final String hostname) { | ||
| Objects.requireNonNull(hostname, "'hostname' cannot be null."); | ||
|
|
||
| final URI uri = createURI(hostname, HTTPS_PORT); | ||
| final ProxySelector proxySelector = ProxySelector.getDefault(); | ||
| if (proxySelector == null) { | ||
| return false; | ||
| } | ||
|
|
||
| final List<Proxy> proxies = proxySelector.select(uri); | ||
| return isProxyAddressLegal(proxies); | ||
| } | ||
|
|
||
| @Override | ||
| public String getHostname() { | ||
| final InetSocketAddress socketAddress = getProxyAddress(); | ||
| return socketAddress.getHostString(); | ||
| } | ||
|
|
||
| @Override | ||
| public int getProtocolPort() { | ||
| final InetSocketAddress socketAddress = getProxyAddress(); | ||
| return socketAddress.getPort(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onTransportError(Event event) { | ||
| super.onTransportError(event); | ||
|
|
||
| final Transport transport = event.getTransport(); | ||
| final Connection connection = event.getConnection(); | ||
| if (connection == null || transport == null) { | ||
| return; | ||
| } | ||
|
|
||
| final ErrorCondition errorCondition = transport.getCondition(); | ||
| if (errorCondition == null || !(errorCondition.getCondition().equals(ConnectionError.FRAMING_ERROR) | ||
| || errorCondition.getCondition().equals(AmqpErrorCode.PROTON_IO_ERROR))) { | ||
| return; | ||
| } | ||
|
|
||
| final String hostName = event.getReactor().getConnectionAddress(connection); | ||
| final ProxySelector proxySelector = ProxySelector.getDefault(); | ||
| final boolean isProxyConfigured = proxySelector != null | ||
| || (proxyConfiguration != null && proxyConfiguration.isProxyAddressConfigured()); | ||
|
|
||
| if (!isProxyConfigured || ImplUtils.isNullOrEmpty(hostName)) { | ||
| return; | ||
| } | ||
|
|
||
| final String[] hostNameParts = hostName.split(":"); | ||
| if (hostNameParts.length != 2) { | ||
| return; | ||
|
conniey marked this conversation as resolved.
|
||
| } | ||
|
|
||
| int port; | ||
| try { | ||
| port = Integer.parseInt(hostNameParts[1]); | ||
| } catch (NumberFormatException ignore) { | ||
| return; | ||
|
conniey marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // since proton library communicates all errors based on amqp-error-condition | ||
| // it swallows the IOException and translates it to proton-io errorCode | ||
| // we reconstruct the IOException in this case - but, callstack is lost | ||
| final IOException ioException = new IOException(errorCondition.getDescription()); | ||
| final URI url = createURI(amqpHostname, getProtocolPort()); | ||
| final InetSocketAddress address = new InetSocketAddress(hostNameParts[0], port); | ||
|
|
||
| logger.error(String.format("Failed to connect to url: '%s', proxy host: '%s'", | ||
| url.toString(), address.getHostString()), ioException); | ||
|
|
||
| if (proxySelector != null) { | ||
| proxySelector.connectFailed(url, address, ioException); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void addTransportLayers(final Event event, final TransportInternal transport) { | ||
| super.addTransportLayers(event, transport); | ||
|
|
||
| // Checking that the proxy configuration is not null and not equal to the system defaults option. | ||
| final ProxyImpl proxy = proxyConfiguration != null && !(proxyConfiguration == ProxyConfiguration.SYSTEM_DEFAULTS) | ||
| ? new ProxyImpl(getProtonConfiguration()) | ||
| : new ProxyImpl(); | ||
|
|
||
| // host name used to create proxy connect request | ||
| // after creating the socket to proxy | ||
| final String hostname = event.getConnection().getHostname(); | ||
| final ProxyHandler proxyHandler = new ProxyHandlerImpl(); | ||
| proxy.configure(hostname, null, proxyHandler, transport); | ||
|
|
||
| transport.addTransportLayer(proxy); | ||
|
|
||
| logger.info("addProxyHandshake: hostname[{}]", hostname); | ||
| } | ||
|
|
||
| private InetSocketAddress getProxyAddress() { | ||
| if (proxyConfiguration != null && proxyConfiguration.isProxyAddressConfigured()) { | ||
| return (InetSocketAddress) proxyConfiguration.getProxyAddress().address(); | ||
| } | ||
|
|
||
| final URI serviceUri = createURI(amqpHostname, HTTPS_PORT); | ||
| final ProxySelector proxySelector = ProxySelector.getDefault(); | ||
| if (proxySelector == null) { | ||
| throw logger.logExceptionAsError(new IllegalStateException(PROXY_SELECTOR_HAS_BEEN_MODIFIED)); | ||
| } | ||
|
|
||
| final List<Proxy> proxies = proxySelector.select(serviceUri); | ||
| if (!isProxyAddressLegal(proxies)) { | ||
| throw logger.logExceptionAsError(new IllegalStateException(PROXY_SELECTOR_HAS_BEEN_MODIFIED)); | ||
| } | ||
|
|
||
| final Proxy proxy = proxies.get(0); | ||
| return (InetSocketAddress) proxy.address(); | ||
| } | ||
|
|
||
| private com.microsoft.azure.proton.transport.proxy.ProxyConfiguration getProtonConfiguration() { | ||
| final ProxyAuthenticationType type = getProtonAuthType(proxyConfiguration.getAuthentication()); | ||
| final String username = proxyConfiguration.hasUserDefinedCredentials() | ||
| ? proxyConfiguration.getCredential().getUserName() | ||
| : null; | ||
| final String password = proxyConfiguration.hasUserDefinedCredentials() | ||
| ? new String(proxyConfiguration.getCredential().getPassword()) | ||
| : null; | ||
|
|
||
| return new com.microsoft.azure.proton.transport.proxy.ProxyConfiguration(type, | ||
| proxyConfiguration.getProxyAddress(), username, password); | ||
| } | ||
|
|
||
| private ProxyAuthenticationType getProtonAuthType(com.azure.core.amqp.models.ProxyAuthenticationType type) { | ||
|
conniey marked this conversation as resolved.
Outdated
|
||
| switch (type) { | ||
| case DIGEST: | ||
| return ProxyAuthenticationType.DIGEST; | ||
| case BASIC: | ||
| return ProxyAuthenticationType.BASIC; | ||
| case NONE: | ||
| return ProxyAuthenticationType.NONE; | ||
| default: | ||
| throw logger.logExceptionAsError(new IllegalArgumentException( | ||
| "This authentication type is unknown:" + type.name())); | ||
| } | ||
| } | ||
|
|
||
| private static URI createURI(final String hostname, final int port) { | ||
| return URI.create(String.format(Locale.ROOT, HTTPS_URI_FORMAT, hostname, port)); | ||
| } | ||
|
|
||
| /** | ||
| * This always selects the first proxy in the list instead of going through all the available ones. | ||
| * | ||
| * @param proxies List of proxies available. | ||
| * | ||
| * @return {@code true} if the first proxy in the list is an HTTP proxy and is an IP address. | ||
| */ | ||
| private static boolean isProxyAddressLegal(final List<Proxy> proxies) { | ||
| // we look only at the first proxy in the list | ||
| // if the proxy can be translated to InetSocketAddress | ||
| // only then - can we parse it to hostName and Port | ||
| // which is required by qpid-proton-j library reactor.connectToHost() API | ||
| return proxies != null | ||
|
conniey marked this conversation as resolved.
Outdated
|
||
| && !proxies.isEmpty() | ||
| && proxies.get(0).type() == Proxy.Type.HTTP | ||
| && proxies.get(0).address() != null | ||
| && proxies.get(0).address() instanceof InetSocketAddress; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.