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 : Fixes query support on encrypted parameters and fixes samples #2445

Merged
merged 58 commits into from
May 13, 2021
Merged
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
9f14c48
Remove caching of AeadAes256CbcHmac256EncryptionAlgorithm object
kr-santosh Mar 11, 2021
f5c8441
Merge branch 'master' into users/sakulk/removealgocaching
kr-santosh Mar 11, 2021
7fa67b6
Update DotNetPreviewSDKAPI.json
kr-santosh Mar 11, 2021
9755436
Merge branch 'master' into users/sakulk/removealgocaching
kr-santosh Mar 22, 2021
bc81034
Merge branch 'master' into users/sakulk/removealgocaching
kr-santosh Mar 25, 2021
5fea326
Merge branch 'master' into users/sakulk/removealgocaching
kr-santosh Mar 29, 2021
e695aa8
Updates to latest Cryptography package.Test updates.
kr-santosh Mar 29, 2021
86ed4b5
Minor Refactoring.
kr-santosh Mar 29, 2021
5a02a6c
Fixes as per review comments.
kr-santosh Mar 29, 2021
c038300
Changes as per review comments.
kr-santosh Apr 9, 2021
f780a77
Merge branch 'master' into users/sakulk/removealgocaching
kr-santosh Apr 9, 2021
02a394c
Update MdeCustomEncryptionTests.cs
kr-santosh Apr 9, 2021
10f97eb
Updated contracts.
kr-santosh Apr 9, 2021
9310436
Update MdeEncryptionTests.cs
kr-santosh Apr 9, 2021
0ebd6f4
Merge branch 'master' into users/sakulk/removealgocaching
kr-santosh Apr 14, 2021
67a13bb
Fixes Encryption Cosmos Client Encryption Policy and Keys cache.
kr-santosh Apr 20, 2021
0985bae
Update EncryptionContainer.cs
kr-santosh Apr 20, 2021
fe75344
Update EncryptionContainer.cs
kr-santosh Apr 20, 2021
a5360b7
Merge branch 'master' of https://github.com/Azure/azure-cosmos-dotnet-v3
kr-santosh Apr 20, 2021
03c3fdb
Merge branch 'master' into users/sakulk/fixclientcache
kr-santosh Apr 20, 2021
1841a51
Update EncryptionCosmosClient.cs
kr-santosh Apr 20, 2021
8456a91
Fixes.
kr-santosh Apr 27, 2021
cf2392d
Merge branch 'master' of https://github.com/Azure/azure-cosmos-dotnet-v3
kr-santosh Apr 27, 2021
51fbdb0
Merge branch 'master' into users/sakulk/fixclientcache
kr-santosh Apr 27, 2021
2f08396
Fixes.
kr-santosh Apr 27, 2021
e3c2e2d
Fixes as per review comments and Refactoring.
kr-santosh Apr 28, 2021
69b361a
Refactoring
kr-santosh Apr 28, 2021
1862729
Update EncryptionContainer.cs
kr-santosh Apr 28, 2021
fa8897a
Merge branch 'master' into users/sakulk/fixclientcache
kr-santosh Apr 28, 2021
5a5dfec
Update EncryptionSettingForProperty.cs
kr-santosh Apr 28, 2021
b8900d8
Merge branch 'master' of https://github.com/Azure/azure-cosmos-dotnet-v3
kr-santosh Apr 29, 2021
77930c5
Merge branch 'master' into users/sakulk/fixclientcache
kr-santosh Apr 29, 2021
1a0b2d6
Fixes as per review comments.
kr-santosh Apr 30, 2021
13b356d
Merge branch 'master' of https://github.com/Azure/azure-cosmos-dotnet-v3
kr-santosh Apr 30, 2021
05591d2
Merge branch 'master' into users/sakulk/fixclientcache
kr-santosh Apr 30, 2021
d55161e
Update EncryptionContainer.cs
kr-santosh Apr 30, 2021
a0e3037
Fixes as per review comments.
kr-santosh Apr 30, 2021
01ea0be
Update EncryptionContainer.cs
kr-santosh Apr 30, 2021
32261f7
Fixes as per review comments.
kr-santosh Apr 30, 2021
dcb30f5
Fixes as per review comments.
kr-santosh May 1, 2021
c004ffc
Allows object values for Queries on Encrypted Parameter
kr-santosh May 4, 2021
cbd1c69
Merge branch 'master' of https://github.com/Azure/azure-cosmos-dotnet-v3
kr-santosh May 4, 2021
53f4441
Merge branch 'master' into users/sakulk/fixQueryObjAndRWWoAccess
kr-santosh May 4, 2021
5cc28a7
Fixes and Updated Sample Code.
kr-santosh May 6, 2021
5c711c6
Merge branch 'master' of https://github.com/Azure/azure-cosmos-dotnet-v3
kr-santosh May 7, 2021
10d930f
Merge branch 'master' into users/sakulk/fixQueryObjAndRWWoAccess
kr-santosh May 7, 2021
d97d15a
Reverts.
kr-santosh May 7, 2021
1d0ffd4
Reverts
kr-santosh May 7, 2021
9b20293
Fixes as per review comments.
kr-santosh May 7, 2021
fd221c4
Update Program.cs
kr-santosh May 7, 2021
8a2cf90
Update Encryption.csproj
kr-santosh May 7, 2021
aa03e98
Merge branch 'master' of https://github.com/Azure/azure-cosmos-dotnet-v3
kr-santosh May 11, 2021
969f299
Merge branch 'master' into users/sakulk/fixQueryObjAndRWWoAccess
kr-santosh May 11, 2021
aa83cdd
Fixes as per review comments.
kr-santosh May 12, 2021
6116dc1
Fixed issue with Multi DataType array.
kr-santosh May 13, 2021
6ab6384
Merge branch 'master' of https://github.com/Azure/azure-cosmos-dotnet-v3
kr-santosh May 13, 2021
ec82b07
Merge branch 'master' into users/sakulk/fixQueryObjAndRWWoAccess
kr-santosh May 13, 2021
e8f6738
Refactoring.
kr-santosh May 13, 2021
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
Prev Previous commit
Refactoring.
  • Loading branch information
