Skip to content
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

Authorization: Fixes DocumentClientException being thrown on write operations #1909

Merged
merged 3 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}