-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client Encryption: Fixes System.Text custom serializer issue with Dat…
…aEncryptionKeyContainer operations. (#3386) * Use Cosmos Base serializer for DEK serialization. * Update DataEncryptionKeyFeedIterator.cs * Update MdeCustomEncryptionTests.cs * Update MdeCustomEncryptionTests.cs * updated tests. * Update DataEncryptionKeyContainerCore.cs * updated changelogs and build props * fixes as per review comment * Update DataEncryptionKeyContainerCore.cs * fixes.
- Loading branch information
1 parent
1151f55
commit 0dcd4a8
Showing
8 changed files
with
505 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyFeedIterator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Encryption.Custom | ||
{ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
internal sealed class DataEncryptionKeyFeedIterator : FeedIterator | ||
{ | ||
private readonly FeedIterator feedIterator; | ||
|
||
public DataEncryptionKeyFeedIterator( | ||
FeedIterator feedIterator) | ||
{ | ||
this.feedIterator = feedIterator; | ||
} | ||
|
||
public override bool HasMoreResults => this.feedIterator.HasMoreResults; | ||
|
||
public override Task<ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return this.feedIterator.ReadNextAsync(cancellationToken); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
Microsoft.Azure.Cosmos.Encryption.Custom/src/DataEncryptionKeyFeedIterator{T}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Encryption.Custom | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json.Linq; | ||
|
||
internal sealed class DataEncryptionKeyFeedIterator<T> : FeedIterator<T> | ||
{ | ||
private readonly FeedIterator feedIterator; | ||
private readonly CosmosResponseFactory responseFactory; | ||
|
||
public DataEncryptionKeyFeedIterator( | ||
DataEncryptionKeyFeedIterator feedIterator, | ||
CosmosResponseFactory responseFactory) | ||
{ | ||
if (!(feedIterator is DataEncryptionKeyFeedIterator)) | ||
{ | ||
throw new ArgumentOutOfRangeException($"{nameof(feedIterator)} must be of type {nameof(DataEncryptionKeyFeedIterator)}."); | ||
} | ||
|
||
this.feedIterator = feedIterator; | ||
this.responseFactory = responseFactory; | ||
} | ||
|
||
public override bool HasMoreResults => this.feedIterator.HasMoreResults; | ||
|
||
public override async Task<FeedResponse<T>> ReadNextAsync(CancellationToken cancellationToken = default) | ||
{ | ||
ResponseMessage responseMessage; | ||
|
||
if (typeof(T) == typeof(DataEncryptionKeyProperties)) | ||
{ | ||
IReadOnlyCollection<T> resource; | ||
(responseMessage, resource) = await this.ReadNextUsingCosmosBaseSerializerAsync(cancellationToken); | ||
|
||
return DecryptableFeedResponse<T>.CreateResponse( | ||
responseMessage, | ||
resource); | ||
} | ||
else | ||
{ | ||
responseMessage = await this.feedIterator.ReadNextAsync(cancellationToken); | ||
} | ||
|
||
return this.responseFactory.CreateItemFeedResponse<T>(responseMessage); | ||
} | ||
|
||
public async Task<(ResponseMessage, List<T>)> ReadNextUsingCosmosBaseSerializerAsync(CancellationToken cancellationToken = default) | ||
{ | ||
CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(options: null); | ||
using (diagnosticsContext.CreateScope("FeedIterator.ReadNextWithoutDecryption")) | ||
{ | ||
ResponseMessage responseMessage = await this.feedIterator.ReadNextAsync(cancellationToken); | ||
List<T> dataEncryptionKeyPropertiesList = null; | ||
|
||
if (responseMessage.IsSuccessStatusCode && responseMessage.Content != null) | ||
{ | ||
dataEncryptionKeyPropertiesList = this.ConvertResponseToDataEncryptionKeyPropertiesList( | ||
responseMessage.Content); | ||
|
||
return (responseMessage, dataEncryptionKeyPropertiesList); | ||
} | ||
|
||
return (responseMessage, dataEncryptionKeyPropertiesList); | ||
} | ||
} | ||
|
||
private List<T> ConvertResponseToDataEncryptionKeyPropertiesList( | ||
Stream content) | ||
{ | ||
JObject contentJObj = EncryptionProcessor.BaseSerializer.FromStream<JObject>(content); | ||
|
||
if (!(contentJObj.SelectToken(Constants.DocumentsResourcePropertyName) is JArray documents)) | ||
{ | ||
throw new InvalidOperationException("Feed Response body contract was violated. Feed Response did not have an array of Documents."); | ||
} | ||
|
||
List<T> dataEncryptionKeyPropertiesList = new List<T>(documents.Count); | ||
|
||
foreach (JToken value in documents) | ||
{ | ||
dataEncryptionKeyPropertiesList.Add(value.ToObject<T>()); | ||
} | ||
|
||
return dataEncryptionKeyPropertiesList; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionCosmosException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Encryption.Custom | ||
{ | ||
using System; | ||
using System.Net; | ||
|
||
internal sealed class EncryptionCosmosException : CosmosException | ||
{ | ||
private readonly CosmosDiagnostics encryptionCosmosDiagnostics; | ||
|
||
public EncryptionCosmosException( | ||
string message, | ||
HttpStatusCode statusCode, | ||
int subStatusCode, | ||
string activityId, | ||
double requestCharge, | ||
CosmosDiagnostics encryptionCosmosDiagnostics) | ||
: base(message, statusCode, subStatusCode, activityId, requestCharge) | ||
{ | ||
this.encryptionCosmosDiagnostics = encryptionCosmosDiagnostics ?? throw new ArgumentNullException(nameof(encryptionCosmosDiagnostics)); | ||
} | ||
|
||
public override CosmosDiagnostics Diagnostics => this.encryptionCosmosDiagnostics; | ||
} | ||
} |
Oops, something went wrong.