Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1c4ad9b

Browse filesBrowse files
authored
fix: prefer proxy password from env var (#468)
Fetch proxy password from /user/me using `ApifyEnvVars.TOKEN` only when not passed through the constructor or `ApifyEnvVars.PROXY_PASSWORD` is not provided.
1 parent 23fac2a commit 1c4ad9b
Copy full SHA for 1c4ad9b

File tree

Expand file treeCollapse file tree

2 files changed

+45
-18
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+45
-18
lines changed

‎src/apify/_proxy_configuration.py

Copy file name to clipboardExpand all lines: src/apify/_proxy_configuration.py
+15-18Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ def __init__(
178178
self._country_code = country_code
179179

180180
async def initialize(self) -> None:
181-
"""Load the Apify Proxy password if the API token is provided and check access to Apify Proxy and proxy groups.
181+
"""Check if using proxy, if so, check the access.
182+
183+
Load the Apify Proxy password from API (only if not passed to constructor or through env var).
182184
183185
Only called if Apify Proxy configuration is used. Also checks if country has access to Apify Proxy groups
184186
if the country code is provided.
@@ -187,7 +189,17 @@ async def initialize(self) -> None:
187189
`ProxyConfiguration` instance instead of calling this manually.
188190
"""
189191
if self._uses_apify_proxy:
190-
await self._maybe_fetch_password()
192+
if not self._password:
193+
await self._maybe_fetch_password()
194+
if not self._password:
195+
raise ValueError(
196+
'Apify Proxy password must be provided using the "password" constructor argument '
197+
f'or the "{ApifyEnvVars.PROXY_PASSWORD}" environment variable. '
198+
f'You can also provide your Apify token via the "${ApifyEnvVars.TOKEN}" environment variable, '
199+
f'so that the SDK can fetch the proxy password from Apify API, '
200+
f'when not provided through constructor or ${ApifyEnvVars.PROXY_PASSWORD}.'
201+
)
202+
191203
await self._check_access()
192204

193205
async def new_proxy_info(
@@ -255,22 +267,7 @@ async def _maybe_fetch_password(self) -> None:
255267
user_info = await self._apify_client.user().get()
256268
if user_info:
257269
password = user_info['proxy']['password']
258-
259-
if self._password:
260-
if self._password != password:
261-
logger.warning(
262-
'The Apify Proxy password you provided belongs to a different user than the Apify '
263-
'token you are using. Are you sure this is correct?'
264-
)
265-
else:
266-
self._password = password
267-
268-
if not self._password:
269-
raise ValueError(
270-
'Apify Proxy password must be provided using the "password" constructor argument '
271-
f'or the "{ApifyEnvVars.PROXY_PASSWORD}" environment variable. If you add '
272-
f'the "{ApifyEnvVars.TOKEN}" environment variable, the password will be automatically inferred.'
273-
)
270+
self._password = password
274271

275272
async def _check_access(self) -> None:
276273
proxy_status_url = f'{self._configuration.proxy_status_url}/?format=json'

‎tests/unit/test_proxy_configuration.py

Copy file name to clipboardExpand all lines: tests/unit/test_proxy_configuration.py
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,36 @@ async def test_initialize_with_manual_password(monkeypatch: pytest.MonkeyPatch,
432432
assert proxy_configuration.is_man_in_the_middle is False
433433

434434

435+
async def test_initialize_prefering_password_from_env_over_calling_api(
436+
monkeypatch: pytest.MonkeyPatch,
437+
respx_mock: MockRouter,
438+
patched_apify_client: ApifyClientAsync,
439+
) -> None:
440+
dummy_proxy_status_url = 'http://dummy-proxy-status-url.com'
441+
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
442+
monkeypatch.setenv(ApifyEnvVars.PROXY_PASSWORD.value, DUMMY_PASSWORD)
443+
444+
respx_mock.get(dummy_proxy_status_url).mock(
445+
httpx.Response(
446+
200,
447+
json={
448+
'connected': True,
449+
'connectionError': None,
450+
'isManInTheMiddle': False,
451+
},
452+
)
453+
)
454+
455+
proxy_configuration = ProxyConfiguration()
456+
457+
await proxy_configuration.initialize()
458+
459+
assert proxy_configuration._password == DUMMY_PASSWORD
460+
assert proxy_configuration.is_man_in_the_middle is False
461+
462+
assert len(patched_apify_client.calls['user']['get']) == 0 # type: ignore[attr-defined]
463+
464+
435465
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
436466
async def test_initialize_with_manual_password_different_than_user_one(
437467
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.