-
Notifications
You must be signed in to change notification settings - Fork 526
[Client encryption] Add 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
Open
MartinSarkany
wants to merge
10
commits into
Azure:master
Choose a base branch
from
MartinSarkany:feature/cosmosdataencryptionkeyprovider-sync-init
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 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 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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are assumptions about the partition-key which were validated in initialization
Its even high impactful post initialization if CosmosDB is unavailable right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it to XML doc.