Code Monkey home page Code Monkey logo

msdocs-flask-web-app-managed-identity's Introduction

Deploy a Python (Flask) app to Azure with Managed Identity

This Python app is a simple restaurant review application using the Flask framework. The app uses Azure App Service, Azure Database for PostgreSQL (Flexible Server) relational database service, and Azure Storage. When deployed, Azure managed identity allows the web app hosted in App Service to connect to the database and storage resources without the need to specify sensitive connection info in code or environment variables.

This sample app can be run locally and then deployed to Azure, hosted in a fully managed Azure App Service. For more information on how to use this web app, see Overview: Deploy a Python web app to Azure with managed identity.

If you need an Azure account, you can create on for free.

A Django sample application with similar functionality is at https://github.com/Azure-Samples/msdocs-django-web-app-managed-identity.

Requirements

The requirements.txt has the following packages:

Package Description
Flask Web application framework.
SQLAlchemy Provides a database abstraction layer to communicate with PostgreSQL.
Flask-SQLAlchemy Adds SQLAlchemy support to Flask application by simplifying using SQLAlchemy. Requires SQLAlchemy.
Flask-Migrate SQLAlchemy database migrations for Flask applications using Alembic. Allows functionality parity with Django version of this sample app.
pyscopg2 PostgreSQL database adapter for Python.
python-dotenv Read key-value pairs from .env file and set them as environment variables. In this sample app, environment variables describe how to connect to the database and storage resources. Because managed identity is used no sensitive information is included in environment variables.

Flask's dotenv support sets environment variables automatically from an .env file.
flask_wtf Form rendering, validation, and CSRF protection for Flask with WTForms. Uses CSRFProtect extension.
azure-blob-storage Microsoft Azure Storage SDK for Python
azure-identity Microsoft Azure Identity Library for Python

DefaultAzureCredential

The DefaultAzureCredential is used in the app.py file. For example:

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient

azure_credential = DefaultAzureCredential(exclude_shared_token_cache_credential=True)
blob_service_client = BlobServiceClient(
    account_url=account_url,
    credential=azure_credential)

The DefaultAzureCredential is also used to get a token for PostgreSQL in the get_conn.py file when running in Azure.

azure_credential = DefaultAzureCredential()
token = azure_credential.get_token("https://ossrdbms-aad.database.windows.net")
conn = str(current_app.config.get('DATABASE_URI')).replace('PASSWORDORTOKEN', token.token)

msdocs-flask-web-app-managed-identity's People

Contributors

hyoshioka0128 avatar microsoft-github-operations[bot] avatar microsoftopensource avatar vmagelo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

msdocs-flask-web-app-managed-identity's Issues

Token Refresh needed?

Line 34 of app.py configures the sqlalchemy connection string with a token from DefaultAzureCredential. Will this token refresh automatically (such as to re-connect to the database) once it expires? This seems to hardcode the token into the connection string. How should this be handled?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.