Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_send_raises_connection_socket_error(self):
mock_transport = Mock(name='transport')
mockcfg = {'getresponse.return_value.read.side_effect':
OSError('timeout')}
mock_transport.configure_mock(**mockcfg)
instance = pyeapi.eapilib.EapiConnection()
instance.transport = mock_transport
try:
instance.send('test')
except pyeapi.eapilib.ConnectionError as err:
error_msg = 'Socket error during eAPI connection: timeout'
self.assertEqual(err.message, error_msg)
def test_send_raises_autocomplete_command_error(self):
message = "runCmds() got an unexpected keyword argument 'autoComplete'"
error = dict(code=9999, message=message, data=[{'errors': ['test']}])
response_dict = dict(jsonrpc='2.0', error=error, id=id(self))
response_json = json.dumps(response_dict)
mock_transport = Mock(name='transport')
mockcfg = {'getresponse.return_value.read.return_value': response_json}
mock_transport.configure_mock(**mockcfg)
instance = pyeapi.eapilib.EapiConnection()
instance.transport = mock_transport
try:
instance.send('test')
except pyeapi.eapilib.CommandError as error:
match = ("autoComplete parameter is not supported in this version"
" of EOS.")
self.assertIn(match, error.message)
def test_request_adds_expandaliases(self):
instance = pyeapi.eapilib.EapiConnection()
request = instance.request(['test'], encoding='json',
expandAliases=True)
data = json.loads(request)
self.assertIn('expandAliases', data['params'])
def test_send_raises_expandaliases_command_error(self):
message = "runCmds() got an unexpected keyword argument" \
" 'expandAliases'"
error = dict(code=9999, message=message, data=[{'errors': ['test']}])
response_dict = dict(jsonrpc='2.0', error=error, id=id(self))
response_json = json.dumps(response_dict)
mock_transport = Mock(name='transport')
mockcfg = {'getresponse.return_value.read.return_value': response_json}
mock_transport.configure_mock(**mockcfg)
instance = pyeapi.eapilib.EapiConnection()
instance.transport = mock_transport
try:
instance.send('test')
except pyeapi.eapilib.CommandError as error:
match = ("expandAliases parameter is not supported in this version"
" of EOS.")
self.assertIn(match, error.message)
def test_execute_raises_connection_error(self):
mock_send = Mock(name='send')
mock_send.side_effect = pyeapi.eapilib.ConnectionError('test', 'test')
instance = pyeapi.eapilib.EapiConnection()
instance.send = mock_send
with self.assertRaises(pyeapi.eapilib.ConnectionError):
instance.execute('test')
path = path or DEFAULT_HTTP_PATH
self.transport = HttpConnection(path, 'localhost', int(port),
timeout=timeout)
class HttpEapiConnection(EapiConnection):
def __init__(self, host, port=None, path=None, username=None,
password=None, timeout=60, **kwargs):
super(HttpEapiConnection, self).__init__()
port = port or DEFAULT_HTTP_PORT
path = path or DEFAULT_HTTP_PATH
self.transport = HttpConnection(path, host, int(port), timeout=timeout)
self.authentication(username, password)
class HttpsEapiConnection(EapiConnection):
def __init__(self, host, port=None, path=None, username=None,
password=None, context=None, timeout=60, **kwargs):
super(HttpsEapiConnection, self).__init__()
port = port or DEFAULT_HTTPS_PORT
path = path or DEFAULT_HTTP_PATH
enforce_verification = kwargs.get('enforce_verification')
if context is None and not enforce_verification:
context = self.disable_certificate_verification()
self.transport = https_connection_factory(path, host, int(port),
context, timeout)
self.authentication(username, password)
def disable_certificate_verification(self):
return response
except(ConnectionError, CommandError, TypeError) as exc:
exc.commands = commands
self.error = exc
raise
class SocketEapiConnection(EapiConnection):
def __init__(self, path=None, timeout=60, **kwargs):
super(SocketEapiConnection, self).__init__()
path = path or DEFAULT_UNIX_SOCKET
self.transport = SocketConnection(path, timeout)
class HttpLocalEapiConnection(EapiConnection):
def __init__(self, port=None, path=None, timeout=60, **kwargs):
super(HttpLocalEapiConnection, self).__init__()
port = port or DEFAULT_HTTP_LOCAL_PORT
path = path or DEFAULT_HTTP_PATH
self.transport = HttpConnection(path, 'localhost', int(port),
timeout=timeout)
class HttpEapiConnection(EapiConnection):
def __init__(self, host, port=None, path=None, username=None,
password=None, timeout=60, **kwargs):
super(HttpEapiConnection, self).__init__()
port = port or DEFAULT_HTTP_PORT
path = path or DEFAULT_HTTP_PATH
self.transport = HttpConnection(path, host, int(port), timeout=timeout)
self.authentication(username, password)
def disable_certificate_verification(self):
# SSL/TLS certificate verification is enabled by default in latest
# Python releases and causes self-signed certificates generated
# on EOS to fail validation (unless explicitly imported).
# Disable the SSL/TLS certificate verification for now.
# Use the approach in PEP476 to disable certificate validation.
# TODO:
# ************************** WARNING *****************************
# This behaviour is considered a *security risk*, so use it
# temporary until a proper fix is implemented.
if hasattr(ssl, '_create_unverified_context'):
return ssl._create_unverified_context()
class HttpsEapiCertConnection(EapiConnection):
def __init__(self, host, port=None, path=None, key_file=None,
cert_file=None, ca_file=None, timeout=60, **kwargs):
if key_file is None or cert_file is None:
raise ValueError("For https_cert connections both a key_file and "
"cert_file are required. A ca_file is also "
"recommended")
super(HttpsEapiCertConnection, self).__init__()
port = port or DEFAULT_HTTPS_PORT
path = path or DEFAULT_HTTP_PATH
self.transport = HTTPSCertConnection(path, host, int(port),
key_file=key_file,
cert_file=cert_file,
ca_file=ca_file, timeout=timeout)