com.microsoft.azure.storage.StorageCredentials Java Examples

The following examples show how to use com.microsoft.azure.storage.StorageCredentials. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AzureBlobResource.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@javax.enterprise.inject.Produces
@Named("azureBlobClient")
public CloudBlob createBlobClient() throws Exception {
    StorageCredentials credentials = StorageCredentials.tryParseCredentials(System.getProperty("azurite.credentials"));
    URI uri = new URI(System.getProperty("azurite.blob.service.url") + "camel-test/test");
    CloudBlockBlob cloudBlockBlob = new CloudBlockBlob(uri, credentials);
    return cloudBlockBlob;
}
 
Example #2
Source File: BlobTestHelper.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
public static CloudBlob getBlobReference(BlobType type, StorageCredentials credentials, URI uri)
        throws StorageException, URISyntaxException {
    CloudBlob blob = null;
    
    switch (type) {
        case APPEND_BLOB:
            blob = new CloudAppendBlob(credentials.transformUri(uri));
            break;

        case BLOCK_BLOB:
            blob = new CloudBlockBlob(credentials.transformUri(uri));
            break;
            
        case PAGE_BLOB:
            blob = new CloudPageBlob(credentials.transformUri(uri));
            break;
            
        case UNSPECIFIED:
            fail();
    }
    
    return blob;
}
 
Example #3
Source File: TableTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
private static void testIsUsePathStyleUri(StorageCredentials creds, String tableEndpoint, boolean usePathStyleUris)
        throws URISyntaxException, InvalidKeyException, StorageException {
    CloudTableClient tableClient = new CloudTableClient(new URI(tableEndpoint), creds);
    assertEquals(usePathStyleUris, tableClient.isUsePathStyleUris());

    CloudTable table = tableClient.getTableReference("mytable");
    assertEquals(tableEndpoint + "/mytable", table.getUri().toString());

    String sasToken = table.generateSharedAccessSignature(null, "fakeIdentifier", null, null, null, null);
    tableClient = new CloudTableClient(new URI(tableEndpoint),
            new StorageCredentialsSharedAccessSignature(sasToken));
    assertEquals(usePathStyleUris, tableClient.isUsePathStyleUris());

    table = new CloudTable(table.getUri(), tableClient.getCredentials());
    assertEquals(tableEndpoint + "/mytable", table.getUri().toString());
}
 
Example #4
Source File: VideoStorageAzure.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
public VideoStorageAzure(List<StorageCredentials> accounts, String container, long accessDurationInMs, boolean instrumentOs, int bufferOsSize, int fetchIsSize) {
   this.containers = new ArrayList<>();
   this.accounts = new HashMap<>();
   this.accessDurationInMs = accessDurationInMs;
   this.instrumentOs = instrumentOs;
   this.bufferOsSize = bufferOsSize;
   this.fetchIsSize = fetchIsSize;

   for (StorageCredentials account : accounts) {
      try {
         CloudStorageAccount storage = new CloudStorageAccount(account,true);
         this.accounts.put(account.getAccountName(), storage.createCloudBlobClient());

         CloudBlobClient client = storage.createCloudBlobClient();
         this.containers.add(getStorageContainer(client,container));
      } catch (Exception ex) {
         throw new RuntimeException(ex);
      }
   }

   log.info("configured azure storage with {} accounts", accounts.size());
}
 
Example #5
Source File: StorageCredentialsHelper.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Signs a request using the specified operation context under the Shared Key authentication scheme.
 * 
 * @param request
 *            An <code>HttpURLConnection</code> object that represents the request to sign.
 * @param contentLength
 *            The length of the content written to the output stream. If unknown, specify -1.
 * @param opContext
 *            An {@link OperationContext} object that represents the context for the current operation. This object
 *            is used to track requests to the storage service, and to provide additional runtime information about
 *            the operation.
 * 
 * @throws InvalidKeyException
 *             If the given key is invalid.
 * @throws StorageException
 *             If a storage service error occurred.
 */
public static void signBlobQueueAndFileRequest(final StorageCredentials creds,
        final java.net.HttpURLConnection request, final long contentLength, OperationContext opContext)
        throws InvalidKeyException, StorageException {
    
    if (creds.getClass().equals(StorageCredentialsAccountAndKey.class)) {
        opContext = opContext == null ? new OperationContext() : opContext;
        request.setRequestProperty(Constants.HeaderConstants.DATE, Utility.getGMTTime());
        final Canonicalizer canonicalizer = CanonicalizerFactory.getBlobQueueFileCanonicalizer(request);

        final String stringToSign = canonicalizer.canonicalize(request, creds.getAccountName(), contentLength);

        final String computedBase64Signature = StorageCredentialsHelper.computeHmac256(creds, stringToSign);

        Logger.debug(opContext, LogConstants.SIGNING, stringToSign);

        request.setRequestProperty(Constants.HeaderConstants.AUTHORIZATION,
                String.format("%s %s:%s", "SharedKey", creds.getAccountName(), computedBase64Signature));
    }
}
 
