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

Azure Storage Common Package #4658

Closed
Closed
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 pom.client.xml
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@
<!-- As recommended by Eng Sys team, removing tracing as it is built and released separately -->
<!-- <module>./sdk/tracing</module>-->
<module>./sdk/identity/azure-identity</module>
<module>./storage/client/common</module>
<module>./storage/client/blob</module>
<module>./storage/client/file</module>
<module>./storage/client/queue</module>
Expand Down
5 changes: 3 additions & 2 deletions storage/client/blob/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
<version>1.0.0-preview.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<groupId>com.azure</groupId>
<artifactId>azure-storage-common</artifactId>
<version>12.0.0-preview.2</version>
</dependency>

<!-- Added this dependency to include necessary annotations used by reactor core.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@

package com.azure.storage.blob;

import com.azure.storage.common.Constants;
import com.azure.storage.common.IPRange;
import com.azure.storage.common.SASProtocol;
import com.azure.storage.common.Utility;
import com.azure.storage.common.credentials.SharedKeyCredential;

import java.security.InvalidKeyException;
import java.time.OffsetDateTime;

/**
* AccountSASSignatureValues is used to generate a Shared Access Signature (SAS) for an Azure Storage account. Once
* all the values here are set appropriately, call generateSASQueryParameters to obtain a representation of the SAS
* which can actually be applied to blob urls. Note: that both this class and {@link SASQueryParameters} exist because
* the former is mutable and a logical representation while the latter is immutable and used to generate actual REST
* AccountSASSignatureValues is used to generate a Shared Access Signature (SAS) for an Azure Storage account. Once all
* the values here are set appropriately, call generateSASQueryParameters to obtain a representation of the SAS which
* can actually be applied to blob urls. Note: that both this class and {@link SASQueryParameters} exist because the
* former is mutable and a logical representation while the latter is immutable and used to generate actual REST
* requests.
* <p>
* Please see
Expand All @@ -24,9 +27,9 @@
* <a href=https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-an-account-sas>here</a> for further
* descriptions of the parameters, including which are required:
*
* @apiNote ## Sample Code \n
* [!code-java[Sample_Code](../azure-storage-java/src/test/java/com/microsoft/azure/storage/Samples.java?name=account_sas "Sample code for AccountSASSignatureValues")] \n
* For more samples, please see the [Samples file](%https://github.com/Azure/azure-storage-java/blob/master/src/test/java/com/microsoft/azure/storage/Samples.java)
* @apiNote ## Sample Code \n [!code-java[Sample_Code](../azure-storage-java/src/test/java/com/microsoft/azure/storage/Samples.java?name=account_sas
* "Sample code for AccountSASSignatureValues")] \n For more samples, please see the [Samples
* file](%https://github.com/Azure/azure-storage-java/blob/master/src/test/java/com/microsoft/azure/storage/Samples.java)
*/
final class AccountSASSignatureValues {

Expand All @@ -47,8 +50,8 @@ final class AccountSASSignatureValues {
private String resourceTypes;

/**
* Initializes an {@code AccountSASSignatureValues} object with the version number set to the default and all
* other values empty.
* Initializes an {@code AccountSASSignatureValues} object with the version number set to the default and all other
* values empty.
*/
AccountSASSignatureValues() {
}
Expand Down Expand Up @@ -163,16 +166,16 @@ public AccountSASSignatureValues services(String services) {
}

/**
* The values that indicate the resource types accessible with this SAS. Please refer
* to {@link AccountSASResourceType} to construct this value.
* The values that indicate the resource types accessible with this SAS. Please refer to {@link
* AccountSASResourceType} to construct this value.
*/
public String resourceTypes() {
return resourceTypes;
}

/**
* The values that indicate the resource types accessible with this SAS. Please refer
* to {@link AccountSASResourceType} to construct this value.
* The values that indicate the resource types accessible with this SAS. Please refer to {@link
* AccountSASResourceType} to construct this value.
*/
public AccountSASSignatureValues resourceTypes(String resourceTypes) {
this.resourceTypes = resourceTypes;
Expand All @@ -183,11 +186,10 @@ public AccountSASSignatureValues resourceTypes(String resourceTypes) {
* Generates a {@link SASQueryParameters} object which contains all SAS query parameters needed to make an actual
* REST request.
*
* @param sharedKeyCredentials
* Credentials for the storage account and corresponding primary or secondary key.
*
* @param sharedKeyCredentials Credentials for the storage account and corresponding primary or secondary key.
* @return {@link SASQueryParameters}
* @throws RuntimeException If the HMAC-SHA256 signature for {@code sharedKeyCredentials} fails to generate.
* @throws RuntimeException If the HMAC-SHA256 algorithm isn't support, if the key isn't a valid Base64 encoded
* string, or the UTF-8 charset isn't supported.
*/
public SASQueryParameters generateSASQueryParameters(SharedKeyCredential sharedKeyCredentials) {
Utility.assertNotNull("SharedKeyCredential", sharedKeyCredentials);
Expand All @@ -198,32 +200,25 @@ public SASQueryParameters generateSASQueryParameters(SharedKeyCredential sharedK
Utility.assertNotNull("version", this.version);

// Signature is generated on the un-url-encoded values.
final String stringToSign = stringToSign(sharedKeyCredentials);

String signature;
try {
signature = sharedKeyCredentials.computeHmac256(stringToSign);
} catch (InvalidKeyException e) {
throw new RuntimeException(e); // The key should have been validated by now. If it is no longer valid here, we fail.
}
String signature = sharedKeyCredentials.computeHmac256(stringToSign(sharedKeyCredentials));

return new SASQueryParameters(this.version, this.services, resourceTypes,
this.protocol, this.startTime, this.expiryTime, this.ipRange, null,
null, this.permissions, signature, null, null, null, null, null, null);
this.protocol, this.startTime, this.expiryTime, this.ipRange, null,
null, this.permissions, signature, null, null, null, null, null, null);
}

private String stringToSign(final SharedKeyCredential sharedKeyCredentials) {
return String.join("\n",
sharedKeyCredentials.accountName(),
AccountSASPermission.parse(this.permissions).toString(), // guarantees ordering
this.services,
resourceTypes,
this.startTime == null ? "" : Utility.ISO_8601_UTC_DATE_FORMATTER.format(this.startTime),
Utility.ISO_8601_UTC_DATE_FORMATTER.format(this.expiryTime),
this.ipRange == null ? (new IPRange()).toString() : this.ipRange.toString(),
this.protocol == null ? "" : this.protocol.toString(),
this.version,
Constants.EMPTY_STRING // Account SAS requires an additional newline character
sharedKeyCredentials.accountName(),
AccountSASPermission.parse(this.permissions).toString(), // guarantees ordering
this.services,
resourceTypes,
this.startTime == null ? "" : Utility.ISO_8601_UTC_DATE_FORMATTER.format(this.startTime),
Utility.ISO_8601_UTC_DATE_FORMATTER.format(this.expiryTime),
this.ipRange == null ? Constants.EMPTY_STRING : this.ipRange.toString(),
this.protocol == null ? "" : this.protocol.toString(),
this.version,
Constants.EMPTY_STRING // Account SAS requires an additional newline character
);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
import com.azure.storage.blob.models.BlobRange;
import com.azure.storage.blob.models.Metadata;
import com.azure.storage.blob.models.SourceModifiedAccessConditions;
import com.azure.storage.common.Constants;
import io.netty.buffer.ByteBuf;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.URL;

import static com.azure.storage.blob.Utility.postProcessResponse;


/**
* Client to an append blob. It may only be instantiated through a {@link BlobClientBuilder#buildAppendBlobAsyncClient()},
* via the method {@link BlobAsyncClient#asAppendBlobAsyncClient()}, or via the method
Expand Down Expand Up @@ -92,7 +90,7 @@ public Mono<Response<AppendBlobItem>> create(BlobHTTPHeaders headers, Metadata m
metadata = (metadata == null) ? new Metadata() : metadata;
accessConditions = (accessConditions == null) ? new BlobAccessConditions() : accessConditions;

return postProcessResponse(this.azureBlobStorage.appendBlobs().createWithRestResponseAsync(null,
return PostProcessor.postProcessResponse(this.azureBlobStorage.appendBlobs().createWithRestResponseAsync(null,
null, 0, null, metadata, null, null,
null, null, headers, accessConditions.leaseAccessConditions(),
accessConditions.modifiedAccessConditions(), Context.NONE))
Expand Down Expand Up @@ -142,7 +140,7 @@ public Mono<Response<AppendBlobItem>> appendBlock(Flux<ByteBuf> data, long lengt
appendBlobAccessConditions = appendBlobAccessConditions == null ? new AppendBlobAccessConditions()
: appendBlobAccessConditions;

return postProcessResponse(this.azureBlobStorage.appendBlobs().appendBlockWithRestResponseAsync(
return PostProcessor.postProcessResponse(this.azureBlobStorage.appendBlobs().appendBlockWithRestResponseAsync(
null, null, data, length, null, null,
null, null, null, null,
appendBlobAccessConditions.leaseAccessConditions(),
Expand Down Expand Up @@ -198,7 +196,7 @@ public Mono<Response<AppendBlobItem>> appendBlockFromUrl(URL sourceURL, BlobRang
destAccessConditions = destAccessConditions == null
? new AppendBlobAccessConditions() : destAccessConditions;

return postProcessResponse(
return PostProcessor.postProcessResponse(
this.azureBlobStorage.appendBlobs().appendBlockFromUrlWithRestResponseAsync(null, null,
sourceURL, 0, sourceRange.toString(), sourceContentMD5, null, null,
destAccessConditions.leaseAccessConditions(),
Expand Down
Loading