Skip to content

Commit

Permalink
ResourceToken support (#622)
Browse files Browse the repository at this point in the history
* start implementing User

* implement user CRUD with tests

* finish implementing users

* start implementing permissions

* add factory methods to PermissionProperties for each resource

* implement permission create and query iterators

* finish implmenting permission CRUD

* implement permission CRUD tests

* add permission iterator tests

* assert permission token after create

* Add contaier resource permission test

* fix bug where it was not using the client with resource token

* add permission resource tests

* update baseline

* address PR comments

* move user and permission test to CosmosBasicQueryTests

* address PR comments

* fix typos

* remove factory methods for constructor overloads. remove permissions for scripts.

* remove PermissionRequestOptions. promote ResourceTokenExpirySeconds to Read, Replace and Create permission.

* add ResourceTokenExpirySecondsHeaderIsAdded test

* add upsert operation for user and permissions and update baseline

* address PR comments

* address PR comments and update baseline

* typo

* one arg per line

* fix errors from pervious master pull

* change accountKey to authKeyOrResourceToken

* fix tests

* fix test

* fix typos
  • Loading branch information
ausfeldt authored and kirankumarkolli committed Aug 21, 2019
1 parent e2a66b7 commit 620b709
Show file tree
Hide file tree
Showing 33 changed files with 2,907 additions and 60 deletions.
24 changes: 12 additions & 12 deletions Microsoft.Azure.Cosmos/src/CosmosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace Microsoft.Azure.Cosmos
/// </example>
/// <example>
/// This example create a <see cref="CosmosClient"/>, <see cref="Database"/>, and a <see cref="Container"/>.
/// The CosmosClient is created with the AccountEndpoint, AccountKey and configured to use "East US 2" region.
/// The CosmosClient is created with the AccountEndpoint, AccountKey or ResourceToken and configured to use "East US 2" region.
/// <code language="c#">
/// <![CDATA[
/// using Microsoft.Azure.Cosmos;
Expand Down Expand Up @@ -167,10 +167,10 @@ public CosmosClient(
/// performance guide at <see href="https://docs.microsoft.com/azure/cosmos-db/performance-tips"/>.
/// </summary>
/// <param name="accountEndpoint">The cosmos service endpoint to use</param>
/// <param name="accountKey">The cosmos account key to use to create the client.</param>
/// <param name="authKeyOrResourceToken">The cosmos account key or resource token to use to create the client.</param>
/// <param name="clientOptions">(Optional) client options</param>
/// <example>
/// The CosmosClient is created with the AccountEndpoint, AccountKey and configured to use "East US 2" region.
/// The CosmosClient is created with the AccountEndpoint, AccountKey or ResourceToken and configured to use "East US 2" region.
/// <code language="c#">
/// <![CDATA[
/// using Microsoft.Azure.Cosmos;
Expand All @@ -195,17 +195,17 @@ public CosmosClient(
/// </remarks>
public CosmosClient(
string accountEndpoint,
string accountKey,
string authKeyOrResourceToken,
CosmosClientOptions clientOptions = null)
{
if (accountEndpoint == null)
{
throw new ArgumentNullException(nameof(accountEndpoint));
}

if (accountKey == null)
if (authKeyOrResourceToken == null)
{
throw new ArgumentNullException(nameof(accountKey));
throw new ArgumentNullException(nameof(authKeyOrResourceToken));
}

if (clientOptions == null)
Expand All @@ -214,7 +214,7 @@ public CosmosClient(
}

this.Endpoint = new Uri(accountEndpoint);
this.AccountKey = accountKey;
this.AccountKey = authKeyOrResourceToken;
CosmosClientOptions clientOptionsClone = clientOptions.Clone();

DocumentClient documentClient = new DocumentClient(
Expand All @@ -238,7 +238,7 @@ public CosmosClient(
/// </summary>
internal CosmosClient(
string accountEndpoint,
string accountKey,
string authKeyOrResourceToken,
CosmosClientOptions cosmosClientOptions,
DocumentClient documentClient)
{
Expand All @@ -247,9 +247,9 @@ internal CosmosClient(
throw new ArgumentNullException(nameof(accountEndpoint));
}

if (accountKey == null)
if (authKeyOrResourceToken == null)
{
throw new ArgumentNullException(nameof(accountKey));
throw new ArgumentNullException(nameof(authKeyOrResourceToken));
}

if (cosmosClientOptions == null)
Expand All @@ -263,7 +263,7 @@ internal CosmosClient(
}

this.Endpoint = new Uri(accountEndpoint);
this.AccountKey = accountKey;
this.AccountKey = authKeyOrResourceToken;

this.Init(cosmosClientOptions, documentClient);
}
Expand All @@ -283,7 +283,7 @@ internal CosmosClient(
public virtual Uri Endpoint { get; }

/// <summary>
/// Gets the AuthKey used by the client from the Azure Cosmos DB service.
/// Gets the AuthKey or resource token used by the client from the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The AuthKey used by the client.
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ public DocumentClient(Uri serviceEndpoint,
/// <seealso cref="ConsistencyLevel"/>
public DocumentClient(
Uri serviceEndpoint,
IList<Permission> permissionFeed,
IList<Documents.Permission> permissionFeed,
ConnectionPolicy connectionPolicy = null,
Documents.ConsistencyLevel? desiredConsistencyLevel = null)
: this(serviceEndpoint,
Expand All @@ -543,7 +543,7 @@ public DocumentClient(
{
}

private static List<ResourceToken> GetResourceTokens(IList<Permission> permissionFeed)
private static List<ResourceToken> GetResourceTokens(IList<Documents.Permission> permissionFeed)
{
if (permissionFeed == null)
{
Expand Down
18 changes: 9 additions & 9 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class CosmosClientBuilder
/// Initialize a new CosmosConfiguration class that holds all the properties the CosmosClient requires.
/// </summary>
/// <param name="accountEndpoint">The Uri to the Cosmos Account. Example: https://{Cosmos Account Name}.documents.azure.com:443/ </param>
/// <param name="accountKey">The key to the account.</param>
/// <param name="authKeyOrResourceToken">The key to the account or resource token.</param>
/// <example>
/// The example below creates a new <see cref="CosmosClientBuilder"/>
/// <code language="c#">
/// <![CDATA[
/// CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(
/// accountEndpoint: "https://testcosmos.documents.azure.com:443/",
/// accountKey: "SuperSecretKey");
/// authKeyOrResourceToken: "SuperSecretKey");
/// CosmosClient client = cosmosClientBuilder.Build();
/// ]]>
/// </code>
Expand All @@ -41,7 +41,7 @@ public class CosmosClientBuilder
/// <![CDATA[
/// CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(
/// accountEndpoint: "https://testcosmos.documents.azure.com:443/",
/// accountKey: "SuperSecretKey")
/// authKeyOrResourceToken: "SuperSecretKey")
/// .WithConsistencyLevel(ConsistencyLevel.Strong)
/// .WithApplicationRegion("East US 2");
/// CosmosClient client = cosmosClientBuilder.Build();
Expand All @@ -50,27 +50,27 @@ public class CosmosClientBuilder
/// </example>
public CosmosClientBuilder(
string accountEndpoint,
string accountKey)
string authKeyOrResourceToken)
{
if (accountEndpoint == null)
{
throw new ArgumentNullException(nameof(CosmosClientBuilder.accountEndpoint));
}

if (accountKey == null)
if (authKeyOrResourceToken == null)
{
throw new ArgumentNullException(nameof(accountKey));
throw new ArgumentNullException(nameof(authKeyOrResourceToken));
}

this.accountEndpoint = accountEndpoint;
this.accountKey = accountKey;
this.accountKey = authKeyOrResourceToken;
}

/// <summary>
/// Extracts the account endpoint and key from the connection string.
/// </summary>
/// <example>"AccountEndpoint=https://mytestcosmosaccount.documents.azure.com:443/;AccountKey={SecretAccountKey};"</example>
/// <param name="connectionString">The connection string must contain AccountEndpoint and AccountKey.</param>
/// <param name="connectionString">The connection string must contain AccountEndpoint and AccountKey or ResourceToken.</param>
public CosmosClientBuilder(string connectionString)
{
if (connectionString == null)
Expand Down Expand Up @@ -131,7 +131,7 @@ public CosmosClientBuilder WithApplicationName(string applicationName)
/// <![CDATA[
/// CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(
/// accountEndpoint: "https://testcosmos.documents.azure.com:443/",
/// accountKey: "SuperSecretKey")
/// authKeyOrResourceToken: "SuperSecretKey")
/// .WithApplicationRegion("East US 2");
/// CosmosClient client = cosmosClientBuilder.Build();
/// ]]>
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Resource/Container/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public abstract Task<ResponseMessage> ReplaceContainerStreamAsync(
/// <example>
/// <code language="c#">
/// <![CDATA[
/// Container container = this.database.Containers["containerId"];
/// Container container = this.database.GetContainer("containerId");
/// ContainerResponse response = await container.DeleteContainerAsync();
/// ]]>
/// </code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Microsoft.Azure.Cosmos
{
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Routing;
Expand Down
30 changes: 30 additions & 0 deletions Microsoft.Azure.Cosmos/src/Resource/CosmosResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@ internal Task<ContainerResponse> CreateContainerResponseAsync(
});
}

internal Task<UserResponse> CreateUserResponseAsync(
User user,
Task<ResponseMessage> cosmosResponseMessageTask)
{
return this.ProcessMessageAsync(cosmosResponseMessageTask, (cosmosResponseMessage) =>
{
UserProperties userProperties = this.ToObjectInternal<UserProperties>(cosmosResponseMessage, this.propertiesSerializer);
return new UserResponse(
cosmosResponseMessage.StatusCode,
cosmosResponseMessage.Headers,
userProperties,
user);
});
}

internal Task<PermissionResponse> CreatePermissionResponseAsync(
Permission permission,
Task<ResponseMessage> cosmosResponseMessageTask)
{
return this.ProcessMessageAsync(cosmosResponseMessageTask, (cosmosResponseMessage) =>
{
PermissionProperties permissionProperties = this.ToObjectInternal<PermissionProperties>(cosmosResponseMessage, this.propertiesSerializer);
return new PermissionResponse(
cosmosResponseMessage.StatusCode,
cosmosResponseMessage.Headers,
permissionProperties,
permission);
});
}

internal Task<DatabaseResponse> CreateDatabaseResponseAsync(
Database database,
Task<ResponseMessage> cosmosResponseMessageTask)
Expand Down
Loading

0 comments on commit 620b709

Please sign in to comment.