Example #6
Source File: StorageCredentialsHelper.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Signs a request using the specified operation context under the Shared Key authentication scheme.
 * 
 * @param request
 *            An <code>HttpURLConnection</code> object that represents the request to sign.
 * @param contentLength
 *            The length of the content written to the output stream. If unknown, specify -1.
 * @param opContext
 *            An {@link OperationContext} object that represents the context for the current operation. This object
 *            is used to track requests to the storage service, and to provide additional runtime information about
 *            the operation.
 * 
 * @throws InvalidKeyException
 *             If the given key is invalid.
 * @throws StorageException
 *             If a storage service error occurred.
 */
public static void signTableRequest(final StorageCredentials creds, final java.net.HttpURLConnection request,
        final long contentLength, OperationContext opContext) throws InvalidKeyException, StorageException {
    if (creds.getClass().equals(StorageCredentialsAccountAndKey.class)) {
        opContext = opContext == null ? new OperationContext() : opContext;
        request.setRequestProperty(Constants.HeaderConstants.DATE, Utility.getGMTTime());

        final Canonicalizer canonicalizer = CanonicalizerFactory.getTableCanonicalizer(request);

        final String stringToSign = canonicalizer.canonicalize(request, creds.getAccountName(), contentLength);

        final String computedBase64Signature = StorageCredentialsHelper.computeHmac256(creds, stringToSign);
        
        Logger.debug(opContext, LogConstants.SIGNING, stringToSign);

        request.setRequestProperty(Constants.HeaderConstants.AUTHORIZATION,
                String.format("%s %s:%s", "SharedKey", creds.getAccountName(), computedBase64Signature));
    }
}
 
Example #7
Source File: CloudFile.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies the passed in URI. Then parses it and uses its components to populate this resource's properties.
 * 
 * @param completeUri
 *            A {@link StorageUri} object which represents the complete URI.
 * @param credentials
 *            A {@link StorageCredentials} object used to authenticate access.
 * @throws StorageException
 *             If a storage service error occurred.
 * @throws URISyntaxException 
 */
private void parseQueryAndVerify(final StorageUri completeUri, final StorageCredentials credentials)
        throws StorageException, URISyntaxException {
   Utility.assertNotNull("completeUri", completeUri);

    if (!completeUri.isAbsolute()) {
        throw new IllegalArgumentException(String.format(SR.RELATIVE_ADDRESS_NOT_PERMITTED, completeUri.toString()));
    }

    this.storageUri = PathUtility.stripURIQueryAndFragment(completeUri);
    
    final StorageCredentialsSharedAccessSignature parsedCredentials = 
            SharedAccessSignatureHelper.parseQuery(completeUri);

    if (credentials != null && parsedCredentials != null) {
        throw new IllegalArgumentException(SR.MULTIPLE_CREDENTIALS_PROVIDED);
    }

    try {
        final boolean usePathStyleUris = Utility.determinePathStyleFromUri(this.storageUri.getPrimaryUri());
        this.fileServiceClient = new CloudFileClient(PathUtility.getServiceClientBaseAddress(
                this.getStorageUri(), usePathStyleUris), credentials != null ? credentials : parsedCredentials);
        this.name = PathUtility.getFileNameFromURI(this.storageUri.getPrimaryUri(), usePathStyleUris);
    }
    catch (final URISyntaxException e) {
        throw Utility.generateNewUnexpectedStorageException(e);
    }

    final HashMap<String, String[]> queryParameters = PathUtility.parseQueryString(completeUri.getQuery());

    final String[] snapshotIDs = queryParameters.get(Constants.QueryConstants.SHARE_SNAPSHOT);
    if (snapshotIDs != null && snapshotIDs.length > 0) {
        this.getShare().snapshotID = snapshotIDs[0];
    }
}
 
Example #8
Source File: CloudFileShare.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies the passed in URI. Then parses it and uses its components to populate this resource's properties.
 * 
 * @param completeUri
 *            A {@link StorageUri} object which represents the complete URI.
 * @param credentials
 *            A {@link StorageCredentials} object used to authenticate access.
 * @throws StorageException
 *             If a storage service error occurred.
 */
