Mongo Python Error :: Troubleshoot an Authentication Error? - python

In trying to build off of this SO post, I've built the below Python script. It should connect to a remote MongoDB (actually a VM on my local machine), run a simple find() operation, then print the results:
from pymongo import MongoClient
MONGO_HOST = "172.17.0.2"
MONGO_PORT = "27017"
MONGO_DB = "myDB"
MONGO_USER = "me"
MONGO_PASS = "password01"
uri = "mongodb://{}:{}#{}:{}/{}?authSource=admin".format(MONGO_USER, MONGO_PASS, MONGO_HOST, MONGO_PORT, MONGO_DB)
client = MongoClient(uri)
myDB = client['myDB']
myTable = myDB['TableA']
limited_cursor = myTable.find({ "data1": "KeyStringHere" }, { "_id": 0, "data2" : 1 }).limit(2)
for document in limited_cursor:
print(document)
When run, the code vomits out a lot of errors:
me#ubuntu1:~/$ /usr/bin/python3 myPythonScript.py
Traceback (most recent call last):
File "myPythonScript.py", line 14, in <module>
for document in limited_cursor:
File "/usr/lib/python3/dist-packages/pymongo/cursor.py", line 1169, in next
if len(self.__data) or self._refresh():
File "/usr/lib/python3/dist-packages/pymongo/cursor.py", line 1085, in _refresh
self.__send_message(q)
File "/usr/lib/python3/dist-packages/pymongo/cursor.py", line 924, in __send_message
**kwargs)
File "/usr/lib/python3/dist-packages/pymongo/mongo_client.py", line 1029, in _send_message_with_response
exhaust)
File "/usr/lib/python3/dist-packages/pymongo/mongo_client.py", line 1040, in _reset_on_error
return func(*args, **kwargs)
File "/usr/lib/python3/dist-packages/pymongo/server.py", line 85, in send_message_with_response
with self.get_socket(all_credentials, exhaust) as sock_info:
File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
return next(self.gen)
File "/usr/lib/python3/dist-packages/pymongo/server.py", line 138, in get_socket
with self.pool.get_socket(all_credentials, checkout) as sock_info:
File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
return next(self.gen)
File "/usr/lib/python3/dist-packages/pymongo/pool.py", line 920, in get_socket
sock_info.check_auth(all_credentials)
File "/usr/lib/python3/dist-packages/pymongo/pool.py", line 609, in check_auth
auth.authenticate(credentials, self)
File "/usr/lib/python3/dist-packages/pymongo/auth.py", line 486, in authenticate
auth_func(credentials, sock_info)
File "/usr/lib/python3/dist-packages/pymongo/auth.py", line 466, in _authenticate_default
return _authenticate_scram_sha1(credentials, sock_info)
File "/usr/lib/python3/dist-packages/pymongo/auth.py", line 209, in _authenticate_scram_sha1
res = sock_info.command(source, cmd)
File "/usr/lib/python3/dist-packages/pymongo/pool.py", line 517, in command
collation=collation)
File "/usr/lib/python3/dist-packages/pymongo/network.py", line 125, in command
parse_write_concern_error=parse_write_concern_error)
File "/usr/lib/python3/dist-packages/pymongo/helpers.py", line 145, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.
me#ubuntu1:~/$
I'm certain the code is crashing on the last line of the script: for document in limited_cursor: print(document) If I comment out those last two lines, the script runs without error... but generates no output.
However, a careful reading of the traceback errors says the root problem is Authentication failed. So what does that mean? Is my URI incorrect?
I'm pretty sure the URI is correct. When I modify the script to print it to the console:
print(uri)
It comes out:
mongodb://me:password01#172.17.0.2:27017/myDB?authSource=admin
That all looks right to me. The IP address and port are def correct, as is the database name. I've also created a me/password01 user within Mongo. From within the Mongo Shell:
> db.getUsers()
[
{
"_id" : "test.me",
"userId" : UUID("5c3b4f1a-d05f-4015-9f6d-0b6cbbd7026c"),
"user" : "me",
"db" : "test",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
]
>
Any advice or tips is appreciated, thank you.

userAdminAnyDatabase role does not allow to search data, it's to manage users.
You need readWriteAnyDatabase role
You created role in the test database but authenticating against admin database: ?authSource=admin.
So either change you to ?authSource=test or (recommended) create the user in admin database. In mongo shell:
> use admin;
> db.createUser( { user: "me" ....})

Related

Why is the connection between a python motor client and a mongoDB being dropped for about 50% of queries?

I'm seeing odd behavior in my application
I have a graphQL python application that lives in a docker container and is using FastAPI and Uvicorn. It connects to a MongoDB in another docker container using Motor. This is run in a Kubernetes instance.
About 50% of the time, but with no real pattern, I won't get results back. The other 50% of the time it works fine.
Based on the mongodb logs it looks like the python application is closing the connection before the response can be sent back. This is not a long running query (< 10ms). Additionally I've tried to set the various timeouts in Motor/PyMongo very high (~3600s) but still get the same behavior.
Client Initialization:
try:
# kwargs contains username, password, connectTimeoutMS, socketTimeoutMS, serverSelectionTimeoutMS
return AsyncIOMotorClient(host, port, appname=appname, connect=True, **kwargs)
except:
#log it
The Query Code:
def callQuery(vendor, name):
match_query = {
"$match": {
"_id.product_vendor": vendor,
"_id.product_name": name,
}
}
add_fields = {
"$addFields": {
"product_name": "$_id.product_name",
"product_vendor": "$_id.product_vendor",
}
}
project = {"$project": {"_id": 0}}
cursor = collection.aggregate([match_query, add_fields, project])
products = await cursor.to_list(length=None) # line 173 in product_lookup to compare to bt below
def submit_aggregation(pipeline: List[dict], collection, **kwargs):
return collection.aggregate(pipeline, **kwargs)
MongoDB logs:
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"D3", "c":"NETWORK", "id":22934, "ctx":"conn5","msg":"Starting server-side compression negotiation"}
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"D3", "c":"NETWORK", "id":22935, "ctx":"conn5","msg":"Compression negotiation not requested by client"}
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"D3", "c":"FTDC", "id":23905, "ctx":"conn5","msg":"Using exhaust for isMaster or hello protocol"}
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"I", "c":"COMMAND", "id":51803, "ctx":"conn5","msg":"Slow query","attr":{"type":"command","ns":"admin.$cmd","appName":"mappingapi","command":{"hello":1,"topologyVersion":{"processId":{"$oid":"619d4e56e9dc669ff6ade24b"},"counter":0},"maxAwaitTimeMS":10000,"$db":"admin","$readPreference":{"mode":"primary"}},"numYields":0,"reslen":313,"locks":{},"remote":"127.0.0.1:41430","protocol":"op_msg","durationMillis":0}}
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"D2", "c":"QUERY", "id":22783, "ctx":"conn5","msg":"Received interrupt request for unknown op","attr":{"opId":25787,"knownOps":[]}}
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"D3", "c":"-", "id":5127803, "ctx":"conn5","msg":"Released the Client","attr":{"client":"conn5"}}
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"D3", "c":"-", "id":5127801, "ctx":"conn5","msg":"Setting the Client","attr":{"client":"conn5"}}
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"D2", "c":"COMMAND", "id":21965, "ctx":"conn5","msg":"About to run the command","attr":{"db":"admin","commandArgs":{"hello":1,"topologyVersion":{"processId":{"$oid":"619d4e56e9dc669ff6ade24b"},"counter":0},"maxAwaitTimeMS":10000,"$db":"admin","$readPreference":{"mode":"primary"}}}}
{"t":{"$date":"2021-11-23T20:59:25.690+00:00"},"s":"D3", "c":"FTDC", "id":23904, "ctx":"conn5","msg":"Using maxAwaitTimeMS for awaitable hello protocol","attr":{"maxAwaitTimeMS":10000}}
Backtrace of the disconnect from the python client:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/promise/promise.py", line 844, in handle_future_result
resolve(future.result())
File "./mappingdb/api.py", line 247, in resolve_productdbProducts
client=get_pdb_client(),
File "/usr/local/lib/python3.7/site-packages/productdata/_utils.py", line 42, in func_future
return await func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/productdata/query.py", line 173, in product_lookup
products = await cursor.to_list(length=None)
File "/usr/local/lib/python3.7/site-packages/motor/core.py", line 1372, in _to_list
result = get_more_result.result()
File "/usr/local/lib/python3.7/site-packages/motor/core.py", line 1611, in _on_started
pymongo_cursor = future.result()
File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.7/site-packages/pymongo/collection.py", line 2507, in aggregate
**kwargs)
File "/usr/local/lib/python3.7/site-packages/pymongo/collection.py", line 2421, in _aggregate
retryable=not cmd._performs_write)
File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1525, in _retryable_read
return func(session, server, sock_info, secondary_ok)
File "/usr/local/lib/python3.7/site-packages/pymongo/aggregation.py", line 149, in get_cursor
user_fields=self._user_fields)
File "/usr/local/lib/python3.7/site-packages/pymongo/pool.py", line 726, in command
self._raise_connection_failure(error)
File "/usr/local/lib/python3.7/site-packages/pymongo/pool.py", line 929, in _raise_connection_failure
_raise_connection_failure(self.address, error)
File "/usr/local/lib/python3.7/site-packages/pymongo/pool.py", line 238, in _raise_connection_failure
raise NetworkTimeout(msg)
graphql.error.located_error.GraphQLLocatedError: productdb:27017: timed out

