-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* V2 Encryption Bug 4MB file downloads (#41813) * testing, need to get this branch on my other workstation * adding broken test * updating tests * tests with logging * updating test and adding fix * making test paramaterized and adding change for only encryption v2 downloads * fixing imports * wip context addition * context implementation and sync / async tests * adding back unpulled tests * consolidating one of the async util methods * addressing all comments besides 1 * removing leftover changes and adding back test for V2 * kyle test * adjusting unencrypted blob length calculation * recording tests and fixing style * addressing comments * changelog update * fixing messed up recorded test * adding more test cases and updating adjustment method signature * adding missed recordings and fixing style * changing offsetAdjustment to a long to prevent int overflow, and added support for unencrypted blob length for blobinputstream * adding test for integer overflow, removing redundant test, adding helper method to blobinputstream * adjusting changelong * removing unused imports * changing blobinputstream util method to use long instead of Long * update versions * update readmes * update changelogs --------- Co-authored-by: Isabelle <[email protected]>
- Loading branch information
Showing
31 changed files
with
333 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...hy/src/main/java/com/azure/storage/blob/specialized/cryptography/EncryptedBlobLength.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.storage.blob.specialized.cryptography; | ||
|
||
import com.azure.core.util.logging.ClientLogger; | ||
|
||
import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.ENCRYPTION_PROTOCOL_V1; | ||
import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.ENCRYPTION_PROTOCOL_V2; | ||
import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.ENCRYPTION_PROTOCOL_V2_1; | ||
import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.NONCE_LENGTH; | ||
import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.TAG_LENGTH; | ||
|
||
/** | ||
* This class provides helper methods for adjusting encrypted downloads. | ||
*/ | ||
final class EncryptedBlobLength { | ||
private static final ClientLogger LOGGER = new ClientLogger(EncryptedBlobLength.class); | ||
|
||
static Long computeAdjustedBlobLength(EncryptionData encryptionData, Long encryptedLength) { | ||
switch (encryptionData.getEncryptionAgent().getProtocol()) { | ||
/* | ||
Technically, the total unencrypted length may be different for v1, | ||
but because this helper method is only used for partitioning ranged downloads, | ||
the size does not need to be adjusted for v1. | ||
*/ | ||
case ENCRYPTION_PROTOCOL_V1: | ||
return encryptedLength; | ||
case ENCRYPTION_PROTOCOL_V2: | ||
case ENCRYPTION_PROTOCOL_V2_1: | ||
long regionLength = encryptionData.getEncryptedRegionInfo().getDataLength(); | ||
long region = (long) Math.ceil((double) encryptedLength / (double) (regionLength + NONCE_LENGTH + TAG_LENGTH)); | ||
long offset = (NONCE_LENGTH + TAG_LENGTH) * region; | ||
return encryptedLength - offset; | ||
default: | ||
throw LOGGER.logExceptionAsError(new IllegalArgumentException("Unexpected protocol version")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...c/test/java/com/azure/storage/blob/specialized/cryptography/EncryptedBlobLengthTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.storage.blob.specialized.cryptography; | ||
|
||
import com.azure.storage.common.implementation.Constants; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.GCM_ENCRYPTION_REGION_LENGTH; | ||
import static com.azure.storage.blob.specialized.cryptography.CryptographyConstants.NONCE_LENGTH; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class EncryptedBlobLengthTests extends BlobCryptographyTestBase { | ||
|
||
//to prevent having to cast int -> long everywhere | ||
private static final long FOUR_MB = 4 * Constants.MB; | ||
private static final long SIXTEEN_MB = 16 * Constants.MB; | ||
private static final long ONE_MB = Constants.MB; | ||
private static final long ONE_KB = Constants.KB; | ||
|
||
@ParameterizedTest | ||
@MethodSource("correctAdjustedLengthV1Supplier") | ||
public void correctAdjustedLengthV1(Long encryptedLength) { | ||
EncryptionData encryptionData = new EncryptionData(); | ||
encryptionData.setEncryptionAgent(new EncryptionAgent("1.0", null)); | ||
|
||
Long newLength = EncryptedBlobLength.computeAdjustedBlobLength(encryptionData, encryptedLength); | ||
assertEquals(encryptedLength, newLength); | ||
} | ||
|
||
private static Stream<Arguments> correctAdjustedLengthV1Supplier() { | ||
return Stream.of( | ||
Arguments.of(FOUR_MB), | ||
Arguments.of(SIXTEEN_MB) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("correctAdjustedLengthV2Supplier") | ||
public void correctAdjustedLengthV2(Long encryptedLength, Long expectedLength) { | ||
EncryptionData encryptionData = new EncryptionData(); | ||
encryptionData.setEncryptionAgent(new EncryptionAgent("2.0", null)); | ||
encryptionData.setEncryptedRegionInfo(new EncryptedRegionInfo(GCM_ENCRYPTION_REGION_LENGTH, NONCE_LENGTH)); | ||
|
||
Long newLength = EncryptedBlobLength.computeAdjustedBlobLength(encryptionData, encryptedLength); | ||
assertEquals(expectedLength, newLength); | ||
} | ||
|
||
private static Stream<Arguments> correctAdjustedLengthV2Supplier() { | ||
return Stream.of( | ||
Arguments.of(FOUR_MB + 28, FOUR_MB), | ||
Arguments.of(FOUR_MB + 57, FOUR_MB + 1), | ||
Arguments.of(SIXTEEN_MB + 112, SIXTEEN_MB), | ||
Arguments.of(28L, 0L), | ||
Arguments.of(0L, 0L), | ||
Arguments.of(ONE_MB + 28, ONE_MB) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("correctAdjustedLengthVariableRegionSupplier") | ||
public void correctAdjustedLengthVariableRegion(Long encryptedLength, Long expectedLength, Long regionLength) { | ||
EncryptionData encryptionData = new EncryptionData(); | ||
encryptionData.setEncryptionAgent(new EncryptionAgent("2.1", null)); | ||
encryptionData.setEncryptedRegionInfo(new EncryptedRegionInfo(regionLength, NONCE_LENGTH)); | ||
|
||
Long newLength = EncryptedBlobLength.computeAdjustedBlobLength(encryptionData, encryptedLength); | ||
assertEquals(expectedLength, newLength); | ||
} | ||
|
||
private static Stream<Arguments> correctAdjustedLengthVariableRegionSupplier() { | ||
return Stream.of( | ||
Arguments.of(FOUR_MB + 112, FOUR_MB, ONE_MB), | ||
Arguments.of(SIXTEEN_MB + 448, SIXTEEN_MB, ONE_MB), | ||
Arguments.of(FOUR_MB + 114688, FOUR_MB, ONE_KB), | ||
Arguments.of(SIXTEEN_MB + 458752, SIXTEEN_MB, ONE_KB), | ||
Arguments.of(FOUR_MB + 448, FOUR_MB, 256 * ONE_KB), | ||
Arguments.of(SIXTEEN_MB + 1792, SIXTEEN_MB, 256 * ONE_KB), | ||
Arguments.of(ONE_MB + 28672, ONE_MB, ONE_KB), | ||
Arguments.of(ONE_MB + 28, ONE_MB, ONE_MB) | ||
); | ||
} | ||
|
||
@Test | ||
public void badProtocol() { | ||
EncryptionData encryptionData = new EncryptionData(); | ||
encryptionData.setEncryptionAgent(new EncryptionAgent("garbage", null)); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> EncryptedBlobLength.computeAdjustedBlobLength(encryptionData, | ||
null)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.