private void parseQueryAndVerify(final StorageUri completeUri, final StorageCredentials credentials)
        throws StorageException {
   Utility.assertNotNull("completeUri", completeUri);

    if (!completeUri.isAbsolute()) {
        throw new IllegalArgumentException(String.format(SR.RELATIVE_ADDRESS_NOT_PERMITTED, completeUri.toString()));
    }

    this.storageUri = PathUtility.stripURIQueryAndFragment(completeUri);

    final HashMap<String, String[]> queryParameters = PathUtility.parseQueryString(completeUri.getQuery());

    final String[] snapshotIDs = queryParameters.get(Constants.QueryConstants.SHARE_SNAPSHOT);
    if (snapshotIDs != null && snapshotIDs.length > 0) {
        this.snapshotID = snapshotIDs[0];
    }

    final StorageCredentialsSharedAccessSignature parsedCredentials = 
            SharedAccessSignatureHelper.parseQuery(completeUri);

    if (credentials != null && parsedCredentials != null) {
        throw new IllegalArgumentException(SR.MULTIPLE_CREDENTIALS_PROVIDED);
    }

    try {
        final boolean usePathStyleUris = Utility.determinePathStyleFromUri(this.storageUri.getPrimaryUri());
        this.fileServiceClient = new CloudFileClient(PathUtility.getServiceClientBaseAddress(
                this.getStorageUri(), usePathStyleUris), credentials != null ? credentials : parsedCredentials);
        this.name = PathUtility.getShareNameFromUri(this.storageUri.getPrimaryUri(), usePathStyleUris);
    }
    catch (final URISyntaxException e) {
        throw Utility.generateNewUnexpectedStorageException(e);
    }
}
 
Example #9
Source File: AzureBlobResource.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() throws Exception {
    StorageCredentials credentials = StorageCredentials.tryParseCredentials(System.getProperty("azurite.credentials"));
    URI uri = new URI(System.getProperty("azurite.blob.service.url") + "camel-test");
    CloudBlobContainer container = new CloudBlobContainer(uri, credentials);
    container.create();
}
 
Example #10
Source File: VideoConfig.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public List<StorageCredentials> getStorageAzureAccounts() {
   List<StorageCredentials> result = new ArrayList<>();

   for (int i = 1; true; ++i) {
      String rawAccount = "video.storage.azure.account" + i;
      ConfigurationKey confAccount = new ConfigurationKey(rawAccount, KeyParser.parse(rawAccount));
      Supplier<String> supAccount = configProvider.getStringSupplier(confAccount, null);
      String account = (supAccount == null) ? null : supAccount.get();

      if (account == null || account.trim().isEmpty()) {
         break;
      }

      try {
         StorageCredentials creds = StorageCredentials.tryParseCredentials(account);
         if (creds == null) {
            throw new RuntimeException("invalid azure storage credentials");
         }

         result.add(creds);
      } catch (InvalidKeyException ex) {
         throw new RuntimeException(ex);
      }
   }

   return result;
}
 
Example #11
Source File: CloudFileDirectory.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies the passed in URI. Then parses it and uses its components to populate this resource's properties.
 * 
 * @param completeUri
 *            A {@link StorageUri} object which represents the complete URI.
 * @param credentials
 *            A {@link StorageCredentials} object used to authenticate access.
 * @throws StorageException
 *             If a storage service error occurred.
 * @throws URISyntaxException 
 */
private void parseQueryAndVerify(final StorageUri completeUri, final StorageCredentials credentials)
        throws StorageException, URISyntaxException {
   Utility.assertNotNull("completeUri", completeUri);

    if (!completeUri.isAbsolute()) {
        throw new IllegalArgumentException(String.format(SR.RELATIVE_ADDRESS_NOT_PERMITTED, completeUri.toString()));
    }

    this.storageUri = PathUtility.stripURIQueryAndFragment(completeUri);
    
    final StorageCredentialsSharedAccessSignature parsedCredentials = 
            SharedAccessSignatureHelper.parseQuery(completeUri);

    if (credentials != null && parsedCredentials != null) {
        throw new IllegalArgumentException(SR.MULTIPLE_CREDENTIALS_PROVIDED);
    }

    try {
        final boolean usePathStyleUris = Utility.determinePathStyleFromUri(this.storageUri.getPrimaryUri());
        this.fileServiceClient = new CloudFileClient(PathUtility.getServiceClientBaseAddress(
                this.getStorageUri(), usePathStyleUris), credentials != null ? credentials : parsedCredentials);
        this.name = PathUtility.getDirectoryNameFromURI(this.storageUri.getPrimaryUri(), usePathStyleUris);
    }
    catch (final URISyntaxException e) {
        throw Utility.generateNewUnexpectedStorageException(e);
    }

    final HashMap<String, String[]> queryParameters = PathUtility.parseQueryString(completeUri.getQuery());

    final String[] snapshotIDs = queryParameters.get(Constants.QueryConstants.SHARE_SNAPSHOT);
    if (snapshotIDs != null && snapshotIDs.length > 0) {
        this.getShare().snapshotID = snapshotIDs[0];
    }
}
 
