Unable to connect to Azure Cosmos DB using Corporate Proxy

Shahnawaz 41 Reputation points
2024-05-30T10:49:50.39+00:00

I am trying to use Azure Cosmos DB as my document database using Python SDK library. And it seems that I unable to connect to cosmos DB using corporate proxy if I pass the proxy information using "ProxyConfiguration" or "ConnectionPolicy". To me its seems like there is an issue with the python SDK. But it I set the proxy at OS level then I am able to establish the connection Azure Cosmos DB but my other API call also go via this proxy and stopped working. Below is my code. Kindly help me to resolve the issue.

import os
from azure.cosmos import CosmosClient, exceptions, PartitionKey, ProxyConfiguration
from azure.cosmos.documents import ConnectionPolicy

URL = "https://company.documents.azure.com:443/"
KEY = "someKey"

# Solution - 1
corp_proxy = ProxyConfiguration()
corp_proxy.Host = 'companyProxyHostname.nic'
corp_proxy.Port = 9090
client = CosmosClient(URL, credential=KEY, proxy_config=corp_proxy)

# Solution - 2
connection_policy = ConnectionPolicy()
connection_policy.ProxyConfiguration = documents.ProxyConfiguration()
connection_policy.ProxyConfiguration.Host = 'companyProxyHostname.nic'
connection_policy.ProxyConfiguration.Port = 9090
client = CosmosClient(URL, credential=KEY, connection_policy=connection_policy)

The below solution works well



import os
from azure.cosmos import CosmosClient, exceptions, PartitionKey, ProxyConfiguration

os.environ['http_proxy'] = 'http://companyProxyHostname.nic:9090'
os.environ['HTTP_PROXY'] = 'http://companyProxyHostname.nic:9090'
os.environ['https_proxy'] = 'http://companyProxyHostname.nic:9090'
os.environ['HTTPS_PROXY'] = 'http://companyProxyHostname.nic:9090'

client = CosmosClient(URL, credential=KEY)
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,680 questions
{count} votes

Accepted answer
  1. GeethaThatipatri-MSFT 29,477 Reputation points Microsoft Employee
    2024-06-04T12:51:17.48+00:00

    @Shahnawaz I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue: You are facing issues connecting to Azure Cosmos DB using a corporate proxy. you have tried passing the proxy information using "ProxyConfiguration" or "ConnectionPolicy" but unable to establish a connection. However, setting the proxy at the OS level works, but it affects other API calls as well.

    Solution: As you mentioned you found that both ProxyConfiguration() and ConnectionPolicy() is not configured correctly, and you were able to resolve the issue by passing the proxies in *kwarg

    client = CosmosClient(URL, credential=KEY, proxies={"https":"hostname:port"})

    Thank you again for sharing your findings, If you have any other questions or are still running into more issues, please let me know. Thank you again for your time and patience throughout this issue.

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

    Regards

    Geetha

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.