Skip to content

Commit b07cec3

Browse files
authored
fix proxy handle in BaseClient (demisto#11950)
* fix proxy handle in BaseClient * update secrets * update secrets * handle proxy and verify be removing the env vars * extract proxy and cert verification to separate methods * fix lint
1 parent 110af2f commit b07cec3

5 files changed

Lines changed: 95 additions & 9 deletions

File tree

Packs/Base/.secrets-ignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@ spki_sha256='94b716aeda21cd661949cfbf3f55457a277da712cdce0ab31989a4f288fad9b9',
7878
"94b716aeda21cd661949cfbf3f55457a277da712cdce0ab31989a4f288fad9b9",
7979
2.23.140.1
8080
'53e6baa124f54462786f1122e98e38ff1be3de82fe2a96b1849a8637043fd847eec7e0f53307bddf7a066565292d500c36c941f1f3bb9dcac807b2f4a0bfce1b',
81-
http://proxy
81+
http://proxy
82+
http://testproxy
83+
https://testproxy

Packs/Base/ReleaseNotes/1_7_30.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#### Scripts
2+
##### CommonServerPython
3+
- Fixed an issue in **BaseClient**, which caused the request to ignore **REQUEST_CA_BUNDLE** when **proxy** was set to **false**.

Packs/Base/Scripts/CommonServerPython/CommonServerPython.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,22 +495,44 @@ def handle_proxy(proxy_param_name='proxy', checkbox_default_value=False, handle_
495495
'https': os.environ.get('HTTPS_PROXY') or os.environ.get('https_proxy', '')
496496
}
497497
else:
498-
for k in ('HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy'):
499-
if k in os.environ:
500-
del os.environ[k]
498+
skip_proxy()
499+
501500
if handle_insecure:
502501
if insecure_param_name is None:
503502
param_names = ('insecure', 'unsecure')
504503
else:
505504
param_names = (insecure_param_name,) # type: ignore[assignment]
506505
for p in param_names:
507506
if demisto.params().get(p, False):
508-
for k in ('REQUESTS_CA_BUNDLE', 'CURL_CA_BUNDLE'):
509-
if k in os.environ:
510-
del os.environ[k]
507+
skip_cert_verification()
508+
511509
return proxies
512510

513511

512+
def skip_proxy():
513+
"""
514+
The function deletes the proxy environment vars in order to http requests to skip routing through proxy
515+
516+
:return: None
517+
:rtype: ``None``
518+
"""
519+
for k in ('HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy'):
520+
if k in os.environ:
521+
del os.environ[k]
522+
523+
524+
def skip_cert_verification():
525+
"""
526+
The function deletes the self signed certificate env vars in order to http requests to skip certificate validation.
527+
528+
:return: None
529+
:rtype: ``None``
530+
"""
531+
for k in ('REQUESTS_CA_BUNDLE', 'CURL_CA_BUNDLE'):
532+
if k in os.environ:
533+
del os.environ[k]
534+
535+
514536
def urljoin(url, suffix=""):
515537
"""
516538
Will join url and its suffix
@@ -5444,7 +5466,10 @@ def __init__(self, base_url, verify=True, proxy=False, ok_codes=tuple(), headers
54445466
self._auth = auth
54455467
self._session = requests.Session()
54465468
if not proxy:
5447-
self._session.trust_env = False
5469+
skip_proxy()
5470+
5471+
if not verify:
5472+
skip_cert_verification()
54485473

54495474
def _implement_retry(self, retries=0,
54505475
status_list_to_retry=None,

Packs/Base/Scripts/CommonServerPython/CommonServerPython_test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,62 @@ def test_http_request_response(self, requests_mock):
20552055
res = self.client._http_request('get', 'event', resp_type='response')
20562056
assert isinstance(res, requests.Response)
20572057

2058+
def test_http_request_proxy_false(self):
2059+
from CommonServerPython import BaseClient
2060+
import requests_mock
2061+
2062+
os.environ['http_proxy'] = 'http://testproxy:8899'
2063+
os.environ['https_proxy'] = 'https://testproxy:8899'
2064+
2065+
os.environ['REQUESTS_CA_BUNDLE'] = '/test1.pem'
2066+
client = BaseClient('http://example.com/api/v2/', ok_codes=(200, 201), proxy=False, verify=True)
2067+
2068+
with requests_mock.mock() as m:
2069+
m.get('http://example.com/api/v2/event')
2070+
2071+
res = client._http_request('get', 'event', resp_type='response')
2072+
2073+
assert m.last_request.verify == '/test1.pem'
2074+
assert not m.last_request.proxies
2075+
assert m.called is True
2076+
2077+
def test_http_request_proxy_true(self):
2078+
from CommonServerPython import BaseClient
2079+
import requests_mock
2080+
2081+
os.environ['http_proxy'] = 'http://testproxy:8899'
2082+
os.environ['https_proxy'] = 'https://testproxy:8899'
2083+
2084+
os.environ['REQUESTS_CA_BUNDLE'] = '/test1.pem'
2085+
client = BaseClient('http://example.com/api/v2/', ok_codes=(200, 201), proxy=True, verify=True)
2086+
2087+
with requests_mock.mock() as m:
2088+
m.get('http://example.com/api/v2/event')
2089+
2090+
res = client._http_request('get', 'event', resp_type='response')
2091+
2092+
assert m.last_request.verify == '/test1.pem'
2093+
assert m.last_request.proxies == {
2094+
'http': 'http://testproxy:8899',
2095+
'https': 'https://testproxy:8899'
2096+
}
2097+
assert m.called is True
2098+
2099+
def test_http_request_verify_false(self):
2100+
from CommonServerPython import BaseClient
2101+
import requests_mock
2102+
2103+
os.environ['REQUESTS_CA_BUNDLE'] = '/test1.pem'
2104+
client = BaseClient('http://example.com/api/v2/', ok_codes=(200, 201), proxy=True, verify=False)
2105+
2106+
with requests_mock.mock() as m:
2107+
m.get('http://example.com/api/v2/event')
2108+
2109+
res = client._http_request('get', 'event', resp_type='response')
2110+
2111+
assert m.last_request.verify is False
2112+
assert m.called is True
2113+
20582114
def test_http_request_not_ok(self, requests_mock):
20592115
from CommonServerPython import DemistoException
20602116
requests_mock.get('http://example.com/api/v2/event', status_code=500)

Packs/Base/pack_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Base",
33
"description": "The base pack for Cortex XSOAR.",
44
"support": "xsoar",
5-
"currentVersion": "1.7.29",
5+
"currentVersion": "1.7.30",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",

0 commit comments

Comments
 (0)