-
Notifications
You must be signed in to change notification settings - Fork 543
[Client encryption]: Adds synchronous initialization to CosmosDataEncryptionKeyProvider #5423
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
kirankumarkolli
merged 17 commits into
Azure:master
from
MartinSarkany:feature/cosmosdataencryptionkeyprovider-sync-init
Apr 28, 2026
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
35e768c
Add sync initialization to CosmosDataEncryptionKeyProvider
MartinSarkany 2f879bc
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
MartinSarkany 9bcb702
Fix contract test
MartinSarkany 4932b33
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
MartinSarkany 22c344b
Fix contracts
MartinSarkany 13ec854
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
MartinSarkany 3e9b5df
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
MartinSarkany 1fcb13b
Merge branch 'feature/cosmosdataencryptionkeyprovider-sync-init' of h…
MartinSarkany 170576e
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
MartinSarkany 78a9470
Prevent race condition, extend documentation
MartinSarkany 54f4609
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
MartinSarkany daf40d8
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
MartinSarkany 7898518
Minor fix
MartinSarkany e5bef98
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
kirankumarkolli ff0693f
Code review improvements
MartinSarkany 867f8b4
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
kirankumarkolli 3adb7d4
Merge branch 'master' into feature/cosmosdataencryptionkeyprovider-sy…
kirankumarkolli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
180 changes: 180 additions & 0 deletions
180
...ts/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/CosmosDataEncryptionKeyProviderTests.cs
This file contains hidden or 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,180 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos.Encryption.Tests | ||
| { | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Cosmos; | ||
| using Microsoft.Azure.Cosmos.Encryption.Custom; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
| using Microsoft.Data.Encryption.Cryptography; | ||
|
|
||
| [TestClass] | ||
| public class CosmosDataEncryptionKeyProviderTests | ||
| { | ||
| private const string ContainerId = "dekContainer"; | ||
|
|
||
| [TestMethod] | ||
| public async Task InitializeAsync_WithValidContainer_CreatesAndSetsContainer() | ||
| { | ||
| Mock<Container> mockContainer = new(MockBehavior.Strict); | ||
| Mock<ContainerResponse> mockContainerResponse = new(MockBehavior.Strict); | ||
| mockContainerResponse.Setup(r => r.Container).Returns(mockContainer.Object); | ||
| mockContainerResponse.Setup(r => r.Resource).Returns(new ContainerProperties(ContainerId, partitionKeyPath: "/id")); | ||
|
|
||
| Mock<Database> mockDatabase = new(MockBehavior.Strict); | ||
| mockDatabase | ||
| .Setup(db => db.CreateContainerIfNotExistsAsync( | ||
| It.Is<string>(s => s == ContainerId), | ||
| It.Is<string>(pk => pk == "/id"), | ||
| It.IsAny<int?>(), | ||
| It.IsAny<RequestOptions>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(mockContainerResponse.Object); | ||
|
|
||
| CosmosDataEncryptionKeyProvider provider = CreateProvider(); | ||
|
|
||
| await provider.InitializeAsync(mockDatabase.Object, ContainerId); | ||
|
|
||
| Assert.AreSame(mockContainer.Object, provider.Container); | ||
|
|
||
| mockDatabase.VerifyAll(); | ||
| mockContainerResponse.VerifyAll(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task InitializeAsync_WithWrongPartitionKey_Throws() | ||
| { | ||
| Mock<Container> mockContainer = new(MockBehavior.Strict); | ||
| Mock<ContainerResponse> mockContainerResponse = new(MockBehavior.Strict); | ||
| mockContainerResponse.Setup(r => r.Container).Returns(mockContainer.Object); | ||
| mockContainerResponse.Setup(r => r.Resource).Returns(new ContainerProperties("dekBad", partitionKeyPath: "/different-id")); | ||
|
|
||
| Mock<Database> mockDatabase = new(MockBehavior.Strict); | ||
| mockDatabase | ||
| .Setup(db => db.CreateContainerIfNotExistsAsync( | ||
| It.Is<string>(s => s == "dekBad"), | ||
| It.Is<string>(pk => pk == "/id"), | ||
| It.IsAny<int?>(), | ||
| It.IsAny<RequestOptions>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(mockContainerResponse.Object); | ||
|
|
||
| CosmosDataEncryptionKeyProvider provider = CreateProvider(); | ||
|
|
||
| ArgumentException ex = await Assert.ThrowsExceptionAsync<ArgumentException>(() => provider.InitializeAsync(mockDatabase.Object, "dekBad")); | ||
|
|
||
| StringAssert.Contains(ex.Message, "partition key definition"); | ||
| Assert.AreEqual("containerId", ex.ParamName); | ||
|
|
||
| mockDatabase.VerifyAll(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Initialize_WithContainer_Succeeds() | ||
| { | ||
| Mock<Container> mockContainer = new(MockBehavior.Strict); | ||
|
|
||
| CosmosDataEncryptionKeyProvider provider = CreateProvider(); | ||
| provider.Initialize(mockContainer.Object); | ||
|
|
||
| Assert.AreSame(mockContainer.Object, provider.Container); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Initialize_WithNullContainer_Throws() | ||
| { | ||
| CosmosDataEncryptionKeyProvider provider = CreateProvider(); | ||
|
|
||
| ArgumentNullException ex = Assert.ThrowsException<ArgumentNullException>(() => provider.Initialize(null)); | ||
|
|
||
| Assert.AreEqual("container", ex.ParamName); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Initialize_Twice_Throws() | ||
| { | ||
| Mock<Container> mockContainer = new(MockBehavior.Strict); | ||
| CosmosDataEncryptionKeyProvider provider = CreateProvider(); | ||
| provider.Initialize(mockContainer.Object); | ||
|
|
||
| InvalidOperationException ex = Assert.ThrowsException<InvalidOperationException>(() => provider.Initialize(mockContainer.Object)); | ||
|
|
||
| StringAssert.Contains(ex.Message, nameof(CosmosDataEncryptionKeyProvider)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task InitializeAsync_AfterInitializeContainer_Throws() | ||
| { | ||
| Mock<Container> mockContainer = new(MockBehavior.Strict); | ||
| Mock<Database> mockDatabase = new(MockBehavior.Strict); | ||
| CosmosDataEncryptionKeyProvider provider = CreateProvider(); | ||
|
|
||
| provider.Initialize(mockContainer.Object); | ||
|
|
||
| await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => provider.InitializeAsync(mockDatabase.Object, "ignored")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void AccessContainer_BeforeInitialization_Throws() | ||
| { | ||
| CosmosDataEncryptionKeyProvider provider = CreateProvider(); | ||
|
|
||
| InvalidOperationException ex = Assert.ThrowsException<InvalidOperationException>(() => _ = provider.Container); | ||
|
|
||
| StringAssert.Contains(ex.Message, nameof(CosmosDataEncryptionKeyProvider)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_EncryptionKeyWrapProvider_SetsProperties() | ||
| { | ||
| #pragma warning disable CS0618 | ||
| Mock<EncryptionKeyWrapProvider> wrapProviderMock = new(MockBehavior.Strict); | ||
|
|
||
| CosmosDataEncryptionKeyProvider provider = new(wrapProviderMock.Object); | ||
|
|
||
| Assert.AreSame(wrapProviderMock.Object, provider.EncryptionKeyWrapProvider); | ||
| Assert.IsNotNull(provider.DataEncryptionKeyContainer); | ||
| Assert.IsNotNull(provider.DekCache); | ||
| #pragma warning restore CS0618 | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_EncryptionKeyStoreProvider_SetsMdePropertiesAndTtl_DefaultInfinite() | ||
| { | ||
| TestEncryptionKeyStoreProvider keyStoreProvider = new(); | ||
|
|
||
| CosmosDataEncryptionKeyProvider provider = new(keyStoreProvider); | ||
|
|
||
| Assert.AreSame(keyStoreProvider, provider.EncryptionKeyStoreProvider); | ||
| Assert.IsNotNull(provider.DekCache); | ||
| Assert.IsNotNull(provider.DataEncryptionKeyContainer); | ||
| Assert.IsTrue(provider.PdekCacheTimeToLive.HasValue); | ||
| Assert.IsTrue(provider.PdekCacheTimeToLive.Value > TimeSpan.Zero); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_KeyStoreProvider_SetsMdePropertiesAndTtl_Custom() | ||
| { | ||
| TimeSpan ttl = TimeSpan.FromMinutes(15); | ||
| TestEncryptionKeyStoreProvider keyStoreProvider = new() | ||
| { | ||
| DataEncryptionKeyCacheTimeToLive = ttl | ||
| }; | ||
|
|
||
| CosmosDataEncryptionKeyProvider provider = new(keyStoreProvider); | ||
|
|
||
| Assert.AreSame(keyStoreProvider, provider.EncryptionKeyStoreProvider); | ||
| Assert.AreEqual(ttl, provider.PdekCacheTimeToLive); | ||
| } | ||
|
|
||
| private static CosmosDataEncryptionKeyProvider CreateProvider() | ||
| { | ||
| return new CosmosDataEncryptionKeyProvider(new TestEncryptionKeyStoreProvider()); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.