sachabruttin / CosmosDbExplorer

A nice client explorer for Azure Cosmos DB
https://www.bruttin.com/CosmosDbExplorer/
MIT License
111 stars 27 forks source link

EnableEndpointDiscovery in ConnectionPolicy must be configurable #61

Closed cmendible closed 2 years ago

cmendible commented 2 years ago

With the current implementation of the GetClent function:

https://github.com/sachabruttin/CosmosDbExplorer/blob/a123ee6187f3aca20d1d7f99be2a248aa3a63d60/src/CosmosDbExplorer/Services/DocumentDbService.cs#L35

it´s not possible to connect with a Cosmos DB database that is protected via private endpoint and also behind an Azure Application Gateway because the default connection policy enables endpoint discovery: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.client.connectionpolicy.enableendpointdiscovery?view=azure-dotnet

CosmosDBExplorer should provide a way to disable endpoint discovery, using code similar to the one shown in the following snippet:

private DocumentClient GetClient(Connection connection)
{
    if (!_clientInstances.ContainsKey(connection))
    {
        var policy = new ConnectionPolicy
        {
            ConnectionMode = connection.ConnectionType == ConnectionType.Gateway ? ConnectionMode.Gateway : ConnectionMode.Direct,
            ConnectionProtocol = connection.ConnectionType == ConnectionType.DirectHttps ? Protocol.Https : Protocol.Tcp,
            EnableEndpointDiscovery = connection.EnableEndpointDiscovery;
    };

        var client = new DocumentClient(connection.DatabaseUri, connection.AuthenticationKey, policy);
        client.OpenAsync();

        _clientInstances.Add(connection, client);
    }

    return _clientInstances[connection];
}

This was also an issue with the vscode-cosmosdb extension. Please check the issue and PR if needed: