diff --git a/Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs b/Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs index db08861171..29f5e9a745 100644 --- a/Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs +++ b/Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs @@ -18,6 +18,7 @@ namespace Microsoft.Azure.Cosmos.Handlers /// internal class RequestInvokerHandler : RequestHandler { + private static (bool, ResponseMessage) clientIsValid = (false, null); private readonly CosmosClient client; private Cosmos.ConsistencyLevel? AccountConsistencyLevel = null; private Cosmos.ConsistencyLevel? RequestedClientConsistencyLevel; @@ -45,7 +46,12 @@ public override async Task 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); @@ -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) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs index 7fa6a784aa..ce6db1de54 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosPermissionTests.cs @@ -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] @@ -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(() => cosmosClient.GetContainer("test", "test").ReadItemAsync("test", new PartitionKey("test"))); + Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode); + } } } diff --git a/changelog.md b/changelog.md index e77179c01d..8400b94627 100644 --- a/changelog.md +++ b/changelog.md @@ -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