@JsonCreator public AzureTableConfiguration(@JsonProperty("accountName") String accountName, @JsonProperty("accountKey") String accountKey, @JsonProperty("timeout")Duration timeout, @JsonProperty("retryInterval") Duration retryInterval, @JsonProperty("retryAttempts") int retryAttempts) { this.retryInterval = checkNotNull(retryInterval, "retryInterval cannot be null"); this.retryAttempts = retryAttempts; this.timeout = checkNotNull(timeout, "timeout cannot be null"); this.storageCredentialsAccountAndKey = new StorageCredentialsAccountAndKey( checkNotNull(accountName, "accountName cannot be null"), checkNotNull(accountKey, "accountKey cannot be null")); }
/** * Connect to Azure storage using account key credentials. */ private void connectUsingConnectionStringCredentials( final String accountName, final String containerName, final String accountKey) throws InvalidKeyException, StorageException, IOException, URISyntaxException { // If the account name is "acc.blob.core.windows.net", then the // rawAccountName is just "acc" String rawAccountName = accountName.split("\\.")[0]; StorageCredentials credentials = new StorageCredentialsAccountAndKey( rawAccountName, accountKey); connectUsingCredentials(accountName, credentials, containerName); }
/** * Helper method that creates CloudStorageAccount Instance using the * storage account key. * @param accountName Name of the storage account * @param accountKey Storage Account key * @return CloudStorageAccount instance for the storage account. * @throws SASKeyGenerationException */ private CloudStorageAccount getStorageAccountInstance(String accountName, String accountKey) throws SASKeyGenerationException { if (!storageAccountMap.containsKey(accountName)) { CloudStorageAccount account = null; try { account = new CloudStorageAccount(new StorageCredentialsAccountAndKey( accountName, accountKey)); } catch (URISyntaxException uriSyntaxEx) { throw new SASKeyGenerationException("Encountered URISyntaxException " + "for account " + accountName, uriSyntaxEx); } storageAccountMap.put(accountName, account); } return storageAccountMap.get(accountName); }
public static CloudBlobDirectory createCloudBlobDirectory(String path) { Map<String, String> config = parseAzureConfigurationFromUri(path); String accountName = config.get(KEY_ACCOUNT_NAME); String key = System.getenv("AZURE_SECRET_KEY"); StorageCredentials credentials = null; try { credentials = new StorageCredentialsAccountAndKey(accountName, key); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Could not connect to the Azure Storage. Please verify if AZURE_SECRET_KEY environment variable " + "is correctly set!"); } String uri = config.get(KEY_STORAGE_URI); String dir = config.get(KEY_DIR); try { return AzureUtilities.cloudBlobDirectoryFrom(credentials, uri, dir); } catch (URISyntaxException | StorageException e) { throw new IllegalArgumentException( "Could not connect to the Azure Storage. Please verify the path provided!"); } }
public static CloudBlobDirectory createCloudBlobDirectory(String path) { Map<String, String> config = parseAzureConfigurationFromUri(path); String accountName = config.get(KEY_ACCOUNT_NAME); String key = System.getenv("AZURE_SECRET_KEY"); StorageCredentials credentials = null; try { credentials = new StorageCredentialsAccountAndKey(accountName, key); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Could not connect to the Azure Storage. Please verify if AZURE_SECRET_KEY environment variable " + "is correctly set!"); } String uri = config.get(KEY_STORAGE_URI); String dir = config.get(KEY_DIR); try { return AzureUtilities.cloudBlobDirectoryFrom(credentials, uri, dir); } catch (URISyntaxException | StorageException e) { throw new IllegalArgumentException( "Could not connect to the Azure Storage. Please verify the path provided!"); } }
private AzurePersistence createAzurePersistence() throws StorageException, URISyntaxException, InvalidKeyException { CloudBlobDirectory cloudBlobDirectory = null; if (accountName != null && uri != null) { String key = System.getenv("AZURE_SECRET_KEY"); StorageCredentials credentials = new StorageCredentialsAccountAndKey(accountName, key); cloudBlobDirectory = AzureUtilities.cloudBlobDirectoryFrom(credentials, uri, dir); } else if (connectionString != null && containerName != null) { cloudBlobDirectory = AzureUtilities.cloudBlobDirectoryFrom(connectionString, containerName, dir); } if (cloudBlobDirectory == null) { throw new IllegalArgumentException("Could not connect to Azure storage. Too few connection parameters specified!"); } return new AzurePersistence(cloudBlobDirectory); }
private AzurePersistence createAzurePersistence() throws StorageException, URISyntaxException, InvalidKeyException { CloudBlobDirectory cloudBlobDirectory = null; if (accountName != null && uri != null) { String key = System.getenv("AZURE_SECRET_KEY"); StorageCredentials credentials = new StorageCredentialsAccountAndKey(accountName, key); cloudBlobDirectory = AzureUtilities.cloudBlobDirectoryFrom(credentials, uri, dir); } else if (connectionString != null && containerName != null) { cloudBlobDirectory = AzureUtilities.cloudBlobDirectoryFrom(connectionString, containerName, dir); } if (cloudBlobDirectory == null) { throw new IllegalArgumentException("Could not connect to Azure storage. Too few connection parameters specified!"); } return new AzurePersistence(cloudBlobDirectory); }
static CloudStorageAccount createStorageAccount(String accountName, Configuration conf, boolean allowAnonymous) throws URISyntaxException, KeyProviderException { String accountKey = AzureNativeFileSystemStore .getAccountKeyFromConfiguration(accountName, conf); final StorageCredentials credentials; if (accountKey == null) { if (allowAnonymous) { credentials = StorageCredentialsAnonymous.ANONYMOUS; } else { LOG.warn("Skipping live Azure test because of missing key for" + " account '" + accountName + "'."); return null; } } else { credentials = new StorageCredentialsAccountAndKey( accountName.split("\\.")[0], accountKey); } return new CloudStorageAccount(credentials); }
static CloudStorageAccount createStorageAccount(String accountName, Configuration conf, boolean allowAnonymous) throws URISyntaxException, KeyProviderException { String accountKey = AzureNativeFileSystemStore .getAccountKeyFromConfiguration(accountName, conf); StorageCredentials credentials; if (accountKey == null && allowAnonymous) { credentials = StorageCredentialsAnonymous.ANONYMOUS; } else { credentials = new StorageCredentialsAccountAndKey( accountName.split("\\.")[0], accountKey); } if (credentials == null) { return null; } else { return new CloudStorageAccount(credentials); } }
@Test public void testStorageCredentialsEmptyKeyValue() throws URISyntaxException, InvalidKeyException { String emptyKeyValueAsString = ""; String emptyKeyConnectionString = String.format(Locale.US, "DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=", ACCOUNT_NAME); try { new StorageCredentialsAccountAndKey(ACCOUNT_NAME, emptyKeyValueAsString); fail("Did not hit expected exception"); } catch (IllegalArgumentException ex) { assertEquals(SR.INVALID_KEY, ex.getMessage()); } try { CloudStorageAccount.parse(emptyKeyConnectionString); fail("Did not hit expected exception"); } catch (IllegalArgumentException ex) { assertEquals(SR.INVALID_CONNECTION_STRING, ex.getMessage()); } try { byte[] emptyKeyValueAsByteArray = new byte[0]; new StorageCredentialsAccountAndKey(ACCOUNT_NAME, emptyKeyValueAsByteArray); fail("Did not hit expected exception"); } catch (IllegalArgumentException ex) { assertEquals(SR.INVALID_KEY, ex.getMessage()); } }
@Test public void testStorageCredentialsNullKeyValue() { String nullKeyValueAsString = null; try { new StorageCredentialsAccountAndKey(ACCOUNT_NAME, nullKeyValueAsString); fail("Did not hit expected exception"); } catch (IllegalArgumentException ex) { assertEquals(SR.STRING_NOT_VALID, ex.getMessage()); } StorageCredentialsAccountAndKey credentials2 = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); assertEquals(ACCOUNT_NAME, credentials2.getAccountName()); assertEquals(ACCOUNT_KEY, Base64.encode(credentials2.exportKey())); byte[] nullKeyValueAsByteArray = null; try { new StorageCredentialsAccountAndKey(ACCOUNT_NAME, nullKeyValueAsByteArray); fail("Did not hit expected exception"); } catch (IllegalArgumentException ex) { assertEquals(SR.INVALID_KEY, ex.getMessage()); } }
@Test public void testStorageCredentialsSharedKey() throws URISyntaxException, StorageException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); assertEquals(ACCOUNT_NAME, cred.getAccountName()); URI testUri = new URI("http://test/abc?querya=1"); assertEquals(testUri, cred.transformUri(testUri)); assertEquals(ACCOUNT_KEY, cred.exportBase64EncodedKey()); byte[] dummyKey = { 0, 1, 2 }; String base64EncodedDummyKey = Base64.encode(dummyKey); cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, base64EncodedDummyKey); assertEquals(base64EncodedDummyKey, cred.exportBase64EncodedKey()); dummyKey[0] = 3; base64EncodedDummyKey = Base64.encode(dummyKey); cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, base64EncodedDummyKey); assertEquals(base64EncodedDummyKey, cred.exportBase64EncodedKey()); }
@Test public void testIsUsePathStyleUri() throws InvalidKeyException, URISyntaxException, StorageException { // normal account StorageCredentials creds = new StorageCredentialsAccountAndKey("testAccountName", "/g6UPBuy0ypCpAbYTL6/KA+dI/7gyoWvLFYmah3IviUP1jykOHHOlA=="); CloudStorageAccount account = new CloudStorageAccount(creds); testIsUsePathStyleUri(creds, account.getTableEndpoint().toString(), false); // normal account with path creds = new StorageCredentialsAccountAndKey("testAccountName", "/g6UPBuy0ypCpAbYTL6/KA+dI/7gyoWvLFYmah3IviUP1jykOHHOlA=="); account = new CloudStorageAccount(creds); testIsUsePathStyleUri(creds, account.getTableEndpoint().toString() + "/mytable", false); // custom endpoint creds = new StorageCredentialsAccountAndKey("testAccountName", "/g6UPBuy0ypCpAbYTL6/KA+dI/7gyoWvLFYmah3IviUP1jykOHHOlA=="); testIsUsePathStyleUri(creds, "http://www.contoso.com", false); // dev store account = CloudStorageAccount.getDevelopmentStorageAccount(); testIsUsePathStyleUri(account.getCredentials(), account.getTableEndpoint().toString(), true); // dev store with proxy account = CloudStorageAccount.getDevelopmentStorageAccount(new URI("http://ipv4.fiddler")); testIsUsePathStyleUri(account.getCredentials(), account.getTableEndpoint().toString(), true); // custom endpoints ipv4 with path-style (internal test) creds = new StorageCredentialsAccountAndKey("testAccountName", "/g6UPBuy0ypCpAbYTL6/KA+dI/7gyoWvLFYmah3IviUP1jykOHHOlA=="); testIsUsePathStyleUri(creds, "http://93.184.216.119/testAccountName", true); }
@Test public void testCloudStorageAccountDefaultStorageAccountWithHttp() throws URISyntaxException, InvalidKeyException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(cred, false); assertEquals(cloudStorageAccount.getBlobEndpoint(), new URI(String.format("http://%s.blob.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getQueueEndpoint(), new URI(String.format("http://%s.queue.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getTableEndpoint(), new URI(String.format("http://%s.table.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getFileEndpoint(), new URI(String.format("http://%s.file.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getBlobStorageUri().getSecondaryUri(), new URI(String.format("http://%s-secondary.blob.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getQueueStorageUri().getSecondaryUri(), new URI(String.format("http://%s-secondary.queue.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getTableStorageUri().getSecondaryUri(), new URI(String.format("http://%s-secondary.table.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getFileStorageUri().getSecondaryUri(), new URI(String.format("http://%s-secondary.file.core.windows.net", ACCOUNT_NAME))); String cloudStorageAccountToStringWithSecrets = cloudStorageAccount.toString(true); CloudStorageAccount testAccount = CloudStorageAccount.parse(cloudStorageAccountToStringWithSecrets); AccountsAreEqual(testAccount, cloudStorageAccount); }
@Test public void testCloudStorageAccountDefaultStorageAccountWithHttps() throws URISyntaxException, InvalidKeyException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(cred, true); assertEquals(cloudStorageAccount.getBlobEndpoint(), new URI(String.format("https://%s.blob.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getQueueEndpoint(), new URI(String.format("https://%s.queue.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getTableEndpoint(), new URI(String.format("https://%s.table.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getFileEndpoint(), new URI(String.format("https://%s.file.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getBlobStorageUri().getSecondaryUri(), new URI(String.format("https://%s-secondary.blob.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getQueueStorageUri().getSecondaryUri(), new URI(String.format("https://%s-secondary.queue.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getTableStorageUri().getSecondaryUri(), new URI(String.format("https://%s-secondary.table.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getFileStorageUri().getSecondaryUri(), new URI(String.format("https://%s-secondary.file.core.windows.net", ACCOUNT_NAME))); String cloudStorageAccountToStringWithSecrets = cloudStorageAccount.toString(true); CloudStorageAccount testAccount = CloudStorageAccount.parse(cloudStorageAccountToStringWithSecrets); AccountsAreEqual(testAccount, cloudStorageAccount); }
@Override public void init() throws Exception { super.init(); // Validate configuration // Can throw IAEs this.validateConfiguration(); try { StorageCredentials credentials = new StorageCredentialsAccountAndKey(storage_account_name, storage_access_key); CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, use_https); CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); containerReference = blobClient.getContainerReference(container); boolean created = containerReference.createIfNotExists(); if (created) { log.info("Created container named '%s'.", container); } else { log.debug("Using existing container named '%s'.", container); } } catch (Exception ex) { log.error("Error creating a storage client! Check your configuration.", ex); } }
@Override public void init() throws Exception { super.init(); // Validate configuration // Can throw IAEs this.validateConfiguration(); try { StorageCredentials credentials = new StorageCredentialsAccountAndKey(storage_account_name, storage_access_key); CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, use_https); CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); containerReference = blobClient.getContainerReference(container); boolean created = containerReference.createIfNotExists(); if (created) { log.info("Created container named '%s'.", container); } else { log.debug("Using existing container named '%s'.", container); } } catch (Exception ex) { log.error("Error creating a storage client! Check your configuration."); throw ex; } }
@Test public void testCloudStorageAccountClientUriVerify() throws URISyntaxException, StorageException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(cred, true); CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient(); CloudBlobContainer container = blobClient.getContainerReference("container1"); assertEquals(cloudStorageAccount.getBlobEndpoint().toString() + "/container1", container.getUri().toString()); CloudQueueClient queueClient = cloudStorageAccount.createCloudQueueClient(); CloudQueue queue = queueClient.getQueueReference("queue1"); assertEquals(cloudStorageAccount.getQueueEndpoint().toString() + "/queue1", queue.getUri().toString()); CloudTableClient tableClient = cloudStorageAccount.createCloudTableClient(); CloudTable table = tableClient.getTableReference("table1"); assertEquals(cloudStorageAccount.getTableEndpoint().toString() + "/table1", table.getUri().toString()); CloudFileClient fileClient = cloudStorageAccount.createCloudFileClient(); CloudFileShare share = fileClient.getShareReference("share1"); assertEquals(cloudStorageAccount.getFileEndpoint().toString() + "/share1", share.getUri().toString()); }
@Test public void testStorageCredentialsSharedKeyUpdateKey() throws URISyntaxException, StorageException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); assertEquals(ACCOUNT_KEY, cred.exportBase64EncodedKey()); // Validate update with byte array byte[] dummyKey = { 0, 1, 2 }; cred.updateKey(dummyKey); String base64EncodedDummyKey = Base64.encode(dummyKey); assertEquals(base64EncodedDummyKey, cred.exportBase64EncodedKey()); // Validate update with string dummyKey[0] = 3; base64EncodedDummyKey = Base64.encode(dummyKey); cred.updateKey(base64EncodedDummyKey); assertEquals(base64EncodedDummyKey, cred.exportBase64EncodedKey()); }
@Test public void testCloudStorageAccountClientMethods() throws URISyntaxException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount account = new CloudStorageAccount(cred, false); CloudBlobClient blob = account.createCloudBlobClient(); CloudQueueClient queue = account.createCloudQueueClient(); CloudTableClient table = account.createCloudTableClient(); CloudFileClient file = account.createCloudFileClient(); // check endpoints assertEquals("Blob endpoint doesn't match account", account.getBlobEndpoint(), blob.getEndpoint()); assertEquals("Queue endpoint doesn't match account", account.getQueueEndpoint(), queue.getEndpoint()); assertEquals("Table endpoint doesn't match account", account.getTableEndpoint(), table.getEndpoint()); assertEquals("File endpoint doesn't match account", account.getFileEndpoint(), file.getEndpoint()); // check storage uris assertEquals("Blob endpoint doesn't match account", account.getBlobStorageUri(), blob.getStorageUri()); assertEquals("Queue endpoint doesn't match account", account.getQueueStorageUri(), queue.getStorageUri()); assertEquals("Table endpoint doesn't match account", account.getTableStorageUri(), table.getStorageUri()); assertEquals("File endpoint doesn't match account", account.getFileStorageUri(), file.getStorageUri()); // check creds assertEquals("Blob creds don't match account", account.getCredentials(), blob.getCredentials()); assertEquals("Queue creds don't match account", account.getCredentials(), queue.getCredentials()); assertEquals("Table creds don't match account", account.getCredentials(), table.getCredentials()); assertEquals("File creds don't match account", account.getCredentials(), file.getCredentials()); }