Git Product home page Git Product logo

azure-python-webapp-sqldb-managed-identity's Introduction

azure-python-webapp-sqldb-managed-identity

Introduction

This is a sample application, used to test how Managed Identity authentication with Azure SQL Database can be implemented in Azure Web App.

It should work as well in an Azure VM.

Local Installation

To work on your local machine:

python -m venv .venv
source .venv/Scripts/activate
pip install -r requirements.txt

Start the development server:

export FLASK_ENV="development"

flask run

Prepare Managed Identity for Database Connectivity

This activity involves two steps:

Azure Deployment Using Kudu Services

  1. Create Azure Web App (i.e. my-webapp)
  2. Open the Web App blade, section Deployment Center
  3. Select and configure Github as a source
  4. Use Kudu as deployment service

Testing the Web App

Navigate to the root of your web app, e.g. https://my-webapp.azurewebsites.net to see the Azure SQL Database version:

{
    "rows":[
        {"":"Microsoft SQL Azure (RTM) - 12.0.2000.8 \n\tMay 15 2020 00:47:08 \n\tCopyright (C) 2019 Microsoft Corporation\n"}
    ]
}

To get more exciting output, try the /tables endpoint which returns a list of tables in the database.

Discussion

To authenticate:

  1. Get Azure AD access token for accessing Azure SQL DB, using Azure Identity SDK for Python
  2. Create connection string with a token.
  3. Create pyodbc connection, using the connection string.

Above steps in a single connect_db function:

def connect_db(server=None, database=None, driver=None):
    DB_SERVER = 'tcp:' + (server or os.environ['DB_SERVER']) + '.database.windows.net'
    DB_NAME = database or os.environ['DB_NAME']
    DB_DRIVER = driver or '{ODBC Driver 17 for SQL Server}'
    DB_RESOURCE_URI = 'https://database.windows.net/'

    az_credential = DefaultAzureCredential()
    access_token = az_credential.get_token(DB_RESOURCE_URI)
    
    token = bytes(access_token.token, 'utf-8')
    exptoken = b"";
    for i in token:
        exptoken += bytes({i});
        exptoken += bytes(1);
    tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
    
    connection_string = 'driver='+DB_DRIVER+';server='+DB_SERVER+';database='+DB_NAME
    conn = pyodbc.connect(connection_string, attrs_before = { 1256:bytearray(tokenstruct) });
    
    return conn

azure-python-webapp-sqldb-managed-identity's People

Contributors

ivangeorgiev avatar

Watchers

 avatar  avatar

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.