Example #12
Source File: PreviewConfig.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public List<StorageCredentials> getStorageAzureAccounts() {
	List<StorageCredentials> result = new ArrayList<>();

	for (int i = 1; true; ++i) {
		String rawAccount = "previews.storage.azure.account" + i;
		ConfigurationKey confAccount = new ConfigurationKey(rawAccount, KeyParser.parse(rawAccount));
		Supplier<String> supAccount = configProvider.getStringSupplier(confAccount, null);
		String account = (supAccount == null) ? null : supAccount.get();

		if (account == null || account.trim().isEmpty()) {
			break;
		}

		try {
			StorageCredentials creds = StorageCredentials.tryParseCredentials(account);
			if (creds == null) {
				throw new RuntimeException("invalid azure storage credentials");
			}

			result.add(creds);
		} catch (InvalidKeyException ex) {
			throw new RuntimeException(ex);
		}
	}

	return result;
}
 
Example #13
Source File: SendRequestIntercept.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for SendRequestThrottle.
 * 
 * @param storageCreds
 *          - storage account credentials for signing packets.
 * 
 */
private SendRequestIntercept(StorageCredentials storageCreds,
    boolean allowConcurrentOOBIo, OperationContext opContext) {
  // Capture the send delay callback interface.
  this.storageCreds = storageCreds;
  this.allowConcurrentOOBIo = allowConcurrentOOBIo;
  this.opContext = opContext;
}
 
Example #14
Source File: CloudBlob.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies the passed in URI. Then parses it and uses its components to populate this resource's properties.
 * 
 * @param completeUri
 *            A {@link StorageUri} object which represents the complete URI.
 * @param credentials
 *            A {@link StorageCredentials} object used to authenticate access.
 * @throws StorageException
 *             If a storage service error occurred.
 */
private void parseQueryAndVerify(final StorageUri completeUri, final StorageCredentials credentials)
        throws StorageException {
   Utility.assertNotNull("completeUri", completeUri);

    if (!completeUri.isAbsolute()) {
        throw new IllegalArgumentException(String.format(SR.RELATIVE_ADDRESS_NOT_PERMITTED, completeUri.toString()));
    }

    this.storageUri = PathUtility.stripURIQueryAndFragment(completeUri);
    
    final HashMap<String, String[]> queryParameters = PathUtility.parseQueryString(completeUri.getQuery());

    final String[] snapshotIDs = queryParameters.get(BlobConstants.SNAPSHOT);
    if (snapshotIDs != null && snapshotIDs.length > 0) {
        this.snapshotID = snapshotIDs[0];
    }
    
    final StorageCredentialsSharedAccessSignature parsedCredentials = 
            SharedAccessSignatureHelper.parseQuery(queryParameters);

    if (credentials != null && parsedCredentials != null) {
        throw new IllegalArgumentException(SR.MULTIPLE_CREDENTIALS_PROVIDED);
    }

    try {
        final boolean usePathStyleUris = Utility.determinePathStyleFromUri(this.storageUri.getPrimaryUri());
        this.blobServiceClient = new CloudBlobClient(PathUtility.getServiceClientBaseAddress(
                this.getStorageUri(), usePathStyleUris), credentials != null ? credentials : parsedCredentials);
        this.name = PathUtility.getBlobNameFromURI(this.storageUri.getPrimaryUri(), usePathStyleUris);
    }
    catch (final URISyntaxException e) {
        throw Utility.generateNewUnexpectedStorageException(e);
    }
}
 
Example #15
Source File: CloudBlobContainer.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies the passed in URI. Then parses it and uses its components to populate this resource's properties.
 * 
 * @param completeUri
 *            A {@link StorageUri} object which represents the complete URI.
 * @param credentials
 *            A {@link StorageCredentials} object used to authenticate access.
 * @throws StorageException
 *             If a storage service error occurred.
 */
