[Internal] Encryption: Add specific encryption emulator tests (#1481)

* [Internal] Encryption: Add specific encryption emulator tests

* Fixed property names
This commit is contained in:
j82w 2020-05-07 09:20:42 -07:00 коммит произвёл GitHub
Родитель a1f81ac5f9
Коммит 47c688b4be
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 321 добавлений и 194 удалений

Просмотреть файл

@ -2,31 +2,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------ //------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests namespace Microsoft.Azure.Cosmos.Encryption.EmulatorTests
{ {
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Encryption;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Cosmos.Scripts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Cosmos.Scripts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using JsonWriter = Json.JsonWriter;
using JsonReader = Json.JsonReader;
using Microsoft.Azure.Cosmos.Encryption;
[TestClass] [TestClass]
public class EncryptionTests public class EncryptionTests
{ {
private static EncryptionKeyWrapMetadata metadata1 = new EncryptionKeyWrapMetadata("metadata1"); private static readonly EncryptionKeyWrapMetadata metadata1 = new EncryptionKeyWrapMetadata("metadata1");
private static EncryptionKeyWrapMetadata metadata2 = new EncryptionKeyWrapMetadata("metadata2"); private static readonly EncryptionKeyWrapMetadata metadata2 = new EncryptionKeyWrapMetadata("metadata2");
private const string metadataUpdateSuffix = "updated"; private const string metadataUpdateSuffix = "updated";
private static TimeSpan cacheTTL = TimeSpan.FromDays(1); private static TimeSpan cacheTTL = TimeSpan.FromDays(1);
@ -34,9 +30,8 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
private static CosmosClient client; private static CosmosClient client;
private static DatabaseInternal databaseCore; private static Database database;
private static DataEncryptionKeyProperties dekProperties; private static DataEncryptionKeyProperties dekProperties;
private static ContainerInternal itemContainerCore;
private static Container itemContainer; private static Container itemContainer;
private static Container keyContainer; private static Container keyContainer;
private static CosmosDataEncryptionKeyProvider dekProvider; private static CosmosDataEncryptionKeyProvider dekProvider;
@ -49,14 +44,12 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
EncryptionTests.encryptor = new TestEncryptor(EncryptionTests.dekProvider); EncryptionTests.encryptor = new TestEncryptor(EncryptionTests.dekProvider);
EncryptionTests.client = EncryptionTests.GetClient(); EncryptionTests.client = EncryptionTests.GetClient();
EncryptionTests.databaseCore = (DatabaseInlineCore)await EncryptionTests.client.CreateDatabaseAsync(Guid.NewGuid().ToString()); EncryptionTests.database = await EncryptionTests.client.CreateDatabaseAsync(Guid.NewGuid().ToString());
EncryptionTests.keyContainer = await EncryptionTests.databaseCore.CreateContainerAsync(Guid.NewGuid().ToString(), "/id", 400); EncryptionTests.keyContainer = await EncryptionTests.database.CreateContainerAsync(Guid.NewGuid().ToString(), "/id", 400);
await EncryptionTests.dekProvider.InitializeAsync(EncryptionTests.databaseCore, EncryptionTests.keyContainer.Id); await EncryptionTests.dekProvider.InitializeAsync(EncryptionTests.database, EncryptionTests.keyContainer.Id);
EncryptionTests.itemContainer = await EncryptionTests.database.CreateContainerAsync(Guid.NewGuid().ToString(), "/PK", 400);
EncryptionTests.itemContainer = await EncryptionTests.databaseCore.CreateContainerAsync(Guid.NewGuid().ToString(), "/PK", 400);
EncryptionTests.itemContainerCore = (ContainerInlineCore)EncryptionTests.itemContainer;
EncryptionTests.dekProperties = await EncryptionTests.CreateDekAsync(EncryptionTests.dekProvider, EncryptionTests.dekId); EncryptionTests.dekProperties = await EncryptionTests.CreateDekAsync(EncryptionTests.dekProvider, EncryptionTests.dekId);
} }
@ -64,9 +57,9 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
[ClassCleanup] [ClassCleanup]
public static async Task ClassCleanup() public static async Task ClassCleanup()
{ {
if (EncryptionTests.databaseCore != null) if (EncryptionTests.database != null)
{ {
using (await EncryptionTests.databaseCore.DeleteStreamAsync()) { } using (await EncryptionTests.database.DeleteStreamAsync()) { }
} }
if (EncryptionTests.client != null) if (EncryptionTests.client != null)
@ -93,7 +86,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
// Use different DEK provider to avoid (unintentional) cache impact // Use different DEK provider to avoid (unintentional) cache impact
CosmosDataEncryptionKeyProvider dekProvider = new CosmosDataEncryptionKeyProvider(new TestKeyWrapProvider()); CosmosDataEncryptionKeyProvider dekProvider = new CosmosDataEncryptionKeyProvider(new TestKeyWrapProvider());
await dekProvider.InitializeAsync(EncryptionTests.databaseCore, EncryptionTests.keyContainer.Id); await dekProvider.InitializeAsync(EncryptionTests.database, EncryptionTests.keyContainer.Id);
DataEncryptionKeyProperties readProperties = await dekProvider.DataEncryptionKeyContainer.ReadDataEncryptionKeyAsync(dekId); DataEncryptionKeyProperties readProperties = await dekProvider.DataEncryptionKeyContainer.ReadDataEncryptionKeyAsync(dekId);
Assert.AreEqual(dekProperties, readProperties); Assert.AreEqual(dekProperties, readProperties);
} }
@ -101,11 +94,11 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
[TestMethod] [TestMethod]
public async Task EncryptionDekReadFeed() public async Task EncryptionDekReadFeed()
{ {
Container newKeyContainer = await EncryptionTests.databaseCore.CreateContainerAsync(Guid.NewGuid().ToString(), "/id", 400); Container newKeyContainer = await EncryptionTests.database.CreateContainerAsync(Guid.NewGuid().ToString(), "/id", 400);
try try
{ {
CosmosDataEncryptionKeyProvider dekProvider = new CosmosDataEncryptionKeyProvider(new TestKeyWrapProvider()); CosmosDataEncryptionKeyProvider dekProvider = new CosmosDataEncryptionKeyProvider(new TestKeyWrapProvider());
await dekProvider.InitializeAsync(EncryptionTests.databaseCore, newKeyContainer.Id); await dekProvider.InitializeAsync(EncryptionTests.database, newKeyContainer.Id);
string contosoV1 = "Contoso_v001"; string contosoV1 = "Contoso_v001";
string contosoV2 = "Contoso_v002"; string contosoV2 = "Contoso_v002";
@ -160,7 +153,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
public async Task EncryptionCreateItemWithoutEncryptionOptions() public async Task EncryptionCreateItemWithoutEncryptionOptions()
{ {
TestDoc testDoc = TestDoc.Create(); TestDoc testDoc = TestDoc.Create();
ItemResponse<TestDoc> createResponse = await EncryptionTests.itemContainerCore.CreateItemAsync( ItemResponse<TestDoc> createResponse = await EncryptionTests.itemContainer.CreateItemAsync(
testDoc, testDoc,
new PartitionKey(testDoc.PK)); new PartitionKey(testDoc.PK));
Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode); Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode);
@ -170,21 +163,21 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
[TestMethod] [TestMethod]
public async Task EncryptionCreateItem() public async Task EncryptionCreateItem()
{ {
TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, testDoc); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, testDoc);
await EncryptionTests.VerifyItemByReadStreamAsync(EncryptionTests.itemContainerCore, testDoc); await EncryptionTests.VerifyItemByReadStreamAsync(EncryptionTests.itemContainer, testDoc);
TestDoc expectedDoc = new TestDoc(testDoc); TestDoc expectedDoc = new TestDoc(testDoc);
await EncryptionTests.ValidateQueryResultsAsync( await EncryptionTests.ValidateQueryResultsAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
"SELECT * FROM c", "SELECT * FROM c",
expectedDoc); expectedDoc);
await EncryptionTests.ValidateQueryResultsAsync( await EncryptionTests.ValidateQueryResultsAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
string.Format( string.Format(
"SELECT * FROM c where c.PK = '{0}' and c.id = '{1}' and c.NonSensitive = '{2}'", "SELECT * FROM c where c.PK = '{0}' and c.id = '{1}' and c.NonSensitive = '{2}'",
expectedDoc.PK, expectedDoc.PK,
@ -193,12 +186,12 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
expectedDoc); expectedDoc);
await EncryptionTests.ValidateQueryResultsAsync( await EncryptionTests.ValidateQueryResultsAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
string.Format("SELECT * FROM c where c.Sensitive = '{0}'", testDoc.Sensitive), string.Format("SELECT * FROM c where c.Sensitive = '{0}'", testDoc.Sensitive),
expectedDoc: null); expectedDoc: null);
await EncryptionTests.ValidateQueryResultsAsync( await EncryptionTests.ValidateQueryResultsAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
queryDefinition: new QueryDefinition( queryDefinition: new QueryDefinition(
"select * from c where c.id = @theId and c.PK = @thePK") "select * from c where c.id = @theId and c.PK = @thePK")
.WithParameter("@theId", expectedDoc.Id) .WithParameter("@theId", expectedDoc.Id)
@ -208,32 +201,32 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
expectedDoc.Sensitive = null; expectedDoc.Sensitive = null;
await EncryptionTests.ValidateQueryResultsAsync( await EncryptionTests.ValidateQueryResultsAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
"SELECT c.id, c.PK, c.Sensitive, c.NonSensitive FROM c", "SELECT c.id, c.PK, c.Sensitive, c.NonSensitive FROM c",
expectedDoc); expectedDoc);
await EncryptionTests.ValidateQueryResultsAsync( await EncryptionTests.ValidateQueryResultsAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
"SELECT c.id, c.PK, c.NonSensitive FROM c", "SELECT c.id, c.PK, c.NonSensitive FROM c",
expectedDoc); expectedDoc);
await EncryptionTests.ValidateSprocResultsAsync( await EncryptionTests.ValidateSprocResultsAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
expectedDoc); expectedDoc);
} }
[TestMethod] [TestMethod]
public async Task EncryptionDecryptQueryResultMultipleDocs() public async Task EncryptionDecryptQueryResultMultipleDocs()
{ {
TestDoc testDoc1 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc testDoc1 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
TestDoc testDoc2 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc testDoc2 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
string query = $"SELECT * FROM c WHERE c.PK in ('{testDoc1.PK}', '{testDoc2.PK}')"; string query = $"SELECT * FROM c WHERE c.PK in ('{testDoc1.PK}', '{testDoc2.PK}')";
await EncryptionTests.ValidateQueryResultsMultipleDocumentsAsync(EncryptionTests.itemContainerCore, testDoc1, testDoc2, query); await EncryptionTests.ValidateQueryResultsMultipleDocumentsAsync(EncryptionTests.itemContainer, testDoc1, testDoc2, query);
// ORDER BY query // ORDER BY query
query = query + " ORDER BY c._ts"; query = query + " ORDER BY c._ts";
await EncryptionTests.ValidateQueryResultsMultipleDocumentsAsync(EncryptionTests.itemContainerCore, testDoc1, testDoc2, query); await EncryptionTests.ValidateQueryResultsMultipleDocumentsAsync(EncryptionTests.itemContainer, testDoc1, testDoc2, query);
} }
[TestMethod] [TestMethod]
@ -242,85 +235,36 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
string dekId1 = "mydek1"; string dekId1 = "mydek1";
await EncryptionTests.CreateDekAsync(EncryptionTests.dekProvider, dekId1); await EncryptionTests.CreateDekAsync(EncryptionTests.dekProvider, dekId1);
TestDoc testDoc1 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc testDoc1 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
TestDoc testDoc2 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, dekId1, TestDoc.PathsToEncrypt); TestDoc testDoc2 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, dekId1, TestDoc.PathsToEncrypt);
string query = $"SELECT * FROM c WHERE c.PK in ('{testDoc1.PK}', '{testDoc2.PK}')"; string query = $"SELECT * FROM c WHERE c.PK in ('{testDoc1.PK}', '{testDoc2.PK}')";
await EncryptionTests.ValidateQueryResultsMultipleDocumentsAsync(EncryptionTests.itemContainerCore, testDoc1, testDoc2, query); await EncryptionTests.ValidateQueryResultsMultipleDocumentsAsync(EncryptionTests.itemContainer, testDoc1, testDoc2, query);
} }
[TestMethod] [TestMethod]
public async Task EncryptionDecryptQueryResultMultipleEncryptedProperties() public async Task EncryptionDecryptQueryResultMultipleEncryptedProperties()
{ {
TestDoc testDoc = await EncryptionTests.CreateItemAsync( TestDoc testDoc = await EncryptionTests.CreateItemAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
EncryptionTests.dekId, EncryptionTests.dekId,
new List<string>() { "/Sensitive", "/NonSensitive" }); new List<string>() { "/Sensitive", "/NonSensitive" });
TestDoc expectedDoc = new TestDoc(testDoc); TestDoc expectedDoc = new TestDoc(testDoc);
await EncryptionTests.ValidateQueryResultsAsync( await EncryptionTests.ValidateQueryResultsAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
"SELECT * FROM c", "SELECT * FROM c",
expectedDoc); expectedDoc);
} }
[TestMethod]
public async Task EncryptionDecryptQueryBinaryResponse()
{
TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
CosmosSerializationFormatOptions options = new CosmosSerializationFormatOptions(
Documents.ContentSerializationFormat.CosmosBinary.ToString(),
(content) => JsonNavigator.Create(content),
() => JsonWriter.Create(JsonSerializationFormat.Binary));
QueryRequestOptions requestOptions = new QueryRequestOptions()
{
CosmosSerializationFormatOptions = options
};
TestDoc expectedDoc = new TestDoc(testDoc);
string query = "SELECT * FROM c";
FeedIterator feedIterator = EncryptionTests.itemContainerCore.GetItemQueryStreamIterator(
query,
requestOptions: requestOptions);
while (feedIterator.HasMoreResults)
{
ResponseMessage response = await feedIterator.ReadNextAsync();
Assert.IsTrue(response.IsSuccessStatusCode);
Assert.IsNull(response.ErrorMessage);
// Copy the stream and check that the first byte is the correct value
MemoryStream memoryStream = new MemoryStream();
response.Content.CopyTo(memoryStream);
byte[] content = memoryStream.ToArray();
response.Content.Position = 0;
// Examine the first buffer byte to determine the serialization format
byte firstByte = content[0];
Assert.AreEqual(128, firstByte);
Assert.AreEqual(JsonSerializationFormat.Binary, (JsonSerializationFormat)firstByte);
IJsonReader reader = JsonReader.Create(content);
IJsonWriter textWriter = JsonWriter.Create(JsonSerializationFormat.Text);
textWriter.WriteAll(reader);
string json = Encoding.UTF8.GetString(textWriter.GetResult().ToArray());
Assert.IsNotNull(json);
Assert.IsTrue(json.Contains(testDoc.Sensitive));
}
}
[TestMethod] [TestMethod]
public async Task EncryptionDecryptQueryValueResponse() public async Task EncryptionDecryptQueryValueResponse()
{ {
TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
string query = "SELECT VALUE COUNT(1) FROM c"; string query = "SELECT VALUE COUNT(1) FROM c";
await EncryptionTests.ValidateQueryResponseAsync(EncryptionTests.itemContainerCore, query); await EncryptionTests.ValidateQueryResponseAsync(EncryptionTests.itemContainer, query);
} }
[TestMethod] [TestMethod]
@ -328,63 +272,63 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{ {
string partitionKey = Guid.NewGuid().ToString(); string partitionKey = Guid.NewGuid().ToString();
TestDoc testDoc1 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt, partitionKey); TestDoc testDoc1 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt, partitionKey);
TestDoc testDoc2 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt, partitionKey); TestDoc testDoc2 = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt, partitionKey);
string query = $"SELECT COUNT(c.Id), c.PK " + string query = $"SELECT COUNT(c.Id), c.PK " +
$"FROM c WHERE c.PK = '{partitionKey}' " + $"FROM c WHERE c.PK = '{partitionKey}' " +
$"GROUP BY c.PK "; $"GROUP BY c.PK ";
await EncryptionTests.ValidateQueryResponseAsync(EncryptionTests.itemContainerCore, query); await EncryptionTests.ValidateQueryResponseAsync(EncryptionTests.itemContainer, query);
} }
[TestMethod] [TestMethod]
public async Task EncryptionRudItem() public async Task EncryptionRudItem()
{ {
TestDoc testDoc = await EncryptionTests.UpsertItemAsync( TestDoc testDoc = await EncryptionTests.UpsertItemAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
TestDoc.Create(), TestDoc.Create(),
EncryptionTests.dekId, EncryptionTests.dekId,
TestDoc.PathsToEncrypt, TestDoc.PathsToEncrypt,
HttpStatusCode.Created); HttpStatusCode.Created);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, testDoc); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, testDoc);
testDoc.NonSensitive = Guid.NewGuid().ToString(); testDoc.NonSensitive = Guid.NewGuid().ToString();
testDoc.Sensitive = Guid.NewGuid().ToString(); testDoc.Sensitive = Guid.NewGuid().ToString();
ItemResponse<TestDoc> upsertResponse = await EncryptionTests.UpsertItemAsync( ItemResponse<TestDoc> upsertResponse = await EncryptionTests.UpsertItemAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
testDoc, testDoc,
EncryptionTests.dekId, EncryptionTests.dekId,
TestDoc.PathsToEncrypt, TestDoc.PathsToEncrypt,
HttpStatusCode.OK); HttpStatusCode.OK);
TestDoc updatedDoc = upsertResponse.Resource; TestDoc updatedDoc = upsertResponse.Resource;
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, updatedDoc); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, updatedDoc);
updatedDoc.NonSensitive = Guid.NewGuid().ToString(); updatedDoc.NonSensitive = Guid.NewGuid().ToString();
updatedDoc.Sensitive = Guid.NewGuid().ToString(); updatedDoc.Sensitive = Guid.NewGuid().ToString();
TestDoc replacedDoc = await EncryptionTests.ReplaceItemAsync( TestDoc replacedDoc = await EncryptionTests.ReplaceItemAsync(
EncryptionTests.itemContainerCore, EncryptionTests.itemContainer,
updatedDoc, updatedDoc,
EncryptionTests.dekId, EncryptionTests.dekId,
TestDoc.PathsToEncrypt, TestDoc.PathsToEncrypt,
upsertResponse.ETag); upsertResponse.ETag);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, replacedDoc); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, replacedDoc);
await EncryptionTests.DeleteItemAsync(EncryptionTests.itemContainerCore, replacedDoc); await EncryptionTests.DeleteItemAsync(EncryptionTests.itemContainer, replacedDoc);
} }
[TestMethod] [TestMethod]
public async Task EncryptionResourceTokenAuthRestricted() public async Task EncryptionResourceTokenAuthRestricted()
{ {
TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
User restrictedUser = EncryptionTests.databaseCore.GetUser(Guid.NewGuid().ToString()); User restrictedUser = EncryptionTests.database.GetUser(Guid.NewGuid().ToString());
await EncryptionTests.databaseCore.CreateUserAsync(restrictedUser.Id); await EncryptionTests.database.CreateUserAsync(restrictedUser.Id);
PermissionProperties restrictedUserPermission = await restrictedUser.CreatePermissionAsync( PermissionProperties restrictedUserPermission = await restrictedUser.CreatePermissionAsync(
new PermissionProperties(Guid.NewGuid().ToString(), PermissionMode.All, EncryptionTests.itemContainer)); new PermissionProperties(Guid.NewGuid().ToString(), PermissionMode.All, EncryptionTests.itemContainer));
@ -392,12 +336,12 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
CosmosDataEncryptionKeyProvider dekProvider = new CosmosDataEncryptionKeyProvider(new TestKeyWrapProvider()); CosmosDataEncryptionKeyProvider dekProvider = new CosmosDataEncryptionKeyProvider(new TestKeyWrapProvider());
TestEncryptor encryptor = new TestEncryptor(dekProvider); TestEncryptor encryptor = new TestEncryptor(dekProvider);
(string endpoint, string _) = TestCommon.GetAccountInfo(); CosmosClient clientForRestrictedUser = TestCommon.CreateCosmosClient(
CosmosClient clientForRestrictedUser = new CosmosClientBuilder(endpoint, restrictedUserPermission.Token) restrictedUserPermission.Token,
.WithEncryptor(encryptor) builder => builder
.Build(); .WithEncryptor(encryptor));
Database databaseForRestrictedUser = clientForRestrictedUser.GetDatabase(EncryptionTests.databaseCore.Id); Database databaseForRestrictedUser = clientForRestrictedUser.GetDatabase(EncryptionTests.database.Id);
Container containerForRestrictedUser = databaseForRestrictedUser.GetContainer(EncryptionTests.itemContainer.Id); Container containerForRestrictedUser = databaseForRestrictedUser.GetContainer(EncryptionTests.itemContainer.Id);
await EncryptionTests.PerformForbiddenOperationAsync(() => await EncryptionTests.PerformForbiddenOperationAsync(() =>
@ -416,20 +360,20 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
[TestMethod] [TestMethod]
public async Task EncryptionResourceTokenAuthAllowed() public async Task EncryptionResourceTokenAuthAllowed()
{ {
User keyManagerUser = EncryptionTests.databaseCore.GetUser(Guid.NewGuid().ToString()); User keyManagerUser = EncryptionTests.database.GetUser(Guid.NewGuid().ToString());
await EncryptionTests.databaseCore.CreateUserAsync(keyManagerUser.Id); await EncryptionTests.database.CreateUserAsync(keyManagerUser.Id);
PermissionProperties keyManagerUserPermission = await keyManagerUser.CreatePermissionAsync( PermissionProperties keyManagerUserPermission = await keyManagerUser.CreatePermissionAsync(
new PermissionProperties(Guid.NewGuid().ToString(), PermissionMode.All, EncryptionTests.keyContainer)); new PermissionProperties(Guid.NewGuid().ToString(), PermissionMode.All, EncryptionTests.keyContainer));
CosmosDataEncryptionKeyProvider dekProvider = new CosmosDataEncryptionKeyProvider(new TestKeyWrapProvider()); CosmosDataEncryptionKeyProvider dekProvider = new CosmosDataEncryptionKeyProvider(new TestKeyWrapProvider());
TestEncryptor encryptor = new TestEncryptor(dekProvider); TestEncryptor encryptor = new TestEncryptor(dekProvider);
(string endpoint, string _) = TestCommon.GetAccountInfo(); CosmosClient clientForKeyManagerUser = TestCommon.CreateCosmosClient(
CosmosClient clientForKeyManagerUser = new CosmosClientBuilder(endpoint, keyManagerUserPermission.Token) keyManagerUserPermission.Token,
.WithEncryptor(encryptor) builder => builder
.Build(); .WithEncryptor(encryptor));
Database databaseForKeyManagerUser = clientForKeyManagerUser.GetDatabase(EncryptionTests.databaseCore.Id); Database databaseForKeyManagerUser = clientForKeyManagerUser.GetDatabase(EncryptionTests.database.Id);
await dekProvider.InitializeAsync(databaseForKeyManagerUser, EncryptionTests.keyContainer.Id); await dekProvider.InitializeAsync(databaseForKeyManagerUser, EncryptionTests.keyContainer.Id);
@ -444,7 +388,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
try try
{ {
await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, new List<string>() { "/id" }); await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, new List<string>() { "/id" });
Assert.Fail("Expected item creation with id specified to be encrypted to fail."); Assert.Fail("Expected item creation with id specified to be encrypted to fail.");
} }
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.BadRequest) catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.BadRequest)
@ -453,7 +397,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
try try
{ {
await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, new List<string>() { "/PK" }); await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, new List<string>() { "/PK" });
Assert.Fail("Expected item creation with PK specified to be encrypted to fail."); Assert.Fail("Expected item creation with PK specified to be encrypted to fail.");
} }
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.BadRequest) catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.BadRequest)
@ -464,31 +408,31 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
[TestMethod] [TestMethod]
public async Task EncryptionBulkCrud() public async Task EncryptionBulkCrud()
{ {
TestDoc docToReplace = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc docToReplace = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
docToReplace.NonSensitive = Guid.NewGuid().ToString(); docToReplace.NonSensitive = Guid.NewGuid().ToString();
docToReplace.Sensitive = Guid.NewGuid().ToString(); docToReplace.Sensitive = Guid.NewGuid().ToString();
TestDoc docToUpsert = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc docToUpsert = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
docToUpsert.NonSensitive = Guid.NewGuid().ToString(); docToUpsert.NonSensitive = Guid.NewGuid().ToString();
docToUpsert.Sensitive = Guid.NewGuid().ToString(); docToUpsert.Sensitive = Guid.NewGuid().ToString();
TestDoc docToDelete = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt); TestDoc docToDelete = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, EncryptionTests.dekId, TestDoc.PathsToEncrypt);
(string endpoint, string authKey) = TestCommon.GetAccountInfo(); CosmosClient clientWithBulk = TestCommon.CreateCosmosClient(builder => builder
CosmosClient clientWithBulk = new CosmosClientBuilder(endpoint, authKey) .WithEncryptor(EncryptionTests.encryptor)
.WithEncryptor(EncryptionTests.encryptor) .WithBulkExecution(true));
.WithBulkExecution(true)
.Build();
DatabaseInternal databaseWithBulk = (DatabaseInlineCore)clientWithBulk.GetDatabase(EncryptionTests.databaseCore.Id); Database databaseWithBulk = clientWithBulk.GetDatabase(EncryptionTests.database.Id);
ContainerInternal containerWithBulk = (ContainerInlineCore)databaseWithBulk.GetContainer(EncryptionTests.itemContainer.Id); Container containerWithBulk = databaseWithBulk.GetContainer(EncryptionTests.itemContainer.Id);
List<Task> tasks = new List<Task>(); List<Task> tasks = new List<Task>
tasks.Add(EncryptionTests.CreateItemAsync(containerWithBulk, EncryptionTests.dekId, TestDoc.PathsToEncrypt)); {
tasks.Add(EncryptionTests.UpsertItemAsync(containerWithBulk, TestDoc.Create(), EncryptionTests.dekId, TestDoc.PathsToEncrypt, HttpStatusCode.Created)); EncryptionTests.CreateItemAsync(containerWithBulk, EncryptionTests.dekId, TestDoc.PathsToEncrypt),
tasks.Add(EncryptionTests.ReplaceItemAsync(containerWithBulk, docToReplace, EncryptionTests.dekId, TestDoc.PathsToEncrypt)); EncryptionTests.UpsertItemAsync(containerWithBulk, TestDoc.Create(), EncryptionTests.dekId, TestDoc.PathsToEncrypt, HttpStatusCode.Created),
tasks.Add(EncryptionTests.UpsertItemAsync(containerWithBulk, docToUpsert, EncryptionTests.dekId, TestDoc.PathsToEncrypt, HttpStatusCode.OK)); EncryptionTests.ReplaceItemAsync(containerWithBulk, docToReplace, EncryptionTests.dekId, TestDoc.PathsToEncrypt),
tasks.Add(EncryptionTests.DeleteItemAsync(containerWithBulk, docToDelete)); EncryptionTests.UpsertItemAsync(containerWithBulk, docToUpsert, EncryptionTests.dekId, TestDoc.PathsToEncrypt, HttpStatusCode.OK),
EncryptionTests.DeleteItemAsync(containerWithBulk, docToDelete)
};
await Task.WhenAll(tasks); await Task.WhenAll(tasks);
} }
@ -504,51 +448,51 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
TestDoc doc2ToCreate = TestDoc.Create(partitionKey); TestDoc doc2ToCreate = TestDoc.Create(partitionKey);
TestDoc doc3ToCreate = TestDoc.Create(partitionKey); TestDoc doc3ToCreate = TestDoc.Create(partitionKey);
ItemResponse<TestDoc> doc1ToReplaceCreateResponse = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, dek1, TestDoc.PathsToEncrypt, partitionKey); ItemResponse<TestDoc> doc1ToReplaceCreateResponse = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, dek1, TestDoc.PathsToEncrypt, partitionKey);
TestDoc doc1ToReplace = doc1ToReplaceCreateResponse.Resource; TestDoc doc1ToReplace = doc1ToReplaceCreateResponse.Resource;
doc1ToReplace.NonSensitive = Guid.NewGuid().ToString(); doc1ToReplace.NonSensitive = Guid.NewGuid().ToString();
doc1ToReplace.Sensitive = Guid.NewGuid().ToString(); doc1ToReplace.Sensitive = Guid.NewGuid().ToString();
TestDoc doc2ToReplace = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, dek2, TestDoc.PathsToEncrypt, partitionKey); TestDoc doc2ToReplace = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, dek2, TestDoc.PathsToEncrypt, partitionKey);
doc2ToReplace.NonSensitive = Guid.NewGuid().ToString(); doc2ToReplace.NonSensitive = Guid.NewGuid().ToString();
doc2ToReplace.Sensitive = Guid.NewGuid().ToString(); doc2ToReplace.Sensitive = Guid.NewGuid().ToString();
TestDoc doc1ToUpsert = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, dek2, TestDoc.PathsToEncrypt, partitionKey); TestDoc doc1ToUpsert = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, dek2, TestDoc.PathsToEncrypt, partitionKey);
doc1ToUpsert.NonSensitive = Guid.NewGuid().ToString(); doc1ToUpsert.NonSensitive = Guid.NewGuid().ToString();
doc1ToUpsert.Sensitive = Guid.NewGuid().ToString(); doc1ToUpsert.Sensitive = Guid.NewGuid().ToString();
TestDoc doc2ToUpsert = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, dek1, TestDoc.PathsToEncrypt, partitionKey); TestDoc doc2ToUpsert = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, dek1, TestDoc.PathsToEncrypt, partitionKey);
doc2ToUpsert.NonSensitive = Guid.NewGuid().ToString(); doc2ToUpsert.NonSensitive = Guid.NewGuid().ToString();
doc2ToUpsert.Sensitive = Guid.NewGuid().ToString(); doc2ToUpsert.Sensitive = Guid.NewGuid().ToString();
TestDoc docToDelete = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainerCore, dek1, TestDoc.PathsToEncrypt, partitionKey); TestDoc docToDelete = await EncryptionTests.CreateItemAsync(EncryptionTests.itemContainer, dek1, TestDoc.PathsToEncrypt, partitionKey);
TransactionalBatchResponse batchResponse = await EncryptionTests.itemContainer.CreateTransactionalBatch(new Cosmos.PartitionKey(partitionKey)) TransactionalBatchResponse batchResponse = await EncryptionTests.itemContainer.CreateTransactionalBatch(new Cosmos.PartitionKey(partitionKey))
.CreateItem(doc1ToCreate, EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainerCore, dek1, TestDoc.PathsToEncrypt)) .CreateItem(doc1ToCreate, EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainer, dek1, TestDoc.PathsToEncrypt))
.CreateItemStream(doc2ToCreate.ToStream(), EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainerCore, dek2, TestDoc.PathsToEncrypt)) .CreateItemStream(doc2ToCreate.ToStream(), EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainer, dek2, TestDoc.PathsToEncrypt))
.ReplaceItem(doc1ToReplace.Id, doc1ToReplace, EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainerCore, dek2, TestDoc.PathsToEncrypt, doc1ToReplaceCreateResponse.ETag)) .ReplaceItem(doc1ToReplace.Id, doc1ToReplace, EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainer, dek2, TestDoc.PathsToEncrypt, doc1ToReplaceCreateResponse.ETag))
.CreateItem(doc3ToCreate) .CreateItem(doc3ToCreate)
.ReplaceItemStream(doc2ToReplace.Id, doc2ToReplace.ToStream(), EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainerCore, dek2, TestDoc.PathsToEncrypt)) .ReplaceItemStream(doc2ToReplace.Id, doc2ToReplace.ToStream(), EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainer, dek2, TestDoc.PathsToEncrypt))
.UpsertItem(doc1ToUpsert, EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainerCore, dek1, TestDoc.PathsToEncrypt)) .UpsertItem(doc1ToUpsert, EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainer, dek1, TestDoc.PathsToEncrypt))
.DeleteItem(docToDelete.Id) .DeleteItem(docToDelete.Id)
.UpsertItemStream(doc2ToUpsert.ToStream(), EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainerCore, dek2, TestDoc.PathsToEncrypt)) .UpsertItemStream(doc2ToUpsert.ToStream(), EncryptionTests.GetBatchItemRequestOptions(EncryptionTests.itemContainer, dek2, TestDoc.PathsToEncrypt))
.ExecuteAsync(); .ExecuteAsync();
Assert.AreEqual(HttpStatusCode.OK, batchResponse.StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponse.StatusCode);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, doc1ToCreate); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, doc1ToCreate);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, doc2ToCreate); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, doc2ToCreate);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, doc3ToCreate); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, doc3ToCreate);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, doc1ToReplace); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, doc1ToReplace);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, doc2ToReplace); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, doc2ToReplace);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, doc1ToUpsert); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, doc1ToUpsert);
await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainerCore, doc2ToUpsert); await EncryptionTests.VerifyItemByReadAsync(EncryptionTests.itemContainer, doc2ToUpsert);
ResponseMessage readResponseMessage = await EncryptionTests.itemContainer.ReadItemStreamAsync(docToDelete.Id, new PartitionKey(docToDelete.PK)); ResponseMessage readResponseMessage = await EncryptionTests.itemContainer.ReadItemStreamAsync(docToDelete.Id, new PartitionKey(docToDelete.PK));
Assert.AreEqual(HttpStatusCode.NotFound, readResponseMessage.StatusCode); Assert.AreEqual(HttpStatusCode.NotFound, readResponseMessage.StatusCode);
} }
private static async Task ValidateSprocResultsAsync(ContainerInternal containerCore, TestDoc expectedDoc) private static async Task ValidateSprocResultsAsync(Container container, TestDoc expectedDoc)
{ {
string sprocId = Guid.NewGuid().ToString(); string sprocId = Guid.NewGuid().ToString();
string sprocBody = @"function(docId) { string sprocBody = @"function(docId) {
@ -564,10 +508,10 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
}"; }";
StoredProcedureResponse storedProcedureResponse = StoredProcedureResponse storedProcedureResponse =
await containerCore.Scripts.CreateStoredProcedureAsync(new StoredProcedureProperties(sprocId, sprocBody)); await container.Scripts.CreateStoredProcedureAsync(new StoredProcedureProperties(sprocId, sprocBody));
Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode); Assert.AreEqual(HttpStatusCode.Created, storedProcedureResponse.StatusCode);
StoredProcedureExecuteResponse<TestDoc> sprocResponse = await containerCore.Scripts.ExecuteStoredProcedureAsync<TestDoc>( StoredProcedureExecuteResponse<TestDoc> sprocResponse = await container.Scripts.ExecuteStoredProcedureAsync<TestDoc>(
sprocId, sprocId,
new PartitionKey(expectedDoc.PK), new PartitionKey(expectedDoc.PK),
parameters: new dynamic[] { expectedDoc.Id }); parameters: new dynamic[] { expectedDoc.Id });
@ -577,7 +521,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
// One of query or queryDefinition is to be passed in non-null // One of query or queryDefinition is to be passed in non-null
private static async Task ValidateQueryResultsAsync( private static async Task ValidateQueryResultsAsync(
ContainerInternal containerCore, Container container,
string query = null, string query = null,
TestDoc expectedDoc = null, TestDoc expectedDoc = null,
QueryDefinition queryDefinition = null) QueryDefinition queryDefinition = null)
@ -586,11 +530,11 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
FeedIterator<TestDoc> queryResponseIterator; FeedIterator<TestDoc> queryResponseIterator;
if (query != null) if (query != null)
{ {
queryResponseIterator = containerCore.GetItemQueryIterator<TestDoc>(query, requestOptions: requestOptions); queryResponseIterator = container.GetItemQueryIterator<TestDoc>(query, requestOptions: requestOptions);
} }
else else
{ {
queryResponseIterator = containerCore.GetItemQueryIterator<TestDoc>(queryDefinition, requestOptions: requestOptions); queryResponseIterator = container.GetItemQueryIterator<TestDoc>(queryDefinition, requestOptions: requestOptions);
} }
FeedResponse<TestDoc> readDocs = await queryResponseIterator.ReadNextAsync(); FeedResponse<TestDoc> readDocs = await queryResponseIterator.ReadNextAsync();
@ -609,12 +553,12 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
} }
private static async Task ValidateQueryResultsMultipleDocumentsAsync( private static async Task ValidateQueryResultsMultipleDocumentsAsync(
ContainerInternal containerCore, Container container,
TestDoc testDoc1, TestDoc testDoc1,
TestDoc testDoc2, TestDoc testDoc2,
string query) string query)
{ {
FeedIterator<TestDoc> queryResponseIterator = containerCore.GetItemQueryIterator<TestDoc>(query); FeedIterator<TestDoc> queryResponseIterator = container.GetItemQueryIterator<TestDoc>(query);
FeedResponse<TestDoc> readDocs = await queryResponseIterator.ReadNextAsync(); FeedResponse<TestDoc> readDocs = await queryResponseIterator.ReadNextAsync();
Assert.AreEqual(null, readDocs.ContinuationToken); Assert.AreEqual(null, readDocs.ContinuationToken);
Assert.AreEqual(2, readDocs.Count); Assert.AreEqual(2, readDocs.Count);
@ -624,9 +568,9 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
} }
} }
private static async Task ValidateQueryResponseAsync(ContainerInternal containerCore, string query) private static async Task ValidateQueryResponseAsync(Container container, string query)
{ {
FeedIterator feedIterator = containerCore.GetItemQueryStreamIterator(query); FeedIterator feedIterator = container.GetItemQueryStreamIterator(query);
while (feedIterator.HasMoreResults) while (feedIterator.HasMoreResults)
{ {
ResponseMessage response = await feedIterator.ReadNextAsync(); ResponseMessage response = await feedIterator.ReadNextAsync();
@ -637,10 +581,8 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
private static CosmosClient GetClient() private static CosmosClient GetClient()
{ {
(string endpoint, string authKey) = TestCommon.GetAccountInfo(); return TestCommon.CreateCosmosClient(
return new CosmosClientBuilder(endpoint, authKey) builder => builder.WithEncryptor(encryptor));
.WithEncryptor(encryptor)
.Build();
} }
private static async Task IterateDekFeedAsync( private static async Task IterateDekFeedAsync(
@ -712,49 +654,49 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
} }
private static async Task<ItemResponse<TestDoc>> UpsertItemAsync( private static async Task<ItemResponse<TestDoc>> UpsertItemAsync(
ContainerInternal containerCore, Container container,
TestDoc testDoc, TestDoc testDoc,
string dekId, string dekId,
List<string> pathsToEncrypt, List<string> pathsToEncrypt,
HttpStatusCode expectedStatusCode) HttpStatusCode expectedStatusCode)
{ {
ItemResponse<TestDoc> upsertResponse = await containerCore.UpsertItemAsync( ItemResponse<TestDoc> upsertResponse = await container.UpsertItemAsync(
testDoc, testDoc,
new PartitionKey(testDoc.PK), new PartitionKey(testDoc.PK),
EncryptionTests.GetRequestOptions(containerCore, dekId, pathsToEncrypt)); EncryptionTests.GetRequestOptions(container, dekId, pathsToEncrypt));
Assert.AreEqual(expectedStatusCode, upsertResponse.StatusCode); Assert.AreEqual(expectedStatusCode, upsertResponse.StatusCode);
Assert.AreEqual(testDoc, upsertResponse.Resource); Assert.AreEqual(testDoc, upsertResponse.Resource);
return upsertResponse; return upsertResponse;
} }
private static async Task<ItemResponse<TestDoc>> CreateItemAsync( private static async Task<ItemResponse<TestDoc>> CreateItemAsync(
ContainerInternal containerCore, Container container,
string dekId, string dekId,
List<string> pathsToEncrypt, List<string> pathsToEncrypt,
string partitionKey = null) string partitionKey = null)
{ {
TestDoc testDoc = TestDoc.Create(partitionKey); TestDoc testDoc = TestDoc.Create(partitionKey);
ItemResponse<TestDoc> createResponse = await containerCore.CreateItemAsync( ItemResponse<TestDoc> createResponse = await container.CreateItemAsync(
testDoc, testDoc,
new PartitionKey(testDoc.PK), new PartitionKey(testDoc.PK),
EncryptionTests.GetRequestOptions(containerCore, dekId, pathsToEncrypt)); EncryptionTests.GetRequestOptions(container, dekId, pathsToEncrypt));
Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode); Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode);
Assert.AreEqual(testDoc, createResponse.Resource); Assert.AreEqual(testDoc, createResponse.Resource);
return createResponse; return createResponse;
} }
private static async Task<ItemResponse<TestDoc>> ReplaceItemAsync( private static async Task<ItemResponse<TestDoc>> ReplaceItemAsync(
ContainerInternal containerCore, Container container,
TestDoc testDoc, TestDoc testDoc,
string dekId, string dekId,
List<string> pathsToEncrypt, List<string> pathsToEncrypt,
string etag = null) string etag = null)
{ {
ItemResponse<TestDoc> replaceResponse = await containerCore.ReplaceItemAsync( ItemResponse<TestDoc> replaceResponse = await container.ReplaceItemAsync(
testDoc, testDoc,
testDoc.Id, testDoc.Id,
new PartitionKey(testDoc.PK), new PartitionKey(testDoc.PK),
EncryptionTests.GetRequestOptions(containerCore, dekId, pathsToEncrypt, etag)); EncryptionTests.GetRequestOptions(container, dekId, pathsToEncrypt, etag));
Assert.AreEqual(HttpStatusCode.OK, replaceResponse.StatusCode); Assert.AreEqual(HttpStatusCode.OK, replaceResponse.StatusCode);
Assert.AreEqual(testDoc, replaceResponse.Resource); Assert.AreEqual(testDoc, replaceResponse.Resource);
@ -762,10 +704,10 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
} }
private static async Task<ItemResponse<TestDoc>> DeleteItemAsync( private static async Task<ItemResponse<TestDoc>> DeleteItemAsync(
ContainerInternal containerCore, Container container,
TestDoc testDoc) TestDoc testDoc)
{ {
ItemResponse<TestDoc> deleteResponse = await containerCore.DeleteItemAsync<TestDoc>( ItemResponse<TestDoc> deleteResponse = await container.DeleteItemAsync<TestDoc>(
testDoc.Id, testDoc.Id,
new PartitionKey(testDoc.PK)); new PartitionKey(testDoc.PK));
@ -775,7 +717,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
} }
private static ItemRequestOptions GetRequestOptions( private static ItemRequestOptions GetRequestOptions(
ContainerInternal containerCore, Container container,
string dekId, string dekId,
List<string> pathsToEncrypt, List<string> pathsToEncrypt,
string ifMatchEtag = null) string ifMatchEtag = null)
@ -793,7 +735,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
} }
private static TransactionalBatchItemRequestOptions GetBatchItemRequestOptions( private static TransactionalBatchItemRequestOptions GetBatchItemRequestOptions(
ContainerInternal containerCore, Container container,
string dekId, string dekId,
List<string> pathsToEncrypt, List<string> pathsToEncrypt,
string ifMatchEtag = null) string ifMatchEtag = null)
@ -815,7 +757,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
ResponseMessage readResponseMessage = await container.ReadItemStreamAsync(testDoc.Id, new PartitionKey(testDoc.PK)); ResponseMessage readResponseMessage = await container.ReadItemStreamAsync(testDoc.Id, new PartitionKey(testDoc.PK));
Assert.AreEqual(HttpStatusCode.OK, readResponseMessage.StatusCode); Assert.AreEqual(HttpStatusCode.OK, readResponseMessage.StatusCode);
Assert.IsNotNull(readResponseMessage.Content); Assert.IsNotNull(readResponseMessage.Content);
TestDoc readDoc = TestCommon.SerializerCore.FromStream<TestDoc>(readResponseMessage.Content); TestDoc readDoc = TestCommon.FromStream<TestDoc>(readResponseMessage.Content);
Assert.AreEqual(testDoc, readDoc); Assert.AreEqual(testDoc, readDoc);
} }
@ -925,7 +867,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
public Stream ToStream() public Stream ToStream()
{ {
return TestCommon.SerializerCore.ToStream(this); return TestCommon.ToStream(this);
} }
} }
@ -990,4 +932,4 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
} }
} }
} }
} }

