Skip to content

Commit

Permalink
Client Encryption: Adds fix for supporting Prefix Partition Key (Hier…
Browse files Browse the repository at this point in the history
…archical partitioning) (#3979)

* Hirarchical pk bug fix

* Hirarchical pk bug fix

* Hirarchical pk bug fix

* Hirarchical pk bug fix

* Hirarchical pk bug fix

* testing new version

* adding more tests

* adding more tests

* adding more tests

* code review changes

* test fix

* test fix

* test fix

* test fix

---------

Co-authored-by: Nalu Tripician <[email protected]>
  • Loading branch information
2 people authored and kundadebdatta committed Aug 8, 2023
1 parent 38b556b commit ee88843
Show file tree
Hide file tree
Showing 5 changed files with 413 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<ClientPreviewVersion>3.35.2</ClientPreviewVersion>
<ClientPreviewSuffixVersion>preview</ClientPreviewSuffixVersion>
<DirectVersion>3.31.3</DirectVersion>
<EncryptionOfficialVersion>2.0.2</EncryptionOfficialVersion>
<EncryptionPreviewVersion>2.0.2</EncryptionPreviewVersion>
<EncryptionOfficialVersion>2.0.3</EncryptionOfficialVersion>
<EncryptionPreviewVersion>2.0.3</EncryptionPreviewVersion>
<EncryptionPreviewSuffixVersion>preview</EncryptionPreviewSuffixVersion>
<CustomEncryptionVersion>1.0.0-preview06</CustomEncryptionVersion>
<HybridRowVersion>1.1.0-preview3</HybridRowVersion>
Expand Down
10 changes: 10 additions & 0 deletions Microsoft.Azure.Cosmos.Encryption/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ Preview features are treated as a separate branch and will not be included in th
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="2.0.3"/> [2.0.3](https://www.nuget.org/packages/Microsoft.Azure.Cosmos.Encryption/2.0.3) - 2023-07-12

#### Added
- [#3979](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/3979) Adds fix for supporting Prefix Partition Key (Hierarchical partitioning).

### <a name="2.0.3-preview"/> [2.0.3-preview](https://www.nuget.org/packages/Microsoft.Azure.Cosmos.Encryption/2.0.3-preview) - 2023-07-12

#### Added
- [#3979](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/3979) Adds fix for supporting Prefix Partition Key (Hierarchical partitioning).

### <a name="2.0.2"/> [2.0.2](https://www.nuget.org/packages/Microsoft.Azure.Cosmos.Encryption/2.0.2) - 2023-06-01

#### Added
Expand Down
17 changes: 12 additions & 5 deletions Microsoft.Azure.Cosmos.Encryption/src/EncryptionContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -957,15 +957,22 @@ internal async Task<string> CheckIfIdIsEncryptedAndGetEncryptedIdAsync(
JArray jArray = JArray.Parse(partitionKey.ToString());

#if ENCRYPTIONPREVIEW
if (jArray.Count > 1)
if (encryptionSettings.PartitionKeyPaths.Count > 1)
{
int i = 0;
int counter = 0;
PartitionKeyBuilder partitionKeyBuilder = new PartitionKeyBuilder();

if (jArray.Count() > encryptionSettings.PartitionKeyPaths.Count())
{
throw new NotSupportedException($"The number of partition keys passed in the query exceeds the number of keys initialized on the container. Container Id : {this.Id}");
}
bool isPkEncrypted = false;

// partitionKeyBuilder expects the paths and values to be in same order.
foreach (string path in encryptionSettings.PartitionKeyPaths)
for(counter = 0; counter < jArray.Count(); counter++)
{
string path = encryptionSettings.PartitionKeyPaths[counter];

// case: partition key path is /a/b/c and the client encryption policy has /a in path.
// hence encrypt the partition key value with using its top level path /a since /c would have been encrypted in the document using /a's policy.
string partitionKeyPath = path.Split('/')[1];
Expand All @@ -975,12 +982,12 @@ internal async Task<string> CheckIfIdIsEncryptedAndGetEncryptedIdAsync(

if (encryptionSettingForProperty == null)
{
partitionKeyBuilder.Add(jArray[i++].ToString());
partitionKeyBuilder.Add(jArray[counter].ToString());
continue;
}

isPkEncrypted = true;
Stream valueStream = EncryptionProcessor.BaseSerializer.ToStream(jArray[i++]);
Stream valueStream = EncryptionProcessor.BaseSerializer.ToStream(jArray[counter]);

Stream encryptedPartitionKey = await EncryptionProcessor.EncryptValueStreamAsync(
valueStreamToEncrypt: valueStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
</ItemGroup>

<ItemGroup Condition=" '$(SdkProjectRef)' != 'True' AND '$(IsPreview)' != 'True' ">
<PackageReference Include="Microsoft.Azure.Cosmos" Version="[3.31.0,3.34.0]" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="[3.31.0,3.35.2]" />
</ItemGroup>

<ItemGroup Condition=" '$(SdkProjectRef)' != 'True' AND '$(IsPreview)' == 'True' ">
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.34.0-preview" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.35.2-preview" />
</ItemGroup>

<ItemGroup Condition=" '$(SdkProjectRef)' == 'True' ">
Expand Down
Loading

0 comments on commit ee88843

Please sign in to comment.