Error trying to connect Celery through SQS using STS

I'm trying to use Celery with SQS as broker. In order to use the SQS from my container I need to assume a role and for that I'm using STS. My code looks like this:
role_info = {
'RoleArn': 'arn:aws:iam::xxxxxxx:role/my-role-execution',
'RoleSessionName': 'roleExecution'
}
sts_client = boto3.client('sts', region_name='eu-central-1')
credentials = sts_client.assume_role(**role_info)
aws_access_key_id = credentials["Credentials"]['AccessKeyId']
aws_secret_access_key = credentials["Credentials"]['SecretAccessKey']
aws_session_token = credentials["Credentials"]["SessionToken"]
os.environ["AWS_ACCESS_KEY_ID"] = aws_access_key_id
os.environ["AWS_SECRET_ACCESS_KEY"] = aws_secret_access_key
os.environ["AWS_DEFAULT_REGION"] = 'eu-central-1'
os.environ["AWS_SESSION_TOKEN"] = aws_session_token
broker = "sqs://"
backend = 'redis://redis-service:6379/0'
celery = Celery('tasks', broker=broker, backend=backend)
celery.conf["task_default_queue"] = 'my-queue'
celery.conf["broker_transport_options"] = {
'region': 'eu-central-1',
'predefined_queues': {
'my-queue': {
'url': 'https://sqs.eu-central-1.amazonaws.com/xxxxxxx/my-queue'
}
}
}
In the same file I have the following task:
#celery.task(name='my-queue.my_task')
def my_task(content) -> int:
print("hello")
return 0
When I execute the following code I get an error:
[2020-09-24 10:38:03,602: CRITICAL/MainProcess] Unrecoverable error: ClientError('An error occurred (AccessDenied) when calling the ListQueues operation: Access to the resource https://eu-central-1.queue.amazonaws.com/ is denied.',)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/kombu/transport/virtual/base.py", line 921, in create_channel
return self._avail_channels.pop()
IndexError: pop from empty list
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/celery/worker/worker.py", line 208, in start
self.blueprint.start(self)
File "/usr/local/lib/python3.6/site-packages/celery/bootsteps.py", line 119, in start
step.start(parent)
File "/usr/local/lib/python3.6/site-packages/celery/bootsteps.py", line 369, in start
return self.obj.start()
File "/usr/local/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 318, in start
blueprint.start(self)
File "/usr/local/lib/python3.6/site-packages/celery/bootsteps.py", line 119, in start
step.start(parent)
File "/usr/local/lib/python3.6/site-packages/celery/worker/consumer/connection.py", line 23, in start
c.connection = c.connect()
File "/usr/local/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 405, in connect
conn = self.connection_for_read(heartbeat=self.amqheartbeat)
File "/usr/local/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 412, in connection_for_read
self.app.connection_for_read(heartbeat=heartbeat))
File "/usr/local/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 439, in ensure_connected
callback=maybe_shutdown,
File "/usr/local/lib/python3.6/site-packages/kombu/connection.py", line 422, in ensure_connection
callback, timeout=timeout)
File "/usr/local/lib/python3.6/site-packages/kombu/utils/functional.py", line 341, in retry_over_time
return fun(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/kombu/connection.py", line 275, in connect
return self.connection
File "/usr/local/lib/python3.6/site-packages/kombu/connection.py", line 823, in connection
self._connection = self._establish_connection()
File "/usr/local/lib/python3.6/site-packages/kombu/connection.py", line 778, in _establish_connection
conn = self.transport.establish_connection()
File "/usr/local/lib/python3.6/site-packages/kombu/transport/virtual/base.py", line 941, in establish_connection
self._avail_channels.append(self.create_channel(self))
File "/usr/local/lib/python3.6/site-packages/kombu/transport/virtual/base.py", line 923, in create_channel
channel = self.Channel(connection)
File "/usr/local/lib/python3.6/site-packages/kombu/transport/SQS.py", line 100, in __init__
self._update_queue_cache(self.queue_name_prefix)
File "/usr/local/lib/python3.6/site-packages/kombu/transport/SQS.py", line 105, in _update_queue_cache
resp = self.sqs.list_queues(QueueNamePrefix=queue_name_prefix)
File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 337, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 656, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the ListQueues operation: Access to the resource https://eu-central-1.queue.amazonaws.com/ is denied.
If I use boto3 directly without Celery, I'm able to connect to the queue and retrieve data without this error. I don't know why Celery/Kombu try to list queues when I specify the predefined_queues configuration, tha is used to avoid these behavior (from docs):
If you want Celery to use a set of predefined queues in AWS, and to never attempt to list SQS queues, nor attempt to create or delete them, pass a map of queue names to URLs using the predefined_queue_urls setting
Source here
Anyone know what happens? How I should modify my code in order to make it work?. Seems that Celery is not using the credentials at all.
The versions I'm using:
celery==4.4.7
boto3==1.14.54
kombu==4.5.0
Thanks!
PS: I created and issue in Github to track if this can be a library error or not...
I solved the problem updating dependencies to the latest versions:
celery==5.0.0
boto3==1.14.54
kombu==5.0.2
pycurl==7.43.0.6
I was able to get celery==4.4.7 and kombu==4.6.11 working by setting the following configuration option:
celery.conf["task_create_missing_queues"] = False

Django Closed Socket Error with Repeated AJAX Calls [WinError 10053] An established connection was aborted by the software in your host machine

I'm trying to implement a live search through the jquery plugin Select2 in my Django 1.11.4 project. Running Python 3.6. When I type in the textbox, the server doesn't seem to be able to handle the number of requests and it closes, followed by these series of errors.
I've spent a couple hours following a couple threads on SO SO(more) , Google, etc but none have a working solution. I thought it was a python version issue, but I've upgraded from 2.7 to 3.6 and still have it. Any help would be great thanks!
This the view that's being called by the ajax call:
def directory_search(request):
# Query text from search bar
query = request.GET.get('q', None)
if query:
# Searches the DB for matching value
customers = Customer.objects.filter(
Q(name__icontains= query)|
Q(contract_account__icontains= query)|
Q(address__icontains= query)|
Q(address__icontains= query)|
Q(email__icontains= query)
).distinct()
# Turns queryset into dict
customers = customers.values("contract_account","name")
# Turns dict into list
customers_list = list(customers)
return JsonResponse(customers_list, safe=False)
else:
return JsonResponse(data={'success': False,'errors': 'No mathing items found'})
This is the js/ajax call for the Select2 plugin:
$(".customer-search-bar").select2({
ajax:{
dataType: 'json',
type: 'GET',
url:"{% url 'portal:directory_search' %}",
data: function (params) {
var queryParameters = {
q: params.term
}
return queryParameters;
},
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.name,
id: item.contract_account
}
})
};
}
},
escapeMarkup: function (markup) { return markup; },
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection,
language: { errorLoading:function(){ return "Searching..." }}
});
Errors: (I just broke them apart to make it more legible, but they occurred in the presented order)
1.
Traceback (most recent call last):
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 775, in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
2.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "C:\Users\leep\PythonStuff\virtual_environments\rpp_3.6\lib\site-packages\django\core\servers\basehttp.py", line 88, in handle_error
super(ServerHandler, self).handle_error()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
3.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 639, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\socketserver.py", line 696, in __init__
self.handle()
File "C:\Users\leep\PythonStuff\virtual_environments\rpp_3.6\lib\site-packages\django\core\servers\basehttp.py", line 155, in handle
handler.run(self.server.get_app())
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "C:\Users\leep\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
It appears that its a python 2.7 known bug, maybe it remains on python 3.6:
https://bugs.python.org/issue14574
I'm having a similar issue in django, also running python 3.6
TypeError: 'NoneType' object is not subscriptable followed by AttributeError: 'NoneType' object has no attribute 'split'
Edit
It seems to be a error on chrome. It didn't come to me, 'cause I was using Opera, but opera uses chromium as well. In internet explorer the error didn't show.
https://code.djangoproject.com/ticket/21227#no1
This is the bug tracker

