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

[Client encryption]: Adds support for Brotli compression #4769

Merged
merged 71 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
d6f89ea
Add baseline benchmarks for `Microsoft.Azure.Cosmos.Encryption.Custom`
juraj-blazek Sep 12, 2024
8928b88
Cleanup
juraj-blazek Sep 12, 2024
9dc7d54
Use set of static test data for benchmarks
juraj-blazek Sep 12, 2024
368dc25
Merge branch 'master' into users/juraj-blazek/encryption-benchmark-ba…
juraj-blazek Sep 12, 2024
38a198a
Add non-allocating APIs to encryptors
juraj-blazek Sep 16, 2024
e0bb8bf
WIP
juraj-blazek Sep 16, 2024
9c3c276
Revert solution update
juraj-blazek Sep 16, 2024
c37a092
Merge branch 'users/juraj-blazek/encryption-benchmark-baseline' of ht…
juraj-blazek Sep 16, 2024
f4db1c6
Merge branch 'master' into users/juraj-blazek/encryption-benchmark-ba…
juraj-blazek Sep 16, 2024
91714f9
Merge branch 'master' into users/juraj-blazek/encryption-benchmark-ba…
juraj-blazek Sep 17, 2024
2058f8b
Merge remote-tracking branch 'origin/master' into users/juraj-blazek/…
juraj-blazek Sep 17, 2024
f33538c
Add implementation, fix tests
juraj-blazek Sep 17, 2024
6923fd2
Switch to randomized encryption for benchmarks
juraj-blazek Sep 17, 2024
4ea8690
Merge branch 'master' into users/juraj-blazek/encryption-benchmark-ba…
kr-santosh Sep 18, 2024
03ef682
Some more array pooling
juraj-blazek Sep 17, 2024
1044a89
Streaming deserialization
juraj-blazek Sep 19, 2024
f1db54d
Merge remote-tracking branch 'origin/master' into users/juraj-blazek/…
juraj-blazek Sep 19, 2024
05bfc50
Cleanup
juraj-blazek Sep 20, 2024
5629f74
Update MDE and rerun benchmarks
juraj-blazek Sep 20, 2024
495d2c4
Add non-allocating APIs to encryptors
juraj-blazek Sep 16, 2024
b3f74b1
Merge branch 'master' into users/juraj-blazek/non-allocating-encryptor
Sep 30, 2024
14bce37
~ drop repeated DEK calls
Sep 30, 2024
4ff1601
! typo
Sep 30, 2024
d8a345c
~ update benchmark
Sep 30, 2024
03c06e0
~ fix tests
Sep 30, 2024
a1025c4
Merge branch 'Azure:master' into users/juraj-blazek/non-allocating-en…
JanHyka Oct 1, 2024
3bf77c8
~ cleanup
Oct 1, 2024
ceaa8b5
+ refresh benchmark
Oct 1, 2024
611b3ac
+ unit test
Oct 1, 2024
9ca89d4
~ merge predecessor PR
Oct 1, 2024
8a78fe8
~ merge fixes and initial cleanup
Oct 1, 2024
8ed2135
~ write directly to output document instead of copying
Oct 1, 2024
bbe9845
! tests
Oct 1, 2024
a107f62
~ retrieve DataEncryptionKey only once per document
Oct 1, 2024
a1ad02b
! fix tests
Oct 1, 2024
4f2f072
~ update Aes algorithm to reuse GetEncryptedByteCount
Oct 2, 2024
cbbeee2
~ refactor EncryptionProcessor
Oct 4, 2024
b6c851c
! names
Oct 4, 2024
72ccae7
~ less static
Oct 4, 2024
8ea5879
Merge branch 'master' into users/juraj-blazek/encryption-array-pooling
Oct 6, 2024
8c60e69
Merge branch 'master' into users/juraj-blazek/encryption-array-pooling
Oct 6, 2024
5554aa0
~ merge fixes
Oct 6, 2024
28620ed
~ cleanup
Oct 6, 2024
eb059c8
~ unwanted changes
Oct 6, 2024
cc2eab5
- unused method
Oct 6, 2024
c9ba300
~ updates (PR)
Oct 6, 2024
9f9cbca
~ add stable vs preview release duplicity
Oct 7, 2024
c347e71
Merge branch 'users/juraj-blazek/encryption-array-pooling' into users…
Oct 7, 2024
64172b8
~ cleanup and parent branch merge
Oct 7, 2024
ff583f9
Merge branch 'master' into users/jan-hyka/refactor-encryption-processor
Oct 7, 2024
0ba8c34
Merge branch 'master' into users/jan-hyka/refactor-encryption-processor
Oct 7, 2024
326b1be
~ master merges
Oct 7, 2024
c520e16
- duplicate
Oct 7, 2024
5c40821
~ cleanup
Oct 7, 2024
6dad4cd
+ initial commit
Oct 8, 2024
99c7a75
+ Add .NET8.0 target for Cosmos.Encryption.Custom
Oct 8, 2024
31c20e7
- remove implicit IsPreview from csproj
Oct 8, 2024
306f655
Merge branch 'users/jan-hyka/add-net8-target' into users/jan-hyka/brotli
Oct 8, 2024
0a627bd
~ fixes
Oct 8, 2024
78da8bb
+ unit tests
Oct 8, 2024
17f79e7
Merge branch 'master' into users/jan-hyka/brotli
Oct 10, 2024
ff08a1f
~ post merge fixes
Oct 10, 2024
d7a5be4
Merge branch 'master' into users/jan-hyka/brotli
Oct 10, 2024
a058cb1
~ fix tests for version difference between stable and preview mode
Oct 10, 2024
ae8049b
Merge branch 'master' into users/jan-hyka/brotli
JanHyka Oct 11, 2024
8630cd4
~ cleanup
Oct 11, 2024
7dd48bb
Merge branch 'users/jan-hyka/brotli' of https://github.com/juraj-blaz…
Oct 11, 2024
450df8b
~ refactor as pre PR
Oct 11, 2024
b061d1a
Merge branch 'master' into users/jan-hyka/brotli
kirankumarkolli Oct 11, 2024
96041ab
! fix breaking change
Oct 11, 2024
e9c7cf1
Merge branch 'users/jan-hyka/brotli' of https://github.com/juraj-blaz…
Oct 11, 2024
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 @@ -33,11 +33,7 @@ public enum CompressionAlgorithm
/// <summary>
/// Gets or sets compression algorithm.
/// </summary>
#if NET8_0_OR_GREATER
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved
public CompressionAlgorithm Algorithm { get; set; } = CompressionAlgorithm.Brotli;
#else
public CompressionAlgorithm Algorithm { get; set; } = CompressionAlgorithm.None;
#endif

