Skip to content

Commit

Permalink
Client Encryption : Adds overload with QueryDefinition for GetDataEnc…
Browse files Browse the repository at this point in the history
…ryptionKeyQueryIterator (#1836)

Add overload with QueryDefinition for GetDataEncryptionKeyQueryIterator() so user doesn't have to necessarily use plaintext query parameters.
  • Loading branch information
anujtoshniwal committed Sep 16, 2020
1 parent 14e11a4 commit 85af166
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,13 @@ public abstract Task<ItemResponse<DataEncryptionKeyProperties>> RewrapDataEncryp
/// <summary>
/// Returns an iterator that can be iterated to get properties of data encryption keys.
/// </summary>
/// <param name="startId">(Optional) Starting value of the range (inclusive) of ids of data encryption keys for which properties needs to be returned.</param>
/// <param name="endId">(Optional) Ending value of the range (inclusive) of ids of data encryption keys for which properties needs to be returned.</param>
/// <param name="isDescending">Whether the results should be returned sorted in descending order of id.</param>
/// <param name="queryText">The cosmos SQL query text.</param>
/// <param name="continuationToken">(Optional) The continuation token in the Azure Cosmos DB service.</param>
/// <param name="requestOptions">(Optional) The options for the request. Set <see cref="QueryRequestOptions.MaxItemCount"/> to restrict the number of results returned.</param>
/// <typeparam name="T">The type of object to query.</typeparam>
/// <returns>An iterator over data encryption keys.</returns>
/// <example>
/// This create the type feed iterator for containers with queryDefinition as input.
/// This creates the type feed iterator for containers with query text as input.
/// <code language="c#">
/// <![CDATA[
/// FeedIterator<DataEncryptionKeyProperties> resultSet = this.cosmosDatabase.GetDataEncryptionKeyQueryIterator();
Expand All @@ -138,6 +137,41 @@ public abstract FeedIterator<T> GetDataEncryptionKeyQueryIterator<T>(
string continuationToken = null,
QueryRequestOptions requestOptions = null);

/// <summary>
/// Returns an iterator that can be iterated to get properties of data encryption keys.
/// </summary>
/// <param name="queryDefinition">The Cosmos SQL query definition.</param>
/// <param name="continuationToken">(Optional) The continuation token in the Azure Cosmos DB service.</param>
/// <param name="requestOptions">(Optional) The options for the request. Set <see cref="QueryRequestOptions.MaxItemCount"/> to restrict the number of results returned.</param>
/// <typeparam name="T">The type of object to query.</typeparam>
/// <returns>An iterator over data encryption keys.</returns>
/// <example>
/// This creates the type feed iterator for containers with queryDefinition as input.
/// The example is to get all the DataEncryptionKeyProperties that have id in the range ["DEK_005", "DEK_015"].
/// <code language="c#">
/// <![CDATA[
/// QueryDefinition queryDefinition = new QueryDefinition("SELECT * from c where c.id >= @startId and c.id <= @endId")
/// .WithParameter("@startId", "DEK_005")
/// .WithParameter("@endId", "DEK_015");
/// FeedIterator<DataEncryptionKeyProperties> resultSet = this.cosmosDatabase.GetDataEncryptionKeyQueryIterator(queryDefinition);
/// while (feedIterator.HasMoreResults)
/// {
/// foreach (DataEncryptionKeyProperties properties in await feedIterator.ReadNextAsync())
/// {
/// Console.WriteLine(properties.Id);
/// }
/// }
/// ]]>
/// </code>
/// </example>
/// <remarks>
/// <see cref="DataEncryptionKey.ReadDataEncryptionKeyAsync" /> is recommended for single data encryption key look-up.
/// </remarks>
public abstract FeedIterator<T> GetDataEncryptionKeyQueryIterator<T>(
QueryDefinition queryDefinition,
string continuationToken = null,
QueryRequestOptions requestOptions = null);

/// <summary>
/// Reads the properties of a data encryption key from the Azure Cosmos service as an asynchronous operation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public override FeedIterator<T> GetDataEncryptionKeyQueryIterator<T>(
return this.DekProvider.Container.GetItemQueryIterator<T>(queryText, continuationToken, requestOptions);
}

