Skip to content

Commit

Permalink
Enabled dev-storage usage, fixed CS
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed May 21, 2012
1 parent acf9fba commit 16fcca8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 78 deletions.
30 changes: 23 additions & 7 deletions lib/Beberlei/AzureBlobStorage/BlobClient.php
Expand Up @@ -81,8 +81,10 @@ class BlobClient
const PAGE_WRITE_UPDATE = 'update';
const PAGE_WRITE_CLEAR = 'clear';

const URL_DEV_BLOB = 'http://127.0.0.1:10000';
const URL_CLOUD_BLOB = 'ssl://blob.core.windows.net';
const URL_DEV_BLOB = 'http://127.0.0.1:10000';
const URL_CLOUD_BLOB = 'ssl://blob.core.windows.net';
const DEVSTORE_ACCOUNT = "devstoreaccount1";
const DEVSTORE_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";

const RESOURCE_CONTAINER = "c";
const RESOURCE_BLOB = "b";
Expand All @@ -102,7 +104,7 @@ class BlobClient
/**
* SharedAccessSignature credentials
*
* @var Microsoft_WindowsAzure_Credentials_SharedAccessSignature
* @var CredentialsAbstract
*/
protected $credentials = null;

Expand All @@ -121,8 +123,15 @@ class BlobClient
*/
protected $accountKey;

/**
* @var string
*/
protected $apiVersion = '2009-09-19';
protected $protocol;

/**
* @var CredentialsAbstract
*/
protected $sharedAccessSignatureCredentials;

/**
* Creates a new BlobClient instance
Expand All @@ -133,11 +142,13 @@ class BlobClient
*/
public function __construct($host = self::URL_DEV_BLOB, $accountName = self::DEVSTORE_ACCOUNT, $accountKey = self::DEVSTORE_KEY)
{
$this->host = $host;
$this->host = $host;
$this->accountName = $accountName;
$this->accountKey = $accountKey;
$this->accountKey = $accountKey;

$this->credentials = new SharedKey($accountName, $accountKey, ($host === self::URL_DEV_BLOB));
$this->sharedAccessSignatureCredentials = new SharedAccessSignature($accountName, $accountKey, ($host === self::URL_DEV_BLOB));

$this->credentials = new SharedKey($accountName, $accountKey, false);
$this->httpClient = new \Beberlei\AzureBlobStorage\Http\SocketClient;
}

Expand Down Expand Up @@ -1769,13 +1780,18 @@ public static function isValidMetadataName($metadataName = '')

return true;
}

/**
* Get base URL for creating requests
*
* @return string
*/
public function getBaseUrl()
{
if ($this->credentials->usePathStyleUri()) {
return $this->host . '/' . $this->accountName;
}

return $this->host;
}

Expand Down
58 changes: 16 additions & 42 deletions lib/Beberlei/AzureBlobStorage/CredentialsAbstract.php
Expand Up @@ -48,21 +48,21 @@ abstract class CredentialsAbstract
*
* @var string
*/
protected $_accountName = '';
protected $accountName = '';

/**
* Account key for Windows Azure
*
* @var string
*/
protected $_accountKey = '';
protected $accountKey = '';

/**
* Use path-style URI's
*
* @var boolean
*/
protected $_usePathStyleUri = false;
protected $usePathStyleUri = false;

/**
* Creates a new Microsoft_WindowsAzure_Credentials_CredentialsAbstract instance
Expand All @@ -76,46 +76,20 @@ public function __construct(
$accountKey = CredentialsAbstract::DEVSTORE_KEY,
$usePathStyleUri = false
) {
$this->_accountName = $accountName;
$this->_accountKey = base64_decode($accountKey);
$this->_usePathStyleUri = $usePathStyleUri;
$this->accountName = $accountName;
$this->accountKey = base64_decode($accountKey);
$this->usePathStyleUri = $usePathStyleUri;
}

/**
* Set account name for Windows Azure
*
* @param string $value
* @return Microsoft_WindowsAzure_Credentials_CredentialsAbstract
*/
public function setAccountName($value = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT)
{
$this->_accountName = $value;
return $this;
}

/**
* Set account key for Windows Azure
*
* @param string $value
* @return Microsoft_WindowsAzure_Credentials_CredentialsAbstract
*/
public function setAccountkey($value = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY)
{
$this->_accountKey = base64_decode($value);
return $this;
}