private void parseQueryAndVerify(final StorageUri completeUri, final StorageCredentials credentials)
        throws StorageException {
   Utility.assertNotNull("completeUri", completeUri);

    if (!completeUri.isAbsolute()) {
        throw new IllegalArgumentException(String.format(SR.RELATIVE_ADDRESS_NOT_PERMITTED, completeUri.toString()));
    }

    this.storageUri = PathUtility.stripURIQueryAndFragment(completeUri);
    
    final StorageCredentialsSharedAccessSignature parsedCredentials = 
            SharedAccessSignatureHelper.parseQuery(completeUri);

    if (credentials != null && parsedCredentials != null) {
        throw new IllegalArgumentException(SR.MULTIPLE_CREDENTIALS_PROVIDED);
    }

    try {
        final boolean usePathStyleUris = Utility.determinePathStyleFromUri(this.storageUri.getPrimaryUri());
        this.blobServiceClient = new CloudBlobClient(PathUtility.getServiceClientBaseAddress(
                this.getStorageUri(), usePathStyleUris), credentials != null ? credentials : parsedCredentials);
        this.name = PathUtility.getContainerNameFromUri(this.storageUri.getPrimaryUri(), usePathStyleUris);
    }
    catch (final URISyntaxException e) {
        throw Utility.generateNewUnexpectedStorageException(e);
    }
}
 
Example #16
Source File: AzureNativeFileSystemStore.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #17
Source File: AzureNativeFileSystemStore.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Connect to Azure storage using shared access signature credentials.
 */
private void connectUsingSASCredentials(final String accountName,
    final String containerName, final String sas) throws InvalidKeyException,
    StorageException, IOException, URISyntaxException {
  StorageCredentials credentials = new StorageCredentialsSharedAccessSignature(
      sas);
  connectingUsingSAS = true;
  connectUsingCredentials(accountName, credentials, containerName);
}
 