kr-santosh committed May 13, 2021
commit e8f67388999d27d43bc54d54a1c801bd4f5c998e
156 changes: 52 additions & 104 deletions Microsoft.Azure.Cosmos.Encryption/src/EncryptionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public static async Task<Stream> EncryptAsync(
foreach (string propertyName in encryptionSettings.PropertiesToEncrypt)
{
// possibly a wrong path configured in the Client Encryption Policy, ignore.
if (!itemJObj.TryGetValue(propertyName, out JToken propertyValue))
JProperty propertyToEncrypt = itemJObj.Property(propertyName);
if (propertyToEncrypt == null)
{
continue;
}
Expand All @@ -73,9 +74,8 @@ public static async Task<Stream> EncryptAsync(
throw new ArgumentException($"Invalid Encryption Setting for the Property:{propertyName}. ");
}

await EncryptPropertyAsync(
itemJObj,
propertyValue,
await EncryptJTokenAsync(
propertyToEncrypt.Value,
settingforProperty,
cancellationToken);
}
Expand Down Expand Up @@ -148,7 +148,15 @@ internal static async Task<Stream> EncryptValueStreamAsync(

JToken propertyValueToEncrypt = EncryptionProcessor.BaseSerializer.FromStream<JToken>(valueStream);

JToken encryptedPropertyValue = await EncryptJTokenAsync(propertyValueToEncrypt, settingsForProperty, cancellationToken);
JToken encryptedPropertyValue = propertyValueToEncrypt;
if (propertyValueToEncrypt.Type == JTokenType.Object || propertyValueToEncrypt.Type == JTokenType.Array)
{
await EncryptJTokenAsync(encryptedPropertyValue, settingsForProperty, cancellationToken);
}
else
{
encryptedPropertyValue = await SerializeAndEncryptValueAsync(propertyValueToEncrypt, settingsForProperty, cancellationToken);
}

return EncryptionProcessor.BaseSerializer.ToStream(encryptedPropertyValue);
}
Expand Down Expand Up @@ -179,74 +187,41 @@ private static JToken DeserializeAndAddProperty(
};
}

private static async Task<JToken> EncryptJTokenAsync(
JToken propertyValueToEncrypt,
private static async Task EncryptJTokenAsync(
JToken jTokenToEncrypt,
EncryptionSettingForProperty encryptionSettingForProperty,
CancellationToken cancellationToken)
{
// Top Level can be an Object
if (propertyValueToEncrypt.Type == JTokenType.Object)
if (jTokenToEncrypt.Type == JTokenType.Object)
{
foreach (JProperty jProperty in propertyValueToEncrypt.Children<JProperty>())
foreach (JProperty jProperty in jTokenToEncrypt.Children<JProperty>())
{
if (jProperty.Value.Type == JTokenType.Object || jProperty.Value.Type == JTokenType.Array)
{
await EncryptJTokenAsync(
jProperty.Value,
encryptionSettingForProperty,
cancellationToken);
}
else
{
jProperty.Value = await SerializeAndEncryptValueAsync(jProperty.Value, encryptionSettingForProperty, cancellationToken);
}
await EncryptJTokenAsync(
jProperty.Value,
encryptionSettingForProperty,
cancellationToken);
}
}
else if (propertyValueToEncrypt.Type == JTokenType.Array)
else if (jTokenToEncrypt.Type == JTokenType.Array)
{
if (propertyValueToEncrypt.Children().Any())
if (jTokenToEncrypt.Children().Any())
{
for (int i = 0; i < propertyValueToEncrypt.Count(); i++)
for (int i = 0; i < jTokenToEncrypt.Count(); i++)
{
if (propertyValueToEncrypt[i].Type == JTokenType.Object || propertyValueToEncrypt[i].Type == JTokenType.Array)
{
await EncryptJTokenAsync(
propertyValueToEncrypt[i],
encryptionSettingForProperty,
cancellationToken);
}
else
{
propertyValueToEncrypt[i] = await SerializeAndEncryptValueAsync(propertyValueToEncrypt[i], encryptionSettingForProperty, cancellationToken);
}
await EncryptJTokenAsync(
jTokenToEncrypt[i],
encryptionSettingForProperty,
cancellationToken);
}
}
}
else
{
propertyValueToEncrypt = await SerializeAndEncryptValueAsync(propertyValueToEncrypt, encryptionSettingForProperty, cancellationToken);
jTokenToEncrypt.Replace(await SerializeAndEncryptValueAsync(jTokenToEncrypt, encryptionSettingForProperty, cancellationToken));
}

return propertyValueToEncrypt;
}

private static async Task EncryptPropertyAsync(
JObject itemJObj,
JToken propertyValue,
EncryptionSettingForProperty encryptionSettingForProperty,
CancellationToken cancellationToken)
{
if (propertyValue.Type == JTokenType.Object || propertyValue.Type == JTokenType.Array)
{
await EncryptJTokenAsync(propertyValue, encryptionSettingForProperty, cancellationToken);
}
else
{
itemJObj.Property(propertyValue.Path).Value = await EncryptJTokenAsync(
propertyValue,
encryptionSettingForProperty,
cancellationToken);
}
return;
}

private static async Task<JToken> SerializeAndEncryptValueAsync(
Expand Down Expand Up @@ -305,66 +280,40 @@ private static async Task<JToken> DecryptAndDeserializeValueAsync(
(TypeMarker)cipherTextWithTypeMarker[0]);
}

private static async Task DecryptPropertyAsync(
JObject itemJObj,
private static async Task DecryptJTokenAsync(
JToken jTokenToDecrypt,
EncryptionSettingForProperty encryptionSettingForProperty,
string propertyName,
JToken propertyValue,
CancellationToken cancellationToken)
{
if (propertyValue.Type == JTokenType.Object)
if (jTokenToDecrypt.Type == JTokenType.Object)
{
foreach (JProperty jProperty in propertyValue.Children<JProperty>())
foreach (JProperty jProperty in jTokenToDecrypt.Children<JProperty>())
{
if (jProperty.Value.Type == JTokenType.Object || jProperty.Value.Type == JTokenType.Array)
{
await DecryptPropertyAsync(
itemJObj,
encryptionSettingForProperty,
jProperty.Name,
jProperty.Value,
cancellationToken);
}
else
{
jProperty.Value = await DecryptAndDeserializeValueAsync(
jProperty.Value,
encryptionSettingForProperty,
cancellationToken);
}
await DecryptJTokenAsync(
jProperty.Value,
encryptionSettingForProperty,
cancellationToken);
}
}
else if (propertyValue.Type == JTokenType.Array)
else if (jTokenToDecrypt.Type == JTokenType.Array)
{
if (propertyValue.Children().Any())
if (jTokenToDecrypt.Children().Any())
{
for (int i = 0; i < propertyValue.Count(); i++)
for (int i = 0; i < jTokenToDecrypt.Count(); i++)
{
if (propertyValue[i].Type == JTokenType.Object || propertyValue[i].Type == JTokenType.Array)
{
await DecryptPropertyAsync(
itemJObj,
encryptionSettingForProperty,
propertyValue[i].Path,
propertyValue[i],
cancellationToken);
}
else
{
propertyValue[i] = await DecryptAndDeserializeValueAsync(
propertyValue[i],
encryptionSettingForProperty,
cancellationToken);
}
await DecryptJTokenAsync(
jTokenToDecrypt[i],
encryptionSettingForProperty,
cancellationToken);
}
}
}
else
{
itemJObj.Property(propertyName).Value = await DecryptAndDeserializeValueAsync(
itemJObj.Property(propertyName).Value,
jTokenToDecrypt.Replace(await DecryptAndDeserializeValueAsync(
jTokenToDecrypt,
encryptionSettingForProperty,
cancellationToken);
cancellationToken));
}
}

Expand All @@ -378,7 +327,8 @@ private static async Task DecryptObjectAsync(

foreach (string propertyName in encryptionSettings.PropertiesToEncrypt)
{
if (document.TryGetValue(propertyName, out JToken propertyValue))
JProperty propertyToDecrypt = document.Property(propertyName);
if (propertyToDecrypt != null)
{
EncryptionSettingForProperty settingsForProperty = encryptionSettings.GetEncryptionSettingForProperty(propertyName);

Expand All @@ -387,11 +337,9 @@ private static async Task DecryptObjectAsync(
throw new ArgumentException($"Invalid Encryption Setting for Property:{propertyName}. ");
}

await DecryptPropertyAsync(
document,
await DecryptJTokenAsync(
propertyToDecrypt.Value,
settingsForProperty,
propertyName,
propertyValue,
cancellationToken);
}
}
Expand Down