Skip to content

Commit db15b93

Browse files
Merge branch 'master' into users/nalutripician/semanticRerank
2 parents f2e0e5b + 9e09d15 commit db15b93

44 files changed

Lines changed: 2314 additions & 1210 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Microsoft.Azure.Cosmos.Encryption.Custom/src/CompressionOptions.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

Microsoft.Azure.Cosmos.Encryption.Custom/src/Constants.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ internal static class Constants
1313
public const string EncryptionDekId = "_en";
1414
public const string EncryptionFormatVersion = "_ef";
1515
public const string EncryptedPaths = "_ep";
16-
public const string CompressionAlgorithm = "_ce";
17-
public const string CompressedEncryptedPaths = "_cp";
1816
public const int DekPropertiesDefaultTTLInMinutes = 120;
1917
}
2018
}

Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyContainerCore.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public override async Task<ItemResponse<DataEncryptionKeyProperties>> CreateData
9494
}
9595
else if (string.Equals(encryptionAlgorithm, CosmosEncryptionAlgorithm.MdeAeadAes256CbcHmac256Randomized, StringComparison.Ordinal))
9696
{
97-
(wrappedDek, updatedMetadata) = this.GenerateAndWrapPdekForMdeEncAlgo(id, encryptionKeyWrapMetadata);
97+
(wrappedDek, updatedMetadata) = await this.GenerateAndWrapPdekForMdeEncAlgoAsync(id, encryptionKeyWrapMetadata, cancellationToken);
9898
}
9999
#pragma warning restore CS0618 // Type or member is obsolete
100100

