reproduce-14067.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Sample code to reproduce timeout issue described in
  2. # https://github.com/Azure/azure-sdk-for-python/issues/14067
  3. # Run mitmproxy and enter interceptions mode
  4. # > type i and then specify .*
  5. # Allow the first request to AAD to pass hitting A or double a
  6. mitm_proxy= "http://127.0.0.1:8080"
  7. container_name= "issue14067"
  8. blob_to_read = "debug.log"
  9. # Configure a proxy
  10. # https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-configure-proxy?tabs=cmd
  11. import io
  12. from io import BytesIO
  13. import os
  14. os.environ["HTTP_PROXY"] = mitm_proxy
  15. os.environ["HTTPS_PROXY"] = mitm_proxy
  16. # Retrieve the storage account and the storage key
  17. import json
  18. settings= {}
  19. with open('./settings.json') as f:
  20. settings = json.load(f)
  21. account_name = settings["STORAGE_ACCOUNT_NAME"]
  22. # Configure identity that has "Storage Blob Data Reader" access
  23. os.environ["AZURE_CLIENT_ID"] = settings["AZURE_CLIENT_ID"]
  24. os.environ["AZURE_CLIENT_SECRET"] = settings["AZURE_CLIENT_SECRET"]
  25. os.environ["AZURE_TENANT_ID"] = settings["AZURE_TENANT_ID"]
  26. # Create the client
  27. from azure.storage.blob.aio import (
  28. BlobServiceClient,
  29. ContainerClient,
  30. BlobClient,
  31. )
  32. from azure.core.exceptions import (
  33. ResourceNotFoundError,
  34. ClientAuthenticationError
  35. )
  36. from azure.identity.aio import DefaultAzureCredential
  37. async def download_blob_using_blobservice(account_name: str, credential: DefaultAzureCredential, container_name:str , blob_name: str, file_stream: io.BytesIO):
  38. try:
  39. # Timeout didn't work on this code...
  40. blob_service = BlobServiceClient(f"{account_name}.blob.core.windows.net", credential=credential, connection_timeout=1, read_timeout=1)
  41. blob_client = blob_service.get_blob_client(container_name, blob_name)
  42. storage_stream_downloader = await blob_client.download_blob()
  43. await storage_stream_downloader.readinto(file_stream)
  44. return
  45. except ResourceNotFoundError:
  46. raise KeyError(blob_name)
  47. except ClientAuthenticationError:
  48. raise
  49. async def download_blob_using_blobclient(account_name: str, credential:DefaultAzureCredential, container_name:str , blob_name: str, file_stream: io.BytesIO):
  50. try:
  51. blob_client = BlobClient(f"{account_name}.blob.core.windows.net", credential=credential, container_name=container_name, blob_name=blob_name, connection_timeout=1, read_timeout=1)
  52. storage_stream_downloader = await blob_client.download_blob()
  53. await storage_stream_downloader.readinto(file_stream)
  54. return
  55. except ResourceNotFoundError:
  56. raise KeyError(blob_name)
  57. except ClientAuthenticationError:
  58. raise
  59. # Execute method
  60. from io import (
  61. BytesIO,
  62. TextIOWrapper
  63. )
  64. import asyncio
  65. def execute_code(loop, timeout=None):
  66. with BytesIO() as file_stream:
  67. service_principal = DefaultAzureCredential(exclude_cli_credential=True)
  68. future = asyncio.run_coroutine_threadsafe(
  69. download_blob_using_blobclient(account_name,service_principal, container_name, blob_to_read, file_stream),
  70. loop=loop)
  71. future.result(timeout)
  72. file_stream.flush()
  73. file_stream.seek(0)
  74. bw=TextIOWrapper(file_stream).read()
  75. print(bw)
  76. return
  77. loop = asyncio.get_event_loop()
  78. future = loop.run_in_executor(None, execute_code, loop)
  79. loop.run_until_complete(future)