public override FeedIterator<T> GetDataEncryptionKeyQueryIterator<T>(
QueryDefinition queryDefinition,
string continuationToken = null,
QueryRequestOptions requestOptions = null)
{
return this.DekProvider.Container.GetItemQueryIterator<T>(queryDefinition, continuationToken, requestOptions);
}

public override async Task<ItemResponse<DataEncryptionKeyProperties>> CreateDataEncryptionKeyAsync(
string id,
string encryptionAlgorithm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public override FeedIterator<T> GetDataEncryptionKeyQueryIterator<T>(
requestOptions);
}

public override FeedIterator<T> GetDataEncryptionKeyQueryIterator<T>(
QueryDefinition queryDefinition,
string continuationToken = null,
QueryRequestOptions requestOptions = null)
{
return this.dataEncryptionKeyContainerCore.GetDataEncryptionKeyQueryIterator<T>(
queryDefinition,
continuationToken,
requestOptions);
}

public override Task<ItemResponse<DataEncryptionKeyProperties>> CreateDataEncryptionKeyAsync(
string id,
string encryptionAlgorithm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,16 @@ await EncryptionTests.IterateDekFeedAsync(
"SELECT TOP 1 * from c where c.id >= 'Contoso_v000' and c.id <= 'Contoso_v999' ORDER BY c.id DESC");

// Ensure only required results are returned
QueryDefinition queryDefinition = new QueryDefinition("SELECT * from c where c.id >= @startId and c.id <= @endId ORDER BY c.id ASC")
.WithParameter("@startId", "Contoso_v000")
.WithParameter("@endId", "Contoso_v999");
await EncryptionTests.IterateDekFeedAsync(
dekProvider,
new List<string> { contosoV1, contosoV2 },
isExpectedDeksCompleteSetForRequest: true,
isResultOrderExpected: true,
"SELECT * from c where c.id >= 'Contoso_v000' and c.id <= 'Contoso_v999' ORDER BY c.id ASC");
query: null,
queryDefinition: queryDefinition);

// Test pagination
await EncryptionTests.IterateDekFeedAsync(
Expand Down Expand Up @@ -1231,7 +1235,8 @@ private static async Task IterateDekFeedAsync(
bool isExpectedDeksCompleteSetForRequest,
bool isResultOrderExpected,
string query,
int? itemCountInPage = null)
int? itemCountInPage = null,
QueryDefinition queryDefinition = null)
{
int remainingItemCount = expectedDekIds.Count;
QueryRequestOptions requestOptions = null;
Expand All @@ -1243,10 +1248,20 @@ private static async Task IterateDekFeedAsync(
};
}

FeedIterator<DataEncryptionKeyProperties> dekIterator = dekProvider.DataEncryptionKeyContainer
.GetDataEncryptionKeyQueryIterator<DataEncryptionKeyProperties>(
FeedIterator<DataEncryptionKeyProperties> dekIterator;

if (queryDefinition != null)
{
dekIterator = dekProvider.DataEncryptionKeyContainer.GetDataEncryptionKeyQueryIterator<DataEncryptionKeyProperties>(
queryDefinition,
requestOptions: requestOptions);
}
else
{
dekIterator = dekProvider.DataEncryptionKeyContainer.GetDataEncryptionKeyQueryIterator<DataEncryptionKeyProperties>(
query,
requestOptions: requestOptions);
}

Assert.IsTrue(dekIterator.HasMoreResults);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@
"DataEncryptionKeyContainer": {
"Subclasses": {},
"Members": {
"Microsoft.Azure.Cosmos.FeedIterator`1[T] GetDataEncryptionKeyQueryIterator[T](Microsoft.Azure.Cosmos.QueryDefinition, System.String, Microsoft.Azure.Cosmos.QueryRequestOptions)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.FeedIterator`1[T] GetDataEncryptionKeyQueryIterator[T](Microsoft.Azure.Cosmos.QueryDefinition, System.String, Microsoft.Azure.Cosmos.QueryRequestOptions)"
},
"Microsoft.Azure.Cosmos.FeedIterator`1[T] GetDataEncryptionKeyQueryIterator[T](System.String, System.String, Microsoft.Azure.Cosmos.QueryRequestOptions)": {
"Type": "Method",
"Attributes": [],
Expand Down

0 comments on commit 85af166

Please sign in to comment.