Skip to content
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

ConnectionString : Default scheme to SB:// in Endpoint if not specified #14600

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
1 change: 1 addition & 0 deletions eng/versioning/version_client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ com.microsoft.azure:spring-cloud-azure-eventhubs-stream-binder;1.2.8-beta.1;1.2.
# unreleased_<groupId>:<artifactId>;dependency-version
# note: The unreleased dependencies will not be manipulated with the automatic PR creation code.
unreleased_com.azure:azure-core;1.8.0-beta.1
unreleased_com.azure:azure-core-amqp;1.5.0-beta.1
unreleased_com.azure:azure-messaging-servicebus;7.0.0-beta.5
unreleased_com.azure:azure-security-keyvault-keys;4.3.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

package com.azure.core.amqp.implementation;

import com.azure.core.util.CoreUtils;
import com.azure.core.util.logging.ClientLogger;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;
Expand All @@ -12,15 +15,22 @@
* The set of properties that comprise a connection string from the Azure portal.
*/
public class ConnectionStringProperties {
private final ClientLogger logger = new ClientLogger(ConnectionStringProperties.class);

private static final String TOKEN_VALUE_SEPARATOR = "=";
private static final String ENDPOINT_SCHEME_SB_PREFIX = "sb://";
private static final String ENDPOINT_SCHEME_HTTP_PREFIX = "http://";
private static final String ENDPOINT_SCHEME_HTTPS_PREFIX = "https://";
private static final String TOKEN_VALUE_PAIR_DELIMITER = ";";
private static final String ENDPOINT = "Endpoint";
private static final String SHARED_ACCESS_KEY_NAME = "SharedAccessKeyName";
private static final String SHARED_ACCESS_KEY = "SharedAccessKey";
private static final String ENTITY_PATH = "EntityPath";
private static final String ERROR_MESSAGE_FORMAT = "Could not parse 'connectionString'. Expected format: "
+ "'Endpoint={endpoint};SharedAccessKeyName={sharedAccessKeyName};"
+ "SharedAccessKey={sharedAccessKey};EntityPath={eventHubName}'. Actual: %s";
+ "SharedAccessKey={sharedAccessKey};EntityPath={entityPath}'. Actual: %s";
private static final String ERROR_MESSAGE_ENDPOINT_FORMAT = "'Endpoint' must be provided in 'connectionString'."
+ " Actual: %s";

private final URI endpoint;
private final String entityPath;
Expand Down Expand Up @@ -60,8 +70,9 @@ public ConnectionStringProperties(String connectionString) {
final String value = pair[1].trim();

if (key.equalsIgnoreCase(ENDPOINT)) {
final String endpointUri = validateAndUpdateDefaultScheme(value, connectionString);
try {
endpoint = new URI(value);
endpoint = new URI(endpointUri);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(
String.format(Locale.US, "Invalid endpoint: %s", tokenValuePair), e);
Expand Down Expand Up @@ -123,4 +134,25 @@ public String getSharedAccessKeyName() {
public String getSharedAccessKey() {
return sharedAccessKey;
}

/*
* The function checks for pre existing scheme of "sb://" , "http://" or "https://". If the scheme is not provided
* in endpoint, it will set the default scheme to "sb://".
*/
private String validateAndUpdateDefaultScheme(final String endpoint, final String connectionString) {
String updatedEndpoint = endpoint.trim();

if (CoreUtils.isNullOrEmpty(endpoint)) {
throw logger.logExceptionAsError(new IllegalArgumentException(String.format(Locale.US,
ERROR_MESSAGE_ENDPOINT_FORMAT, connectionString)));

}
final String endpointLowerCase = endpoint.toLowerCase(Locale.getDefault());
if (!endpointLowerCase.startsWith(ENDPOINT_SCHEME_SB_PREFIX)
&& !endpointLowerCase.startsWith(ENDPOINT_SCHEME_HTTP_PREFIX)
&& !endpointLowerCase.startsWith(ENDPOINT_SCHEME_HTTPS_PREFIX)) {
updatedEndpoint = ENDPOINT_SCHEME_SB_PREFIX + endpoint;
}
return updatedEndpoint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public void differentEndpointScheme() {
Assertions.assertEquals(EVENT_HUB, properties.getEntityPath());
}

@Test
public void noEndpointSchemeDefault() {
// Arrange
final String connectionString = getConnectionString(HOST, EVENT_HUB, SAS_KEY, SAS_VALUE);

// Act
ConnectionStringProperties properties = new ConnectionStringProperties(connectionString);

// Assert
Assertions.assertEquals("sb", properties.getEndpoint().getScheme());
}

/**
* Verifies we can create ConnectionStringProperties even if there is an extraneous component.
*/
Expand Down
1 change: 1 addition & 0 deletions sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Release History

## 5.2.0-beta.3 (Unreleased)
- Default scheme to 'sb://' if no scheme is set in 'Endpoint'.

## 5.2.0-beta.2 (2020-08-14)
- Support for object serializer to send and receive strongly-typed objects.
Expand Down
2 changes: 1 addition & 1 deletion sdk/eventhubs/azure-messaging-eventhubs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-amqp</artifactId>
<version>1.4.0</version> <!-- {x-version-update;com.azure:azure-core-amqp;dependency} -->
<version>1.5.0-beta.1</version> <!-- {x-version-update;unreleased_com.azure:azure-core-amqp;dependency} -->
</dependency>

<!-- Test dependencies -->
Expand Down
2 changes: 1 addition & 1 deletion sdk/servicebus/azure-messaging-servicebus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-amqp</artifactId>
<version>1.4.0</version> <!-- {x-version-update;com.azure:azure-core-amqp;dependency} -->
<version>1.5.0-beta.1</version> <!-- {x-version-update;unreleased_com.azure:azure-core-amqp;dependency} -->
</dependency>
<dependency>
<groupId>com.azure</groupId>
Expand Down