def do_call(self, url, data, method, calltimeout=SOCKET_TIMEOUT): if 'http' not in url: if self.san_address: url = '%s%s' % (self.san_address, url) kwargs = {'timeout': calltimeout} if data: kwargs['data'] = json.dumps(data) if method in ('POST', 'PUT', 'GET', 'DELETE'): func = getattr(self.session, method.lower()) else: msg = _("Request method %s is invalid.") % method LOG.error(msg) raise exception.StorageBackendException(msg) res = None try: res = func(url, **kwargs) except requests.exceptions.ConnectTimeout as ct: LOG.error('Connect Timeout err: {}'.format(ct)) raise exception.InvalidIpOrPort() except requests.exceptions.ReadTimeout as rt: LOG.error('Read timed out err: {}'.format(rt)) raise exception.StorageBackendException(six.text_type(rt)) except requests.exceptions.SSLError as e: LOG.error('SSLError for %s %s' % (method, url)) err_str = six.text_type(e) if 'certificate verify failed' in err_str: raise exception.SSLCertificateFailed() else: raise exception.SSLHandshakeFailed() except Exception as err: LOG.exception( 'Bad response from server: %(url)s.' ' Error: %(err)s', { 'url': url, 'err': err }) if 'WSAETIMEDOUT' in str(err): raise exception.ConnectTimeout() elif 'Failed to establish a new connection' in str(err): LOG.error('Failed to establish: {}'.format(err)) raise exception.InvalidIpOrPort() elif 'Read timed out' in str(err): raise exception.StorageBackendException(six.text_type(err)) else: raise exception.BadResponse() return res
def create(self): ssh = paramiko.SSHClient() try: if self.ssh_pub_key is None: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) else: host_key = '%s %s %s' % \ (self.ssh_host, self.ssh_pub_key_type, self.ssh_pub_key) self.set_host_key(host_key, ssh) ssh.connect(hostname=self.ssh_host, port=self.ssh_port, username=self.ssh_username, password=cryptor.decode(self.ssh_password), timeout=self.ssh_conn_timeout) if self.conn_timeout: transport = ssh.get_transport() transport.set_keepalive(self.SOCKET_TIMEOUT) return ssh except Exception as e: err = six.text_type(e) LOG.error('doexec InvalidUsernameOrPassword error') if 'timed out' in err: raise exception.InvalidIpOrPort() elif 'No authentication methods available' in err \ or 'Authentication failed' in err: raise exception.InvalidUsernameOrPassword() elif 'not a valid RSA private key file' in err: raise exception.InvalidPrivateKey() elif 'not found in known_hosts' in err: raise exception.SSHNotFoundKnownHosts(self.ssh_host) else: raise exception.SSHException(err)
def do_exec(self, command_str): result = '' try: with self.item() as ssh: utils.check_ssh_injection(command_str) if command_str is not None and ssh is not None: stdin, stdout, stderr = ssh.exec_command(command_str) res, err = stdout.read(), stderr.read() re = res if res else err result = re.decode() except paramiko.AuthenticationException as ae: LOG.error('doexec Authentication error:{}'.format(ae)) raise exception.InvalidUsernameOrPassword() except Exception as e: err = six.text_type(e) LOG.error(err) if 'timed out' in err \ or 'SSH connect timeout' in err\ or 'Unable to connect to port' in err: raise exception.ConnectTimeout() elif 'No authentication methods available' in err \ or 'Authentication failed' in err \ or 'Invalid username or password' in err: raise exception.InvalidUsernameOrPassword() elif 'not a valid RSA private key file' in err \ or 'not a valid RSA private key' in err: raise exception.InvalidPrivateKey() elif 'invalid command name' in err: raise exception.InvalidIpOrPort() elif 'login failed' in err: raise exception.StorageBackendException() else: raise exception.SSHException(err) return result
def login(self): """Login dell_emc unity storage array.""" try: with self.session_lock: data = {} if self.session is None: self.init_http_head() self.session.headers.update({"X-EMC-REST-CLIENT": "true"}) self.session.auth = requests.auth.HTTPBasicAuth( self.rest_username, cryptor.decode(self.rest_password)) res = self.call_with_token(RestHandler.REST_AUTH_URL, data, 'GET') if res.status_code == 200: self.session.headers[RestHandler.AUTH_KEY] = \ cryptor.encode(res.headers[RestHandler.AUTH_KEY]) else: LOG.error("Login error.URL: %s,Reason: %s.", RestHandler.REST_AUTH_URL, res.text) if 'Unauthorized' in res.text: raise exception.InvalidUsernameOrPassword() elif 'Forbidden' in res.text: raise exception.InvalidIpOrPort() else: raise exception.BadResponse(res.text) except Exception as e: LOG.error("Login error: %s", six.text_type(e)) raise e
def login(self): try: result = self.ssh_do_exec(['cluster-show -y']) if 'EVS' not in result: raise exception.InvalidIpOrPort() except Exception as e: LOG.error("Failed to login hnas %s" % (six.text_type(e))) raise e
def login(self): try: result = self.ssh_do_exec('cluster identity show') if 'is not a recognized command' in result \ or 'command not found' in result: raise exception.InvalidIpOrPort() except Exception as e: LOG.error("Failed to login netapp %s" % (six.text_type(e))) raise e
def login(self): try: with self.ssh_pool.item() as ssh: result = SSHHandler.do_exec('lssystem', ssh) if 'is not a recognized command' in result: raise exception.InvalidIpOrPort() except Exception as e: LOG.error("Failed to login ibm storwize_svc %s" % (six.text_type(e))) raise e
def do_exec_shell(self, command_list): result = '' try: with self.item() as ssh: if command_list and ssh: channel = ssh.invoke_shell() for command in command_list: utils.check_ssh_injection(command) channel.send(command + '\n') time.sleep(0.5) channel.send("exit" + "\n") channel.close() while True: resp = channel.recv(9999).decode('utf8') if not resp: break result += resp if 'is not a recognized command' in result \ or 'Unknown command' in result: raise exception.InvalidIpOrPort() except paramiko.AuthenticationException as ae: LOG.error('doexec Authentication error:{}'.format(ae)) raise exception.InvalidUsernameOrPassword() except Exception as e: err = six.text_type(e) LOG.error(err) if 'timed out' in err \ or 'SSH connect timeout' in err: raise exception.SSHConnectTimeout() elif 'No authentication methods available' in err \ or 'Authentication failed' in err \ or 'Invalid username or password' in err: raise exception.InvalidUsernameOrPassword() elif 'not a valid RSA private key file' in err \ or 'not a valid RSA private key' in err: raise exception.InvalidPrivateKey() elif 'Unable to connect to port' in err \ or 'Invalid ip or port' in err: raise exception.InvalidIpOrPort() else: raise exception.SSHException(err) return result
def init_connection(self, access_info): """ Given the access_info get a connection to VMAX storage """ try: ver, self.uni_version = self.rest.get_uni_version() LOG.info('Connected to Unisphere Version: {0}'.format(ver)) except exception.InvalidUsernameOrPassword as e: msg = "Failed to connect VMAX. Reason: {}".format(e.msg) LOG.error(msg) raise e except (exception.SSLCertificateFailed, exception.SSLHandshakeFailed) as e: msg = ("Failed to connect to VMAX: {}".format(e)) LOG.error(msg) raise except Exception as err: msg = ("Failed to connect to VMAX. Host or Port is not correct: " "{}".format(err)) LOG.error(msg) raise exception.InvalidIpOrPort() if not self.uni_version: msg = "Invalid input. Failed to get vmax unisphere version" raise exception.InvalidInput(msg) self.array_id = access_info.get('extra_attributes', {}). \ get('array_id', None) try: # Get array details from unisphere array = self.rest.get_array_detail(version=self.uni_version) if not array: msg = "Failed to get array details" raise exception.InvalidInput(msg) if len(array['symmetrixId']) == EMBEDDED_UNISPHERE_ARRAY_COUNT: if not self.array_id: self.array_id = array['symmetrixId'][0] elif self.array_id != array['symmetrixId'][0]: msg = "Invalid array_id. Expected id: {}". \ format(array['symmetrixId']) raise exception.InvalidInput(msg) except exception.SSLCertificateFailed: LOG.error('SSL certificate failed when init connection for VMax') raise except Exception as err: msg = "Failed to get array details from VMAX: {}".format(err) raise exception.StorageBackendException(msg) if not self.array_id: msg = "Input array_id is missing. Supported ids: {}". \ format(array['symmetrixId']) raise exception.InvalidInput(msg)
def init_connection(self, access_info): """ Given the access_info get a connection to VMAX storage """ try: ver, self.uni_version = self.rest.get_uni_version() LOG.info('Connected to Unisphere Version: {0}'.format(ver)) except exception.InvalidUsernameOrPassword as e: msg = "Failed to connect VMAX. Reason: {}".format(e.msg) LOG.error(msg) raise e except (exception.SSLCertificateFailed, exception.SSLHandshakeFailed) as e: msg = ("Failed to connect to VMAX: {}".format(e)) LOG.error(msg) raise except Exception as err: msg = ("Failed to connect to VMAX. Host or Port is not correct: " "{}".format(err)) LOG.error(msg) raise exception.InvalidIpOrPort() if not self.uni_version: msg = "Invalid input. Failed to get vmax unisphere version" raise exception.InvalidInput(msg)
def init_connection(self, access_info): """ Given the access_info get a connection to VMAX storage """ try: ver, self.uni_version = self.rest.get_uni_version() LOG.info('Connected to Unisphere Version: {0}'.format(ver)) except exception.InvalidUsernameOrPassword as e: msg = "Failed to connect VMAX. Reason: {}".format(e.msg) LOG.error(msg) raise e except (exception.SSLCertificateFailed, exception.SSLHandshakeFailed) as e: msg = ("Failed to connect to VMAX: {}".format(e)) LOG.error(msg) raise except Exception as err: msg = ("Failed to connect to VMAX. Host or Port is not correct: " "{}".format(err)) LOG.error(msg) raise exception.InvalidIpOrPort() if not self.uni_version: msg = "Invalid input. Failed to get vmax unisphere version" raise exception.InvalidInput(msg) self.array_id = access_info.get('extra_attributes', {}). \ get('array_id', None) try: # Get array details from unisphere array = self.rest.get_array_detail(version=self.uni_version) if not array: msg = "Failed to get array details" raise exception.InvalidInput(msg) if len(array['symmetrixId']) == EMBEDDED_UNISPHERE_ARRAY_COUNT: if not self.array_id: self.array_id = array['symmetrixId'][0] elif self.array_id != array['symmetrixId'][0]: msg = "Invalid array_id. Expected id: {}". \ format(array['symmetrixId']) raise exception.InvalidInput(msg) else: # Get first local array id array_ids = array.get('symmetrixId', list()) for array_id in array_ids: array_info = self.rest.get_array_detail( version=self.uni_version, array=array_id) if array_info.get('local'): LOG.info("Adding local VMAX array {}".format(array_id)) if not self.array_id: self.array_id = array_id break else: LOG.info( "Skipping remote VMAX array {}".format(array_id)) if not self.array_id: msg = "Failed to get VMAX array id from Unisphere" raise exception.InvalidInput(msg) except Exception: LOG.error("Failed to init_connection to VMAX") raise if not self.array_id: msg = "Input array_id is missing. Supported ids: {}". \ format(array['symmetrixId']) raise exception.InvalidInput(msg)