Unable to connect NEO4J using neo4j python driver - python

I'm unable to connect to Neo4j using neo4j-python-driver version 5.3.0.
Requirement is using Neo4j python driver to query NEO4J DB using cypher queries.
It gives the error failed to connect to server even when the database is up and running and i can able to login and use through NEO4J Desktop.
Getting below error
neo4j.exceptions.ServiceUnavailable: Couldn't connect to <URI>:7687 (resolved to ()):
[SSLCertVerificationError] Connection Failed. Please ensure that your database is listening on the correct host and port and that you have enabled encryption if required. Note that the default encryption setting has changed in Neo4j 4.0. See the docs for more information. Failed to establish encrypted connection. (code 1: Operation not permitted)
Note : URI hided in the above error.
I have added the exception to Ignores certificate verification issue, but it won't solve the issue.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
Appreciate your help to resolve the issue.
i'm connecting via below snippet
from neo4j import GraphDatabase
import urllib3
#Ignores certificate verification issue
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Initialize connection to database
driver = GraphDatabase.driver('bolt+s://<URI>:7687', auth=(',username', '<password'))
query = 'match (n:Event{name:"test"}) return n'
#Run Cypher query
with driver.session() as session:
info = session.run(query)
for item in info:
print(item)

No way to know how you are connecting, but we do:
from neo4j import GraphDatabase
address="bolt://localhost:7687"
auth=('neo4j', "password")
driver = GraphDatabase.driver(address, auth=auth, encrypted=False)
....

Have you tried py2neo?
I use below with dev running in docker and prod running Aura.
from py2neo import Graph
self.graph = Graph(os.getenv('DB_URI'), auth=(
os.getenv('DB_USER'), os.getenv('DB_PASS')))
DB_URI is 'neo4j://0.0.0.0:7687' on dev
and 'neo4j+s://xxxx' on prod

Related

How to connect a remote Hive server from Python using JDBC and SSL?

I have below Hive client details which require an SSL connection to access the database
client_url="jdbc:hive2://myhiveserver.india.mycompany.com:9029/default;ssl=true;transportMode=http;httpPath=gateway/default/hive;trustStorePassword=selfsign"
certificate=mycertficate.pem
So which is the right approach to access Hive using JDBC and SSL in Python? I don't see right example for accessing Hive with attaching SSL certificate in my case?
using jaydebeapi and jpype worked for me.
below files need to be added to jvm
krb5.config file - your admin will be able to provide this
keytab file - for user trying to access hive
jaas file - (sample mentioned below)
code:
jaas = './config/jass.conf'
krb5 = './config/krb5.conf'
keytab = './config/<userid>.keytab'
jar = './jars/HiveJDBC41.jar'
principal = "<host>#<domain>"
jdbc_url = "jdbc:hive2://<host>:<port>/default;KrbRealm=<domain>;AuthMech=1;KrbHostFQDN=<host>;KrbServiceName=hive;principal=<host>#<domain>;ssl=1;"
jvm_path = jpype.getDefaultJVMPath()
jpype.startJVM(jvm_path,
'-Djava.class.path='+ jar,
'-Djavax.security.auth.useSubjectCredsOnly=true',
'-Djava.security.krb5.conf='+krb5,
'-Djava.security.auth.login.config='+jaas,
'-Djava.security.auth.login.config.keyTabFile='+keytab,
'-Djava.security.auth.login.config.principal='+ principal,
'-Dhadoop.security.authentication="kerberos"',
)
conn = jaydebeapi.connect("com.cloudera.hive.jdbc41.HS2Driver", jdbc_url)
jaas file:
Client {
com.sun.security.auth.module.Krb5LoginModule required
useTicketCache=true
doNotPrompt=true
useKeyTab=true
principal="<host>#<domain>"
debug=true
keyTab="./config/<userid>.keytab";
};

AWS RDS Proxy error (postgres) - RDS Proxy currently doesn’t support command-line options

I try to read or write from/to an AWS RDS Proxy with a postgres RDS as the endpoint.
The operation works with psql but fails on the same client with pg8000 or psycopg2 as client libraries in Python.
The operation works with with pg8000 and psycopg2 if I use the RDS directly as endpoint (without the RDS proxy).
sqlaclchemy/psycopg2 error message:
Feature not supported: RDS Proxy currently doesn’t support command-line options.
A minimal version of the code I use:
from sqlalchemy import create_engine
import os
from dotenv import load_dotenv
load_dotenv()
login_string = os.environ['login_string_proxy']
engine = create_engine(login_string, client_encoding="utf8", echo=True, connect_args={'options': '-csearch_path={}'.format("testing")})
engine.execute(f"INSERT INTO testing.mytable (product) VALUES ('123')")
pg8000: the place it stops / waits for something is in core.py:
def sock_read(b):
try:
return self._sock.read(b)
except OSError as e:
raise InterfaceError("network error on read") from e
A minimal version of the code I use:
import pg8000
import os
from dotenv import load_dotenv
load_dotenv()
db_connection = pg8000.connect(database=os.environ['database'], host=os.environ['host'], port=os.environ['port'], user=os.environ['user'], password=os.environ['password'])
db_connection.run(f"INSERT INTO mytable (data) VALUES ('data')")
db_connection.commit()
db_connection.close()
The logs in the RDS Proxy looks always normal for all the examples I mentioned - e.g.:
A new client connected from ...:60614.
Received Startup Message: [username="", database="", protocolMajorVersion=3, protocolMinorVersion=0, sslEnabled=false]
Proxy authentication with PostgreSQL native password authentication succeeded for user "" with TLS off.
A TCP connection was established from the proxy at ...:42795 to the database at ...:5432.
The new database connection successfully authenticated with TLS off.
I opened up all ports via security groups on the RDS and the RDS proxy and I used an EC2 inside the VPC.
I tried with autocommit on and off.
The 'command-line option" being referred to is the -csearch_path={}.
Remove that, and then once the connection is established execute set search_path = whatever as your first query.
This is a known issue that pg8000 can't connect to AWS RDS proxy (postgres). I did a PR https://github.com/tlocke/pg8000/pull/72 let see if Tony Locke (the father of pg8000) approves the change. ( if not you have to change the lines of the core.py https://github.com/tlocke/pg8000/pull/72/files )
self._write(FLUSH_MSG)
if (code != PASSWORD):
self._write(FLUSH_MSG)