/**
* Set use path-style URI's
*
* @param boolean $value
* @return Microsoft_WindowsAzure_Credentials_CredentialsAbstract
*/
public function setUsePathStyleUri($value = false)
{
$this->_usePathStyleUri = $value;
return $this;
}
/**
* Check if this credentials want path-style uris (dev-storage)
*
* @return bool
*/
public function usePathStyleUri()
{
return $this->usePathStyleUri;
}

/**
* Sign request URL with credentials
Expand Down Expand Up @@ -163,7 +137,7 @@ abstract public function signRequestHeaders(
* @param mixed $valueIfNotSet
* @return mixed
*/
protected function _issetOr($array, $key, $valueIfNotSet)
protected function issetOr($array, $key, $valueIfNotSet)
{
return isset($array[$key]) ? $array[$key] : $valueIfNotSet;
}
Expand Down
20 changes: 10 additions & 10 deletions lib/Beberlei/AzureBlobStorage/SharedAccessSignature.php
Expand Up @@ -27,7 +27,7 @@ class SharedAccessSignature extends CredentialsAbstract
*
* @var array
*/
protected $_permissionSet = array();
protected $permissionSet = array();

/**
* Creates a new Credentials_SharedAccessSignature instance
Expand All @@ -43,7 +43,7 @@ public function __construct(
$usePathStyleUri = false, $permissionSet = array()
) {
parent::__construct($accountName, $accountKey, $usePathStyleUri);
$this->_permissionSet = $permissionSet;
$this->permissionSet = $permissionSet;
}

/**
Expand All @@ -53,7 +53,7 @@ public function __construct(
*/
public function getPermissionSet()
{
return $this->_permissionSet;
return $this->permissionSet;
}

/**
Expand All @@ -71,11 +71,11 @@ public function getPermissionSet()
public function setPermissionSet($value = array())
{
foreach ($value as $url) {
if (strpos($url, $this->_accountName) === false) {
if (strpos($url, $this->accountName) === false) {
throw new Exception('The permission set can only contain URLs for the account name specified in the Credentials_SharedAccessSignature instance.');
}
}
$this->_permissionSet = $value;
$this->permissionSet = $value;
}

/**
Expand All @@ -98,7 +98,7 @@ public function createSignature(
$identifier = ''
) {
// Determine path
if ($this->_usePathStyleUri) {
if ($this->usePathStyleUri) {
$path = substr($path, strpos($path, '/'));
}

Expand All @@ -108,9 +108,9 @@ public function createSignature(
}

// Build canonicalized resource string
$canonicalizedResource = '/' . $this->_accountName;
/*if ($this->_usePathStyleUri) {
$canonicalizedResource .= '/' . $this->_accountName;
$canonicalizedResource = '/' . $this->accountName;
/*if ($this->usePathStyleUri) {
$canonicalizedResource .= '/' . $this->accountName;
}*/
$canonicalizedResource .= $path;

