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.
Related
i recently started experimenting in the RingCentral sandbox environment and facing issues with this part of the code
rcsdk = SDK(CLIENTID,CLIENTSECRET,SERVERURL)
platform = rcsdk.platform()
try:
platform.login(USERNAME,EXTENSION,PASSWORD,JWT)
except Exception as e:
sys.exit("Unable to authenticate to platform. Check credentials." + str(e))
I went to know if there is an alternative to code
Looking at your code snippet, I can see that the platform.login() function signature is incorrect as you are passing extra argument.
Correct function signatures in Python are:
Logging with username, password flow: platform.login(USERNAME, EXTENSION, PASSWORD)
Logging with JWT : platform.login( jwt=JWT_TOKEN )
Make sure to replace the UPPERCASE String with the actual credential found in RingCentral Developer Portal for your app's sandbox environment and it should work.
Reference:
https://developers.ringcentral.com/guide/authentication
I want to implement a check if import pdblp is active and if not exit the session.
I note from this link (Bloomberg Anywhere + pdblp or xbbg + not logged in) that a session:
remains logged in for 3 days.
is logged out if a session is opened on another pc.
Therefore, i want to implement a try-execpt block like this:
import pdblp
# check if connected
try:
con = pdblp.BCon(timeout=5000)
con.start()
except Exception as e:
print('not logged in:', e)
my question is, would the above be sufficient to validate the connection ?
(ie. would the above throw an error, e).
TL;DR
Use the newer blp package instead of blpapi, which is no longer supported by the creator.
pip install blp
try:
from blp import blp
con = blp.BlpQuery().start()# change debug to true to see issues
except:
print('NO BLOOMBERG')
Yes, your try-except is sufficient. The except statement will throw you the error to know that the Bloomberg connection is not working (the link you included to the other SO article correctly noted that the python API will only work under the same conditions that the Excel API does for Bloomberg).
However, it was troublesome for me that con = pdblp.BCon(timeout=5000) con.start() would try to connect for nearly 1 minute. The new blp package will kick out an error in 17 seconds. Just change your con to the new .start()
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
I cannot give too many details due to confidentiality, but I will try to specify as best as I can.
I have an AWS role that is going to be used to call an API and has the correct permissions.
I am using Boto3 to attempt to assume the role.
In my python code I have
sts_client = boto3.client('sts')
response = sts_client.assume_role(
RoleArn="arn:aws:iam::ACCNAME:role/ROLENAME",
RoleSessionName="filler",
)
With this code, I get this error:
"An error occurred (InvalidClientTokenId) when calling the AssumeRole operation: The security token included in the request is invalid."
Any help would be appreciated. Thanks
When you construct the client in this way, e.g. sts_client = boto3.client('sts'), it uses the boto3 DEFAULT_SESSION, which pulls from your ~/.aws/credentials file (possibly among other locations; I did not investigate further).
When I ran into this, the values for aws_access_key_id, aws_secret_access_key, and aws_session_token were stale. Updating them in the default configuration file (or simply overriding them directly in the client call) resolved this issue:
sts_client = boto3.client('sts',
aws_access_key_id='aws_access_key_id',
aws_secret_access_key='aws_secret_access_key',
aws_session_token='aws_session_token')
As an aside, I found that enabling stream logging was helpful and used the output to dive into the boto3 source code and find the issue: boto3.set_stream_logger('').
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')