Skip to content

Commit c7987ba

Browse files
authored
added url decoding of proxy credentials (#111)
* added url decoding of proxy credentials * code was generated * Add unit test * test passworded url encoded
1 parent f39a1bd commit c7987ba

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
[PyPI History][1]
44
[1]: https://pypi.org/project/demisto-py/#history
55

6+
## 3.2.10
7+
* Added URL decoding of proxy url credentials.
8+
69
## 3.2.9
710
* Added the ability to add custom request headers.
811

demisto_client/demisto_api/rest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
try:
2929
import urllib3
30+
import urllib
3031
except ImportError:
3132
raise ImportError('Swagger python client requires urllib3.')
3233

@@ -88,7 +89,8 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
8889
proxy_headers = None
8990
parsed_proxy_url = urllib3.util.parse_url(configuration.proxy)
9091
if parsed_proxy_url.auth is not None:
91-
proxy_headers = urllib3.util.make_headers(proxy_basic_auth=parsed_proxy_url.auth)
92+
configuration.proxy_auth = urllib.parse.unquote(parsed_proxy_url.auth)
93+
proxy_headers = urllib3.util.make_headers(proxy_basic_auth=configuration.proxy_auth)
9294

9395
self.pool_manager = urllib3.ProxyManager(
9496
num_pools=pools_size,

gen-code.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mv README.md.org README.md
3232
sed -i "${INPLACE[@]}" -e 's/^from demisto_client.demisto_api.models.advance_arg import AdvanceArg/# &/' demisto_client/demisto_api/models/operator_argument.py
3333
sed -i "${INPLACE[@]}" -e 's/^from demisto_client.demisto_api.models.group import Group/# &/' demisto_client/demisto_api/models/groups.py
3434
sed -i "${INPLACE[@]}" -e 's/^from demisto_client.demisto_api.models.investigation_playbook import InvestigationPlaybook/# &/' demisto_client/demisto_api/models/investigation_playbook_task.py
35+
3536
# __api_call needs to work with dict objects and not Classes when updating data such as POST requests
3637
sed -i "${INPLACE[@]}" -e 's/config = self.configuration/&; body = demisto_client.to_extended_dict(body) # noqa: E702/' demisto_client/demisto_api/api_client.py
3738
# Update the docs
@@ -73,11 +74,14 @@ sed -i "${INPLACE[@]}" -e 's/"""Custom error messages for exception"""/"""Custom
7374
else:\
7475
sensitive_logging = False/' demisto_client/demisto_api/rest.py
7576

77+
sed -i "${INPLACE[@]}" -e 's/import urllib3/import urllib3\
78+
import urllib/' demisto_client/demisto_api/rest.py
7679
sed -i "${INPLACE[@]}" -e 's/if configuration.proxy:/if configuration.proxy:\
7780
proxy_headers = None\
7881
parsed_proxy_url = urllib3.util.parse_url(configuration.proxy)\
7982
if parsed_proxy_url.auth is not None:\
80-
proxy_headers = urllib3.util.make_headers(proxy_basic_auth=parsed_proxy_url.auth)\
83+
configuration.proxy_auth = urllib.parse.unquote(parsed_proxy_url.auth)\
84+
proxy_headers = urllib3.util.make_headers(proxy_basic_auth=configuration.proxy_auth)\
8185
/' demisto_client/demisto_api/rest.py
8286

8387
sed -i "${INPLACE[@]}" -e 's/proxy_url=configuration.proxy,/proxy_headers=proxy_headers,\

tests/mocks_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,51 @@ def run():
405405
assert_reset()
406406

407407

408+
class TestWithProxyBasicAuthentication(unittest.TestCase):
409+
410+
def test_generic_request(self):
411+
"""
412+
Given:
413+
- Request which use proxy authentication
414+
When:
415+
- Environment variable HTTP_PROXY and HTTPS_PROXY which contains a username and password with special characters
416+
Then:
417+
- Ensure the request made to the correct url via correct proxy username and password. Special character should be decoded.
418+
"""
419+
import sys
420+
# Error should be the same in both Py2 and Py3, but Py2 does not support unittest mock in
421+
# the same way
422+
if sys.version_info[0] > 2:
423+
from unittest import mock
424+
425+
@mock.patch.dict(os.environ, {"HTTP_PROXY": "http://user1:pass%21%23%24%25%5E@localhost:8080"})
426+
@mock.patch.dict(os.environ, {"HTTPS_PROXY": "http://user1:pass%21%23%24%25%5E@localhost:8080"})
427+
@responses.activate
428+
def run():
429+
responses.add('POST', 'http://localhost:8080/test',
430+
body="Good",
431+
status=200,
432+
content_type='text/plain')
433+
api_instance = demisto_client.configure(base_url=host, api_key=api_key, debug=False)
434+
435+
api_instance.generic_request('/test', 'POST',
436+
body="this is a test",
437+
content_type='text/plain',
438+
accept='text/plain')
439+
440+
assert len(responses.calls) == 1
441+
assert responses.calls[0].request.url == 'http://localhost:8080/test'
442+
assert responses.calls[0].request.host == 'localhost'
443+
assert responses.calls[0].request.scheme == 'http'
444+
assert api_instance.api_client.configuration.proxy_auth == 'user1:pass!#$%^'
445+
446+
else:
447+
def run():
448+
assert 1 == 1
449+
run()
450+
assert_reset()
451+
452+
408453
def test_import_incidentfields(mocker):
409454
"""
410455
Given:

0 commit comments

Comments
 (0)