Skip to content

Commit 734a530

Browse files
authored
Add trust_env in aiohttp ClientSession for improved proxy handling (permitio#277)
* Add trust_env in aiohttp ClientSession for improved proxy handling * Add NO_PROXY default environment variable to Dockerfile for local development * Enable trust_env in ClientSession for improved proxy handling * Updated opal-common and opal-client to version 0.8.2rc2 (for PDP v0.9.3-rc.1 release) * Update opal to version 0.8.2
1 parent 8eb57bf commit 734a530

File tree

7 files changed

+15
-7
lines changed

7 files changed

+15
-7
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ ENV PDP_HORIZON_HOST=0.0.0.0
167167
ENV PDP_HORIZON_PORT=7001
168168
ENV PDP_PORT=7000
169169
ENV PDP_PYTHON_PATH=python3
170+
ENV NO_PROXY=localhost,127.0.0.1,::1
170171

171172
# 7000 pdp port
172173
# 7001 horizon port

horizon/enforcer/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ async def post_to_opa(request: Request, path: str, data: dict | None):
206206
_set_use_debugger(data)
207207
try:
208208
logger.debug(f"calling OPA at '{url}' with input: {data}")
209-
async with aiohttp.ClientSession() as session: # noqa: SIM117
209+
async with aiohttp.ClientSession(trust_env=True) as session: # noqa: SIM117
210210
async with session.post(
211211
url,
212212
data=json.dumps(data) if data is not None else None,

horizon/facts/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def client(self) -> AsyncClient:
2727
self._client = AsyncClient(
2828
base_url=sidecar_config.CONTROL_PLANE,
2929
headers={"Authorization": f"Bearer {env_api_key}"},
30+
trust_env=True,
3031
)
3132
return self._client
3233

horizon/opal_relay_api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _apply_context(self, context: dict[str, str]):
9595
def api_session(self) -> ClientSession:
9696
if self._api_session is None:
9797
env_api_key = get_env_api_key()
98-
self._api_session = ClientSession(headers={"Authorization": f"Bearer {env_api_key}"})
98+
self._api_session = ClientSession(headers={"Authorization": f"Bearer {env_api_key}"}, trust_env=True)
9999
return self._api_session
100100

101101
async def relay_session(self) -> ClientSession:
@@ -133,7 +133,9 @@ async def relay_session(self) -> ClientSession:
133133
f"Server responded to token request with an invalid result: {text}",
134134
) from e
135135
self._relay_token = obj.token
136-
self._relay_session = ClientSession(headers={"Authorization": f"Bearer {self._relay_token}"})
136+
self._relay_session = ClientSession(
137+
headers={"Authorization": f"Bearer {self._relay_token}"}, trust_env=True
138+
)
137139
return self._relay_session
138140

139141
async def send_ping(self):

horizon/proxy/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ async def proxy_request_to_cloud_service(
200200

201201
logger.info(f"Proxying request: {request.method} {path}")
202202

203-
async with aiohttp.ClientSession() as session:
203+
async with aiohttp.ClientSession(
204+
trust_env=True,
205+
) as session:
204206
if request.method == HTTP_GET:
205207
async with session.get(path, headers=headers, params=params) as backend_response:
206208
return await proxy_response(backend_response)

horizon/state.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ async def _report(self, state: PersistentState | None = None):
198198
if state is not None:
199199
self._state = state.copy()
200200
config_url = f"{sidecar_config.CONTROL_PLANE}{sidecar_config.REMOTE_STATE_ENDPOINT}"
201-
async with aiohttp.ClientSession() as session:
201+
async with aiohttp.ClientSession(
202+
trust_env=True,
203+
) as session:
202204
logger.info("Reporting status update to server...")
203205
response = await session.post(
204206
url=config_url,

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ httpx>=0.27.0,<1
1414
# TODO: change to use re2 in the future, currently not supported in alpine due to c++ library issues
1515
# google-re2 # use re2 instead of re for regex matching because it's simiplier and safer for user inputted regexes
1616
protobuf>=3.20.2 # not directly required, pinned by Snyk to avoid a vulnerability
17-
opal-common==0.8.1
18-
opal-client==0.8.1
17+
opal-common==0.8.2
18+
opal-client==0.8.2

0 commit comments

Comments
 (0)