Expand All @@ -123,7 +123,7 @@ public function createSignature(
$stringToSign[] = $identifier;

$stringToSign = implode("\n", $stringToSign);
$signature = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
$signature = base64_encode(hash_hmac('sha256', $stringToSign, $this->accountKey, true));

return $signature;
}
Expand Down
30 changes: 15 additions & 15 deletions lib/Beberlei/AzureBlobStorage/SharedKey.php
Expand Up @@ -65,7 +65,7 @@ public function signRequestHeaders(


// Determine path
if ($this->_usePathStyleUri) {
if ($this->usePathStyleUri) {
$path = substr($path, strpos($path, '/'));
}

Expand Down Expand Up @@ -98,9 +98,9 @@ public function signRequestHeaders(
sort($canonicalizedHeaders);

// Build canonicalized resource string
$canonicalizedResource = '/' . $this->_accountName;
if ($this->_usePathStyleUri) {
$canonicalizedResource .= '/' . $this->_accountName;
$canonicalizedResource = '/' . $this->accountName;
if ($this->usePathStyleUri) {
$canonicalizedResource .= '/' . $this->accountName;
}
$canonicalizedResource .= $path;
if (count($query) > 0) {
Expand All @@ -126,29 +126,29 @@ public function signRequestHeaders(
// Create string to sign
$stringToSign = array();
$stringToSign[] = strtoupper($httpVerb); // VERB
$stringToSign[] = $this->_issetOr($headers, 'Content-Encoding', ''); // Content-Encoding
$stringToSign[] = $this->_issetOr($headers, 'Content-Language', ''); // Content-Language
$stringToSign[] = $this->issetOr($headers, 'Content-Encoding', ''); // Content-Encoding
$stringToSign[] = $this->issetOr($headers, 'Content-Language', ''); // Content-Language
$stringToSign[] = $contentLength; // Content-Length
$stringToSign[] = $this->_issetOr($headers, 'Content-MD5', ''); // Content-MD5
$stringToSign[] = $this->_issetOr($headers, 'Content-Type', ''); // Content-Type
$stringToSign[] = $this->issetOr($headers, 'Content-MD5', ''); // Content-MD5
$stringToSign[] = $this->issetOr($headers, 'Content-Type', ''); // Content-Type
$stringToSign[] = ""; // Date
$stringToSign[] = $this->_issetOr($headers, 'If-Modified-Since', ''); // If-Modified-Since
$stringToSign[] = $this->_issetOr($headers, 'If-Match', ''); // If-Match
$stringToSign[] = $this->_issetOr($headers, 'If-None-Match', ''); // If-None-Match
$stringToSign[] = $this->_issetOr($headers, 'If-Unmodified-Since', ''); // If-Unmodified-Since
$stringToSign[] = $this->_issetOr($headers, 'Range', ''); // Range
$stringToSign[] = $this->issetOr($headers, 'If-Modified-Since', ''); // If-Modified-Since
$stringToSign[] = $this->issetOr($headers, 'If-Match', ''); // If-Match
$stringToSign[] = $this->issetOr($headers, 'If-None-Match', ''); // If-None-Match
$stringToSign[] = $this->issetOr($headers, 'If-Unmodified-Since', ''); // If-Unmodified-Since
$stringToSign[] = $this->issetOr($headers, 'Range', ''); // Range

if (!$forTableStorage && count($canonicalizedHeaders) > 0) {
$stringToSign[] = implode("\n", $canonicalizedHeaders); // Canonicalized headers
}

$stringToSign[] = $canonicalizedResource; // Canonicalized resource
$stringToSign = implode("\n", $stringToSign);
$signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
$signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->accountKey, true));

// Sign request
$headers[CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
$headers['Authorization'] = 'SharedKey ' . $this->_accountName . ':' . $signString;
$headers['Authorization'] = 'SharedKey ' . $this->accountName . ':' . $signString;

// Return headers
return $headers;
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Expand Up @@ -14,6 +14,7 @@

<php>
<!-- Beware: Testing against a real Azure account can require you to pay the transation costs -->
<var name="AZURESTORAGE_TYPE" value="dev" /><!-- or: production -->
<var name="AZURESTORAGE_HOST" value="" />
<var name="AZURESTORAGE_ACCOUNT" value="" />
<var name="AZURESTORAGE_KEY" value="" />
Expand Down
7 changes: 3 additions & 4 deletions tests/Beberlei/Tests/AzureBlobStorage/BlobTestCase.php
Expand Up @@ -40,11 +40,10 @@ protected function tearDown()

protected function createStorageInstance()
{
$storageClient = null;
if (true) {
$storageClient = new BlobClient($GLOBALS['AZURESTORAGE_HOST'], $GLOBALS['AZURESTORAGE_ACCOUNT'], $GLOBALS['AZURESTORAGE_KEY'], false);
if ($GLOBALS['AZURESTORAGE_TYPE'] == "dev") {
$storageClient = new BlobClient();
} else {
$storageClient = new BlobClient(TESTS_BLOB_HOST_DEV, TESTS_STORAGE_ACCOUNT_DEV, TESTS_STORAGE_KEY_DEV, true, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
$storageClient = new BlobClient($GLOBALS['AZURESTORAGE_HOST'], $GLOBALS['AZURESTORAGE_ACCOUNT'], $GLOBALS['AZURESTORAGE_KEY'], false);
}

return $storageClient;
Expand Down

0 comments on commit 16fcca8

Please sign in to comment.