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

Modified logic for request condition validation to throw after determining all invalid conditions #22532

Merged
merged 4 commits into from
Jun 25, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,44 @@

package com.azure.storage.blob.implementation.util;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public enum BlobRequestConditionProperty {
LEASE_ID,
TAGS_CONDITIONS,
IF_MODIFIED_SINCE,
IF_UNMODIFIED_SINCE,
IF_MATCH,
IF_NONE_MATCH;
LEASE_ID("LeaseId"),
TAGS_CONDITIONS("TagsConditions"),
IF_MODIFIED_SINCE("IfModifiedSince"),
IF_UNMODIFIED_SINCE("IfUnmodifiedSince"),
IF_MATCH("IfMatch"),
IF_NONE_MATCH("IfNoneMatch");

/** The actual serialized value for a BlobRequestConditionProperty instance. */
private final String value;

BlobRequestConditionProperty(String value) {
this.value = value;
}

/**
* Parses a serialized value to a BlobRequestConditionProperty instance.
*
* @param value the serialized value to parse.
* @return the parsed BlobRequestConditionProperty object, or null if unable to parse.
*/
@JsonCreator
public static BlobRequestConditionProperty fromString(String value) {
BlobRequestConditionProperty[] items = BlobRequestConditionProperty.values();
for (BlobRequestConditionProperty item : items) {
if (item.toString().equalsIgnoreCase(value)) {
return item;
}
}
return null;
}

@JsonValue
@Override
public String toString() {
return this.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -504,37 +504,65 @@ public static BlobQueryHeaders transformQueryHeaders(HttpHeaders headers) {
}

public static void validateConditionsNotPresent(BlobRequestConditions requestConditions,
EnumSet<BlobRequestConditionProperty> invalidConditions) {
EnumSet<BlobRequestConditionProperty> invalidConditions, String operationName, String parameterName) {
if (requestConditions == null) {
return;
}
if (invalidConditions.contains(BlobRequestConditionProperty.LEASE_ID)
&& requestConditions.getLeaseId() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'leaseId' is not applicable to this API."));
}
if (invalidConditions.contains(BlobRequestConditionProperty.TAGS_CONDITIONS)
&& requestConditions.getTagsConditions() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'tagsConditions' is not applicable to "
+ "this API."));
}
if (invalidConditions.contains(BlobRequestConditionProperty.IF_MODIFIED_SINCE)
&& requestConditions.getIfModifiedSince() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifModifiedSince' is not applicable to "
+ "this API."));
}
if (invalidConditions.contains(BlobRequestConditionProperty.IF_UNMODIFIED_SINCE)
&& requestConditions.getIfUnmodifiedSince() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifUnmodifiedSince' is not applicable to "
+ "this API."));
}
if (invalidConditions.contains(BlobRequestConditionProperty.IF_MATCH)
&& requestConditions.getIfMatch() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifMatch' is not applicable to this API."));
List<String> invalidConditionsFound = null;

for (BlobRequestConditionProperty condition : invalidConditions) {
switch (condition) {
case LEASE_ID:
if (requestConditions.getLeaseId() != null) {
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
: invalidConditionsFound;
invalidConditionsFound.add(BlobRequestConditionProperty.LEASE_ID.toString());
}
break;
case TAGS_CONDITIONS:
if (requestConditions.getTagsConditions() != null) {
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
: invalidConditionsFound;
invalidConditionsFound.add(BlobRequestConditionProperty.TAGS_CONDITIONS.toString());
}
break;
case IF_MODIFIED_SINCE:
if (requestConditions.getIfModifiedSince() != null) {
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
: invalidConditionsFound;
invalidConditionsFound.add(BlobRequestConditionProperty.IF_MODIFIED_SINCE.toString());
}
break;
case IF_UNMODIFIED_SINCE:
if (requestConditions.getIfUnmodifiedSince() != null) {
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
: invalidConditionsFound;
invalidConditionsFound.add(BlobRequestConditionProperty.IF_UNMODIFIED_SINCE.toString());
}
break;
case IF_MATCH:
if (requestConditions.getIfMatch() != null) {
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
: invalidConditionsFound;
invalidConditionsFound.add(BlobRequestConditionProperty.IF_MATCH.toString());
}
break;
case IF_NONE_MATCH:
if (requestConditions.getIfNoneMatch() != null) {
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
: invalidConditionsFound;
invalidConditionsFound.add(BlobRequestConditionProperty.IF_NONE_MATCH.toString());
}
break;
default:
break;
}
}
if (invalidConditions.contains(BlobRequestConditionProperty.IF_NONE_MATCH)
&& requestConditions.getIfNoneMatch() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifNoneMatch' is not applicable to this "
+ "API."));
if (invalidConditionsFound != null && !invalidConditionsFound.isEmpty()) {
String unsupported = String.join(", ", invalidConditionsFound);
throw LOGGER.logExceptionAsError(new IllegalArgumentException(
String.format("%s does not support the %s request condition(s) for parameter '%s'.",
operationName, unsupported, parameterName)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,8 @@ Mono<Response<BlobImmutabilityPolicy>> setImmutabilityPolicyWithResponse(
ModelHelper.validateConditionsNotPresent(finalRequestConditions,
EnumSet.of(BlobRequestConditionProperty.LEASE_ID, BlobRequestConditionProperty.TAGS_CONDITIONS,
BlobRequestConditionProperty.IF_MATCH, BlobRequestConditionProperty.IF_NONE_MATCH,
BlobRequestConditionProperty.IF_MODIFIED_SINCE));
BlobRequestConditionProperty.IF_MODIFIED_SINCE), "setImmutabilityPolicy(WithResponse)",
"requestConditions");

return this.azureBlobStorage.getBlobs().setImmutabilityPolicyWithResponseAsync(containerName, blobName, null,
null, finalRequestConditions.getIfUnmodifiedSince(), finalImmutabilityPolicy.getExpiryTime(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,16 @@ class ImmutableStorageWithVersioningTest extends APISpec {

then:
def e = thrown(IllegalArgumentException)
e.getMessage() == wrongCondition + " is not applicable to this API."
e.getMessage() == String.format("%s does not support the %s request condition(s) for parameter 'requestConditions'.", "setImmutabilityPolicy(WithResponse)", wrongCondition)

where:
leaseId | tags | ifMatch | ifNoneMatch | ifModifiedSince || wrongCondition
"leaseId" | null | null | null | null || "'leaseId'"
null | "tagsConditions" | null | null | null || "'tagsConditions'"
null | null | "ifMatch" | null | null || "'ifMatch'"
null | null | null | "ifNoneMatch" | null || "'ifNoneMatch'"
null | null | null | null | oldDate || "'ifModifiedSince'"
"leaseId" | null | null | null | null || "LeaseId"
null | "tagsConditions" | null | null | null || "TagsConditions"
null | null | "ifMatch" | null | null || "IfMatch"
null | null | null | "ifNoneMatch" | null || "IfNoneMatch"
null | null | null | null | oldDate || "IfModifiedSince"
"leaseId" | "tagsConditions" | "ifMatch" | "ifNoneMatch" | oldDate || "LeaseId, TagsConditions, IfModifiedSince, IfMatch, IfNoneMatch"
}

def "set immutability policy error"() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
{
"networkCallRecords" : [ {
"Method" : "PUT",
"Uri" : "https://REDACTED.blob.core.windows.net/1e8e8c1301e8e8c13d1e4448478653498e9004a219f2?restype=container",
"Uri" : "https://REDACTED.blob.core.windows.net/1e8e8c1301e8e8c134c16197853efefaa66994939a49?restype=container",
"Headers" : {
"x-ms-version" : "2020-10-02",
"User-Agent" : "azsdk-java-azure-storage-blob/12.12.0-beta.2 (11.0.7; Windows 10; 10.0)",
"x-ms-client-request-id" : "da7835f2-2cb0-40eb-9f13-0e808de4b12f"
"User-Agent" : "azsdk-java-azure-storage-blob/12.13.0-beta.1 (11.0.7; Windows 10; 10.0)",
"x-ms-client-request-id" : "38ccfa4b-f382-457e-8fcb-488cfede7886"
},
"Response" : {
"Transfer-Encoding" : "chunked",
"x-ms-version" : "2020-10-02",
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
"eTag" : "0x8D93288C91D6509",
"Last-Modified" : "Fri, 18 Jun 2021 18:42:12 GMT",
"eTag" : "0x8D9375C70795929",
"Last-Modified" : "Thu, 24 Jun 2021 22:07:21 GMT",
"retry-after" : "0",
"StatusCode" : "201",
"x-ms-request-id" : "fc34c3f0-f01e-0001-1571-649c3e000000",
"x-ms-client-request-id" : "da7835f2-2cb0-40eb-9f13-0e808de4b12f",
"Date" : "Fri, 18 Jun 2021 18:42:11 GMT"
"x-ms-request-id" : "8dd6dbdf-401e-0021-1f45-69bb8d000000",
"x-ms-client-request-id" : "38ccfa4b-f382-457e-8fcb-488cfede7886",
"Date" : "Thu, 24 Jun 2021 22:07:20 GMT"
},
"Exception" : null
}, {
"Method" : "PUT",
"Uri" : "https://REDACTED.blob.core.windows.net/1e8e8c1311e8e8c13d1e86956ffcc18240e2b4ad1b61/1e8e8c1321e8e8c13d1e55651a3205039d57541cc88c",
"Uri" : "https://REDACTED.blob.core.windows.net/1e8e8c1311e8e8c134c1025670c7fcee3e5fd4a51807/1e8e8c1321e8e8c134c169613ee066555b8ed4e12bb0",
"Headers" : {
"x-ms-version" : "2020-10-02",
"User-Agent" : "azsdk-java-azure-storage-blob/12.12.0-beta.2 (11.0.7; Windows 10; 10.0)",
"x-ms-client-request-id" : "b96decb4-acca-4a6c-ada0-f82fe8090be7",
"User-Agent" : "azsdk-java-azure-storage-blob/12.13.0-beta.1 (11.0.7; Windows 10; 10.0)",
"x-ms-client-request-id" : "5b028f24-a557-4591-a57c-deca6a9d878b",
"Content-Type" : "application/octet-stream"
},
"Response" : {
"Transfer-Encoding" : "chunked",
"x-ms-version" : "2020-10-02",
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
"x-ms-content-crc64" : "AAAAAAAAAAA=",
"Last-Modified" : "Fri, 18 Jun 2021 18:42:15 GMT",
"x-ms-version-id" : "2021-06-18T18:42:15.2590905Z",
"Last-Modified" : "Thu, 24 Jun 2021 22:07:23 GMT",
"x-ms-version-id" : "2021-06-24T22:07:23.7038470Z",
"retry-after" : "0",
"StatusCode" : "201",
"x-ms-request-server-encrypted" : "true",
"Date" : "Fri, 18 Jun 2021 18:42:15 GMT",
"Date" : "Thu, 24 Jun 2021 22:07:22 GMT",
"Content-MD5" : "1B2M2Y8AsgTpgAmY7PhCfg==",
"eTag" : "0x8D93288CB03A639",
"x-ms-request-id" : "fc34c401-f01e-0001-2271-649c3e000000",
"x-ms-client-request-id" : "b96decb4-acca-4a6c-ada0-f82fe8090be7"
"eTag" : "0x8D9375C71E5C586",
"x-ms-request-id" : "8dd6dbf3-401e-0021-2c45-69bb8d000000",
"x-ms-client-request-id" : "5b028f24-a557-4591-a57c-deca6a9d878b"
},
"Exception" : null
} ],
"variables" : [ "1e8e8c1301e8e8c13d1e4448478653498e9004a219f2", "1e8e8c1311e8e8c13d1e86956ffcc18240e2b4ad1b61", "1e8e8c1321e8e8c13d1e55651a3205039d57541cc88c", "2021-06-18T18:42:16.251301Z" ]
"variables" : [ "1e8e8c1301e8e8c134c16197853efefaa66994939a49", "1e8e8c1311e8e8c134c1025670c7fcee3e5fd4a51807", "1e8e8c1321e8e8c134c169613ee066555b8ed4e12bb0", "2021-06-24T22:07:24.781735500Z" ]
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
{
"networkCallRecords" : [ {
"Method" : "PUT",
"Uri" : "https://REDACTED.blob.core.windows.net/0795bd5200795bd5264a871964e42487c620d4113860?restype=container",
"Uri" : "https://REDACTED.blob.core.windows.net/0795bd5200795bd52d4e792326e09af9e30304f7c95d?restype=container",
"Headers" : {
"x-ms-version" : "2020-10-02",
"User-Agent" : "azsdk-java-azure-storage-blob/12.12.0-beta.2 (11.0.7; Windows 10; 10.0)",
"x-ms-client-request-id" : "57037666-1f95-4d07-9930-802b4c50fce4"
"User-Agent" : "azsdk-java-azure-storage-blob/12.13.0-beta.1 (11.0.7; Windows 10; 10.0)",
"x-ms-client-request-id" : "fc52422a-ff7c-4fc6-b325-b5fec7607ace"
},
"Response" : {
"Transfer-Encoding" : "chunked",
"x-ms-version" : "2020-10-02",
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
"eTag" : "0x8D93288CC0E4AE6",
"Last-Modified" : "Fri, 18 Jun 2021 18:42:17 GMT",
"eTag" : "0x8D9375C72E47FA9",
"Last-Modified" : "Thu, 24 Jun 2021 22:07:25 GMT",
"retry-after" : "0",
"StatusCode" : "201",
"x-ms-request-id" : "fc34c419-f01e-0001-3671-649c3e000000",
"x-ms-client-request-id" : "57037666-1f95-4d07-9930-802b4c50fce4",
"Date" : "Fri, 18 Jun 2021 18:42:16 GMT"
"x-ms-request-id" : "8dd6dbfb-401e-0021-3245-69bb8d000000",
"x-ms-client-request-id" : "fc52422a-ff7c-4fc6-b325-b5fec7607ace",
"Date" : "Thu, 24 Jun 2021 22:07:25 GMT"
},
"Exception" : null
}, {
"Method" : "PUT",
"Uri" : "https://REDACTED.blob.core.windows.net/0795bd5210795bd5264a783820c02b3169fc34074a83/0795bd5220795bd5264a93710522f48ec6a7f418d8e3",
"Uri" : "https://REDACTED.blob.core.windows.net/0795bd5210795bd52d4e888129f3fcdcd93de47a491b/0795bd5220795bd52d4e26102b804dc34efd24decb63",
"Headers" : {
"x-ms-version" : "2020-10-02",
"User-Agent" : "azsdk-java-azure-storage-blob/12.12.0-beta.2 (11.0.7; Windows 10; 10.0)",
"x-ms-client-request-id" : "f6693c7e-5d3d-4091-9256-e17a00c03888",
"User-Agent" : "azsdk-java-azure-storage-blob/12.13.0-beta.1 (11.0.7; Windows 10; 10.0)",
"x-ms-client-request-id" : "cb80a63c-6649-4bd0-981d-399bab45a4b1",
"Content-Type" : "application/octet-stream"
},
"Response" : {
"Transfer-Encoding" : "chunked",
"x-ms-version" : "2020-10-02",
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
"x-ms-content-crc64" : "AAAAAAAAAAA=",
"Last-Modified" : "Fri, 18 Jun 2021 18:42:19 GMT",
"x-ms-version-id" : "2021-06-18T18:42:19.0179529Z",
"Last-Modified" : "Thu, 24 Jun 2021 22:07:26 GMT",
"x-ms-version-id" : "2021-06-24T22:07:26.2584157Z",
"retry-after" : "0",
"StatusCode" : "201",
"x-ms-request-server-encrypted" : "true",
"Date" : "Fri, 18 Jun 2021 18:42:18 GMT",
"Date" : "Thu, 24 Jun 2021 22:07:26 GMT",
"Content-MD5" : "1B2M2Y8AsgTpgAmY7PhCfg==",
"eTag" : "0x8D93288CD4134C9",
"x-ms-request-id" : "fc34c428-f01e-0001-4271-649c3e000000",
"x-ms-client-request-id" : "f6693c7e-5d3d-4091-9256-e17a00c03888"
"eTag" : "0x8D9375C736B915D",
"x-ms-request-id" : "8dd6dc13-401e-0021-4945-69bb8d000000",
"x-ms-client-request-id" : "cb80a63c-6649-4bd0-981d-399bab45a4b1"
},
"Exception" : null
} ],
"variables" : [ "0795bd5200795bd5264a871964e42487c620d4113860", "0795bd5210795bd5264a783820c02b3169fc34074a83", "0795bd5220795bd5264a93710522f48ec6a7f418d8e3", "2021-06-18T18:42:19.976318300Z" ]
"variables" : [ "0795bd5200795bd52d4e792326e09af9e30304f7c95d", "0795bd5210795bd52d4e888129f3fcdcd93de47a491b", "0795bd5220795bd52d4e26102b804dc34efd24decb63", "2021-06-24T22:07:27.286533500Z" ]
}
Loading