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] Patch: Adds client encryption support for patch #2448

Merged
merged 5 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
131 changes: 127 additions & 4 deletions Microsoft.Azure.Cosmos.Encryption/src/EncryptionContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.Azure.Cosmos.Encryption
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Data.Encryption.Cryptography;
using Newtonsoft.Json.Linq;

internal sealed class EncryptionContainer : Container
Expand Down Expand Up @@ -657,24 +658,146 @@ internal sealed class EncryptionContainer : Container
this.ResponseFactory);
}

public override Task<ItemResponse<T>> PatchItemAsync<T>(
public async override Task<ItemResponse<T>> PatchItemAsync<T>(
string id,
PartitionKey partitionKey,
IReadOnlyList<PatchOperation> patchOperations,
PatchItemRequestOptions requestOptions = null,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
ResponseMessage responseMessage = await this.PatchItemStreamAsync(
id,
partitionKey,
patchOperations,
requestOptions,
cancellationToken);

return this.ResponseFactory.CreateItemResponse<T>(responseMessage);
}

public override Task<ResponseMessage> PatchItemStreamAsync(
public async override Task<ResponseMessage> PatchItemStreamAsync(
string id,
PartitionKey partitionKey,
IReadOnlyList<PatchOperation> patchOperations,
PatchItemRequestOptions requestOptions = null,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
EncryptionSettings encryptionSettings = await this.GetOrUpdateEncryptionSettingsFromCacheAsync(
obsoleteEncryptionSettings: null,
cancellationToken: cancellationToken);

CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);
using (diagnosticsContext.CreateScope("PatchItem"))
{
List<PatchOperation> encryptedPatchOperations = await this.PatchItemHelperAsync(
id,
partitionKey,
patchOperations,
encryptionSettings,
cancellationToken);

ResponseMessage responseMessage = await this.container.PatchItemStreamAsync(
id,
partitionKey,
encryptedPatchOperations,
requestOptions,
cancellationToken);

responseMessage.Content = await EncryptionProcessor.DecryptAsync(
responseMessage.Content,
encryptionSettings,
diagnosticsContext,
cancellationToken);

return responseMessage;
}
}

private async Task<List<PatchOperation>> PatchItemHelperAsync(
string id,
PartitionKey partitionKey,
IReadOnlyList<PatchOperation> patchOperations,
EncryptionSettings encryptionSettings,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(id))
anujtoshniwal marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentNullException(nameof(id));
}

if (partitionKey == null)
{
throw new ArgumentNullException(nameof(partitionKey));
}

if (patchOperations == null ||
!patchOperations.Any())
{
throw new ArgumentNullException(nameof(patchOperations));
}

List<PatchOperation> encryptedPatchOperations = new List<PatchOperation>(patchOperations.Count);

foreach (PatchOperation patchOperation in patchOperations)
{
if (patchOperation.OperationType == PatchOperationType.Remove)
{
encryptedPatchOperations.Add(patchOperation);
continue;
}

if (string.IsNullOrWhiteSpace(patchOperation.Path) || patchOperation.Path[0] != '/')
{
throw new ArgumentException($"Invalid path '{patchOperation.Path}'.");
}

// get the top level path's encryption setting.
EncryptionSettingForProperty settingforProperty = encryptionSettings.GetEncryptionSettingForProperty(
patchOperation.Path.Split('/')[1]);

// non-encrypted path
if (settingforProperty == null)
{
encryptedPatchOperations.Add(patchOperation);
continue;
}
else if (patchOperation.OperationType == PatchOperationType.Increment)
{
throw new InvalidOperationException($"Increment patch operation is not allowed for encrypted path '{patchOperation.Path}'.");
}

if (!patchOperation.TrySerializeValueParameter(this.CosmosSerializer, out Stream valueParam))
{
throw new ArgumentException($"Cannot serialize value parameter for operation: {patchOperation.OperationType}, path: {patchOperation.Path}.");
}

AeadAes256CbcHmac256EncryptionAlgorithm aeadAes256CbcHmac256EncryptionAlgorithm = await settingforProperty.BuildEncryptionAlgorithmForSettingAsync(cancellationToken);

JToken propertyValue = EncryptionProcessor.BaseSerializer.FromStream<JToken>(valueParam);
JToken encryptedPropertyValue = EncryptionProcessor.EncryptProperty(
propertyValue,
aeadAes256CbcHmac256EncryptionAlgorithm);

switch (patchOperation.OperationType)
{
case PatchOperationType.Add:
encryptedPatchOperations.Add(PatchOperation.Add(patchOperation.Path, this.CosmosSerializer.ToStream(encryptedPropertyValue)));
break;

case PatchOperationType.Replace:
encryptedPatchOperations.Add(PatchOperation.Replace(patchOperation.Path, this.CosmosSerializer.ToStream(encryptedPropertyValue)));
break;

case PatchOperationType.Set:
encryptedPatchOperations.Add(PatchOperation.Set(patchOperation.Path, this.CosmosSerializer.ToStream(encryptedPropertyValue)));
break;

default:
throw new NotSupportedException(nameof(patchOperation.OperationType));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This should probably explain that encryption does not support this type of operation.

}
}

return encryptedPatchOperations;
}

public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder<T>(
Expand Down
67 changes: 67 additions & 0 deletions Microsoft.Azure.Cosmos.Encryption/src/EncryptionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,73 @@ private static (TypeMarker, byte[]) Serialize(JToken propertyValue)
};
}

internal static JToken EncryptProperty(
anujtoshniwal marked this conversation as resolved.
Show resolved Hide resolved
JToken propertyValue,
AeadAes256CbcHmac256EncryptionAlgorithm aeadAes256CbcHmac256EncryptionAlgorithm)
{
/* Top Level can be an Object*/
if (propertyValue.Type == JTokenType.Object)
{
foreach (JProperty jProperty in propertyValue.Children<JProperty>())
{
jProperty.Value = EncryptionProcessor.EncryptProperty(
jProperty.Value,
aeadAes256CbcHmac256EncryptionAlgorithm);
}
}
else if (propertyValue.Type == JTokenType.Array)
{
if (propertyValue.Children().Any())
{
// objects as array elements.
if (propertyValue.Children().First().Type == JTokenType.Object)
{
foreach (JObject arrayjObject in propertyValue.Children<JObject>())
{
foreach (JProperty jProperty in arrayjObject.Properties())
{
jProperty.Value = EncryptionProcessor.EncryptProperty(
jProperty.Value,
aeadAes256CbcHmac256EncryptionAlgorithm);
}
}
}

// array as elements.
else if (propertyValue.Children().First().Type == JTokenType.Array)
{
foreach (JArray jArray in propertyValue.Value<JArray>())
{
for (int i = 0; i < jArray.Count(); i++)
{
// iterates over individual elements
jArray[i] = EncryptionProcessor.EncryptProperty(
jArray[i],
aeadAes256CbcHmac256EncryptionAlgorithm);
}
}
}

// array of primitive types.
else
{
for (int i = 0; i < propertyValue.Count(); i++)
{
propertyValue[i] = SerializeAndEncryptValue(propertyValue[i], aeadAes256CbcHmac256EncryptionAlgorithm);
}
}
}
}
else
{
propertyValue = SerializeAndEncryptValue(
propertyValue,
aeadAes256CbcHmac256EncryptionAlgorithm);
}

return propertyValue;
}

private static void EncryptProperty(
JObject itemJObj,
JToken propertyValue,
Expand Down
Loading