Authentication failed to connect to mongodb using pymongo

We have written a piece of code in python script using pymongo that connects to mongodb.
username = 'abc'
password = 'xxxxxx'
server = 'dns name of that server'
port = 27017
In program, the code looks like:
import pymongo
from pymongo import MongoClient
client = MongoClient(url, serverSelectionTimeoutMS=300)
database = client.database_name
data_insert = database.collection_name.insert_one({'id': 1, 'name': xyz})
When I tried to do these operations, it raises an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 1114, in next
if len(self.__data) or self._refresh():
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 1036, in _refresh
self.__collation))
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 873, in __send_message
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 905, in _send_message_with_response
exhaust)
File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 916, in _reset_on_error
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pymongo/server.py", line 99, in send_message_with_response
with self.get_socket(all_credentials, exhaust) as sock_info:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/pymongo/server.py", line 168, in get_socket
with self.pool.get_socket(all_credentials, checkout) as sock_info:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 792, in get_socket
sock_info.check_auth(all_credentials)
File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 512, in check_auth
auth.authenticate(credentials, self)
File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 470, in authenticate
auth_func(credentials, sock_info)
File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 450, in _authenticate_default
return _authenticate_scram_sha1(credentials, sock_info)
File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 201, in _authenticate_scram_sha1
res = sock_info.command(source, cmd)
File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 419, in command
collation=collation)
File "/usr/local/lib/python2.7/dist-packages/pymongo/network.py", line 116, in command
parse_write_concern_error=parse_write_concern_error)
File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 210, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.
In MongoDB, while performing queries we are getting the responses normally, without raising any errors.
Because the other answers to your question didn't work for me, I'm going to copy and paste my answer from a similar question.
If you've tried the above answers and you're still getting an error:
pymongo.errors.OperationFailure: Authentication failed.
There's a good chance you need to add ?authSource=admin to the end of your uri.
Here's a working solution that I'm using with MongoDB server version 4.2.6 and MongoDB shell version v3.6.9.
from pymongo import MongoClient
# Replace these with your server details
MONGO_HOST = "XX.XXX.XXX.XXX"
MONGO_PORT = "27017"
MONGO_DB = "database"
MONGO_USER = "admin"
MONGO_PASS = "pass"
uri = "mongodb://{}:{}#{}:{}/{}?authSource=admin".format(MONGO_USER, MONGO_PASS, MONGO_HOST, MONGO_PORT, MONGO_DB)
client = MongoClient(uri)
Similar fix for command line is adding --authenticationDatabase admin
Well, I have been stuck with the same error for almost 3-4 hours. I came across solution with the following steps:
from your shell connect to MongoDB by typing: mongo
afterwards, create a database: use test_database
Now create a user with the following command with readWrite and dbAdmin privileges.
db.createUser(
{
user: "test_user",
pwd: "testing12345",
roles: [ "readWrite", "dbAdmin" ]
}
);
This will prompt Successfully added user: { "user" : "test_user", "roles" : [ "readWrite", "dbAdmin" ] }
you can check by typing: show users.
It will also show you DB name you created before in the json.
now you should be able to insert data to your database:
client = MongoClient("mongodb://test_user:myuser123#localhost:27017/test_database")
db = client.test_database
data = {"initial_test":"testing"}
db["my_collection"].insert_one(data).inserted_id
I ran into this error, and my problem was with the password.
I had what I believe to be a special character in the Master account. Changing the password to be only alphanumeric fixed it for me.
Code snippet
client = pymongo.MongoClient(
'mongodb://username:alphaNumericPassword#localhost:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'
)
# Specify the database to be used
db = client['prod-db']

