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 all 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
21 changes: 20 additions & 1 deletion Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.Azure.Cosmos.Handlers
/// </summary>
internal class RequestInvokerHandler : RequestHandler
{
private static (bool, ResponseMessage) clientIsValid = (false, null);
private readonly CosmosClient client;
private Cosmos.ConsistencyLevel? AccountConsistencyLevel = null;
private Cosmos.ConsistencyLevel? RequestedClientConsistencyLevel;
Expand Down Expand Up @@ -45,7 +46,12 @@ public override async Task<ResponseMessage> SendAsync(
}

await this.ValidateAndSetConsistencyLevelAsync(request);
await this.client.DocumentClient.EnsureValidClientAsync();
(bool isError, ResponseMessage errorResponse) = await this.EnsureValidClientAsync(request);
if (isError)
{
return errorResponse;
}

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

private async Task<(bool, ResponseMessage)> EnsureValidClientAsync(RequestMessage request)
{
try
{
await this.client.DocumentClient.EnsureValidClientAsync();
return RequestInvokerHandler.clientIsValid;
}
catch (DocumentClientException dce)
{
return (true, 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 @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#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
- [#772](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/772) Fixed Throughput throwing when custom serializer used or offer doesn't exists
- [#785](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/785) Incorrect key to throw CosmosExceptions with HttpStatusCode.Unauthorized status code

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

Expand Down