Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Parse EntityPath in Service Bus connection strings #999

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 src/Common/Internal/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Resources {
const CERTIFICATE_PATH_NAME = 'CertificatePath';
const SERVICE_MANAGEMENT_ENDPOINT_NAME = 'ServiceManagementEndpoint';
const SERVICE_BUS_ENDPOINT_NAME = 'Endpoint';
const SERVICE_BUS_ENTITY_PATH = 'EntityPath';
const SHARED_SECRET_ISSUER_NAME = 'SharedSecretIssuer';
const SHARED_SECRET_VALUE_NAME = 'SharedSecretValue';
const SHARED_SHARED_ACCESS_KEY_NAME = 'SharedAccessKeyName';
Expand Down
46 changes: 42 additions & 4 deletions src/Common/Internal/ServiceBusSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class ServiceBusSettings extends ServiceSettings {
*/
private $_serviceBusEndpointUri;

/**
* @var string|null
*/
private $_serviceBusEntityPath;

/**
* @var string
*/
Expand Down Expand Up @@ -90,6 +95,13 @@ class ServiceBusSettings extends ServiceSettings {
*/
private static $_serviceBusEndpointSetting;

/**
* Validator for the EntityPath setting.
*
* @var array
*/
private static $_serviceBusEntityPathSetting;

/**
* Validator for the StsEndpoint setting. Must be a valid Uri.
*
Expand Down Expand Up @@ -128,6 +140,10 @@ protected static function init() {
Validate::getIsValidUri()
);

self::$_serviceBusEntityPathSetting = self::setting(
Resources::SERVICE_BUS_ENTITY_PATH
);

self::$_wrapNameSetting = self::setting(
Resources::SHARED_SECRET_ISSUER_NAME
);
Expand All @@ -150,6 +166,7 @@ protected static function init() {
);

self::$validSettingKeys[] = Resources::SERVICE_BUS_ENDPOINT_NAME;
self::$validSettingKeys[] = Resources::SERVICE_BUS_ENTITY_PATH;
self::$validSettingKeys[] = Resources::SHARED_SECRET_ISSUER_NAME;
self::$validSettingKeys[] = Resources::SHARED_SECRET_VALUE_NAME;
self::$validSettingKeys[] = Resources::SHARED_SHARED_ACCESS_KEY_NAME;
Expand All @@ -161,14 +178,16 @@ protected static function init() {
* Creates new Service Bus settings instance.
* @param type $serviceBusEndpoint The Service Bus endpoint uri
* @param type $filter
* @param string $serviceBusEntityPath
*/
public function __construct(
$serviceBusEndpoint,
$filter
$filter,
$serviceBusEntityPath = null
) {
$this->_serviceBusEndpointUri = $serviceBusEndpoint;
$this->_filter = $filter;

$this->_serviceBusEntityPath = $serviceBusEntityPath;
}

/**
Expand All @@ -182,6 +201,7 @@ private static function createServiceBusWithWrapAuthentication(array $tokenizedS
self::$_wrapPasswordSetting,
];
$optional = [
self::$_serviceBusEntityPathSetting,
self::$_wrapEndpointUriSetting,
];

Expand All @@ -191,6 +211,10 @@ private static function createServiceBusWithWrapAuthentication(array $tokenizedS
Resources::SERVICE_BUS_ENDPOINT_NAME,
$tokenizedSettings
);
$entityPath = Utilities::tryGetValueInsensitive(
Resources::SERVICE_BUS_ENTITY_PATH,
$tokenizedSettings
);

// Parse the namespace part from the URI
$namespace = explode('.', parse_url($endpoint, PHP_URL_HOST));
Expand All @@ -214,7 +238,7 @@ private static function createServiceBusWithWrapAuthentication(array $tokenizedS
$issuerName,
$issuerValue,
self::createWrapService($wrapEndpointUri)
));
), $entityPath);
}
/**
* @param array $tokenizedSettings
Expand All @@ -227,6 +251,7 @@ private static function createServiceBusWithSasAuthentication(array $tokenizedSe
self::$_sasKeySetting,
];
$optional = [
self::$_serviceBusEntityPathSetting,
self::$_wrapEndpointUriSetting,
];
$matchedSpecs = self::getMatchedSpecs($tokenizedSettings, $required, $optional, $connectionString);
Expand All @@ -235,6 +260,10 @@ private static function createServiceBusWithSasAuthentication(array $tokenizedSe
Resources::SERVICE_BUS_ENDPOINT_NAME,
$tokenizedSettings
);
$entityPath = Utilities::tryGetValueInsensitive(
Resources::SERVICE_BUS_ENTITY_PATH,
$tokenizedSettings
);

$sharedAccessKeyName = Utilities::tryGetValueInsensitive(
Resources::SHARED_SHARED_ACCESS_KEY_NAME,
Expand All @@ -248,7 +277,7 @@ private static function createServiceBusWithSasAuthentication(array $tokenizedSe
return new self($endpoint, new SASFilter(
$sharedAccessKeyName,
$sharedAccessKey
));
), $entityPath);
}
/**
* @param $wrapEndpointUri
Expand Down Expand Up @@ -305,6 +334,15 @@ public function getServiceBusEndpointUri() {
return $this->_serviceBusEndpointUri;
}

/**
* Gets the Service Bus entity path.
*
* @return string|null
*/
public function getServiceBusEntityPath() {
return $this->_serviceBusEntityPath;
}

/**
* Gets the wrap endpoint URI.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testCreateFromConnectionStringWithInvalidServiceBusKeyFail()
$expectedMsg = sprintf(
Resources::INVALID_CONNECTION_STRING_SETTING_KEY,
$invalidKey,
implode("\n", ['Endpoint', 'SharedSecretIssuer', 'SharedSecretValue'])
implode("\n", ['Endpoint', 'EntityPath', 'SharedSecretIssuer', 'SharedSecretValue'])
);
$this->setExpectedException('\RuntimeException', $expectedMsg);

Expand Down Expand Up @@ -182,22 +182,24 @@ public function testGetFilter()
* @covers \WindowsAzure\Common\Internal\ServiceSettings::parseAndValidateKeys
* @covers \WindowsAzure\Common\Internal\ServiceSettings::noMatch
*/
public function testCreateFromConnectionStringWithCaseInvesitive()
public function testCreateFromConnectionStringWithCaseInsensitive()
{
// Setup
$namepspace = 'mynamespace';
$expectedServiceBusEndpoint = "https://$namepspace.servicebus.windows.net";
$namespace = 'mynamespace';
$expectedServiceBusEndpoint = "https://$namespace.servicebus.windows.net";
$expectedServiceBusEntityPath = 'myqueue';
$expectedWrapName = 'myname';
$expectedWrapPassword = 'mypassword';
$expectedWrapEndpointUri = "https://$namepspace-sb.accesscontrol.windows.net/WRAPv0.9";
$connectionString = "eNdPoinT=$expectedServiceBusEndpoint;sHarEdsecRetiSsuer=$expectedWrapName;shArEdsecrEtvAluE=$expectedWrapPassword";
$expectedWrapEndpointUri = "https://$namespace-sb.accesscontrol.windows.net/WRAPv0.9";
$connectionString = "eNdPoinT=$expectedServiceBusEndpoint;sHarEdsecRetiSsuer=$expectedWrapName;shArEdsecrEtvAluE=$expectedWrapPassword;eNtItYpAtH=$expectedServiceBusEntityPath";

// Test
$actual = ServiceBusSettings::createFromConnectionString($connectionString);

// Assert
$this->assertInstanceOf('WindowsAzure\Common\Internal\IServiceFilter', $actual->getFilter());
$this->assertEquals($expectedServiceBusEndpoint, $actual->getServiceBusEndpointUri());
$this->assertEquals($expectedServiceBusEntityPath, $actual->getServiceBusEntityPath());
}

/**
Expand Down