py2neo (Neo4j) : py2neo.packages.httpstream.http.SocketError: Operation not permitted

I am running Neo4j 2.2.1 in ubuntu Amazon EC2 instance. When I am trying to connect through python using py2neo-2.0.7, I am getting following error :
py2neo.packages.httpstream.http.SocketError: Operation not permitted
I am able to access the web-interface through http://52.10.**.***:7474/browser/
CODE :-
from py2neo import Graph, watch, Node, Relationship
url_graph_conn = "https://neo4j:password#52.10.**.***:7474/db/data/"
print url_graph_conn
my_conn = Graph(url_graph_conn)
babynames = my_conn.find("BabyName")
for babyname in babynames:
print 2
Error message :-
https://neo4j:password#52.10.**.***:7474/db/data/
Traceback (most recent call last):
File "C:\Users\rharoon002\eclipse_workspace\peace\peace\core\graphconnection.py", line 39, in <module>
for babyname in babynames:
File "C:\Python27\lib\site-packages\py2neo\core.py", line 770, in find
response = self.cypher.post(statement, parameters)
File "C:\Python27\lib\site-packages\py2neo\core.py", line 667, in cypher
metadata = self.resource.metadata
File "C:\Python27\lib\site-packages\py2neo\core.py", line 213, in metadata
self.get()
File "C:\Python27\lib\site-packages\py2neo\core.py", line 258, in get
response = self.__base.get(headers=headers, redirect_limit=redirect_limit, **kwargs)
File "C:\Python27\lib\site-packages\py2neo\packages\httpstream\http.py", line 966, in get
return self.__get_or_head("GET", if_modified_since, headers, redirect_limit, **kwargs)
File "C:\Python27\lib\site-packages\py2neo\packages\httpstream\http.py", line 943, in __get_or_head
return rq.submit(redirect_limit=redirect_limit, **kwargs)
File "C:\Python27\lib\site-packages\py2neo\packages\httpstream\http.py", line 433, in submit
http, rs = submit(self.method, uri, self.body, self.headers)
File "C:\Python27\lib\site-packages\py2neo\packages\httpstream\http.py", line 362, in submit
raise SocketError(code, description, host_port=uri.host_port)
py2neo.packages.httpstream.http.SocketError: Operation not permitted
You are trying to access neo4j via https on the standard port for http (7474):
url_graph_conn = "https://neo4j:password#52.10.**.***:7474/db/data/"
The standard port for a https connection is 7473. Try:
url_graph_conn = "https://neo4j:password#52.10.**.***:7473/db/data/"
And make sure you can access the web interface via https:
https://52.10.**.***:7473/browser/
You can change/see the port settings in your neo4j-server.properties file.

Categories

Resources