|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. |
| 4 | +# |
| 5 | + |
| 6 | +import logging |
| 7 | +import os |
| 8 | +from unittest import mock |
| 9 | +from urllib.parse import urlparse |
| 10 | + |
| 11 | +from snowflake.connector.vendored.requests.exceptions import ConnectTimeout, HTTPError |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +# Import shared functions |
| 17 | +from ...csp_helpers import ( |
| 18 | + FakeAwsEnvironment, |
| 19 | + FakeAzureFunctionMetadataService, |
| 20 | + FakeAzureVmMetadataService, |
| 21 | + FakeGceMetadataService, |
| 22 | + FakeMetadataService, |
| 23 | + NoMetadataService, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def build_response(content: bytes, status_code: int = 200): |
| 28 | + """Builds an aiohttp-compatible response object with the given status code and content.""" |
| 29 | + |
| 30 | + class AsyncResponse: |
| 31 | + def __init__(self, content, status_code): |
| 32 | + self.ok = status_code < 400 |
| 33 | + self.status = status_code |
| 34 | + self._content = content |
| 35 | + |
| 36 | + async def read(self): |
| 37 | + return self._content |
| 38 | + |
| 39 | + return AsyncResponse(content, status_code) |
| 40 | + |
| 41 | + |
| 42 | +class FakeMetadataServiceAsync(FakeMetadataService): |
| 43 | + def _async_request(self, method, url, headers=None, timeout=None): |
| 44 | + """Entry point for the aiohttp mock.""" |
| 45 | + logger.debug(f"Received async request: {method} {url} {str(headers)}") |
| 46 | + parsed_url = urlparse(url) |
| 47 | + |
| 48 | + # Create async context manager for aiohttp response |
| 49 | + class AsyncResponseContextManager: |
| 50 | + def __init__(self, response): |
| 51 | + self.response = response |
| 52 | + |
| 53 | + async def __aenter__(self): |
| 54 | + return self.response |
| 55 | + |
| 56 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 57 | + pass |
| 58 | + |
| 59 | + # Create aiohttp-compatible response mock |
| 60 | + class AsyncResponse: |
| 61 | + def __init__(self, requests_response): |
| 62 | + self.ok = requests_response.ok |
| 63 | + self.status = requests_response.status_code |
| 64 | + self._content = requests_response.content |
| 65 | + |
| 66 | + async def read(self): |
| 67 | + return self._content |
| 68 | + |
| 69 | + if not parsed_url.hostname == self.expected_hostname: |
| 70 | + logger.debug( |
| 71 | + f"Received async request to unexpected hostname {parsed_url.hostname}" |
| 72 | + ) |
| 73 | + import aiohttp |
| 74 | + |
| 75 | + raise aiohttp.ClientError() |
| 76 | + |
| 77 | + # Get the response from the subclass handler, catch exceptions and convert them |
| 78 | + try: |
| 79 | + sync_response = self.handle_request(method, parsed_url, headers, timeout) |
| 80 | + async_response = AsyncResponse(sync_response) |
| 81 | + return AsyncResponseContextManager(async_response) |
| 82 | + except (HTTPError, ConnectTimeout) as e: |
| 83 | + import aiohttp |
| 84 | + |
| 85 | + # Convert requests exceptions to aiohttp exceptions so they get caught properly |
| 86 | + raise aiohttp.ClientError() from e |
| 87 | + |
| 88 | + def __enter__(self): |
| 89 | + self.reset_defaults() |
| 90 | + self.patchers = [] |
| 91 | + # Mock aiohttp for async requests |
| 92 | + self.patchers.append( |
| 93 | + mock.patch("aiohttp.ClientSession.request", side_effect=self._async_request) |
| 94 | + ) |
| 95 | + for patcher in self.patchers: |
| 96 | + patcher.__enter__() |
| 97 | + return self |
| 98 | + |
| 99 | + |
| 100 | +class NoMetadataServiceAsync(FakeMetadataServiceAsync, NoMetadataService): |
| 101 | + pass |
| 102 | + |
| 103 | + |
| 104 | +class FakeAzureVmMetadataServiceAsync( |
| 105 | + FakeMetadataServiceAsync, FakeAzureVmMetadataService |
| 106 | +): |
| 107 | + pass |
| 108 | + |
| 109 | + |
| 110 | +class FakeAzureFunctionMetadataServiceAsync( |
| 111 | + FakeMetadataServiceAsync, FakeAzureFunctionMetadataService |
| 112 | +): |
| 113 | + def __enter__(self): |
| 114 | + # Set environment variables first (like Azure Function service) |
| 115 | + os.environ["IDENTITY_ENDPOINT"] = self.identity_endpoint |
| 116 | + os.environ["IDENTITY_HEADER"] = self.identity_header |
| 117 | + |
| 118 | + # Then set up the metadata service mocks |
| 119 | + FakeMetadataServiceAsync.__enter__(self) |
| 120 | + return self |
| 121 | + |
| 122 | + def __exit__(self, *args, **kwargs): |
| 123 | + # Clean up async mocks first |
| 124 | + FakeMetadataServiceAsync.__exit__(self, *args, **kwargs) |
| 125 | + |
| 126 | + # Then clean up environment variables |
| 127 | + os.environ.pop("IDENTITY_ENDPOINT", None) |
| 128 | + os.environ.pop("IDENTITY_HEADER", None) |
| 129 | + |
| 130 | + |
| 131 | +class FakeGceMetadataServiceAsync(FakeMetadataServiceAsync, FakeGceMetadataService): |
| 132 | + pass |
| 133 | + |
| 134 | + |
| 135 | +class FakeAwsEnvironmentAsync(FakeAwsEnvironment): |
| 136 | + """Emulates the AWS environment-specific functions used in async wif_util.py. |
| 137 | +
|
| 138 | + Unlike the other metadata services, the HTTP calls made by AWS are deep within boto libaries, so |
| 139 | + emulating them here would be complex and fragile. Instead, we emulate the higher-level functions |
| 140 | + called by the connector code. |
| 141 | + """ |
| 142 | + |
| 143 | + async def get_region(self): |
| 144 | + return self.region |
| 145 | + |
| 146 | + async def get_arn(self): |
| 147 | + return self.arn |
| 148 | + |
| 149 | + async def get_credentials(self): |
| 150 | + return self.credentials |
| 151 | + |
| 152 | + def __enter__(self): |
| 153 | + # First call the parent's __enter__ to get base functionality |
| 154 | + super().__enter__() |
| 155 | + |
| 156 | + # Then add async-specific patches |
| 157 | + async def async_get_credentials(): |
| 158 | + return self.credentials |
| 159 | + |
| 160 | + async def async_get_caller_identity(): |
| 161 | + return {"Arn": self.arn} |
| 162 | + |
| 163 | + async def async_get_region(): |
| 164 | + return await self.get_region() |
| 165 | + |
| 166 | + async def async_get_arn(): |
| 167 | + return await self.get_arn() |
| 168 | + |
| 169 | + # Mock aioboto3.Session.get_credentials (IS async) |
| 170 | + self.patchers.append( |
| 171 | + mock.patch( |
| 172 | + "snowflake.connector.aio._wif_util.aioboto3.Session.get_credentials", |
| 173 | + side_effect=async_get_credentials, |
| 174 | + ) |
| 175 | + ) |
| 176 | + |
| 177 | + # Mock the async AWS region and ARN functions |
| 178 | + self.patchers.append( |
| 179 | + mock.patch( |
| 180 | + "snowflake.connector.aio._wif_util.get_aws_region", |
| 181 | + side_effect=async_get_region, |
| 182 | + ) |
| 183 | + ) |
| 184 | + |
| 185 | + self.patchers.append( |
| 186 | + mock.patch( |
| 187 | + "snowflake.connector.aio._wif_util.get_aws_arn", |
| 188 | + side_effect=async_get_arn, |
| 189 | + ) |
| 190 | + ) |
| 191 | + |
| 192 | + # Mock the async STS client for direct aioboto3 usage |
| 193 | + class MockStsClient: |
| 194 | + async def __aenter__(self): |
| 195 | + return self |
| 196 | + |
| 197 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 198 | + pass |
| 199 | + |
| 200 | + async def get_caller_identity(self): |
| 201 | + return await async_get_caller_identity() |
| 202 | + |
| 203 | + def mock_session_client(service_name): |
| 204 | + if service_name == "sts": |
| 205 | + return MockStsClient() |
| 206 | + return None |
| 207 | + |
| 208 | + self.patchers.append( |
| 209 | + mock.patch( |
| 210 | + "snowflake.connector.aio._wif_util.aioboto3.Session.client", |
| 211 | + side_effect=mock_session_client, |
| 212 | + ) |
| 213 | + ) |
| 214 | + |
| 215 | + # Start the additional async patches |
| 216 | + for patcher in self.patchers[-4:]: # Only start the new patches we just added |
| 217 | + patcher.__enter__() |
| 218 | + return self |
| 219 | + |
| 220 | + def __exit__(self, *args, **kwargs): |
| 221 | + # Call parent's exit to clean up base patches |
| 222 | + super().__exit__(*args, **kwargs) |
0 commit comments