|
| 1 | +import logging |
1 | 2 | from typing import Union, cast
|
2 | 3 | from typing_extensions import Literal, Protocol
|
3 | 4 |
|
4 | 5 | import httpx
|
5 | 6 | import pytest
|
6 | 7 | from respx import MockRouter
|
7 | 8 |
|
| 9 | +from openai._utils import SensitiveHeadersFilter, is_dict |
8 | 10 | from openai._models import FinalRequestOptions
|
9 | 11 | from openai.lib.azure import AzureOpenAI, AsyncAzureOpenAI
|
10 | 12 |
|
@@ -148,3 +150,102 @@ def token_provider() -> str:
|
148 | 150 |
|
149 | 151 | assert calls[0].request.headers.get("Authorization") == "Bearer first"
|
150 | 152 | assert calls[1].request.headers.get("Authorization") == "Bearer second"
|
| 153 | + |
| 154 | + |
| 155 | +class TestAzureLogging: |
| 156 | + |
| 157 | + @pytest.fixture(autouse=True) |
| 158 | + def logger_with_filter(self) -> logging.Logger: |
| 159 | + logger = logging.getLogger("openai") |
| 160 | + logger.setLevel(logging.DEBUG) |
| 161 | + logger.addFilter(SensitiveHeadersFilter()) |
| 162 | + return logger |
| 163 | + |
| 164 | + @pytest.mark.respx() |
| 165 | + def test_azure_api_key_redacted(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None: |
| 166 | + respx_mock.post( |
| 167 | + "https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01" |
| 168 | + ).mock( |
| 169 | + return_value=httpx.Response(200, json={"model": "gpt-4"}) |
| 170 | + ) |
| 171 | + |
| 172 | + client = AzureOpenAI( |
| 173 | + api_version="2024-06-01", |
| 174 | + api_key="example_api_key", |
| 175 | + azure_endpoint="https://example-resource.azure.openai.com", |
| 176 | + ) |
| 177 | + |
| 178 | + with caplog.at_level(logging.DEBUG): |
| 179 | + client.chat.completions.create(messages=[], model="gpt-4") |
| 180 | + |
| 181 | + for record in caplog.records: |
| 182 | + if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]): |
| 183 | + assert record.args["headers"]["api-key"] == "<redacted>" |
| 184 | + |
| 185 | + |
| 186 | + @pytest.mark.respx() |
| 187 | + def test_azure_bearer_token_redacted(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None: |
| 188 | + respx_mock.post( |
| 189 | + "https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01" |
| 190 | + ).mock( |
| 191 | + return_value=httpx.Response(200, json={"model": "gpt-4"}) |
| 192 | + ) |
| 193 | + |
| 194 | + client = AzureOpenAI( |
| 195 | + api_version="2024-06-01", |
| 196 | + azure_ad_token="example_token", |
| 197 | + azure_endpoint="https://example-resource.azure.openai.com", |
| 198 | + ) |
| 199 | + |
| 200 | + with caplog.at_level(logging.DEBUG): |
| 201 | + client.chat.completions.create(messages=[], model="gpt-4") |
| 202 | + |
| 203 | + for record in caplog.records: |
| 204 | + if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]): |
| 205 | + assert record.args["headers"]["Authorization"] == "<redacted>" |
| 206 | + |
| 207 | + |
| 208 | + @pytest.mark.asyncio |
| 209 | + @pytest.mark.respx() |
| 210 | + async def test_azure_api_key_redacted_async(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None: |
| 211 | + respx_mock.post( |
| 212 | + "https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01" |
| 213 | + ).mock( |
| 214 | + return_value=httpx.Response(200, json={"model": "gpt-4"}) |
| 215 | + ) |
| 216 | + |
| 217 | + client = AsyncAzureOpenAI( |
| 218 | + api_version="2024-06-01", |
| 219 | + api_key="example_api_key", |
| 220 | + azure_endpoint="https://example-resource.azure.openai.com", |
| 221 | + ) |
| 222 | + |
| 223 | + with caplog.at_level(logging.DEBUG): |
| 224 | + await client.chat.completions.create(messages=[], model="gpt-4") |
| 225 | + |
| 226 | + for record in caplog.records: |
| 227 | + if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]): |
| 228 | + assert record.args["headers"]["api-key"] == "<redacted>" |
| 229 | + |
| 230 | + |
| 231 | + @pytest.mark.asyncio |
| 232 | + @pytest.mark.respx() |
| 233 | + async def test_azure_bearer_token_redacted_async(self, respx_mock: MockRouter, caplog: pytest.LogCaptureFixture) -> None: |
| 234 | + respx_mock.post( |
| 235 | + "https://example-resource.azure.openai.com/openai/deployments/gpt-4/chat/completions?api-version=2024-06-01" |
| 236 | + ).mock( |
| 237 | + return_value=httpx.Response(200, json={"model": "gpt-4"}) |
| 238 | + ) |
| 239 | + |
| 240 | + client = AsyncAzureOpenAI( |
| 241 | + api_version="2024-06-01", |
| 242 | + azure_ad_token="example_token", |
| 243 | + azure_endpoint="https://example-resource.azure.openai.com", |
| 244 | + ) |
| 245 | + |
| 246 | + with caplog.at_level(logging.DEBUG): |
| 247 | + await client.chat.completions.create(messages=[], model="gpt-4") |
| 248 | + |
| 249 | + for record in caplog.records: |
| 250 | + if is_dict(record.args) and record.args.get("headers") and is_dict(record.args["headers"]): |
| 251 | + assert record.args["headers"]["Authorization"] == "<redacted>" |
0 commit comments