简体   繁体   中英

Login to Flask app with database credentials

This may be more of a concept question as opposed to a "How do I" question. Currently, I am looking to create a flask app to help me create somewhat complex MongoDB entries. This worked well originally in my CLI based script when these entries were more simple. I am looking for a way to create a safer way to deploy this app on some docker containers without giving the application itself access to the database. The idea I am trying to work out is, can I create a flask app that has no access to the DB until the user logs in and, that users login credentials are the database credentials. I am struggling to work this out, if anyone could provide some insight I would be most thankful.

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker


app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")


# sqlalchemy
if not os.getenv("DATABASE_URL"):
    raise RuntimeError("DATABASE_URL is not set")

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

In general practice, we setup environment variables before running the application so that the code itself do not explicitly show database url.

In mac and linux this is done using

export DATABASE_URL=yourdatabaseurl
flask run

In windows

set DATABASE_URL=yourdatabaseurl
flask run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM