While trying to connect to the database I get a strange error:
DatabaseError: SQLCODE -1829 in CONNECT:
ì¦à : Cannot open file 'os.iem'
ì¦à : Cannot open file 'os.iem'
I can confirm that the file is present in $INFORMIXDIR/msg/en_us/0333/ directory. The environment variables INFORMIXDIR, INFORMIXSERVER and ONCONFIG are set correctly and as expected by my instance. Any clues on what I might be doing wrong?
Am connecting using informixdb (version 2.5) and am connecting to Informix version 11.5. The user who is connecting has the requisite permissions.
ok figured this one out! It appears only the env values set before the import of the informixdb module affect the way the module works. So the following does not work:
import informixdb
os.environ["INFORMIXDIR"] = "/opt/informix"
...
def conn(db):
informixdb.connect(db, self.username, self.passwd)
...
conn('local')
whereas the following does:
os.environ["INFORMIXDIR"] = "/opt/informix"
import informixdb
...
def conn(db):
informixdb.connect(db, self.username, self.passwd)
...
conn('local')
Related
I am connecting to a db using jaydebeapi and a jdbc driver using the following snippet, that works fine when the all parameters are correctly specified.
I am storing my credentials in environment variables e.g. os.environ.get('credentials')
import jaydebeapi as dbdriver
import os
cnxn = None
server_name=''
server_jdbc_port=
server_database=''
name_env_credentials=''
name_env_pwd=''
try:
driver_path = "XXXXXXX"
conn_uri = "jdbc:://%s:%s/%s?" % (server_name,server_jdbc_port, server_database)
cnxn = dbdriver.connect( "XXXXXXX",
conn_uri,
driver_args = {"user": os.environ.get(name_env_credentials),
"password": os.environ.get(name_env_pwd)},
jars = driver_path)
print('Connection open')
except (Exception) as error:
raise error
finally: # in any case close connection and free resources
if cnxn is not None:
cnxn.close()
print('Database connection closed.')
If there are some errors in my credentials/access rights I get, which is ok.
java.sql.SQLExceptionPyRaisable: java.sql.SQLException: authentication error:
Insufficient privileges to connect to the database 'XXXXX'
However, when I wrap the above into a function, pytest-parametrized on server_name,server_jdbc_port,server_database,name_env_credentials,name_env_pwd.
######test_issue.py
#pytest.mark.parametrize("server_name,server_jdbc_port,server_database,name_env_credentials,name_env_pwd", testdata)
def test_connectivity():
# above code
When I run
pytest test_issue.py
and an error in raised, the I find my credentials in plain text in the logs, which is a problem.
To be precise, I am not seeing the content name_env_credentials (would be acceptable) but really the contents of os.environ.get(name_env_credentials) (below for example MYPLAINTEXTUSERNAME) in the test logs/tracebacks
test_issue.py:56: in test_connectivity
cnxn = dbdriver.connect( "--------",
/opt/conda/lib/python3.8/site-packages/jaydebeapi/__init__.py:412: in connect
jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
[........]
driver_args = {'password': MYPLAINTEXTPASSWORD, 'user': MYPLAINTEXTUSERNAME}
[........]
jaydebeapi.__version__='1.2.3'
You are using the default traceback logger of pytest, which also logs the arguments passed to a specific function. One way to solve this kind of leak is to use another traceback mode. You can find the general documentation at this
link. In that link you can find two interesting traceback modes:
--tb=short
--tb=native
Both give you all the information you need during your specific test since:
They still give you information about the tests failed or succeded
Since you are using parametrized tests, the logs about failures will look like FAILED test_issue.py::test_connectivity[server_name-server_jdbc_port-server_database-name_env_credentials-name_env_pwd], in this way you can identify the actual failing test
Mind that this solution, while avoiding to log the credentials used at test time, these credentials used during testing must not be the same that will be used outside the test environment. If you are concerned with the fact that in case of failures in production, the logger could leak your credentials, you should setup you logger accordingly and avoid to log the default text of the exception.
I'm trying to use mongodb but for some reason i cant put data into a collection.
Here's my code:
import pymongo
from pymongo import MongoClient
mongo_url = "mongodb+srv://<User>:<Password>#cluster0.yozx6.mongodb.net/<dbname>?retryWrites=true&w=majority"
cluster = MongoClient(mongo_url)
db = cluster["TestDatabse"]
collection = db["TestCollection"]
post = {"number": 7}
collection.insert_one(post)
for some reason the data the collection.insert_one line isn't working and it isn't giving an error message either. The program seems to get stuck on it. Can somebody please tell me what I'm doing wrong and how I can fix it.
There are different things to verify for you.
Version compatibility:
First of all insertOne was introduced inmongoDB 3.2, so make sure that you are connecting to a newer pymongo version, and also that the version of pymongo is compatible with your version
Network connection:
Make sure you have a stable connection to your DB if it is remote.
Then use:
result = cluster.admin.command("ismaster")
to check if the db is accesible, if this throws a ConnectionError there is a problem with the connection.
User permissions
Check if the user and password you are using has permissions to insert documents to the given collection.
On the mongo shell:
db.getRoles(
{
rolesInfo: 1,
showPrivileges:true,
showBuiltinRoles: true
}
)
should show:
roles: [
{
role: "readWrite",
db: "TestDatabse"
}
]
for the user User.
Result of insert
Check the result. insert_one returns inserted_id and acknowledged.
res = collection.insert_one(post)
res.acknowledged # should be True
res.inserted_id
Logs
Check your logs, you can find the log path for your server by running:
cat /etc/mongod.conf | grep log
I am stuck using python-ldap (used by flask_appbuilder) for a project. I noticed that the python-ldap is failing with "Error initializing SSL/TLS". To verify that my ldap settings were correct, I wrote a script using ldap3 alongside python-ldap and noticed that ldap3 succeeds when using the same configuration as python-ldap.
The test code is:
import ldap3
import ldap
import ssl
URL = 'ldaps://....example.com:636'
USER = 'CN=...'
PASS = '...'
SEARCH = 'OU=...'
FILTER = '(&(objectClass=user))'
# ldap3
server = ldap3.Server(
URL,
use_ssl=True,
tls=ldap3.Tls(validate=ssl.CERT_REQUIRED),
)
conn = ldap3.Connection(server, USER, PASS)
conn.bind()
conn.search(SEARCH, FILTER)
# python-ldap
con = ldap.initialize(URL, 4)
con.set_option(ldap.OPT_REFERRALS, 0)
con.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
con.set_option(ldap.OPT_X_TLS_NEWCTX,0)
con.start_tls_s()
con.bind_s(USER, PASS)
I am able to successfully get results from the ldap3 block, but when the python-ldap start_tls_s block is reached I get:
ldap.UNAVAILABLE: {'info': u'00000000: LdapErr: DSID-0C091338, comment: Error initializing SSL/TLS, data 0, v4563', 'desc': u'Server is unavailable'}
The behaviour is consistent across both my laptop and a test machine (both are linux boxes). Does anyone know why this difference occurs?
I have deployed this Python app on Heroku and i want it to connect to a MongoDB Atlas cluster. I used my string to connect to the cluster, but for some reason i keep getting raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: bad auth Authentication failed. I checked twice and both the user and the password are correct. Any idea on why this is happening?
from pymongo import MongoClient
import time
import random
import time
import datetime
client = MongoClient('mongodb+srv://USER:<MYPASSWORD>#test-2liju.mongodb.net/test?retryWrites=true')
db = client.one
mycol = client["tst"]
while True:
test = int(random.randrange(-99999990,90000000,1))
dic = {"num": test}
result = db.tst.insert_one(dic)
print(test)
time.sleep(5)
Stupid error, i had to type MYPASSWORD instead of <MYPASSWORD>, without the <>
Don't use any special char in password, like '+' or '='.
I use OpenSSL to generate a password like u4wY9AOwnOLMY+h9EQ==. Came across bad auth Authentication failed.
After using MongoDB Compass it told me don't use special char, so I remove those and use like 'u4wY9AOwnOLMYh9EQ'.
Then it works.
check the compatibility of the version of the Python driver you choose from the Mongodb Atlas Connections. versions above 3.4 are not supported by mongoengine flask
mongodb_uri = "mongodb://[username:password#]XX.XX.XX.XX"
client = MongoClient(mongodb_uri)
db = client['database']
print(db)
collection_taxonomy = db['collection']
doc = collection_taxonomy.find()
pprint.pprint(doc)
for each_doc in doc:
pprint.pprint(each_doc)
I am getting time out error as I try to print each document of the collection. However, I do not get time out error when I try to connect to localhost.
Tried connecting with connect=False
client = MongoClient(mongodb_uri,connect=False)
Still I get time out error while i print each document.
What could be wrong? Appreciate if someone can help me .
I am using Python 3.5 and Pymongo 3.5.1
Thanks,
-Roopa
is "mongodb://[username:password#]XX.XX.XX.XX" the actual value of mongodb_uri or have you substituted that for the value in your actual application?
The "getaddrinfo failed" message indicates that the hostname you put in mongodb_uri is invalid.
Removed square brackets([]) after substituting values in actual application.
"mongodb://username:password#XX.XX.XX.XX"
Works like a charm.!
Thanks a ton.
Roopa
I got the same error when i had a restricted rights on the user account which was trying to connect, so please try changing the user access rights or use a different account with higher privileges
user with the below rights failed
readWrite#dbname.colname
user with the below rights worked (note this is the user created for Atlas application)
atlasAdmin#admin
The URI should be like "mongodb://username:password#host", where the host is the hostname or IP.
This happened to me when I was connecting with the name, but the host name changed, so I changed the URI to connect via the machine's IP.