@@ -403,7 +403,7 @@ internal async Task<InMemoryRawDek> FetchUnwrappedAsync(
403403
{
404404
if (string.Equals(dekProperties.EncryptionAlgorithm, CosmosEncryptionAlgorithm.MdeAeadAes256CbcHmac256Randomized, StringComparison.Ordinal))
405405
{
406-
DataEncryptionKey dek = this.InitMdeEncryptionAlgorithm(dekProperties, withRawKey);
406+
DataEncryptionKey dek = await this.InitMdeEncryptionAlgorithmAsync(dekProperties, withRawKey, cancellationToken);
407407

408408
// TTL is not used since DEK is not cached.
409409
return new InMemoryRawDek(dek, TimeSpan.FromMilliseconds(0));
@@ -546,7 +546,7 @@ internal async Task<InMemoryRawDek> UnwrapAsync(
546546
cancellationToken);
547547
}
548548

549-
private (byte[], EncryptionKeyWrapMetadata) GenerateAndWrapPdekForMdeEncAlgo(string id, EncryptionKeyWrapMetadata encryptionKeyWrapMetadata)
549+
private async Task<(byte[], EncryptionKeyWrapMetadata)> GenerateAndWrapPdekForMdeEncAlgoAsync(string id, EncryptionKeyWrapMetadata encryptionKeyWrapMetadata, CancellationToken cancellationToken)
550550
{
551551
if (this.DekProvider.MdeKeyWrapProvider == null)
552552
{
@@ -559,9 +559,10 @@ internal async Task<InMemoryRawDek> UnwrapAsync(
559559
encryptionKeyWrapMetadata.Value,
560560
this.DekProvider.MdeKeyWrapProvider.EncryptionKeyStoreProvider);
561561

562-
ProtectedDataEncryptionKey protectedDataEncryptionKey = new (
562+
ProtectedDataEncryptionKey protectedDataEncryptionKey = await ProtectedDataEncryptionKey.CreateAsync(
563563
id,
564-
keyEncryptionKey);
564+
keyEncryptionKey,
565+
cancellationToken).ConfigureAwait(false);
565566

566567
byte[] wrappedDek = protectedDataEncryptionKey.EncryptedValue;
567568
EncryptionKeyWrapMetadata updatedMetadata = encryptionKeyWrapMetadata;
@@ -591,20 +592,21 @@ private async Task<EncryptionKeyUnwrapResult> UnWrapDekMdeEncAlgoAsync(
591592
return unwrapResult;
592593
}
593594

594-
internal DataEncryptionKey InitMdeEncryptionAlgorithm(DataEncryptionKeyProperties dekProperties, bool withRawKey = false)
595+
internal async Task<DataEncryptionKey> InitMdeEncryptionAlgorithmAsync(DataEncryptionKeyProperties dekProperties, bool withRawKey, CancellationToken cancellationToken)
595596
{
596597
if (this.DekProvider.MdeKeyWrapProvider == null)
597598
{
598599
throw new InvalidOperationException($"For use of '{CosmosEncryptionAlgorithm.MdeAeadAes256CbcHmac256Randomized}' algorithm, " +
599600
"Encryptor or CosmosDataEncryptionKeyProvider needs to be initialized with EncryptionKeyStoreProvider.");
600601
}
601602

602-
return new MdeEncryptionAlgorithm(
603+
return await MdeEncryptionAlgorithm.CreateAsync(
603604
dekProperties,
604605
Data.Encryption.Cryptography.EncryptionType.Randomized,
605606
this.DekProvider.MdeKeyWrapProvider.EncryptionKeyStoreProvider,
606607
this.DekProvider.PdekCacheTimeToLive,
607-
withRawKey);
608+
withRawKey,
609+
cancellationToken);
608610
}
609611

610612
private async Task<DataEncryptionKeyProperties> ReadResourceAsync(

Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionFormatVersion.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ internal static class EncryptionFormatVersion
88
{
99
public const int AeAes = 2;
1010
public const int Mde = 3;
11-
public const int MdeWithCompression = 4;
1211
}
1312
}

Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionOptions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ public sealed class EncryptionOptions
4848
/// </remarks>
4949
public string EncryptionAlgorithm { get; set; }
5050

51-
/// <summary>
52-
/// Gets or sets payload compression mode
53-
/// </summary>
54-
public CompressionOptions CompressionOptions { get; set; } = new CompressionOptions();
55-
5651
/// <summary>
5752
/// Gets or sets list of JSON paths to encrypt on the payload.
5853
/// Only top level paths are supported.

Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionOptionsExtensions.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,6 @@ internal static void Validate(this EncryptionOptions options)
5050
throw new InvalidOperationException($"{nameof(options.PathsToEncrypt)} includes a invalid path: '{path}'.");
5151
}
5252
}
53-
54-
options.CompressionOptions?.Validate();
55-
}
56-
57-
internal static void Validate(this CompressionOptions options)
58-
{
59-
if (options.MinimalCompressedLength < 0)
60-
{
61-
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
62-
throw new ArgumentOutOfRangeException(nameof(options.MinimalCompressedLength));
63-
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
64-
}
6553
}
6654
}
6755
}

Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProperties.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,18 @@ internal class EncryptionProperties
3030
[JsonPropertyName(Constants.EncryptedPaths)]
3131
public IEnumerable<string> EncryptedPaths { get; }
3232

