Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to mysql server with SSL from a flask app

I want to connect to a mysql server via flask and

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://your-username:your-password@localhost/schema'

How can I add ca-cert, client-key and client-cert to the connection?

like image 817
toto11 Avatar asked Sep 11 '14 10:09

toto11


2 Answers

You can add theses informations in your URI like this :

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://your-username:your-password@localhost/schema?ssl_key=MyCertFolder/client-key.pem&ssl_cert=MyCertFolder/client-cert.pem'

Or using the SQLAlchemy create_engine, take a look to this post

like image 111
Thomas N. Avatar answered Jan 03 '23 19:01

Thomas N.


Another solution is to use sqlalchemy.engine.url.URL to define the URL.

sqlUrl = sqlalchemy.engine.url.URL(
    drivername="mysql+pymysql",
    username=db_user,
    password=db_pass,
    host=db_host,
    port=3306,
    database=db_name,
    query={"ssl_ca": "main_app/certs/BaltimoreCyberTrustRoot.crt.pem"},
)
create_engine(sqlUrl)

You can include SSL parameters as a dictionary in the query argument.

This approach is useful if you are using Flask to initialize the SqlAlchemy engine with a config parameter like SQLALCHEMY_DATABASE_URI rather than directly using create_engine.

like image 38
STEM FabLab Avatar answered Jan 03 '23 19:01

STEM FabLab