Просмотреть файл

@ -0,0 +1,54 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsTestProject>true</IsTestProject>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Platform>AnyCPU</Platform>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RootNamespace>Microsoft.Azure.Cosmos</RootNamespace>
<AssemblyName>Microsoft.Azure.Cosmos.Encryption.EmulatorTests</AssemblyName>
<IsEmulatorTest>true</IsEmulatorTest>
<EmulatorFlavor>master</EmulatorFlavor>
<DisableCopyEmulator>True</DisableCopyEmulator>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
<PackageReference Include="System.Security.SecureString" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.8.11" />
<PackageReference Include="System.Reflection" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\Microsoft.Azure.Cosmos\tests\Microsoft.Azure.Cosmos.EmulatorTests\Utils\ConfigurationManager.cs" Link="Utils\ConfigurationManager.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\Microsoft.Azure.Cosmos\tests\Microsoft.Azure.Cosmos.EmulatorTests\settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Microsoft.Azure.Cosmos.Encryption\src\Microsoft.Azure.Cosmos.Encryption.csproj" />
</ItemGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
<DelaySign>true</DelaySign>
<AssemblyOriginatorKeyFile>..\..\..\testkey.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>

Просмотреть файл

@ -0,0 +1,88 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Encryption.EmulatorTests
{
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Cosmos.Utils;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Text;
internal static class TestCommon
{
internal static Stream ToStream<T>(T input)
{
string s = JsonConvert.SerializeObject(input);
return new MemoryStream(Encoding.UTF8.GetBytes(s));
}
internal static T FromStream<T>(Stream stream)
{
using (StreamReader sr = new StreamReader(stream))
using (JsonReader reader = new JsonTextReader(sr))
{
JsonSerializer serializer = new JsonSerializer();
return serializer.Deserialize<T>(reader);
}
}
internal static MemoryStream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
private static (string endpoint, string authKey) GetAccountInfo()
{
string authKey = ConfigurationManager.AppSettings["MasterKey"];
string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"];
return (endpoint, authKey);
}
internal static CosmosClientBuilder GetClientBuilder(string resourceToken)
{
(string endpoint, string authKey) accountInfo = TestCommon.GetAccountInfo();
CosmosClientBuilder clientBuilder = new CosmosClientBuilder(
accountEndpoint: accountInfo.endpoint,
authKeyOrResourceToken: resourceToken ?? accountInfo.authKey);
return clientBuilder;
}
internal static CosmosClient CreateCosmosClient(Action<CosmosClientBuilder> customizeClientBuilder = null)
{
CosmosClientBuilder cosmosClientBuilder = GetClientBuilder(resourceToken: null);
customizeClientBuilder?.Invoke(cosmosClientBuilder);
return cosmosClientBuilder.Build();
}
internal static CosmosClient CreateCosmosClient(string resourceToken, Action<CosmosClientBuilder> customizeClientBuilder = null)
{
CosmosClientBuilder cosmosClientBuilder = GetClientBuilder(resourceToken);
customizeClientBuilder?.Invoke(cosmosClientBuilder);
return cosmosClientBuilder.Build();
}
internal static CosmosClient CreateCosmosClient(
bool useGateway)
{
CosmosClientBuilder cosmosClientBuilder = GetClientBuilder(resourceToken: null);
if (useGateway)
{
cosmosClientBuilder.WithConnectionModeGateway();
}
return cosmosClientBuilder.Build();
}
}
}