Example #18
Source File: AzureConnectionWithToken.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public CloudStorageAccount getCloudStorageAccount() {
    try {
        String token = tokenGetter.retrieveAccessToken();

        StorageCredentials credentials = new StorageCredentialsToken(accountName, token);
        return new CloudStorageAccount(credentials, true);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #19
Source File: CloudQueue.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies the passed in URI. Then parses it and uses its components to populate this resource's properties.
 * 
 * @param completeUri
 *            A {@link StorageUri} object which represents the complete URI.
 * @param credentials
 *            A {@link StorageCredentials} object used to authenticate access.
 * @throws StorageException
 *             If a storage service error occurred.
 */
private void parseQueryAndVerify(final StorageUri completeUri, final StorageCredentials credentials) 
        throws StorageException {
    Utility.assertNotNull("completeUri", completeUri);

    if (!completeUri.isAbsolute()) {
        throw new IllegalArgumentException(String.format(SR.RELATIVE_ADDRESS_NOT_PERMITTED, completeUri.toString()));
    }

    this.storageUri = PathUtility.stripURIQueryAndFragment(completeUri);
    
    final StorageCredentialsSharedAccessSignature parsedCredentials = 
            SharedAccessSignatureHelper.parseQuery(completeUri);

    if (credentials != null && parsedCredentials != null) {
        throw new IllegalArgumentException(SR.MULTIPLE_CREDENTIALS_PROVIDED);
    }

    try {
        final boolean usePathStyleUris = Utility.determinePathStyleFromUri(this.storageUri.getPrimaryUri());
        this.queueServiceClient = new CloudQueueClient(PathUtility.getServiceClientBaseAddress(
                this.getStorageUri(), usePathStyleUris), credentials != null ? credentials : parsedCredentials);
        this.name = PathUtility.getContainerNameFromUri(storageUri.getPrimaryUri(), usePathStyleUris);
    }
    catch (final URISyntaxException e) {
        throw Utility.generateNewUnexpectedStorageException(e);
    }
}
 
Example #20
Source File: PathUtility.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the canonical path for an object from the credentials.
 * 
 * @param credentials
 *            the credentials to use.
 * @param absolutePath
 *            the Absolute path of the object.
 * @return the canonical path for an object from the credentials
 */
public static String getCanonicalPathFromCredentials(final StorageCredentials credentials, final String absolutePath) {
    final String account = credentials.getAccountName();

    if (account == null) {
        final String errorMessage = SR.CANNOT_CREATE_SAS_FOR_GIVEN_CREDENTIALS;
        throw new IllegalArgumentException(errorMessage);
    }
    final StringBuilder builder = new StringBuilder("/");
    builder.append(account);
    builder.append(absolutePath);
    return builder.toString();
}
 
Example #21
Source File: CloudTable.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies the passed in URI. Then parses it and uses its components to populate this resource's properties.
 * 
 * @param completeUri
 *            A {@link StorageUri} object which represents the complete URI.
 * @param credentials
 *            A {@link StorageCredentials} object used to authenticate access.
 * @throws StorageException
 *             If a storage service error occurred.
 */  
private void parseQueryAndVerify(final StorageUri completeUri, final StorageCredentials credentials) 
        throws StorageException {
    Utility.assertNotNull("completeUri", completeUri);

    if (!completeUri.isAbsolute()) {
        throw new IllegalArgumentException(String.format(SR.RELATIVE_ADDRESS_NOT_PERMITTED, completeUri.toString()));
    }

    this.storageUri = PathUtility.stripURIQueryAndFragment(completeUri);
    
    final StorageCredentialsSharedAccessSignature parsedCredentials = 
            SharedAccessSignatureHelper.parseQuery(completeUri);

    if (credentials != null && parsedCredentials != null) {
        throw new IllegalArgumentException(SR.MULTIPLE_CREDENTIALS_PROVIDED);
    }

    try {
        final boolean usePathStyleUris = Utility.determinePathStyleFromUri(this.storageUri.getPrimaryUri());
        this.tableServiceClient = new CloudTableClient(PathUtility.getServiceClientBaseAddress(
                this.getStorageUri(), usePathStyleUris), credentials != null ? credentials : parsedCredentials);
        this.name = PathUtility.getTableNameFromUri(storageUri.getPrimaryUri(), usePathStyleUris);
    }
    catch (final URISyntaxException e) {
        throw Utility.generateNewUnexpectedStorageException(e);
    }
}
 
Example #22
Source File: BlobContainerProvider.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
protected BlobContainerProvider(AzureStorageFileSystem parent, String account, String connection, StorageCredentials credentials, boolean useAzureAD) throws IOException {
  this.parent = parent;
  try {
    this.account = account;
    this.connection = new URI(connection);
    cloudBlobClient = new CloudBlobClient(this.connection, credentials);
  } catch (URISyntaxException e) {
    throw new IOException(e);
  }
}
 
Example #23
Source File: SendRequestIntercept.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for SendRequestThrottle.
 * 
 * @param storageCreds
 *          - storage account credentials for signing packets.
 * 
 */
private SendRequestIntercept(StorageCredentials storageCreds,
    boolean allowConcurrentOOBIo, OperationContext opContext) {
  // Capture the send delay callback interface.
  this.storageCreds = storageCreds;
  this.allowConcurrentOOBIo = allowConcurrentOOBIo;
  this.opContext = opContext;
}
 
Example #24
Source File: FileSasTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileSAS() throws InvalidKeyException, IllegalArgumentException, StorageException,
        URISyntaxException, InterruptedException {
    SharedAccessFilePolicy policy = createSharedAccessPolicy(
            EnumSet.of(SharedAccessFilePermissions.READ, SharedAccessFilePermissions.LIST), 300);
    FileSharePermissions perms = new FileSharePermissions();

    perms.getSharedAccessPolicies().put("readperm", policy);
    this.share.uploadPermissions(perms);
    Thread.sleep(30000);

    CloudFile sasFile = new CloudFile(
            new URI(this.file.getUri().toString() + "?" + this.file.generateSharedAccessSignature(null, "readperm")));
    sasFile.download(new ByteArrayOutputStream());

    // do not give the client and check that the new file's client has the correct permissions
    CloudFile fileFromUri = new CloudFile(PathUtility.addToQuery(this.file.getStorageUri(),
            this.file.generateSharedAccessSignature(null, "readperm")));
    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(),
            fileFromUri.getServiceClient().getCredentials().getClass().toString());
    
    // create credentials from sas
    StorageCredentials creds = new StorageCredentialsSharedAccessSignature(
            this.file.generateSharedAccessSignature(policy, null, null));
    CloudFileClient client = new CloudFileClient(sasFile.getServiceClient().getStorageUri(), creds);

    CloudFile fileFromClient = client.getShareReference(this.file.getShare().getName()).getRootDirectoryReference()
            .getFileReference(this.file.getName());
    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(),
            fileFromClient.getServiceClient().getCredentials().getClass().toString());
    assertEquals(client, fileFromClient.getServiceClient());
}
 
Example #25
Source File: AzureNativeFileSystemStore.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Connect to Azure storage using shared access signature credentials.
 */
