Skip to content

Commit 3961c2e

Browse files
committed
Enable HTTP Proxies for iControlRESTTokenAuth
Issues: Fixes #172 Problem: The `proxies` attribute is available on the `iControlRESTSession` object, however it does not get passed to the `iControlRESTTokenAuth` object. As a result, if all communication with a BigIP device needs to take place via a Proxy, authentication will fail and timeout. Analysis: I added `proxies` as a Kwarg on the `iControlRESTTokenAuth` object. I then passed that argument in from the `iControlRESTSession` object instantiations of the `iControlRESTTokenAuth` object. I then added `proxies=self.proxies` to the two instances of `requests.<verb>` in the `iControlRESTTokenAuth` object. Tests: I am unable to run the tests as listed in the documentation as even in 1.0 branch, `py.test --cov ./ --cov-report=html` returns: `argparse.ArgumentError: argument --bigip: conflicting option string: --bigip` I did successfully test with and without proxies, by installing my fork/branch and testing against a live BigIP With Proxies: In [1]: paste from f5.bigip import ManagementRoot from getpass import getpass password = getpass() user = "test_user" host = "test_f5.local" mah_f5 = ManagementRoot(hostname=host, username=user, password=password, proxies={"https": "socks5://127.0.0.1:8000"}, auth_provider="tmos") mah_f5.tmos_version ## -- End pasted text -- Password: Out[1]: '14.1.2.3' Without Proxies: In [1]: paste from f5.bigip import ManagementRoot from getpass import getpass password = getpass() user = "test_user" host = "localhost" port = 30002 mah_f5 = ManagementRoot(hostname=host, port=port, username=user, password=password, auth_provider="tmos") mah_f5.tmos_version ## -- End pasted text -- Password: Out[1]: '14.1.2.3'
1 parent cd1ab32 commit 3961c2e

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

icontrol/authtoken.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class iControlRESTTokenAuth(AuthBase):
5555
BigIP should consult when creating the token.
5656
:param str verify: The path to a CA bundle containing the \
5757
CA certificate for SSL validation
58+
:param proxies: A dict of proxy information for Requests to utilize \
59+
on this connection to the BigIP
5860
5961
If ``username`` is configured locally on the BigIP,
6062
``login_provider_name`` should be ``"tmos"`` (default). Otherwise
@@ -63,10 +65,11 @@ class iControlRESTTokenAuth(AuthBase):
6365
of ``login_provider_name``.
6466
"""
6567
def __init__(self, username, password, login_provider_name='tmos',
66-
verify=False, auth_provider=None):
68+
verify=False, auth_provider=None, proxies=None):
6769
self.username = username
6870
self.password = password
6971
self.login_provider_name = login_provider_name
72+
self.proxies = proxies
7073
self.token = None
7174
self.expiration = None
7275
self.attempts = 0
@@ -95,7 +98,7 @@ def get_auth_providers(self, netloc):
9598
"""
9699
url = "https://%s/info/system?null" % (netloc)
97100

98-
response = requests.get(url, verify=self.verify)
101+
response = requests.get(url, verify=self.verify, proxies=self.proxies)
99102
if not response.ok or not hasattr(response, "json"):
100103
error_message = '%s Unexpected Error: %s for uri: %s\nText: %r' %\
101104
(response.status_code,
@@ -147,7 +150,8 @@ def get_new_token(self, netloc):
147150
login_url,
148151
json=login_body,
149152
verify=self.verify,
150-
auth=HTTPBasicAuth(self.username, self.password)
153+
auth=HTTPBasicAuth(self.username, self.password),
154+
proxies=self.proxies,
151155
)
152156
self.attempts += 1
153157
if not response.ok or not hasattr(response, "json"):

icontrol/session.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,22 +428,22 @@ def __init__(self, username, password, **kwargs):
428428

429429
# Handle token-based auth.
430430
if token_to_use:
431-
self.session.auth = iControlRESTTokenAuth('admin', 'admin')
431+
self.session.auth = iControlRESTTokenAuth('admin', 'admin', proxies=proxies)
432432
self.session.auth.token = token_to_use
433433
else:
434434
if auth_provider:
435435
self.session.auth = iControlRESTTokenAuth(
436-
username, password, auth_provider=auth_provider, verify=verify
436+
username, password, auth_provider=auth_provider, verify=verify, proxies=proxies
437437
)
438438
else:
439439
if token_auth is True:
440440
self.session.auth = iControlRESTTokenAuth(
441-
username, password, verify=verify
441+
username, password, verify=verify, proxies=proxies
442442
)
443443
elif token_auth:
444444
# Truthy but not true: non-default loginAuthProvider
445445
self.session.auth = iControlRESTTokenAuth(
446-
username, password, token_auth, verify=verify
446+
username, password, token_auth, verify=verify, proxies=proxies
447447
)
448448
else:
449449
self.session.auth = (username, password)

0 commit comments

Comments
 (0)