microsoftgraph / microsoftgraph/msgraph-sdk-python

Proxy is not respected

Open
#535 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
630
Forks
96
Avg merge
15h 20m
Merged PRs (30d)
3

Description

I followed the steps here and its not using the proxy at all. My requests just time out: https://learn.microsoft.com/en-us/graph/sdks/customize-client?tabs=python#configuring-the-http-proxy-for-the-client

While normal requests using httx work, the requests to Azure do not. I'm guessing that this new client is not accepted somewhere down the chain

import asyncio
from azure.identity.aio import ClientSecretCredential
from django.conf import settings
from httpx import AsyncClient
from kiota_authentication_azure.azure_identity_authentication_provider import (
    AzureIdentityAuthenticationProvider,
)
from msgraph import GraphRequestAdapter, GraphServiceClient
from msgraph.generated.users.users_request_builder import UsersRequestBuilder
from msgraph_core import GraphClientFactory


def get_azure_client() -> tuple[GraphServiceClient, AsyncClient]:
    credential = ClientSecretCredential(
        settings.AZURE_TENANT_ID, settings.AZURE_CLIENT_ID, settings.AZURE_CLIENT_SECRET  # type: ignore
    )
    auth_provider = AzureIdentityAuthenticationProvider(
        credential, scopes=["https://graph.microsoft.com/.default"]
    )
    # see: https://learn.microsoft.com/en-us/graph/sdks/customize-client?tabs=python#configuring-the-http-proxy-for-the-client
    httpx_proxies = {
        "http://": settings.INTERNET_PROXY,
        "https://": settings.INTERNET_PROXY,
    }
    http_client = AsyncClient(proxies=httpx_proxies)  # type: ignore
    http_client = GraphClientFactory.create_with_default_middleware(client=http_client)
    adapter = GraphRequestAdapter(auth_provider, http_client)
    return GraphServiceClient(request_adapter=adapter), http_client


async def amain() -> None:
    client, http_client = get_azure_client()
    await http_client.get("https://microsoft.com") # this works
    await client.groups.by_group_id(  # this times out
        settings.AZURE_GROUP_ID
    ).transitive_members.get()


if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        loop.run_until_complete(amain())
    except KeyboardInterrupt:
        pass

/cc @nejch

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at GraphClientFactory.create_with_default_middleware and GraphRequestAdapter, then compare the direct AsyncClient request with the GraphServiceClient request. Reproduce the timeout using the supplied proxy configuration and trace whether the configured AsyncClient is preserved through the middleware and adapter. Done means Graph requests use the configured proxy rather than timing out.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, python
Domain
api, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.