Просмотреть файл

@ -13,6 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Cosmos.Perf
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Cosmos.Encryption", "Microsoft.Azure.Cosmos.Encryption\src\Microsoft.Azure.Cosmos.Encryption.csproj", "{4D0FBC91-268E-44DD-AC10-6851A69C52FB}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Cosmos.Encryption", "Microsoft.Azure.Cosmos.Encryption\src\Microsoft.Azure.Cosmos.Encryption.csproj", "{4D0FBC91-268E-44DD-AC10-6851A69C52FB}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Encryption", "Encryption", "{B382B965-7D1B-4DE8-8007-197741C49F88}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.Cosmos.Encryption.EmulatorTests", "Microsoft.Azure.Cosmos.Encryption\tests\EmulatorTests\Microsoft.Azure.Cosmos.Encryption.EmulatorTests.csproj", "{D6A405BF-15E1-46D3-8C13-210BA043ADC5}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Cover|Any CPU = Cover|Any CPU Cover|Any CPU = Cover|Any CPU
@ -83,10 +87,26 @@ Global
{4D0FBC91-268E-44DD-AC10-6851A69C52FB}.Release|Any CPU.Build.0 = Release|Any CPU {4D0FBC91-268E-44DD-AC10-6851A69C52FB}.Release|Any CPU.Build.0 = Release|Any CPU
{4D0FBC91-268E-44DD-AC10-6851A69C52FB}.Release|x64.ActiveCfg = Release|Any CPU {4D0FBC91-268E-44DD-AC10-6851A69C52FB}.Release|x64.ActiveCfg = Release|Any CPU
{4D0FBC91-268E-44DD-AC10-6851A69C52FB}.Release|x64.Build.0 = Release|Any CPU {4D0FBC91-268E-44DD-AC10-6851A69C52FB}.Release|x64.Build.0 = Release|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Cover|Any CPU.ActiveCfg = Debug|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Cover|Any CPU.Build.0 = Debug|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Cover|x64.ActiveCfg = Debug|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Cover|x64.Build.0 = Debug|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Debug|x64.ActiveCfg = Debug|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Debug|x64.Build.0 = Debug|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Release|Any CPU.Build.0 = Release|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Release|x64.ActiveCfg = Release|Any CPU
{D6A405BF-15E1-46D3-8C13-210BA043ADC5}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4D0FBC91-268E-44DD-AC10-6851A69C52FB} = {B382B965-7D1B-4DE8-8007-197741C49F88}
{D6A405BF-15E1-46D3-8C13-210BA043ADC5} = {B382B965-7D1B-4DE8-8007-197741C49F88}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C6A1D820-CB03-4DE6-87D1-46EF476F0040} SolutionGuid = {C6A1D820-CB03-4DE6-87D1-46EF476F0040}
EndGlobalSection EndGlobalSection

