Skip to content

Commit

Permalink
Client Encryption(Custom): Adds fix for deserialization issue for inv…
Browse files Browse the repository at this point in the history
…alid date type. (Azure#2834)

This PR adds a fix to pass serialization settings to disable date parsing during deserialization, which is already being done during serialization but I missed to pass the same during deserialization.

Adds change log for custom encryption package and bumps up package version.
  • Loading branch information
kr-santosh authored Oct 29, 2021
1 parent 89f8148 commit 47e9ee7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ClientPreviewSuffixVersion>preview</ClientPreviewSuffixVersion>
<DirectVersion>3.23.1</DirectVersion>
<EncryptionVersion>1.0.0-previewV17</EncryptionVersion>
<CustomEncryptionVersion>1.0.0-preview</CustomEncryptionVersion>
<CustomEncryptionVersion>1.0.0-preview02</CustomEncryptionVersion>
<HybridRowVersion>1.1.0-preview3</HybridRowVersion>
<LangVersion>9.0</LangVersion>
<AboveDirBuildProps>$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))</AboveDirBuildProps>
Expand Down
13 changes: 13 additions & 0 deletions Microsoft.Azure.Cosmos.Encryption.Custom/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Preview features are treated as a separate branch and will not be included in the official release until the feature is ready. Each preview release lists all the additional features that are enabled.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### <a name="1.0.0-preview02"/> [1.0.0-preview02](https://www.nuget.org/packages/Microsoft.Azure.Cosmos.Encryption.custom/1.0.0-preview02) - 2021-10-29

#### Fixes
- [#2834](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/2834) Adds fix for deserialization issue for invalid date type.


### <a name="1.0.0-preview"/> [1.0.0-preview](https://www.nuget.org/packages/Microsoft.Azure.Cosmos.Encryption.custom/1.0.0-preview) - 2021-10-20
- First preview of custom client-side encryption feature. See https://aka.ms/CosmosClientEncryption for more information on client-side encryption support in Azure Cosmos DB.
50 changes: 23 additions & 27 deletions Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ namespace Microsoft.Azure.Cosmos.Encryption.Custom
/// </summary>
internal static class EncryptionProcessor
{
internal static readonly CosmosJsonDotNetSerializer BaseSerializer = new CosmosJsonDotNetSerializer(
new JsonSerializerSettings()
{
DateParseHandling = DateParseHandling.None,
});
private static readonly SqlSerializerFactory SqlSerializerFactory = new SqlSerializerFactory();

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

private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings()
{
DateParseHandling = DateParseHandling.None,
};

internal static readonly CosmosJsonDotNetSerializer BaseSerializer = new CosmosJsonDotNetSerializer(JsonSerializerSettings);

/// <remarks>
/// If there isn't any PathsToEncrypt, input stream will be returned without any modification.
Expand Down Expand Up @@ -469,11 +475,6 @@ private static JObject RetrieveEncryptionProperties(

private static (TypeMarker, byte[]) Serialize(JToken propertyValue)
{
SqlSerializerFactory sqlSerializerFactory = new SqlSerializerFactory();

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

switch (propertyValue.Type)
{
case JTokenType.Undefined:
Expand All @@ -483,17 +484,17 @@ private static (TypeMarker, byte[]) Serialize(JToken propertyValue)
Debug.Assert(false, "Null type should have been handled by caller");
return (TypeMarker.Null, null);
case JTokenType.Boolean:
return (TypeMarker.Boolean, sqlSerializerFactory.GetDefaultSerializer<bool>().Serialize(propertyValue.ToObject<bool>()));
return (TypeMarker.Boolean, SqlSerializerFactory.GetDefaultSerializer<bool>().Serialize(propertyValue.ToObject<bool>()));
case JTokenType.Float:
return (TypeMarker.Double, sqlSerializerFactory.GetDefaultSerializer<double>().Serialize(propertyValue.ToObject<double>()));
return (TypeMarker.Double, SqlSerializerFactory.GetDefaultSerializer<double>().Serialize(propertyValue.ToObject<double>()));
case JTokenType.Integer:
return (TypeMarker.Long, sqlSerializerFactory.GetDefaultSerializer<long>().Serialize(propertyValue.ToObject<long>()));
return (TypeMarker.Long, SqlSerializerFactory.GetDefaultSerializer<long>().Serialize(propertyValue.ToObject<long>()));
case JTokenType.String:
return (TypeMarker.String, sqlVarCharSerializer.Serialize(propertyValue.ToObject<string>()));
return (TypeMarker.String, SqlVarCharSerializer.Serialize(propertyValue.ToObject<string>()));
case JTokenType.Array:
return (TypeMarker.Array, sqlVarCharSerializer.Serialize(propertyValue.ToString()));
return (TypeMarker.Array, SqlVarCharSerializer.Serialize(propertyValue.ToString()));
case JTokenType.Object:
return (TypeMarker.Object, sqlVarCharSerializer.Serialize(propertyValue.ToString()));
return (TypeMarker.Object, SqlVarCharSerializer.Serialize(propertyValue.ToString()));
default:
throw new InvalidOperationException($" Invalid or Unsupported Data Type Passed : {propertyValue.Type}");
}
Expand All @@ -505,30 +506,25 @@ private static void DeserializeAndAddProperty(
JObject jObject,
string key)
{
SqlSerializerFactory sqlSerializerFactory = new SqlSerializerFactory();

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

switch (typeMarker)
{
case TypeMarker.Boolean:
jObject.Add(key, sqlSerializerFactory.GetDefaultSerializer<bool>().Deserialize(serializedBytes));
jObject.Add(key, SqlSerializerFactory.GetDefaultSerializer<bool>().Deserialize(serializedBytes));
break;
case TypeMarker.Double:
jObject.Add(key, sqlSerializerFactory.GetDefaultSerializer<double>().Deserialize(serializedBytes));
jObject.Add(key, SqlSerializerFactory.GetDefaultSerializer<double>().Deserialize(serializedBytes));
break;
case TypeMarker.Long:
jObject.Add(key, sqlSerializerFactory.GetDefaultSerializer<long>().Deserialize(serializedBytes));
jObject.Add(key, SqlSerializerFactory.GetDefaultSerializer<long>().Deserialize(serializedBytes));
break;
case TypeMarker.String:
jObject.Add(key, sqlVarCharSerializer.Deserialize(serializedBytes));
jObject.Add(key, SqlVarCharSerializer.Deserialize(serializedBytes));
break;
case TypeMarker.Array:
jObject.Add(key, JsonConvert.DeserializeObject<JArray>(sqlVarCharSerializer.Deserialize(serializedBytes)));
jObject.Add(key, JsonConvert.DeserializeObject<JArray>(SqlVarCharSerializer.Deserialize(serializedBytes), JsonSerializerSettings));
break;
case TypeMarker.Object:
jObject.Add(key, JsonConvert.DeserializeObject<JObject>(sqlVarCharSerializer.Deserialize(serializedBytes)));
jObject.Add(key, JsonConvert.DeserializeObject<JObject>(SqlVarCharSerializer.Deserialize(serializedBytes), JsonSerializerSettings));
break;
default:
Debug.Fail(string.Format("Unexpected type marker {0}", typeMarker));
Expand Down

0 comments on commit 47e9ee7

Please sign in to comment.