def do_exec(self, command_str): """Execute command""" re = None try: if command_str is not None: self.connect() re = self.exec_command(command_str) except paramiko.AuthenticationException as ae: LOG.error('doexec Authentication error:{}'.format(ae)) raise exception.InvalidUsernameOrPassword() except Exception as e: LOG.error('doexec InvalidUsernameOrPassword error:{}'.format(e)) if 'WSAETIMEDOUT' in str(e): raise exception.SSHConnectTimeout() elif 'No authentication methods available' in str(e) \ or 'Authentication failed' in str(e): raise exception.InvalidUsernameOrPassword() elif 'not a valid RSA private key file' in str(e): raise exception.InvalidPrivateKey() elif 'not found in known_hosts' in str(e): raise exception.SSHNotFoundKnownHosts(self.ssh_host) else: raise exception.SSHException() finally: self.close() return re
def do_exec(command_str, ssh): result = None try: 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('doexec InvalidUsernameOrPassword error') if 'timed out' in err: raise exception.SSHConnectTimeout() 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() else: raise exception.SSHException(err) return result
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() else: raise exception.SSHException(err) if 'invalid command name' in result or 'login failed' in result or\ 'is not a recognized command' in result: raise exception.StorageBackendException(result) return result
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 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: data = { 'request': { 'params': { "username": self.rest_username, "password": cryptor.decode(self.rest_password) } } } with self.session_lock: if self.session is None: self.init_http_head() res = self.call_with_token( RestHandler.REST_TOKEN_URL, data, 'POST') if res.status_code == 200: result = res.json() self.session.headers['X-Auth-Token'] = \ cryptor.encode(result.get('token').get('token')) else: LOG.error("Login error. URL: %(url)s,Reason: %(reason)s.", {"url": RestHandler.REST_TOKEN_URL, "reason": res.text}) if 'Authentication has failed' in res.text: raise exception.InvalidUsernameOrPassword() else: raise exception.StorageBackendException(res.text) except Exception as e: LOG.error("Login error: %s", six.text_type(e)) raise e finally: data = None
def login(self): try: data = {} self.init_http_head() self.session.headers.update({ "username": self.rest_username, "password": cryptor.decode(self.rest_password) }) res = self.do_call(consts.REST_AUTH_URL, data, 'GET') if res.status_code != 200: LOG.error("Login error. URL: %(url)s\n" "Reason: %(reason)s.", { "url": consts.REST_AUTH_URL, "reason": res.text }) if 'User authentication failed' in res.text: raise exception.InvalidUsernameOrPassword() else: raise exception.StorageBackendException( six.text_type(res.text)) except Exception as e: LOG.error("Login error: %s", six.text_type(e)) raise e
def login(self): try: access_session = self.rest_auth_token if self.rest_auth_token is None: url = RestHandler.REST_AUTH_URL data = {} self.init_rest_client() res = self. \ do_call(url, data, 'GET', calltimeout=consts.SOCKET_TIMEOUT) if res.status_code == 200: access_session = res.headers['EMC-CSRF-TOKEN'] self.rest_auth_token = access_session self.session.headers[ RestHandler.REST_AUTH_KEY] = access_session else: LOG.error( "Login error. URL: %(url)s\n" "Reason: %(reason)s.", { "url": url, "reason": res.text }) if 'invalid username or password' in res.text: raise exception.InvalidUsernameOrPassword() else: raise exception.BadResponse(res.text) else: LOG.error('Login Parameter error') return access_session except Exception as e: LOG.error("Login error: %s", six.text_type(e)) raise e
def do_exec_shell(self, command_list, exe_time): 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 + '\r\n') time.sleep(exe_time) channel.send("exit" + "\r\n") channel.close() while True: resp = channel.recv(9999).decode('utf8') if not resp: time.sleep(exe_time) break result += resp if 'is not a recognized command' in result \ or 'Unknown command' in result: raise exception.StorageBackendException(result) 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 login(self): """Login Hpe3par storage array.""" try: access_session = self.rest_client.rest_auth_token if self.rest_client.san_address: url = RestHandler.REST_AUTH_URL data = { "user": self.rest_client.rest_username, "password": cryptor.decode(self.rest_client.rest_password) } self.session_lock.acquire() if self.rest_client.rest_auth_token is not None: return self.rest_client.rest_auth_token self.rest_client.init_http_head() res = self.rest_client. \ do_call(url, data, 'POST', calltimeout=consts.SOCKET_TIMEOUT) if res is None: LOG.error('Login res is None') raise exception.InvalidResults('res is None') if res.status_code == consts. \ LOGIN_SUCCESS_STATUS_CODES: result = res.json() access_session = result.get('key') self.rest_client.rest_auth_token = access_session self.rest_client.session.headers[ RestHandler.REST_AUTH_KEY] = access_session else: LOG.error( "Login error. URL: %(url)s\n" "Reason: %(reason)s.", { "url": url, "reason": res.text }) if 'invalid username or password' in res.text: raise exception.InvalidUsernameOrPassword() else: raise exception.BadResponse(res.text) else: LOG.error('Login Parameter error') return access_session except Exception as e: LOG.error("Login error: %s", six.text_type(e)) raise e finally: self.session_lock.release()
def login(self): """Login Hpe3par storage array.""" access_session = self.REST_AUTH_TOKEN if access_session is None: if self.san_address: url = RestClient.REST_AUTH_URL data = {"user": self.rest_username, "password": self.rest_password } self.init_http_head() res = self.do_call(url, data, 'POST', calltimeout=consts.SOCKET_TIMEOUT) if res is not None: # check login status 201 if res.status_code == consts.LOGIN_SUCCESS_STATUS_CODES: result = res.json() access_session = result.get('key') self.REST_AUTH_TOKEN = access_session # set taken in the header, # key is X-HP3PAR-WSAPI-SessionKey self.session.headers[ RestClient.REST_AUTH_KEY] = access_session else: LOG.error("Login error. URL: %(url)s\n" "Reason: %(reason)s.", {"url": url, "reason": res.text}) if 'invalid username or password' in res.text: raise exception.InvalidUsernameOrPassword() else: raise exception.Invalid() else: LOG.error('login res is None') raise exception.InvalidResults('res is None') else: LOG.error('login Parameter error') else: LOG.error( "No login required!self.access_session have value=={}".format( access_session)) if access_session is None: msg = _("Failed to login with all rest URLs.") LOG.error(msg) raise exception.BadRequest(reason=msg) return access_session
def get_token(self): try: succeed = False if self.san_address: url = '%s/%s/sessions' % \ (RestHandler.COMM_URL, self.storage_device_id) data = {} with self.session_lock: if self.session is None: self.init_http_head() self.session.auth = \ requests.auth.HTTPBasicAuth( self.rest_username, cryptor.decode(self.rest_password)) res = self.call_with_token(url, data, 'POST', 30) if res.status_code == 200: succeed = True result = res.json() self.session_id = cryptor.encode( result.get('sessionId')) access_session = 'Session %s' % result.get('token') self.session.headers[ RestHandler.AUTH_KEY] = cryptor.encode( access_session) else: LOG.error( "Login error. URL: %(url)s\n" "Reason: %(reason)s.", { "url": url, "reason": res.text }) if 'authentication failed' in res.text: raise exception.InvalidUsernameOrPassword() elif 'KART30005-E' in res.text: raise exception.StorageBackendException( six.text_type(res.text)) else: raise exception.BadResponse(res.text) else: LOG.error('Token Parameter error') return succeed except Exception as e: LOG.error("Get token error: %s", six.text_type(e)) raise e
def login(self): try: res = self.get_rest_info(consts.REST_AUTH_LOGIN, 'login', 'GET') if res: self.rest_auth_token = res else: LOG.error("Login error. URL: %(url)s\n" "Reason: %(reason)s.", { "url": consts.REST_AUTH_LOGIN, "reason": res.text }) if 'User authentication failed' in res.text: raise exception.InvalidUsernameOrPassword() else: raise exception.StorageBackendException( six.text_type(res.text)) except Exception as e: LOG.error("Login error: %s", six.text_type(e)) raise exception.InvalidResults(e)
def get_unisphere_version(self): """Get the unisphere version from the server. :returns: version dict """ post_90_endpoint = '/version' pre_91_endpoint = '/system/version' status_code, version_dict = self.request( post_90_endpoint, GET, timeout=VERSION_GET_TIME_OUT) if status_code is not STATUS_200: status_code, version_dict = self.request( pre_91_endpoint, GET, timeout=VERSION_GET_TIME_OUT) if status_code == STATUS_401: raise exception.InvalidUsernameOrPassword() if not version_dict: LOG.error("Unisphere version info not found.") return version_dict
def login(self): try: data = { 'username': self.rest_username, 'password': cryptor.decode(self.rest_password) } self.init_http_head() token_res = self.do_call(RestHandler.REST_AUTH_URL, data, method='POST') if token_res.json().get('msg') == consts.LOGIN_PASSWORD_ERR: LOG.error( "Login error, Obtaining the token is abnormal. " "status_code:%s, URL: %s", token_res.status_code, RestHandler.REST_AUTH_URL) raise exception.InvalidUsernameOrPassword( 'Obtaining the token is abnormal') if token_res.status_code != consts.SUCCESS_STATUS_CODE or not \ token_res.json().get('api_token'): LOG.error( "Login error, Obtaining the token is abnormal. " "status_code:%s, URL: %s", token_res.status_code, RestHandler.REST_AUTH_URL) raise exception.StorageBackendException( 'Obtaining the token is abnormal') session_res = self.do_call(RestHandler.REST_SESSION_URL, token_res.json(), method='POST') if session_res.status_code != consts.SUCCESS_STATUS_CODE or not \ session_res.json().get('username'): LOG.error( "Login error, Obtaining the session is abnormal." "status_code:%s, URL: %s", session_res.status_code, RestHandler.REST_SESSION_URL) raise exception.StorageBackendException( 'Obtaining the session is abnormal.') except Exception as e: LOG.error("Login error: %s", six.text_type(e)) raise e finally: data = None token_res = None