I am attempting to connect and authenticate to a remote database host (dotcloud, mongolabs, etc) using MongoKit within Flask. Connecting to the server seems to work fine. However I am unable to authenticate to the database. Presumably this should work:
from mongokit import Connection
connection = Connection(my_remote_host, my_remote_port)
connection.my_database.authenticate(my_admin_user, my_admin_password)
the call to authenticate() returns True, yet subsequent calls to fetch data throw:
OperationFailure: database error: unauthorized db
Anyone know what might be happening here?
This is likely due to the current behavior of authenticate() in pymongo. Pymongo doesn't cache authentication credentials between threads, so each thread must authenticate individually. See the note in the pymongo documentation about using authenticate() in a multi-threaded environment.
Related
I want to setup a local db2 database to allow local users to connect without using password (specifically via python).
I can connect to the database from cli w/o password as db2 connect to <DATABASE>.
However, when trying to connect from within python using official ibm_db api as
ibm_db.connect("database", "", "")
throws the following error:
SQL 300082N Security processing failed with reason "17" ("UNSUPPORTED FUNCTION"). SQLSTATE=08001 SQLCODE=-30082
Based on the documentation for authentication options, I have set the following options:
AUTHENTICATION=CLIENT
TRUST_CLNTAUTH=CLIENT
TRUST_ALLCLNTS=YES
however, I am still getting the same error.
P.S. #1: I am not concerned about user authentication as they have already been authenticated before been allowed to log in to the server.
P.S. #2: A similar question has already been asked at DB2 connection without specifying username and password. However, I need to connect via python and even with the setting 1 and 3 prescribed in the accepted answer, the connection fails.
P.S. #3: Possibly relevant link - http://www-01.ibm.com/support/docview.wss?uid=swg21237107
I have a web application which communicates with a database using SQLAlchemy. The connection is done using certificates and keys. The problem is that the certificates are changing every now and then while the application is still online (which means that the sqlalchemy session is already initiated with the old certificates). Hence, when the certificates have changed, I get an psycopg2.OperationalError saying that the connection was closed unexpectedly.
I must restart the app for the session to load the new certificates when initiating to fix the problem.
My session is created once when the app is starting and then I use the same session for all database actions.
My question is how can I check if the session is still valid before using it?
Many thanks
I am trying to connect to LDAP server for authentication. Our LDAP server use SSL but we don't use any SSL certificate.
Following is my code:
I have two url provided by system admin. There are as follows:
url1 = "ldap://100.x.x.x:389"
url2 = "ldaps://10.x.x.x:636"
My first questionis which url I should use ? what is difference between ldap:// and ldaps://
LDAP authentication code is as follows, I have tried to use both(url1 and url2):
conn = ldap.initialize(url)
ldap.TLS_AVAIL
1
conn.simple_bind_s(
'CN={0},ou=users,DC=compnay,DC=com'.format(myemail),
mypassword
)
conn.simple_bind(
'CN={0},ou=users,DC=compnay,DC=com'.format(myemail),
mypassword
)
if i used first url (url1) with simple_bind_s, then following is error:
INVALID_CREDENTIALS: {'info': u'80090308: LdapErr: DSID-0C0903D9, comment: AcceptSecurityContext error, data 52e, v2580', 'desc': u'Invalid credentials'}
but when I use with simple_bind, it gives me int even though password or username is wrong.
What is difference between simple_bind_s and simple_bind. How can I use simple_bind for authentication?
The difference between simple_bind() and simple_bind_s() is that simple_bind() is asynchronous and simple_bind_s() is synchronous.
The synchronous version makes your program wait until it is finished and then returns the results, where the asynchronous version returns an id code immediately and continues working in the background, and then later you call result() with the id code to get the results.
So your call to simple_bind() likely did fail; you just don't know it because you haven't fetched the result yet.
Most ldap functions have asynchronous and synchronous versions, such as add() and add_s(), delete() and delete_s(), search() and search_s(), etc. Some ldap operations (especially searching) can take a long time to complete, so you'd use the asynchronous versions if you don't want to your program to have long pauses.
Most of the blog posts and examples on the web, in purpose of connecting to the MongoDB using Mongoengine in Python/Django, have suggested that we should add these lines to the settings.py file of the app:
from mongoengine import connect
connect('project1', host='localhost')
It works fine for most of the cases except one I have faced recently:
When the database is down!
Let say if db goes down, the process that is taking care of the web server (in my case, Supervisord) will stop running the app because of Exception that connect throws. It may try few more times but after its timeout reached, it will stop trying.
So even if your app has some parts that are not tied to db, they will also break down.
A quick solution to this is adding a try/exception block to the connect code:
try:
connect('project1', host='localhost')
except Exception as e:
print(e)
but I am looking for a better and clean way to handle this.
Unfortunately this is not really possible with mongoengine unless you go with the try-except solution like you did.
You could try to connect with pure pymongo version 3.0+ using MongoClient and registering the connection manually in the mongoengine.connection._connection_settings dictionary (quite hacky but should work). From pymongo documentation:
Changed in version 3.0: MongoClient is now the one and only client class for a standalone server, mongos, or replica set. It includes the functionality that had been split into MongoReplicaSetClient: it can connect to a replica set, discover all its members, and monitor the set for stepdowns, elections, and reconfigs.
The MongoClient constructor no longer blocks while connecting to the server or servers, and it no longer raises ConnectionFailure if they are unavailable, nor ConfigurationError if the user’s credentials are wrong. Instead, the constructor returns immediately and launches the connection process on background threads.
I am getting the following error:
db assertion failure, assertion: 'unauthorized db:db1 lock type:-1 client:', assertionCode: 10057
I am able to access the MongoDB database by running python on the shell of my server. But when I try to access my site i get this unauthorised errors.
Any fix for this error?
This means that your database is using authentication. In such a setup, you must authenticate a valid user before you can perform any operations (queries, commands, updates, etc). You can do so with the db.auth(username, password) helper in the mongo shell (described in the MongoDB docs), and with Python, you can use the authenticate(username, password) method of the Database object (described in the PyMongo docs)