Using credentials from Db2 (Warehouse) on Cloud to initialize flask-sqlalchemy

In a Flask app with flask-sqlalchemy, I am trying to initialize a connection to Db2 Warehouse on Cloud by setting SQLALCHEMY_DATABASE_URI to one of the parts provided in the service credentials. In the past, going with the uri component worked fine, but my new service has SSL connections only.
app.config['SQLALCHEMY_DATABASE_URI']=dbInfo['uri']
This results in connection errors
File "/home/vcap/deps/0/python/lib/python3.6/site-packages/ibm_db_dbi.py", line 592, in connect
conn = ibm_db.connect(dsn, '', '', conn_options)
Exception: [IBM][CLI Driver] SQL30081N A communication error has been detected. Communication protocol being used: "TCP/IP". Communication API being used: "SOCKETS". Location where the error was detected: "52.117.199.197". Communication function detecting the error: "recv". Protocol specific error code(s): "104", "*", "0". SQLSTATE=08001 SQLCODE=-30081 During handling of the above exception, another exception occurred:
It seems that the driver is not accepting the ssl=true option specified in the URI string. What parts of the service credentials should I use? Would I need to build the URI string manually?
This is only a partial answer because of a workaround. I am using the port information from the service credentials to modify the connection URI:
if dbInfo['port']==50001:
# if we are on the SSL port, add additional parameter for the driver
app.config['SQLALCHEMY_DATABASE_URI']=dbInfo['uri']+"Security=SSL;"
else:
app.config['SQLALCHEMY_DATABASE_URI']=dbInfo['uri']
By adding Security=SSL to the uri, the driver picks up the info on SSL and uses the correct settings to connect to Db2.

Is it possible to connect to eDirectory with python ldap3?

I am trying to connect to eDirectory using python. It is not as easy as connecting to active directory using python so I am wondering if this is even possible. I am currently running python3.4
I'm the author of ldap3, I use eDirectory for testing the library.
just try the following code:
from ldap3 import Server, Connection, ALL, SUBTREE
server = Server('your_server_name', get_info=ALL) # don't user get_info if you don't need info on the server and the schema
connection = Connection(server, 'your_user_name_dn', 'your_password')
connection.bind()
if connection.search('your_search_base','(objectClass=*)', SUBTREE, attributes = ['cn', 'objectClass', 'your_attribute'])
for entry in connection.entries:
print(entry.entry_get_dn())
print(entry.cn, entry.objectClass, entry.your_attribute)
connection.unbind()
If you need a secure connection just change the server definition to:
server = Server('your_server_name', get_info=ALL, use_tls=True) # default tls configuration on port 636
Also, any example in the docs at https://ldap3.readthedocs.org/en/latest/quicktour.html should work with eDirectory.
Bye,
Giovanni

Failed to Login as 'Domain\ComputerName' pyodbc with py2exe

Ok so I have a script that connects to a mssql db and i need to run as a service which I have already accomplished but when I run it as a service it overrides my credentials that I have put in when i connect to the db with the ad computer account.
It runs perfect when i run it on its own and not as a service.
My Connection String is:
'DRIVER={SQL Server};SERVER=MyServer;DATABASE=MyDB;UID=DOMAIN\myusername;PWD=A;Trusted_Connection=True'
The Error is:
Error: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'DOMAIN\COMPUTERNAME')
Any Advice?
In the last project I worked on, I found that DRIVER={SQL Server};SERVER=SERVERNAME;DATABASE=DBName is sufficient to initiate a db connection in trusted mode.
If it still does not work, it is probably either
1) the account DEEPTHOUGHT on mssql server is not set up properly.
2) the runAs in the service is not set up properly (why error message mentions 'ComputerName' instead of 'DEEPTHOUGHT'?)
The following connection string will use Windows authentication, using the account running the service to authenticate with the database. Change the service account to one that has database access:
'DRIVER={SQL Server};SERVER=SERVERNAME;DATABASE=DBName;Trusted_Connection=yes'
To change service account:
Start -> Run -> services.msc
Right-click service -> Properties
Log On tab
OK/Apply to save changes

Categories