Просмотреть файл

@ -222,7 +222,6 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\Microsoft.Azure.Cosmos.Encryption\src\Microsoft.Azure.Cosmos.Encryption.csproj" />
<ProjectReference Include="..\..\src\Microsoft.Azure.Cosmos.csproj" /> <ProjectReference Include="..\..\src\Microsoft.Azure.Cosmos.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

Просмотреть файл

@ -14,18 +14,18 @@ namespace Microsoft.Azure.Cosmos.Utils
/// </summary> /// </summary>
internal static class ConfigurationManager internal static class ConfigurationManager
{ {
const string GatewayEndpointSettingName = "GatewayEndpoint"; private const string GatewayEndpointSettingName = "GatewayEndpoint";
const string GatewayEndpointEnvironmentName = "COSMOSDBEMULATOR_ENDPOINT"; private const string GatewayEndpointEnvironmentName = "COSMOSDBEMULATOR_ENDPOINT";
static ConfigurationManager() static ConfigurationManager()
{ {
AppSettings = new Dictionary<string, string>(); AppSettings = new Dictionary<string, string>();
var builder = new ConfigurationBuilder() IConfigurationBuilder builder = new ConfigurationBuilder()
.AddJsonFile($"settings.json", true, true); .AddJsonFile($"settings.json", true, true);
var configuration = builder.Build(); IConfigurationRoot configuration = builder.Build();
foreach(var configurationSetting in configuration.GetSection("AppSettings").GetChildren()) foreach (IConfigurationSection configurationSetting in configuration.GetSection("AppSettings").GetChildren())
{ {
string configurationValue = configurationSetting.Value; string configurationValue = configurationSetting.Value;
if (string.Equals(GatewayEndpointSettingName, configurationSetting.Key)) if (string.Equals(GatewayEndpointSettingName, configurationSetting.Key))

Просмотреть файл

@ -70,3 +70,27 @@ jobs:
nugetConfigPath: NuGet.config nugetConfigPath: NuGet.config
publishTestResults: true publishTestResults: true
testRunTitle: Microsoft.Azure.Cosmos.EmulatorTests testRunTitle: Microsoft.Azure.Cosmos.EmulatorTests
- job:
displayName: Encryption EmulatorTests ${{ parameters.BuildConfiguration }}
timeoutInMinutes: 90
condition: and(succeeded(), eq('${{ parameters.OS }}', 'Windows'))
pool:
vmImage: ${{ parameters.VmImage }}
steps:
- checkout: self # self represents the repo where the initial Pipelines YAML file was found
clean: true # if true, execute `execute git clean -ffdx && git reset --hard HEAD` before fetching
- template: emulator-setup.yml
- task: DotNetCoreCLI@2
displayName: Microsoft.Azure.Cosmos.Encryption.EmulatorTests
condition: succeeded()
inputs:
command: test
projects: 'Microsoft.Azure.Cosmos.Encryption/tests/EmulatorTests/*.csproj'
arguments: ${{ parameters.Arguments }} --configuration ${{ parameters.BuildConfiguration }} /p:OS=${{ parameters.OS }}
nugetConfigPath: NuGet.config
publishTestResults: true
testRunTitle: Microsoft.Azure.Cosmos.Encryption.EmulatorTests