private void connectUsingSASCredentials(final String accountName,
    final String containerName, final String sas) throws InvalidKeyException,
    StorageException, IOException, URISyntaxException {
  StorageCredentials credentials = new StorageCredentialsSharedAccessSignature(
      sas);
  connectingUsingSAS = true;
  connectUsingCredentials(accountName, credentials, containerName);
}
 
Example #26
Source File: SasTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testBlobSaS() throws InvalidKeyException, IllegalArgumentException, StorageException,
        URISyntaxException, InterruptedException {
    SharedAccessBlobPolicy sp = createSharedAccessPolicy(
            EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.LIST), 3600);
    BlobContainerPermissions perms = new BlobContainerPermissions();

    perms.getSharedAccessPolicies().put("readperm", sp);
    this.container.uploadPermissions(perms);
    Thread.sleep(30000);

    CloudBlockBlob sasBlob = new CloudBlockBlob(new URI(this.blob.getUri().toString() + "?"
            + this.blob.generateSharedAccessSignature(null, "readperm")));
    sasBlob.download(new ByteArrayOutputStream());

    // do not give the client and check that the new blob's client has the correct perms
    CloudBlob blobFromUri = new CloudBlockBlob(PathUtility.addToQuery(this.blob.getStorageUri(),
            this.blob.generateSharedAccessSignature(null, "readperm")));
    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), blobFromUri.getServiceClient()
            .getCredentials().getClass().toString());

    // create credentials from sas
    StorageCredentials creds = new StorageCredentialsSharedAccessSignature(
            this.blob.generateSharedAccessSignature(null, "readperm"));
    CloudBlobClient bClient = new CloudBlobClient(sasBlob.getServiceClient().getStorageUri(), creds);

    CloudBlockBlob blobFromClient = bClient.getContainerReference(this.blob.getContainer().getName())
            .getBlockBlobReference(this.blob.getName());
    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), blobFromClient.getServiceClient()
            .getCredentials().getClass().toString());
    assertEquals(bClient, blobFromClient.getServiceClient());
}
 
Example #27
Source File: AzureStorageCredentialsDetails.java    From nifi with Apache License 2.0 4 votes vote down vote up
public AzureStorageCredentialsDetails(String storageAccountName, String storageSuffix, StorageCredentials storageCredentials) {
    this.storageAccountName = storageAccountName;
    this.storageSuffix = storageSuffix;
    this.storageCredentials = storageCredentials;
}
 
Example #28
Source File: TableTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testTableSas() throws StorageException, URISyntaxException, InvalidKeyException, InterruptedException {
    CloudTableClient tClient = TableTestHelper.createCloudTableClient();

    // use capital letters to make sure SAS signature converts name to lower case correctly
    String name = "CAPS" + TableTestHelper.generateRandomTableName();
    CloudTable table = tClient.getTableReference(name);
    table.create();

    TablePermissions expectedPermissions = new TablePermissions();
    String identifier = UUID.randomUUID().toString();
    // Add a policy, check setting and getting.
    SharedAccessTablePolicy policy1 = new SharedAccessTablePolicy();
    Calendar now = GregorianCalendar.getInstance();
    now.add(Calendar.MINUTE, -10);
    policy1.setSharedAccessStartTime(now.getTime());
    now.add(Calendar.MINUTE, 30);
    policy1.setSharedAccessExpiryTime(now.getTime());

    policy1.setPermissions(EnumSet.of(SharedAccessTablePermissions.ADD, SharedAccessTablePermissions.QUERY,
            SharedAccessTablePermissions.UPDATE, SharedAccessTablePermissions.DELETE));
    expectedPermissions.getSharedAccessPolicies().put(identifier, policy1);

    table.uploadPermissions(expectedPermissions);
    Thread.sleep(30000);

    // Insert 500 entities in Batches to query
    for (int i = 0; i < 5; i++) {
        TableBatchOperation batch = new TableBatchOperation();

        for (int j = 0; j < 100; j++) {
            Class1 ent = TableTestHelper.generateRandomEntity("javatables_batch_" + Integer.toString(i));
            ent.setRowKey(String.format("%06d", j));
            batch.insert(ent);
        }

        table.execute(batch);
    }

    String sasString = table.generateSharedAccessSignature(policy1, null, "javatables_batch_0", "0",
            "javatables_batch_9", "9");
    CloudTableClient tableClientFromPermission = new CloudTableClient(tClient.getEndpoint(),
            new StorageCredentialsSharedAccessSignature(sasString));

    CloudTable policySasTable = tableClientFromPermission.getTableReference(name);

    // do not give the client and check that the new table's client has the correct perms
    CloudTable tableFromUri = new CloudTable(PathUtility.addToQuery(table.getStorageUri(), table
            .generateSharedAccessSignature((SharedAccessTablePolicy) null, identifier, "javatables_batch_0", "0",
                    "javatables_batch_9", "9")));
    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), tableFromUri.getServiceClient()
            .getCredentials().getClass().toString());

    // create credentials from sas
    StorageCredentials creds = new StorageCredentialsSharedAccessSignature(
            table.generateSharedAccessSignature((SharedAccessTablePolicy) null, identifier, "javatables_batch_0", "0",
                    "javatables_batch_9", "9"));
    CloudTableClient tableClient = new CloudTableClient(policySasTable.getServiceClient().getStorageUri(), creds);

    // set some arbitrary settings to make sure they are passed on
    tableClient.getDefaultRequestOptions().setLocationMode(LocationMode.PRIMARY_THEN_SECONDARY);
    tableClient.getDefaultRequestOptions().setTimeoutIntervalInMs(1000);
    tableClient.getDefaultRequestOptions().setTablePayloadFormat(TablePayloadFormat.JsonNoMetadata);
    tableClient.getDefaultRequestOptions().setRetryPolicyFactory(new RetryNoRetry());

    tableFromUri = tableClient.getTableReference(table.getName());
    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), tableFromUri.getServiceClient()
            .getCredentials().getClass().toString());

    assertEquals(tableClient.getDefaultRequestOptions().getLocationMode(), tableFromUri.getServiceClient()
            .getDefaultRequestOptions().getLocationMode());
    assertEquals(tableClient.getDefaultRequestOptions().getTimeoutIntervalInMs(), tableFromUri.getServiceClient()
            .getDefaultRequestOptions().getTimeoutIntervalInMs());
    assertEquals(tableClient.getDefaultRequestOptions().getTablePayloadFormat(), tableFromUri.getServiceClient()
            .getDefaultRequestOptions().getTablePayloadFormat());
    assertEquals(tableClient.getDefaultRequestOptions().getRetryPolicyFactory().getClass(), tableFromUri
            .getServiceClient().getDefaultRequestOptions().getRetryPolicyFactory().getClass());
}
 
