Skip to content

[Client encryption]: Add JsonNodeSqlSerializer #4779

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 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
99c7a75
+ Add .NET8.0 target for Cosmos.Encryption.Custom
Oct 8, 2024
31c20e7
- remove implicit IsPreview from csproj
Oct 8, 2024
90b2cec
+ JsonNodeSqlSerializer
Oct 9, 2024
9215f67
~ Merge branch 'master' into users/jan-hyka/jsonnode_serializer
Oct 10, 2024
38cc928
- remove explicit System.Text.Json 8.0.5
Oct 10, 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 @@ -38,11 +38,11 @@
</ItemGroup>

<ItemGroup Condition=" '$(IsPreview)' != 'True' ">
<PackageReference Include="Microsoft.Data.Encryption.Cryptography" Version="1.2.0" />
<PackageReference Include="Microsoft.Data.Encryption.Cryptography" Version="1.2.0" />
</ItemGroup>

<ItemGroup Condition=" '$(IsPreview)' == 'True' ">
<PackageReference Include="Microsoft.Data.Encryption.Cryptography" Version="2.0.0-pre007" />
<PackageReference Include="Microsoft.Data.Encryption.Cryptography" Version="2.0.0-pre007" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ internal virtual (TypeMarker typeMarker, byte[] serializedBytes, int serializedB
(buffer, length) = SerializeString(propertyValue.ToObject<string>());
return (TypeMarker.String, buffer, length);
case JTokenType.Array:
(buffer, length) = SerializeString(propertyValue.ToString());
(buffer, length) = SerializeString(propertyValue.ToString(Formatting.None));
return (TypeMarker.Array, buffer, length);
case JTokenType.Object:
(buffer, length) = SerializeString(propertyValue.ToString());
(buffer, length) = SerializeString(propertyValue.ToString(Formatting.None));
return (TypeMarker.Object, buffer, length);
default:
throw new InvalidOperationException($" Invalid or Unsupported Data Type Passed : {propertyValue.Type}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------

#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER
namespace Microsoft.Azure.Cosmos.Encryption.Custom.Transformation
{
using System;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Data.Encryption.Cryptography.Serializers;

internal class JsonNodeSqlSerializer
{
private static readonly SqlBitSerializer SqlBoolSerializer = new ();
private static readonly SqlFloatSerializer SqlDoubleSerializer = new ();
private static readonly SqlBigIntSerializer SqlLongSerializer = new ();

// UTF-8 encoding.
private static readonly SqlVarCharSerializer SqlVarCharSerializer = new (size: -1, codePageCharacterEncoding: 65001);

#pragma warning disable SA1101 // Prefix local calls with this - false positive on SerializeFixed
internal virtual (TypeMarker typeMarker, byte[] serializedBytes, int serializedBytesCount) Serialize(JsonNode propertyValue, ArrayPoolManager arrayPoolManager)
{
byte[] buffer;
int length;

if (propertyValue == null)
{
return (TypeMarker.Null, null, -1);
}

switch (propertyValue.GetValueKind())
{
case JsonValueKind.Undefined:
Debug.Assert(false, "Undefined value cannot be in the JSON");
return (default, null, -1);
case JsonValueKind.Null:
Debug.Assert(false, "Null type should have been handled by caller");
return (TypeMarker.Null, null, -1);
case JsonValueKind.True:
(buffer, length) = SerializeFixed(SqlBoolSerializer, true);
return (TypeMarker.Boolean, buffer, length);
case JsonValueKind.False:
(buffer, length) = SerializeFixed(SqlBoolSerializer, false);
return (TypeMarker.Boolean, buffer, length);
case JsonValueKind.Number:
if (long.TryParse(propertyValue.ToJsonString(), out long longValue))
{
(buffer, length) = SerializeFixed(SqlLongSerializer, longValue);
return (TypeMarker.Long, buffer, length);
}
else if (double.TryParse(propertyValue.ToJsonString(), out double doubleValue))
{
(buffer, length) = SerializeFixed(SqlDoubleSerializer, doubleValue);
return (TypeMarker.Double, buffer, length);
}
else
{
throw new InvalidOperationException("Unsupported Number type");
}

case JsonValueKind.String:
(buffer, length) = SerializeString(propertyValue.GetValue<string>());
return (TypeMarker.String, buffer, length);
case JsonValueKind.Array:
(buffer, length) = SerializeString(propertyValue.ToJsonString());
return (TypeMarker.Array, buffer, length);
case JsonValueKind.Object:
(buffer, length) = SerializeString(propertyValue.ToJsonString());
return (TypeMarker.Object, buffer, length);
default:
throw new InvalidOperationException($" Invalid or Unsupported Data Type Passed : {propertyValue.GetValueKind()}");
}

(byte[], int) SerializeFixed<T>(IFixedSizeSerializer<T> serializer, T value)
{
byte[] buffer = arrayPoolManager.Rent(serializer.GetSerializedMaxByteCount());
int length = serializer.Serialize(value, buffer);
return (buffer, length);
}

(byte[], int) SerializeString(string value)
{
byte[] buffer = arrayPoolManager.Rent(SqlVarCharSerializer.GetSerializedMaxByteCount(value.Length));
int length = SqlVarCharSerializer.Serialize(value, buffer);
return (buffer, length);
}
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RootNamespace>Microsoft.Azure.Cosmos.Encryption.Tests</RootNamespace>
<LangVersion>$(LangVersion)</LangVersion>
<DefineConstants Condition=" '$(IsPreview)' == 'True' ">$(DefineConstants);ENCRYPTION_CUSTOM_PREVIEW</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER

namespace Microsoft.Azure.Cosmos.Encryption.Tests.Transformation
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.Azure.Cosmos.Encryption.Custom;
using Microsoft.Azure.Cosmos.Encryption.Custom.Transformation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;

[TestClass]
public class JsonNodeSqlSerializerTests
{
private static ArrayPoolManager _poolManager;

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
_ = context;
_poolManager = new ArrayPoolManager();
}

[TestMethod]
[DynamicData(nameof(SerializationSamples))]
public void Serialize_SupportedValue(JsonNode testNode, byte expectedType, byte[] expectedBytes, int expectedLength)
{
JsonNodeSqlSerializer serializer = new();

(TypeMarker serializedType, byte[] serializedBytes, int serializedBytesCount) = serializer.Serialize(testNode, _poolManager);

Assert.AreEqual((TypeMarker)expectedType, serializedType);
Assert.AreEqual(expectedLength, serializedBytesCount);
if (expectedLength == -1)
{
Assert.IsTrue(serializedBytes == null);
}
else
{
Assert.IsTrue(expectedBytes.SequenceEqual(serializedBytes.AsSpan(0, serializedBytesCount).ToArray()));
}
}

public static IEnumerable<object[]> SerializationSamples
{
get
{
List<object[]> values = new()
{
new object[] {JsonValue.Create((string)null), (byte)TypeMarker.Null, null, -1 },
new object[] {JsonValue.Create(true), (byte)TypeMarker.Boolean, GetNewtonsoftValueEquivalent(true), 8},
new object[] {JsonValue.Create(false), (byte)TypeMarker.Boolean, GetNewtonsoftValueEquivalent(false), 8},
new object[] {JsonValue.Create(192), (byte)TypeMarker.Long, GetNewtonsoftValueEquivalent(192), 8},
new object[] {JsonValue.Create(192.5), (byte)TypeMarker.Double, GetNewtonsoftValueEquivalent(192.5), 8},
new object[] {JsonValue.Create(testString), (byte)TypeMarker.String, GetNewtonsoftValueEquivalent(testString), 11},
new object[] {JsonValue.Create(testArray), (byte)TypeMarker.Array, GetNewtonsoftValueEquivalent(testArray), 10},
new object[] {JsonValue.Create(testClass), (byte)TypeMarker.Object, GetNewtonsoftValueEquivalent(testClass), 33}
};

return values;
}
}

private static readonly string testString = "Hello world";
private static readonly int[] testArray = new[] {10, 18, 19};
private static readonly TestClass testClass = new() { SomeInt = 1, SomeString = "asdf" };

private class TestClass
{
public int SomeInt { get; set; }
public string SomeString { get; set; }
}

private static byte[] GetNewtonsoftValueEquivalent<T>(T value)
{
JObjectSqlSerializer serializer = new ();
JToken token = value switch
{
int[] => new JArray(value),
TestClass => JObject.FromObject(value),
_ => new JValue(value),
};
(TypeMarker _, byte[] bytes, int lenght) = serializer.Serialize(token, _poolManager);
return bytes.AsSpan(0, lenght).ToArray();
}

}
}

#endif