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

Incorrect key to throw CosmosExceptions with HttpStatusCode.Unauthorized status code #785

Merged
merged 7 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 19 additions & 1 deletion Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public override async Task<ResponseMessage> SendAsync(
}

await this.ValidateAndSetConsistencyLevelAsync(request);
await this.client.DocumentClient.EnsureValidClientAsync();
(bool clientIsValid, ResponseMessage errorResponse) = await this.EnsureValidClientAsync(request);
ealsur marked this conversation as resolved.
Show resolved Hide resolved
if (!clientIsValid)
{
return errorResponse;
}

await request.AssertPartitioningDetailsAsync(this.client, cancellationToken);
this.FillMultiMasterContext(request);
return await base.SendAsync(request, cancellationToken);
Expand Down Expand Up @@ -178,6 +183,19 @@ internal static HttpMethod GetHttpMethod(
}
}

private async Task<(bool, ResponseMessage)> EnsureValidClientAsync(RequestMessage request)
{
try
{
await this.client.DocumentClient.EnsureValidClientAsync();
return (true, null);
ealsur marked this conversation as resolved.
Show resolved Hide resolved
}
catch (DocumentClientException dce)
{
return (false, dce.ToCosmosResponseMessage(request));
}
}

private void FillMultiMasterContext(RequestMessage request)
{
if (this.client.DocumentClient.UseMultipleWriteLocations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Scripts;
using Microsoft.Azure.Cosmos.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
Expand Down Expand Up @@ -221,5 +222,22 @@ public async Task ItemResourcePermissionTest()
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
}
}

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

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

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);
}
}
}
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#756](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/756) Change Feed Processor with WithStartTime would execute the delegate the first time with no items.
- [#761](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/761) CosmosClient deadlocks when using a custom Task Scheduler like Orleans (Thanks to jkonecki)
- [#769](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/769) Session Consistency + Gateway mode session-token bug fix: Under few rare non-success cases session token might be in-correct
- [#785](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/785) Unauthorized errors during client initialization now generate CosmosExceptions
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved

## [3.1.1](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.1.1) - 2019-08-12

Expand Down