Skip to content

Commit

Permalink
Authorization: Fixes DocumentClientException being thrown on write op…
Browse files Browse the repository at this point in the history
…erations (#1909)

When a wrong key is used for a Write operation, the client was throwing a DocumentClientException instead of a CosmosException
  • Loading branch information
ealsur authored Oct 6, 2020
1 parent 826f48c commit 67a2c58
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ public override async Task<IEnumerable<string>> GetPartitionKeyRangesAsync(
/// <returns>A <see cref="Task"/> containing the <see cref="ContainerProperties"/> for this container.</returns>
public override async Task<ContainerProperties> GetCachedContainerPropertiesAsync(CancellationToken cancellationToken = default)
{
ClientCollectionCache collectionCache = await this.ClientContext.DocumentClient.GetCollectionCacheAsync();
try
{
ClientCollectionCache collectionCache = await this.ClientContext.DocumentClient.GetCollectionCacheAsync();
return await collectionCache.ResolveByNameAsync(HttpConstants.Versions.CurrentVersion, this.LinkUri, cancellationToken);
}
catch (DocumentClientException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,47 @@ public async Task EnsureUnauthorized_ThrowsCosmosClientException()
// Take the key and change some middle character
authKey = authKey.Replace("m", "M");

CosmosClient cosmosClient = new CosmosClient(
using CosmosClient cosmosClient = new CosmosClient(
endpoint,
authKey);

CosmosException exception = await Assert.ThrowsExceptionAsync<CosmosException>(() => cosmosClient.GetContainer("test", "test").ReadItemAsync<dynamic>("test", new PartitionKey("test")));
Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode);
}

[TestMethod]
public async Task EnsureUnauthorized_Writes_ThrowsCosmosClientException()
{
string authKey = ConfigurationManager.AppSettings["MasterKey"];
string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"];

// Take the key and change some middle character
authKey = authKey.Replace("m", "M");

using CosmosClient cosmosClient = new CosmosClient(
endpoint,
authKey);
CosmosException exception = await Assert.ThrowsExceptionAsync<CosmosException>(() => cosmosClient.GetContainer("test", "test").CreateItemAsync<dynamic>(new { id = "test" }));
Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode);
}

[TestMethod]
public async Task EnsureUnauthorized_Query_ThrowsCosmosClientException()
{
string authKey = ConfigurationManager.AppSettings["MasterKey"];
string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"];

// Take the key and change some middle character
authKey = authKey.Replace("m", "M");

using CosmosClient cosmosClient = new CosmosClient(
endpoint,
authKey);

using FeedIterator<dynamic> iterator = cosmosClient.GetContainer("test", "test").GetItemQueryIterator<dynamic>("SELECT * FROM c");

CosmosException exception = await Assert.ThrowsExceptionAsync<CosmosException>(() => iterator.ReadNextAsync());
Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode);
}
}
}

0 comments on commit 67a2c58

Please sign in to comment.