Skip to content

Commit

Permalink
[Fix] Exclude localhost from hitting proxies for metadata service req…
Browse files Browse the repository at this point in the history
…uests (#877)

## What changes are proposed in this pull request?

Exclude localhost from hitting proxies for metadata service requests

`requests` package doesn't do it by default.

Relevant issue:
databricks/databricks-vscode#916 (comment)

## How is this tested?

Tested manually by running this code against real metadata service of
the logged in databricks vscode extension:
```python
import requests
import os

os.environ["HTTP_PROXY"] = "http://test.com"

resp = requests.get("http://127.0.0.1:53468/redacted",
                    timeout=10000,
                    headers={
                        "X-Databricks-Metadata-Version": "1",
                        "X-Databricks-Host": "https://redacted.databricks.com/"
                    }, proxies={"no_proxy": "localhost,127.0.0.1"})

print(resp.text)
``` 
The code fails without the no_proxy option
  • Loading branch information
ilia-db authored Jan 30, 2025
1 parent 95277c8 commit aa4f0f3
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions databricks/sdk/credentials_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,12 +676,18 @@ def __init__(self, cfg: 'Config'):
self.host = cfg.host

def refresh(self) -> Token:
resp = requests.get(self.url,
timeout=self._metadata_service_timeout,
headers={
self.METADATA_SERVICE_VERSION_HEADER: self.METADATA_SERVICE_VERSION,
self.METADATA_SERVICE_HOST_HEADER: self.host
})
resp = requests.get(
self.url,
timeout=self._metadata_service_timeout,
headers={
self.METADATA_SERVICE_VERSION_HEADER: self.METADATA_SERVICE_VERSION,
self.METADATA_SERVICE_HOST_HEADER: self.host
},
proxies={
# Explicitly exclude localhost from being proxied. This is necessary
# for Metadata URLs which typically point to localhost.
"no_proxy": "localhost,127.0.0.1"
})
json_resp: dict[str, Union[str, float]] = resp.json()
access_token = json_resp.get("access_token", None)
if access_token is None:
Expand Down

0 comments on commit aa4f0f3

Please sign in to comment.