Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testSocketMissing(self):
conn_params = {
'unix_sock': "/file-does-not-exist",
'user': "doesn't-matter"}
self.assertRaises(pg8000.InterfaceError, pg8000.connect, **conn_params)
def connect_to_postgres():
try:
return connect(host=host, user=user, port=port,
database=database)
except (InterfaceError, ProgrammingError):
return False
def connect_to_postgres():
try:
return connect(host=host, user=u"postgres", port=port,
database=database)
except (InterfaceError, ProgrammingError) as e:
Message.new(
message_type=u"acceptance:integration:postgres_connect",
exception=unicode(e.__class__), reason=unicode(e)).write()
return False
def commit(self):
# There's a threading bug here. If a query is sent after the
# commit, but before the begin, it will be executed immediately
# without a surrounding transaction. Like all threading bugs -- it
# sounds unlikely, until it happens every time in one
# application... however, to fix this, we need to lock the
# database connection entirely, so that no cursors can execute
# statements on other threads. Support for that type of lock will
# be done later.
if self.conn == None:
raise InterfaceError("connection is closed")
self.conn.commit()
self.conn.begin()
def rollback(self):
# see bug description in commit.
if self.conn == None:
raise InterfaceError("connection is closed")
self.conn.rollback()
self.conn.begin()
def fetchone(self):
if self.cursor == None:
raise InterfaceError("cursor is closed")
return self.cursor.read_tuple()
def ok(self, conn, user, password=None, **kwargs):
if password == None:
raise InterfaceError("server requesting MD5 password authentication, but no password was provided")
pwd = "md5" + md5.new(md5.new(password + user).hexdigest() + self.salt).hexdigest()
conn._send(Protocol.PasswordMessage(pwd))
msg = conn._read_message()
if isinstance(msg, Protocol.AuthenticationRequest):
return msg.ok(conn, user)
elif isinstance(msg, Protocol.ErrorResponse):
if msg.code == "28000":
raise InterfaceError("md5 password authentication failed")
else:
raise InternalError("server returned unexpected error %r" % msg)
else:
raise InternalError("server returned unexpected response %r" % msg)
}
kw = dict((params_map[k], v) for (k, v) in iteritems(module.params)
if k in params_map and v != "")
# If a login_unix_socket is specified, incorporate it here.
is_localhost = "host" not in kw or kw["host"] == "" or kw["host"] == "localhost"
if is_localhost and module.params["login_unix_socket"] != "":
kw["host"] = module.params["login_unix_socket"]
cursor = None
try:
pg8000.paramstyle = "pyformat"
db_connection = pg8000.connect(**kw)
db_connection.autocommit = False
cursor = db_connection.cursor()
except InterfaceError:
e = get_exception()
module.fail_json(msg="unable to connect to database, check credentials and SSL flag!: %s " % e)
except Exception:
e = get_exception()
module.fail_json(msg="unable to connect to database: %s" % e)
kw = {'user': user, 'group': group}
changed = False
user_added = False
group_added = False
user_removed = False
group_removed = False
# ===========================================
# Main decision tree
#
def execute(self, operation, args=()):
if self.cursor == None:
raise InterfaceError("cursor is closed")
new_query, new_args = DBAPI.convert_paramstyle(DBAPI.paramstyle, operation, args)
try:
self.cursor.execute(new_query, *new_args)
except:
# any error will rollback the transaction to-date
self.cursor.connection.rollback()
raise