Skip to content

Commit

Permalink
[Client encryption]: Drops JsonNode based processor (#4846)
Browse files Browse the repository at this point in the history
# Pull Request Template

## Description

With new Stream based processor the JsonNode option is excessive and not
really needed. We are dropping all of its code.

## Type of change

Please delete options that are not relevant.

- [] New feature (non-breaking change which adds functionality)

## Closing issues

Contributes to #4678

---------

Co-authored-by: Juraj Blazek <[email protected]>
Co-authored-by: juraj-blazek <[email protected]>
Co-authored-by: Santosh Kulkarni <[email protected]>
Co-authored-by: Kiran Kumar Kolli <[email protected]>
  • Loading branch information
5 people authored Oct 25, 2024
1 parent 3a8fb03 commit 4a70bc3
Show file tree
Hide file tree
Showing 13 changed files with 1 addition and 884 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ public enum JsonProcessor
Newtonsoft,

#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
/// <summary>
/// System.Text.Json
/// </summary>
/// <remarks>Available with .NET8.0 package only.</remarks>
SystemTextJson,

/// <summary>
/// Ut8JsonReader/Writer
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ namespace Microsoft.Azure.Cosmos.Encryption.Custom
using System.IO;
using System.Linq;
using System.Text;
#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
using System.Text.Json;
using System.Text.Json.Nodes;
#endif
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Encryption.Custom.Transformation;
Expand All @@ -33,7 +29,6 @@ internal static class EncryptionProcessor
internal static readonly CosmosJsonDotNetSerializer BaseSerializer = new (JsonSerializerSettings);

#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
private static readonly JsonWriterOptions JsonWriterOptions = new () { SkipValidation = true };
private static readonly StreamProcessor StreamProcessor = new ();
#endif

Expand Down Expand Up @@ -158,7 +153,6 @@ public static async Task EncryptAsync(
{
JsonProcessor.Newtonsoft => await DecryptAsync(input, encryptor, diagnosticsContext, cancellationToken),
#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
JsonProcessor.SystemTextJson => await DecryptJsonNodeAsync(input, encryptor, diagnosticsContext, cancellationToken),
JsonProcessor.Stream => await DecryptStreamAsync(input, encryptor, diagnosticsContext, cancellationToken),
#endif
_ => throw new InvalidOperationException("Unsupported Json Processor")
Expand Down Expand Up @@ -229,43 +223,6 @@ public static async Task<DecryptionContext> DecryptAsync(
}
#endif

#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
public static async Task<(Stream, DecryptionContext)> DecryptJsonNodeAsync(
Stream input,
Encryptor encryptor,
CosmosDiagnosticsContext diagnosticsContext,
CancellationToken cancellationToken)
{
if (input == null)
{
return (input, null);
}

Debug.Assert(input.CanSeek);
Debug.Assert(encryptor != null);
Debug.Assert(diagnosticsContext != null);

JsonNode document = await JsonNode.ParseAsync(input, cancellationToken: cancellationToken);

(JsonNode decryptedDocument, DecryptionContext context) = await DecryptAsync(document, encryptor, diagnosticsContext, cancellationToken);
if (context == null)
{
input.Position = 0;
return (input, null);
}

await input.DisposeAsync();

MemoryStream ms = new ();
Utf8JsonWriter writer = new (ms, EncryptionProcessor.JsonWriterOptions);

System.Text.Json.JsonSerializer.Serialize(writer, decryptedDocument);

ms.Position = 0;
return (ms, context);
}
#endif

#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
public static async Task<(Stream, DecryptionContext)> DecryptStreamAsync(
Stream input,
Expand Down Expand Up @@ -327,53 +284,6 @@ public static async Task<DecryptionContext> DecryptAsync(
return (document, decryptionContext);
}

#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
public static async Task<(JsonNode, DecryptionContext)> DecryptAsync(
JsonNode document,
Encryptor encryptor,
CosmosDiagnosticsContext diagnosticsContext,
CancellationToken cancellationToken)
{
Debug.Assert(document != null);

Debug.Assert(encryptor != null);

if (!document.AsObject().TryGetPropertyValue(Constants.EncryptedInfo, out JsonNode encryptionPropertiesNode))
{
return (document, null);
}

EncryptionProperties encryptionProperties;
try
{
encryptionProperties = System.Text.Json.JsonSerializer.Deserialize<EncryptionProperties>(encryptionPropertiesNode);
}
catch (Exception)
{
return (document, null);
}

DecryptionContext decryptionContext = await DecryptInternalAsync(encryptor, diagnosticsContext, document, encryptionProperties, cancellationToken);

return (document, decryptionContext);
}

private static async Task<DecryptionContext> DecryptInternalAsync(Encryptor encryptor, CosmosDiagnosticsContext diagnosticsContext, JsonNode itemNode, EncryptionProperties encryptionProperties, CancellationToken cancellationToken)
{
DecryptionContext decryptionContext = encryptionProperties.EncryptionAlgorithm switch
{
CosmosEncryptionAlgorithm.MdeAeadAes256CbcHmac256Randomized => await MdeEncryptionProcessor.DecryptObjectAsync(
itemNode,
encryptor,
encryptionProperties,
diagnosticsContext,
cancellationToken),
_ => throw new NotSupportedException($"Encryption Algorithm : {encryptionProperties.EncryptionAlgorithm} is not supported."),
};
return decryptionContext;
}
#endif

private static async Task<DecryptionContext> DecryptInternalAsync(Encryptor encryptor, CosmosDiagnosticsContext diagnosticsContext, JObject itemJObj, JObject encryptionPropertiesJObj, CancellationToken cancellationToken)
{
EncryptionProperties encryptionProperties = encryptionPropertiesJObj.ToObject<EncryptionProperties>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ internal class MdeEncryptionProcessor
internal MdeJObjectEncryptionProcessor JObjectEncryptionProcessor { get; set; } = new MdeJObjectEncryptionProcessor();

#if NET8_0_OR_GREATER
internal MdeJsonNodeEncryptionProcessor JsonNodeEncryptionProcessor { get; set; } = new MdeJsonNodeEncryptionProcessor();

internal StreamProcessor StreamProcessor { get; set; } = new StreamProcessor();
#endif

Expand All @@ -36,9 +34,6 @@ public async Task<Stream> EncryptAsync(
{
case JsonProcessor.Newtonsoft:
return await this.JObjectEncryptionProcessor.EncryptAsync(input, encryptor, encryptionOptions, token);

case JsonProcessor.SystemTextJson:
return await this.JsonNodeEncryptionProcessor.EncryptAsync(input, encryptor, encryptionOptions, token);
case JsonProcessor.Stream:
MemoryStream ms = new ();
await this.StreamProcessor.EncryptStreamAsync(input, ms, encryptor, encryptionOptions, token);
Expand All @@ -65,18 +60,6 @@ internal async Task<DecryptionContext> DecryptObjectAsync(
{
return await this.JObjectEncryptionProcessor.DecryptObjectAsync(document, encryptor, encryptionProperties, diagnosticsContext, cancellationToken);
}

#if NET8_0_OR_GREATER
internal async Task<DecryptionContext> DecryptObjectAsync(
JsonNode document,
Encryptor encryptor,
EncryptionProperties encryptionProperties,
CosmosDiagnosticsContext diagnosticsContext,
CancellationToken cancellationToken)
{
return await this.JsonNodeEncryptionProcessor.DecryptObjectAsync(document, encryptor, encryptionProperties, diagnosticsContext, cancellationToken);
}
#endif
}
}
#endif
Loading

0 comments on commit 4a70bc3

Please sign in to comment.