/// <summary>
/// Gets or sets compression level.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Microsoft.Azure.Cosmos.Encryption.Custom.Transformation
{
#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
#if NET8_0_OR_GREATER

using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO.Compression;

internal class BrotliCompressor : IDisposable
Expand Down Expand Up @@ -44,13 +45,13 @@ public BrotliCompressor(CompressionLevel compressionLevel)
this.encoder = new BrotliEncoder(BrotliCompressor.GetQualityFromCompressionLevel(compressionLevel), DefaultWindow);
}

public virtual int Compress(EncryptionProperties properties, string path, byte[] bytes, int length, byte[] outputBytes)
public virtual int Compress(Dictionary<string, int> compressedPaths, string path, byte[] bytes, int length, byte[] outputBytes)
{
OperationStatus status = this.encoder.Compress(bytes.AsSpan(0, length), outputBytes, out int bytesConsumed, out int bytesWritten, true);

ThrowIfFailure(status, length, bytesConsumed);

properties.CompressedEncryptedPaths[path] = length;
compressedPaths[path] = length;

return bytesWritten;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ public async Task<Stream> EncryptAsync(

DataEncryptionKey encryptionKey = await encryptor.GetEncryptionKeyAsync(encryptionOptions.DataEncryptionKeyId, encryptionOptions.EncryptionAlgorithm, token);

EncryptionProperties encryptionProperties = new (
encryptionFormatVersion: 4,
encryptionOptions.EncryptionAlgorithm,
encryptionOptions.DataEncryptionKeyId,
encryptedData: null,
pathsEncrypted,
encryptionOptions.CompressionOptions.Algorithm,
new Dictionary<string, int>());
bool compressionEnabled = encryptionOptions.CompressionOptions.Algorithm != CompressionOptions.CompressionAlgorithm.None;

#if NET8_0_OR_GREATER
BrotliCompressor compressor = encryptionOptions.CompressionOptions.Algorithm == CompressionOptions.CompressionAlgorithm.Brotli
? new BrotliCompressor(encryptionOptions.CompressionOptions.CompressionLevel) : null;
#endif
Dictionary<string, int> compressedPaths = new ();

foreach (string pathToEncrypt in encryptionOptions.PathsToEncrypt)
{
Expand Down Expand Up @@ -78,7 +72,7 @@ public async Task<Stream> EncryptAsync(
if (compressor != null && (processedBytesLength >= encryptionOptions.CompressionOptions.MinimalCompressedLength))
{
byte[] compressedBytes = arrayPoolManager.Rent(BrotliCompressor.GetMaxCompressedSize(processedBytesLength));
processedBytesLength = compressor.Compress(encryptionProperties, pathToEncrypt, processedBytes, processedBytesLength, compressedBytes);
processedBytesLength = compressor.Compress(compressedPaths, pathToEncrypt, processedBytes, processedBytesLength, compressedBytes);
processedBytes = compressedBytes;
}
#endif
Expand All @@ -93,6 +87,14 @@ public async Task<Stream> EncryptAsync(
#if NET8_0_OR_GREATER
compressor?.Dispose();
#endif
EncryptionProperties encryptionProperties = new (
encryptionFormatVersion: compressionEnabled ? 4 : 3,
encryptionOptions.EncryptionAlgorithm,
encryptionOptions.DataEncryptionKeyId,
encryptedData: null,
pathsEncrypted,
encryptionOptions.CompressionOptions.Algorithm,
compressedPaths);

itemJObj.Add(Constants.EncryptedInfo, JObject.FromObject(encryptionProperties));
#if NET8_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class EncryptionPropertiesTests
[TestMethod]
public void Ctor_AssignsAllMandatoryProperties()
{
EncryptionProperties properties = new EncryptionProperties(
EncryptionProperties properties = new (
11,
"algorithm",
"dek-id",
Expand All @@ -32,7 +32,7 @@ public void Ctor_AssignsAllMandatoryProperties()
[TestMethod]
public void Ctor_AssignsAllProperties()
{
EncryptionProperties properties = new EncryptionProperties(
EncryptionProperties properties = new (
11,
"algorithm",
"dek-id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,12 @@ private static async Task<JObject> VerifyEncryptionSucceeded(TestDoc testDoc, En

Assert.IsNotNull(encryptionProperties);
Assert.AreEqual(dekId, encryptionProperties.DataEncryptionKeyId);
#if ENCRYPTION_CUSTOM_PREVIEW
Assert.AreEqual(4, encryptionProperties.EncryptionFormatVersion);
#else
Assert.AreEqual(3, encryptionProperties.EncryptionFormatVersion);
#endif

int expectedVersion =
(encryptionOptions.CompressionOptions.Algorithm != CompressionOptions.CompressionAlgorithm.None)
? 4 : 3;
Assert.AreEqual(expectedVersion, encryptionProperties.EncryptionFormatVersion);

Assert.IsNull(encryptionProperties.EncryptedData);
Assert.IsNotNull(encryptionProperties.EncryptedPaths);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.Azure.Cosmos.Encryption.Tests.Transformation
{
#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
#if NET8_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.IO.Compression;
Expand Down Expand Up @@ -44,7 +44,7 @@ public void GetQuality_ThrowsForUnknownLevels()
public void CompressAndDecompress_HasSameResult(CompressionLevel compressionLevel, int payloadSize)
{
BrotliCompressor compressor = new (compressionLevel);
EncryptionProperties properties = new (0, "", "", null, null, CompressionOptions.CompressionAlgorithm.Brotli, new Dictionary<string, int>());
Dictionary<string, int> properties = new ();
string path = "somePath";

byte[] bytes = new byte[payloadSize];
Expand All @@ -59,9 +59,9 @@ public void CompressAndDecompress_HasSameResult(CompressionLevel compressionLeve

Console.WriteLine($"Original: {bytes.Length} Compressed: {compressedBytesSize}");

Assert.IsTrue(properties.CompressedEncryptedPaths.ContainsKey(path));
Assert.IsTrue(properties.ContainsKey(path));

int recordedSize = properties.CompressedEncryptedPaths["somePath"];
int recordedSize = properties["somePath"];
Assert.AreEqual(bytes.Length, recordedSize);

byte[] decompressedBytes = new byte[recordedSize];
Expand Down