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

CosmosClient with wrong key throws DocumentClientException instead of CosmosException #1906

Closed
NRam0s opened this issue Oct 6, 2020 · 10 comments · Fixed by #1909
Closed
Labels
bug Something isn't working customer-reported Issue created by a customer

Comments

@NRam0s
Copy link

NRam0s commented Oct 6, 2020

This ticket relates with another one that was been previously closed (#785). I have basically implemented exactly what was described on that ticket.
I am trying to create a cosmos client with a wrong key. I am able to get a DB and a container but as soon as I try to do a container action I get a DocumentClientException.

new CosmosClient(_appSettings.CosmosDbEndpoint, wrongKey,
       new CosmosClientOptions()
       {
           ConnectionMode = ConnectionMode.Direct,
           SerializerOptions = new CosmosSerializationOptions
           {
              PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
            }
         });

_database = client.GetDatabase(_databaseId);
_container = _database.GetContainer(_transactionsContainerId);
try
{
   var response = await _container.CreateItemAsync(item);
   response.Resource.ETag = response.ETag;

   return response.Resource;
}
catch (CosmosException e)
{
 ...
}
catch (DocumentClientException e1)
{
...
}
catch (Exception e)
{
... The exception comes here as nothing seems to be able to catch a DocumentClientException
 }

We are using Microsoft.Azure.Cosmos 3.13.0

One of my colleagues did some research and he found that the issue might be on this line of code. It might need to be inside the try catch: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/Resource/ClientContextCore.cs#L332

Some more details I have found. If we initialize the client with a valid key and then the key gets rotated, we get the CosmosException as expected with the following details:
Response status code does not indicate success: Unauthorized (401); Substatus: 0; ActivityId: XXX; Reason: (Message: {"Errors":["The MAC signature found in the HTTP request is not the same as the computed signature. Server used following string to sign - XXX...

However, as I mentioned above, the DocumentClientException is still thrown if the client is initialized already with an invalid key.

@j82w
Copy link
Contributor

j82w commented Oct 6, 2020

  1. How are you catching the DocumentClientException it is an internal type?
  2. Is the incorrect key a resource token? Is there any chance you can provide a repro of the issue? I just ran the following test and it passed. It seems to be testing the exact scenario you are describing.

@j82w j82w added customer-reported Issue created by a customer needs-investigation labels Oct 6, 2020
@NRam0s
Copy link
Author

NRam0s commented Oct 6, 2020

  1. How are you catching the DocumentClientException it is an internal type?
  2. Is the incorrect key a resource token? Is there any chance you can provide a repro of the issue? I just ran the following test and it passed. It seems to be testing the exact scenario you are describing.

1 - I am not catching it. If I debug I can see the type of the exception. I did try to import the nuget package "Microsoft.Azure.DocumentDB.Core" which imports the type (DocumentClientException), however the exception still does not fall on that catch block, although while debugging it shoes the type as being DocumentClientException
2- I will need some time to create a test repo as I am currently experiencing this on a repo from my organisation (I will update here once I get to create a small test repo)

@ealsur
Copy link
Member

ealsur commented Oct 6, 2020

Could it be that you are getting some logs / events? Sometimes the SDK does have DocumentClientException internally that might popup in event tracing, but the public facing type is CosmosException (there are places where we convert the DocumentClientException to CosmosException before doing the actual throw).

@NRam0s
Copy link
Author

NRam0s commented Oct 6, 2020

As I mentioned. I won't be able to create a test repo that quickly. But here are some pics attached to show what I am getting
1
2
3

@NRam0s
Copy link
Author

NRam0s commented Oct 6, 2020

In regards to the way we set the client, I have attached the picture below. We have a DI which gets sorted at runtime. All I doin order to reproduce the error is while debugging, just change a letter on the primary key obtained and that makes the exception occur.
4

@j82w
Copy link
Contributor

j82w commented Oct 6, 2020

Can you provide the full exception with the stack trace?

@ealsur
Copy link
Member

ealsur commented Oct 6, 2020

@j82w This reproes on Write operations:

[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").CreateItemAsync<dynamic>(new { id = "test" }));
    Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode);
}
 ActivityId: 5bea91c4-7655-461e-a21c-226c79280ff4, Microsoft.Azure.Documents.Common/2.11.0, Windows/10.0.19042 cosmos-netstandard-sdk/3.13.3
    Stack Trace:    at Microsoft.Azure.Cosmos.GatewayStoreClient.ParseResponseAsync(HttpResponseMessage responseMessage, JsonSerializerSettings serializerSettings, DocumentServiceRequest request) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\GatewayStoreClient.cs:line 117
       at Microsoft.Azure.Cosmos.GatewayAccountReader.GetDatabaseAccountAsync(Uri serviceEndpoint) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\GatewayAccountReader.cs:line 50
       at Microsoft.Azure.Cosmos.Routing.GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(Uri defaultEndpoint, IList`1 locations, Func`2 getDatabaseAccountFn) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\Routing\GlobalEndpointManager.cs:line 109
       at Microsoft.Azure.Cosmos.GatewayAccountReader.InitializeReaderAsync() in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\GatewayAccountReader.cs:line 59
       at Microsoft.Azure.Cosmos.CosmosAccountServiceConfiguration.InitializeAsync() in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\Resource\Settings\CosmosAccountServiceConfiguration.cs:line 60
       at Microsoft.Azure.Cosmos.DocumentClient.InitializeGatewayConfigurationReaderAsync() in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\DocumentClient.cs:line 6509
       at Microsoft.Azure.Cosmos.DocumentClient.GetInitializationTaskAsync(IStoreClientFactory storeClientFactory) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\DocumentClient.cs:line 925
       at Microsoft.Azure.Cosmos.DocumentClient.EnsureValidClientAsync() in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\DocumentClient.cs:line 1406
       at Microsoft.Azure.Cosmos.DocumentClient.GetCollectionCacheAsync() in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\DocumentClient.cs:line 542
       at Microsoft.Azure.Cosmos.ContainerCore.GetCachedContainerPropertiesAsync(CancellationToken cancellationToken) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\Resource\Container\ContainerCore.cs:line 310
       at Microsoft.Azure.Cosmos.ContainerCore.GetPartitionKeyPathTokensAsync(CancellationToken cancellationToken) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\Resource\Container\ContainerCore.cs:line 343
       at Microsoft.Azure.Cosmos.ContainerCore.GetPartitionKeyValueFromStreamAsync(Stream stream, CancellationToken cancellation) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\Resource\Container\ContainerCore.Items.cs:line 765
       at Microsoft.Azure.Cosmos.ContainerCore.ExtractPartitionKeyAndProcessItemStreamAsync[T](Nullable`1 partitionKey, String itemId, T item, OperationType operationType, ItemRequestOptions requestOptions, CosmosDiagnosticsContext diagnosticsContext, CancellationToken cancellationToken) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\Resource\Container\ContainerCore.Items.cs:line 673
       at Microsoft.Azure.Cosmos.ContainerCore.CreateItemAsync[T](CosmosDiagnosticsContext diagnosticsContext, T item, Nullable`1 partitionKey, ItemRequestOptions requestOptions, CancellationToken cancellationToken) in C:\GIT\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\Resource\Container\ContainerCore.Items.cs:line 72

@ealsur
Copy link
Member

ealsur commented Oct 6, 2020

@NRam0s was right about the location for the fix, sharing PR in a min

@ealsur ealsur added bug Something isn't working customer-reported Issue created by a customer and removed customer-reported Issue created by a customer needs-investigation labels Oct 6, 2020
@j82w j82w closed this as completed in #1909 Oct 6, 2020
@NRam0s
Copy link
Author

NRam0s commented Oct 7, 2020

Thanks guys 👍

@ghost
Copy link

ghost commented Dec 15, 2021

Closing due to in-activity, pease feel free to re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working customer-reported Issue created by a customer
Projects
None yet
3 participants