Example #29
Source File: SasTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
@Category ({ SecondaryTests.class, SlowTests.class })
public void testContainerSaS() throws IllegalArgumentException, StorageException, URISyntaxException,
        InvalidKeyException, InterruptedException {
    SharedAccessBlobPolicy sp1 = createSharedAccessPolicy(
            EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE,
                    SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.DELETE), 3600);
    SharedAccessBlobPolicy sp2 = createSharedAccessPolicy(
            EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.LIST), 3600);
    BlobContainerPermissions perms = new BlobContainerPermissions();

    perms.getSharedAccessPolicies().put("full", sp1);
    perms.getSharedAccessPolicies().put("readlist", sp2);
    this.container.uploadPermissions(perms);
    Thread.sleep(30000);

    String containerReadListSas = this.container.generateSharedAccessSignature(sp2, null);
    CloudBlobContainer readListContainer = new CloudBlobContainer(PathUtility.addToQuery(this.container.getUri(),
            containerReadListSas));

    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), readListContainer.getServiceClient()
            .getCredentials().getClass().toString());

    CloudBlockBlob blobFromSasContainer = readListContainer.getBlockBlobReference(this.blob.getName());
    blobFromSasContainer.download(new ByteArrayOutputStream());

    // do not give the client and check that the new container's client has the correct perms
    CloudBlobContainer containerFromUri = new CloudBlobContainer(PathUtility.addToQuery(
            readListContainer.getStorageUri(), this.container.generateSharedAccessSignature(null, "readlist")));
    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), containerFromUri.getServiceClient()
            .getCredentials().getClass().toString());
    
    // create credentials from sas
    StorageCredentials creds = new StorageCredentialsSharedAccessSignature(
            this.container.generateSharedAccessSignature(null, "readlist"));
    CloudBlobClient bClient = new CloudBlobClient(this.container.getServiceClient().getStorageUri(), creds);

    CloudBlobContainer containerFromClient = bClient.getContainerReference(this.container.getName());
    assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), containerFromClient.getServiceClient()
            .getCredentials().getClass().toString());
    assertEquals(bClient, containerFromClient.getServiceClient());
}
 
Example #30
Source File: AzureIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
private CamelContext createCamelContext(StorageCredentials creds) throws Exception {
    WildFlyCamelContext camelctx = new WildFlyCamelContext();
    Context jndictx = camelctx.getNamingContext();
    jndictx.rebind("creds", creds);
    return camelctx;
}