33-
[JsonProperty(PropertyName = Constants.CompressionAlgorithm)]
34-
[JsonPropertyName(Constants.CompressionAlgorithm)]
35-
public CompressionOptions.CompressionAlgorithm CompressionAlgorithm { get; }
36-
37-
[JsonProperty(PropertyName = Constants.CompressedEncryptedPaths)]
38-
[JsonPropertyName(Constants.CompressedEncryptedPaths)]
39-
public IDictionary<string, int> CompressedEncryptedPaths { get; }
40-
4133
public EncryptionProperties(
4234
int encryptionFormatVersion,
4335
string encryptionAlgorithm,
4436
string dataEncryptionKeyId,
4537
byte[] encryptedData,
46-
IEnumerable<string> encryptedPaths,
47-
CompressionOptions.CompressionAlgorithm compressionAlgorithm = CompressionOptions.CompressionAlgorithm.None,
48-
IDictionary<string, int> compressedEncryptedPaths = null)
38+
IEnumerable<string> encryptedPaths)
4939
{
5040
this.EncryptionFormatVersion = encryptionFormatVersion;
5141
this.EncryptionAlgorithm = encryptionAlgorithm;
5242
this.DataEncryptionKeyId = dataEncryptionKeyId;
5343
this.EncryptedData = encryptedData;
5444
this.EncryptedPaths = encryptedPaths;
55-
this.CompressionAlgorithm = compressionAlgorithm;
56-
this.CompressedEncryptedPaths = compressedEncryptedPaths;
5745
}
5846
}
5947
}

Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeEncryptionAlgorithm.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Microsoft.Azure.Cosmos.Encryption.Custom
66
{
77
using System;
8+
using System.Threading;
9+
using System.Threading.Tasks;
810
using Microsoft.Data.Encryption.Cryptography;
911

1012
/// <summary>
@@ -27,12 +29,13 @@ internal sealed class MdeEncryptionAlgorithm : DataEncryptionKey
2729
/// <see href="http://tools.ietf.org/html/draft-mcgrew-aead-aes-cbc-hmac-sha2-05">here</see> .
2830
/// More specifically this implements AEAD_AES_256_CBC_HMAC_SHA256 algorithm.
2931
/// </summary>
30-
public MdeEncryptionAlgorithm(
32+
public static async Task<MdeEncryptionAlgorithm> CreateAsync(
3133
DataEncryptionKeyProperties dekProperties,
3234
Data.Encryption.Cryptography.EncryptionType encryptionType,
3335
EncryptionKeyStoreProvider encryptionKeyStoreProvider,
3436
TimeSpan? cacheTimeToLive,
35-
bool withRawKey = false)
37+
bool withRawKey,
38+
CancellationToken cancellationToken)
3639
{
3740
#if NET8_0_OR_GREATER
3841
ArgumentNullException.ThrowIfNull(dekProperties);
@@ -54,38 +57,44 @@ public MdeEncryptionAlgorithm(
5457
dekProperties.EncryptionKeyWrapMetadata.Value,
5558
encryptionKeyStoreProvider);
5659

60+
AeadAes256CbcHmac256EncryptionAlgorithm aeadAes256CbcHmac256EncryptionAlgorithm;
61+
byte[] rawKey = null;
62+
5763
if (!withRawKey)
5864
{
5965
ProtectedDataEncryptionKey protectedDataEncryptionKey = cacheTimeToLive.HasValue && cacheTimeToLive.Value == TimeSpan.Zero
60-
? new ProtectedDataEncryptionKey(
66+
? await ProtectedDataEncryptionKey.CreateAsync(
6167
dekProperties.Id,
6268
keyEncryptionKey,
63-
dekProperties.WrappedDataEncryptionKey)
64-
: ProtectedDataEncryptionKey.GetOrCreate(
69+
dekProperties.WrappedDataEncryptionKey,
70+
cancellationToken)
71+
: await ProtectedDataEncryptionKey.GetOrCreateAsync(
6572
dekProperties.Id,
6673
keyEncryptionKey,
67-
dekProperties.WrappedDataEncryptionKey);
68-
this.mdeAeadAes256CbcHmac256EncryptionAlgorithm = AeadAes256CbcHmac256EncryptionAlgorithm.GetOrCreate(
74+
dekProperties.WrappedDataEncryptionKey,
75+
cancellationToken);
76+
aeadAes256CbcHmac256EncryptionAlgorithm = AeadAes256CbcHmac256EncryptionAlgorithm.GetOrCreate(
6977
protectedDataEncryptionKey,
7078
encryptionType,
7179
Version);
7280
}
7381
else
7482
{
75-
byte[] rawKey = keyEncryptionKey.DecryptEncryptionKey(dekProperties.WrappedDataEncryptionKey);
83+
rawKey = await keyEncryptionKey.DecryptEncryptionKeyAsync(dekProperties.WrappedDataEncryptionKey, cancellationToken).ConfigureAwait(false);
7684
PlaintextDataEncryptionKey plaintextDataEncryptionKey = cacheTimeToLive.HasValue && (cacheTimeToLive.Value == TimeSpan.Zero)
7785
? new PlaintextDataEncryptionKey(
7886
dekProperties.Id,
7987
rawKey)
8088
: PlaintextDataEncryptionKey.GetOrCreate(
8189
dekProperties.Id,
8290
rawKey);
83-
this.RawKey = rawKey;
84-
this.mdeAeadAes256CbcHmac256EncryptionAlgorithm = AeadAes256CbcHmac256EncryptionAlgorithm.GetOrCreate(
91+
aeadAes256CbcHmac256EncryptionAlgorithm = AeadAes256CbcHmac256EncryptionAlgorithm.GetOrCreate(
8592
plaintextDataEncryptionKey,
8693
encryptionType,
8794
Version);
8895
}
96+
97+
return new MdeEncryptionAlgorithm(aeadAes256CbcHmac256EncryptionAlgorithm, rawKey);
8998
}
9099

91100
/// <summary>
@@ -106,6 +115,12 @@ public MdeEncryptionAlgorithm(
106115
Version);
107116
}
108117

118+
private MdeEncryptionAlgorithm(AeadAes256CbcHmac256EncryptionAlgorithm aeadAes256CbcHmac256EncryptionAlgorithm, byte[] rawKey)
119+
{
120+
this.mdeAeadAes256CbcHmac256EncryptionAlgorithm = aeadAes256CbcHmac256EncryptionAlgorithm ?? throw new ArgumentNullException(nameof(aeadAes256CbcHmac256EncryptionAlgorithm));
121+
this.RawKey = rawKey;
122+
}
123+
109124
/// <summary>
110125
/// Encrypt data using EncryptionAlgorithm
111126
/// </summary>

Microsoft.Azure.Cosmos.Encryption.Custom/src/MdeServices/MdeKeyWrapProvider.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public MdeKeyWrapProvider(EncryptionKeyStoreProvider encryptionKeyStoreProvider)
2323
this.EncryptionKeyStoreProvider = encryptionKeyStoreProvider ?? throw new ArgumentNullException(nameof(encryptionKeyStoreProvider));
2424
}
2525

26-
public override Task<EncryptionKeyUnwrapResult> UnwrapKeyAsync(
26+
public override async Task<EncryptionKeyUnwrapResult> UnwrapKeyAsync(
2727
byte[] wrappedKey,
2828
EncryptionKeyWrapMetadata metadata,
2929
CancellationToken cancellationToken)
@@ -42,11 +42,12 @@ public override Task<EncryptionKeyUnwrapResult> UnwrapKeyAsync(
4242
metadata.Value,
4343
this.EncryptionKeyStoreProvider);
4444

45-
byte[] result = keyEncryptionKey.DecryptEncryptionKey(wrappedKey);
46-
return Task.FromResult(new EncryptionKeyUnwrapResult(result, TimeSpan.Zero));
45+
byte[] result = await keyEncryptionKey.DecryptEncryptionKeyAsync(wrappedKey, cancellationToken).ConfigureAwait(false);
46+
47+
return new EncryptionKeyUnwrapResult(result, TimeSpan.Zero);
4748
}
4849

49-
public override Task<EncryptionKeyWrapResult> WrapKeyAsync(
50+
public override async Task<EncryptionKeyWrapResult> WrapKeyAsync(
5051
byte[] key,
5152
EncryptionKeyWrapMetadata metadata,
5253
CancellationToken cancellationToken)
@@ -65,8 +66,9 @@ public override Task<EncryptionKeyWrapResult> WrapKeyAsync(
6566
metadata.Value,
6667
this.EncryptionKeyStoreProvider);
6768

68-
byte[] result = keyEncryptionKey.EncryptEncryptionKey(key);
69-
return Task.FromResult(new EncryptionKeyWrapResult(result, metadata));
69+
byte[] result = await keyEncryptionKey.EncryptEncryptionKeyAsync(key, cancellationToken).ConfigureAwait(false);
70+
71+
return new EncryptionKeyWrapResult(result, metadata);
7072
}
7173
}
7274
}

Microsoft.Azure.Cosmos.Encryption.Custom/src/Microsoft.Azure.Cosmos.Encryption.Custom.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
</ItemGroup>
3737

3838
<ItemGroup>
39-
<PackageReference Include="Microsoft.Data.Encryption.Cryptography" Version="2.0.0-pre007" />
39+
<PackageReference Include="Microsoft.Data.Encryption.Cryptography" Version="2.0.0-pre015" />
4040
<PackageReference Include="Azure.Identity" Version="1.11.4" />
4141
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
42-
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
42+
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.6.3" />
4343
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" NoWarn="NU1903" PrivateAssets="All" />
4444
</ItemGroup>
4545

0 commit comments

Comments
 (0)