diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py index d4c174a..538bb3a 100644 --- a/web/pgadmin/__init__.py +++ b/web/pgadmin/__init__.py @@ -124,6 +124,7 @@ def create_app(app_name=config.APP_NAME): # Removes unwanted whitespace from render_template function app.jinja_env.trim_blocks = True app.config.from_object(config) + app.config.update(dict(PROPAGATE_EXCEPTIONS=True)) ########################################################################## # Setup session management diff --git a/web/pgadmin/browser/collection.py b/web/pgadmin/browser/collection.py index 97f58b6..003184e 100644 --- a/web/pgadmin/browser/collection.py +++ b/web/pgadmin/browser/collection.py @@ -85,7 +85,8 @@ class CollectionNodeModule(PgAdminModule, PGChildModule): "_type": 'coll-%s' % (self.node_type), "_id": parent_id, "_pid": parent_id, - "module": 'pgadmin.node.%s' % self.node_type + "module": 'pgadmin.node.%s' % self.node_type, + "subnodes": sorted([m.node_type for m in self.submodules]) } for key in kwargs: diff --git a/web/pgadmin/browser/server_groups/servers/__init__.py b/web/pgadmin/browser/server_groups/servers/__init__.py index c12c0b3..dd4921d 100644 --- a/web/pgadmin/browser/server_groups/servers/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/__init__.py @@ -719,8 +719,16 @@ class ServerNode(PGChildNodeView): from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() + res = conn.connected() - return make_json_response(data={'connected': conn.connected()}) + if res: + from pgadmin.utils.exception import ConnectionLost + try: + conn.execute_scalar('SELECT 1') + except ConnectionLost: + res = False + + return make_json_response(data={'connected': res}) def connect(self, gid, sid): """ @@ -759,8 +767,14 @@ class ServerNode(PGChildNodeView): password = None save_password = False + # Connect the Server + from pgadmin.utils.driver import get_driver + manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) + conn = manager.connection() + if 'password' not in data: - if server.password is None: + conn_passwd = getattr(conn, 'password', None) + if conn_passwd is None and server.password is None: # Return the password template in case password is not # provided, or password has not been saved earlier. return make_json_response( @@ -779,19 +793,15 @@ class ServerNode(PGChildNodeView): data['save_password'] if password and \ 'save_password' in data else False - # Encrypt the password before saving with user's login password key. - try: - password = encrypt(password, user.password) \ - if password is not None else server.password - except Exception as e: - current_app.logger.exception(e) - return internal_server_error(errormsg=e.message) - - # Connect the Server - from pgadmin.utils.driver import get_driver - manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) - conn = manager.connection() + # Encrypt the password before saving with user's login password key. + try: + password = encrypt(password, user.password) \ + if password is not None else server.password + except Exception as e: + current_app.logger.exception(e) + return internal_server_error(errormsg=e.message) + status = True try: status, errmsg = conn.connect( password=password, @@ -799,13 +809,18 @@ class ServerNode(PGChildNodeView): ) except Exception as e: current_app.logger.exception(e) - # TODO:: - # Ask the password again (if existing password couldn't be - # descrypted) - if e.message: - return internal_server_error(errormsg=e.message) - else: - return internal_server_error(errormsg=str(e)) + + return make_json_response( + success=0, + status=401, + result=render_template( + 'servers/password.html', + server_label=server.name, + username=server.username, + errmsg=e.message if e.message else str(e), + _=gettext + ) + ) if not status: current_app.logger.error( diff --git a/web/pgadmin/browser/server_groups/servers/templates/servers/sql/9.1_plus/stats.sql b/web/pgadmin/browser/server_groups/servers/templates/servers/sql/9.1_plus/stats.sql index f8d4a6a..54447ba 100644 --- a/web/pgadmin/browser/server_groups/servers/templates/servers/sql/9.1_plus/stats.sql +++ b/web/pgadmin/browser/server_groups/servers/templates/servers/sql/9.1_plus/stats.sql @@ -1,8 +1,8 @@ SELECT - procpid AS "{{ conn|qtIdent('PID') }}, - usename AS "{{ conn|qtIdent(_('User')|safe) }}, - datname AS "{{ conn|qtIdent(_('Database')|safe) }}, - backend_start AS "{{ conn|qtIdent(_('Backend start')|safe) }}, + procpid AS {{ conn|qtIdent('PID') }}, + usename AS {{ conn|qtIdent(_('User')) }}, + datname AS {{ conn|qtIdent(_('Database')) }}, + backend_start AS {{ conn|qtIdent(_('Backend start')) }}, CASE WHEN client_hostname IS NOT NULL AND client_hostname != '' THEN client_hostname || ':' || client_port @@ -12,22 +12,22 @@ SELECT 'local pipe' ELSE 'localhost:' || client_port - END AS "{{ conn|qtIdent(_('Client')|safe) }}, - application_name AS "{{ conn|qtIdent(_('Application')|safe) }}, - waiting AS "{{ conn|qtIdent(_('Waiting?')|safe) }}, - current_query AS "{{ conn|qtIdent(_('Query')|safe) }}, - query_start AS "{{ conn|qtIdent(_('Query start')|safe) }}, - xact_start AS "{{ conn|qtIdent(_('Xact start')|safe) }} + END AS {{ conn|qtIdent(_('Client')) }}, + application_name AS {{ conn|qtIdent(_('Application')) }}, + waiting AS {{ conn|qtIdent(_('Waiting?')) }}, + current_query AS {{ conn|qtIdent(_('Query')) }}, + query_start AS {{ conn|qtIdent(_('Query start')) }}, + xact_start AS {{ conn|qtIdent(_('Xact start')) }} FROM pg_stat_activity sa WHERE (SELECT r.rolsuper OR r.oid = sa.usesysid FROM pg_roles r WHERE r.rolname = current_user) UNION SELECT - procpid AS "{{ conn|qtIdent('PID') }}, - usename AS "{{ conn|qtIdent(_('User')|safe) }}, - '' AS "{{ conn|qtIdent(_('Database')|safe) }}, - backend_start AS "{{ conn|qtIdent(_('Backend start')|safe) }}, + procpid AS "PID", + usename AS {{ conn|qtIdent(_('User')) }}, + datname AS {{ conn|qtIdent(_('Database')) }}, + backend_start AS {{ conn|qtIdent(_('Backend start')) }}, CASE WHEN client_hostname IS NOT NULL AND client_hostname != '' THEN client_hostname || ':' || client_port @@ -37,12 +37,12 @@ SELECT 'local pipe' ELSE 'localhost:' || client_port - END AS "{{ conn|qtIdent(_('Client')|safe) }}, - {{ _('Streaming Replication') }}|safe|qtLiteral AS "{{ conn|qtIdent(_('Application')|safe) }}, - null AS "{{ conn|qtIdent(_('Waiting?')|safe) }}, - state || ' [sync (state: ' || COALESCE(sync_state, '') || ', priority: ' || sync_priority::text || ')] (' || sent_location || ' sent, ' || write_location || ' written, ' || flush_location || ' flushed, ' || replay_location || ' applied)' AS "{{ conn|qtIdent(_('Query')|safe) }}, - null AS "{{ conn|qtIdent(_('Query start')|safe) }}, - null AS "{{ conn|qtIdent(_('Xact start')|safe) }} + END AS {{ conn|qtIdent(_('Client')) }}, + {{ _('Streaming Replication') }}|qtLiteral AS {{ conn|qtIdent(_('Application')) }}, + null AS {{ conn|qtIdent(_('Waiting?')) }}, + state || ' [sync (state: ' || COALESCE(sync_state, '') || ', priority: ' || sync_priority::text || ')] (' || sent_location || ' sent, ' || write_location || ' written, ' || flush_location || ' flushed, ' || replay_location || ' applied)' AS {{ conn|qtIdent(_('Query')) }}, + null AS {{ conn|qtIdent(_('Query start')) }}, + null AS {{ conn|qtIdent(_('Xact start')) }} FROM pg_stat_replication sa WHERE diff --git a/web/pgadmin/browser/server_groups/servers/templates/servers/sql/9.2_plus/stats.sql b/web/pgadmin/browser/server_groups/servers/templates/servers/sql/9.2_plus/stats.sql index 79ab873..0b397f6 100644 --- a/web/pgadmin/browser/server_groups/servers/templates/servers/sql/9.2_plus/stats.sql +++ b/web/pgadmin/browser/server_groups/servers/templates/servers/sql/9.2_plus/stats.sql @@ -1,8 +1,8 @@ SELECT - pid AS {{ conn|qtIdent('PID') }}, - usename AS {{ conn|qtIdent(_('User')|safe) }}, - datname AS {{ conn|qtIdent(_('Database')|safe) }}, - backend_start AS {{ conn|qtIdent(_('Backend start')|safe) }}, + pid AS "PID", + usename AS {{ conn|qtIdent(_('User')) }}, + datname AS {{ conn|qtIdent(_('Database')) }}, + backend_start AS {{ conn|qtIdent(_('Backend start')) }}, CASE WHEN client_hostname IS NOT NULL AND client_hostname != '' THEN client_hostname || ':' || client_port @@ -12,22 +12,22 @@ SELECT 'local pipe' ELSE 'localhost:' || client_port - END AS {{ conn|qtIdent(_('Client')|safe) }}, - application_name AS {{ conn|qtIdent(_('Application')|safe) }}, - waiting AS {{ conn|qtIdent(_('Waiting?')|safe) }}, - query AS {{ conn|qtIdent(_('Query')|safe) }}, - query_start AS {{ conn|qtIdent(_('Query start')|safe) }}, - xact_start AS {{ conn|qtIdent(_('Xact start')|safe) }} + END AS {{ conn|qtIdent(_('Client')) }}, + application_name AS {{ conn|qtIdent(_('Application')) }}, + waiting AS {{ conn|qtIdent(_('Waiting?')) }}, + query AS {{ conn|qtIdent(_('Query')) }}, + query_start AS {{ conn|qtIdent(_('Query start')) }}, + xact_start AS {{ conn|qtIdent(_('Xact start')) }} FROM pg_stat_activity sa WHERE (SELECT r.rolsuper OR r.oid = sa.usesysid FROM pg_roles r WHERE r.rolname = current_user) UNION SELECT - pid AS {{ conn|qtIdent('PID') }}, - usename AS {{ conn|qtIdent(_('User')|safe) }}, - '' AS {{ conn|qtIdent(_('Database')|safe) }}, - backend_start AS {{ conn|qtIdent(_('Backend start')|safe) }}, + pid AS "PID", + usename AS {{ conn|qtIdent(_('User')) }}, + '' AS {{ conn|qtIdent(_('Database')) }}, + backend_start AS {{ conn|qtIdent(_('Backend start')) }}, CASE WHEN client_hostname IS NOT NULL AND client_hostname != '' THEN client_hostname || ':' || client_port @@ -37,12 +37,12 @@ SELECT 'local pipe' ELSE 'localhost:' || client_port - END AS {{ conn|qtIdent(_('Client')|safe) }}, - {{ _('Streaming Replication')|safe|qtLiteral }} AS {{ conn|qtIdent(_('Application')|safe) }}, - null AS {{ conn|qtIdent(_('Waiting?')|safe) }}, - state || ' [sync (state: ' || COALESCE(sync_state, '') || ', priority: ' || sync_priority::text || ')] (' || sent_location || ' sent, ' || write_location || ' written, ' || flush_location || ' flushed, ' || replay_location || ' applied)' AS {{ conn|qtIdent(_('Query')|safe) }}, - null AS {{ conn|qtIdent(_('Query start')|safe) }}, - null AS {{ conn|qtIdent(_('Xact start')|safe) }} + END AS {{ conn|qtIdent(_('Client')) }}, + {{ _('Streaming Replication')|qtLiteral }} AS {{ conn|qtIdent(_('Application')) }}, + null AS {{ conn|qtIdent(_('Waiting?')) }}, + state || ' [sync (state: ' || COALESCE(sync_state, '') || ', priority: ' || sync_priority::text || ')] (' || sent_location || ' sent, ' || write_location || ' written, ' || flush_location || ' flushed, ' || replay_location || ' applied)' AS {{ conn|qtIdent(_('Query')) }}, + null AS {{ conn|qtIdent(_('Query start')) }}, + null AS {{ conn|qtIdent(_('Xact start')) }} FROM pg_stat_replication sa WHERE diff --git a/web/pgadmin/utils/ajax.py b/web/pgadmin/utils/ajax.py index 01e9556..0e11eac 100644 --- a/web/pgadmin/utils/ajax.py +++ b/web/pgadmin/utils/ajax.py @@ -30,8 +30,9 @@ class DataTypeJSONEncoder(json.JSONEncoder): return json.JSONEncoder.default(self, obj) -def make_json_response(success=1, errormsg='', info='', result=None, - data=None, status=200): +def make_json_response( + success=1, errormsg='', info='', result=None, data=None, status=200 +): """Create a HTML response document describing the results of a request and containing the data.""" doc = dict() @@ -44,7 +45,7 @@ def make_json_response(success=1, errormsg='', info='', result=None, return Response( response=json.dumps(doc, cls=DataTypeJSONEncoder), status=status, - mimetype="text/json" + mimetype="application/json" ) @@ -53,7 +54,7 @@ def make_response(response=None, status=200): return Response( response=json.dumps(response, cls=DataTypeJSONEncoder), status=status, - mimetype="text/json" + mimetype="application/json" ) @@ -120,10 +121,25 @@ def gone(errormsg=''): ) -def not_implemented(errormsg=_('Not implemented.')): +def not_implemented(errormsg=_('Not implemented.'), info='', result=None, data=None): """Create a response with HTTP status code 501 - Not Implemented.""" return make_json_response( status=501, success=0, - errormsg=errormsg + errormsg=errormsg, + info=info, + result=result, + data=data + ) + + +def service_unavailable(errormsg=_("Service Unavailable"), info='', result=None, data=None): + """Create a response with HTTP status code 503 - Server Unavailable.""" + return make_json_response( + status=503, + success=0, + errormsg=errormsg, + info=info, + result=result, + data=data ) diff --git a/web/pgadmin/utils/driver/psycopg2/__init__.py b/web/pgadmin/utils/driver/psycopg2/__init__.py index 05622e6..e3a9e96 100644 --- a/web/pgadmin/utils/driver/psycopg2/__init__.py +++ b/web/pgadmin/utils/driver/psycopg2/__init__.py @@ -30,6 +30,7 @@ from psycopg2.extensions import adapt import config from pgadmin.model import Server, User +from pgadmin.utils.exception import ConnectionLost from .keywords import ScanKeyword from ..abstract import BaseDriver, BaseConnection from .cursor import DictCursor @@ -179,6 +180,7 @@ class Connection(BaseConnection): self.execution_aborted = False self.row_count = 0 self.__notices = None + self.password = None super(Connection, self).__init__() @@ -225,10 +227,13 @@ class Connection(BaseConnection): password = None mgr = self.manager - if 'password' in kwargs: - encpass = kwargs['password'] - else: - encpass = getattr(mgr, 'password', None) + encpass = kwargs['password'] if 'password' in kwargs else None + + if encpass is None: + encpass = self.password or getattr(mgr, 'password', None) + + # Reset the existing connection password + self.password = None if encpass: # Fetch Logged in User Details. @@ -239,6 +244,10 @@ class Connection(BaseConnection): try: password = decrypt(encpass, user.password) + + # password is in bytes, for python3 we need it in string + if isinstance(password, bytes): + password = password.decode() except Exception as e: current_app.logger.exception(e) return False, \ @@ -246,10 +255,6 @@ class Connection(BaseConnection): str(e) ) - # password is in bytes, for python3 we need it in string - if isinstance(password, bytes): - password = password.decode() - try: if hasattr(str, 'decode'): database = self.db.encode('utf-8') @@ -401,11 +406,18 @@ WHERE mgr.server_cls = st break + mgr._update_password(encpass) mgr.update_session() return True, None def __cursor(self, server_cursor=False): + if not self.conn: + raise ConnectionLost( + self.manager.sid, + self.db, + None if self.conn_id[0:3] == u'DB:' else self.conn_id[5:] + ) cur = getattr(g, "{0}#{1}".format( self.manager.sid, self.conn_id.encode('utf-8') @@ -475,7 +487,7 @@ Attempting to reconnect to the database server (#{server_id}) for the connection Connection for server#{0} with database "{1}" was lost. Attempt to reconnect it failed with the error: {2}""" - ).format(self.driver.server_id, self.database, cur) + ).format(self.driver.server_id, self.db, cur) current_app.logger.error(msg) return False, cur @@ -593,6 +605,12 @@ Attempt to reconnect it failed with the error: self.__internal_blocking_execute(cur, query, params) except psycopg2.Error as pe: cur.close() + if not self.connected(): + raise ConnectionLost( + self.manager.sid, + self.db, + None if self.conn_id[0:3] == u'DB:' else self.conn_id[5:] + ) errmsg = self._formatted_exception_msg(pe, formatted_exception_msg) current_app.logger.error( u"Failed to execute query (execute_scalar) for the server #{server_id} - {conn_id} (Query-id: {query_id}):\nError Message:{errmsg}".format( @@ -694,6 +712,12 @@ Failed to execute query (execute_async) for the server #{server_id} - {conn_id} self.__internal_blocking_execute(cur, query, params) except psycopg2.Error as pe: cur.close() + if not self.connected(): + raise ConnectionLost( + self.manager.sid, + self.db, + None if self.conn_id[0:3] == u'DB:' else self.conn_id[5:] + ) errmsg = self._formatted_exception_msg(pe, formatted_exception_msg) current_app.logger.error(u""" Failed to execute query (execute_void) for the server #{server_id} - {conn_id} @@ -733,6 +757,12 @@ Failed to execute query (execute_void) for the server #{server_id} - {conn_id} self.__internal_blocking_execute(cur, query, params) except psycopg2.Error as pe: cur.close() + if not self.connected(): + raise ConnectionLost( + self.manager.sid, + self.db, + None if self.conn_id[0:3] == u'DB:' else self.conn_id[5:] + ) errmsg = self._formatted_exception_msg(pe, formatted_exception_msg) current_app.logger.error( u"Failed to execute query (execute_2darray) for the server #{server_id} - {conn_id} (Query-id: {query_id}):\nError Message:{errmsg}".format( @@ -778,6 +808,12 @@ Failed to execute query (execute_void) for the server #{server_id} - {conn_id} self.__internal_blocking_execute(cur, query, params) except psycopg2.Error as pe: cur.close() + if not self.connected(): + raise ConnectionLost( + self.manager.sid, + self.db, + None if self.conn_id[0:3] == u'DB:' else self.conn_id[5:] + ) errmsg = self._formatted_exception_msg(pe, formatted_exception_msg) current_app.logger.error( u"Failed to execute query (execute_dict) for the server #{server_id}- {conn_id} (Query-id: {query_id}):\nError Message:{errmsg}".format( @@ -867,6 +903,7 @@ Failed to reset the connection to the server due to following error: if self.conn: self.conn.close() self.conn = None + self.password = None def _wait(self, conn): """ @@ -942,6 +979,12 @@ Failed to reset the connection to the server due to following error: try: status = self._wait_timeout(self.conn, ASYNC_WAIT_TIMEOUT) except psycopg2.Error as pe: + if cur.closed: + raise ConnectionLost( + self.manager.sid, + self.db, + self.conn_id[5:] + ) errmsg = self._formatted_exception_msg(pe, formatted_exception_msg) return False, errmsg, None @@ -1242,10 +1285,6 @@ class ServerManager(object): self, database=None, conn_id=None, auto_reconnect=True, did=None, async=None ): - msg_active_conn = gettext( - "Server has no active connection. Please connect to the server." - ) - if database is not None: if hasattr(str, 'decode') and \ not isinstance(database, unicode): @@ -1285,7 +1324,7 @@ WHERE db.oid = {0}""".format(did)) )) if database is None: - raise Exception(msg_active_conn) + raise ConnectionLost(self.sid, None, None) my_id = (u'CONN:{0}'.format(conn_id)) if conn_id is not None else \ (u'DB:{0}'.format(database)) @@ -1387,6 +1426,13 @@ WHERE db.oid = {0}""".format(did)) return True + def _update_password(self, passwd): + self.password = passwd + for conn_id in self.connections: + conn = self.connections[conn_id] + if conn.conn is not None: + conn.password = passwd + def update_session(self): managers = session['__pgsql_server_managers'] \ if '__pgsql_server_managers' in session else dict() diff --git a/web/pgadmin/utils/exception.py b/web/pgadmin/utils/exception.py new file mode 100644 index 0000000..662ddec --- /dev/null +++ b/web/pgadmin/utils/exception.py @@ -0,0 +1,42 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2016, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +from werkzeug.exceptions import HTTPException +from werkzeug.http import HTTP_STATUS_CODES +from flask_babel import gettext as _ +from flask import request + +from pgadmin.utils.ajax import service_unavailable + + +class ConnectionLost(HTTPException): + """ + Exception + """ + + def __init__(self, _server_id, _database_name, _conn_id): + self.sid = _server_id + self.db = _database_name + self.conn_id = _conn_id + HTTPException.__init__(self) + + @property + def name(self): + return HTTP_STATUS_CODES.get(505, 'Service Unavailable') + + def get_response(self, environ=None): + return service_unavailable( + _("Connection to the server has been lost!"), + info="CONNECTION_LOST", + data={ + 'sid': self.sid, + 'database': self.db, + 'conn_id': self.conn_id + } + )