Git Product home page Git Product logo

azure-api's Introduction

Python API for Azure Functions with Terraform

AzureTerraformAzureFunctionsPythonOpenAPI

NOTE: Few manual steps should be done in order CI/CD to work properly.

Required for installation

What would be used ( aka dependencies )


Pre-configuration

After successful creation of Azure subscription and dependencies installation:

  • We need to log in into Azure CLI, after running command below - follow authorization process in your browser:
    az login
# Parameters should be updated according to infrastructure/terraform.tfvars
    az group create --name dev-storage-resource-group --location ukwest
    az storage account create \
      --name devazurestorageaccount \
      --resource-group dev-storage-resource-group \
      --location ukwest \
      --sku Standard_RAGRS \
      --kind StorageV2
    az storage container create --name devtfstate --account-name devazurestorageaccount
  • Let's perform some terraforming from here, navigate to the repo azure-api/infrastructure directory and perform following commands one-by-one:
      terraform init
      terraform validate

      terraform import azurerm_resource_group.resource_group \
        $(az group list | grep resourceGroups/dev-storage-resource-group | cut -d'"' -f 4)
      terraform import azurerm_storage_account.storage_account \
        $(az resource list | grep storageAccounts/devazurestorageaccount | cut -d'"' -f 4)
      
      terraform plan -out deployment.tfplan
      terraform apply -auto-approve deployment.tfplan
  • We should waite some time for infrastructure to start up. (about 10 minutes, depends on the region and setup)

If you want, you can run scraper right now, it should take about 5 minutes to complete.


Secure access setup

  • Navigate to https://portal.azure.com/#blade/HubsExtension/BrowseResource/resourceType/Microsoft.Web%2Fsites/kind/functionapp

  • Click on your app (default: azureapi-dev-function-app), click Authentication (classic) from the left navigation menu

  • Turn On App Service Authentication, choose Log in with Azure Active Directory from drop-down menu, click on Azure Active Directory authentification provider.

    Choose Express mode, provide a name for your app or proceed with given by default and press OK. Click Save on the top left side of configuration

  • Navigate to https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/Overview

  • Click App registrations from the left navigation menu. You should see newly created app under application list, click on it.

  • Click Authentication from the left navigation menu. In the Redirect URIs section update url with https://localhost value. Ensure that checkbox ID tokens (used for implicit and hybrid flows) is marked, press Save on the top left side of configuration

  • As usual, click Certificates & secrets from the left navigation menu. Under Client secrets section, press New client secret button, add optional description and choose expiration period from drop-down menu, click Add

IMPORTANT - Copy prompted client secret value!

  • Click Overview from the left navigation menu. Copy Application (client) ID & Directory (tenant) ID

How to access

We need to use previously copied values to obtain authorization code and exchange it with Access Token:

  • Update the placeholders with appropriate values and follow the url:
  https://login.microsoftonline.com/{{tenant_id}}/oauth2/authorize?client_id={{client_id}}&response_type=code&response_mode=query&resource_id={{client_id}}&redirect_uri=https://localhost
  • Login with your Azure subscription credentials, on pop-up - click Accept
  • Copy code in the link after code part and before session state parameter

https://localhost/?code={{your_code}}&session_state= ...

curl -X POST --form 'grant_type=authorization_code' --form 'client_id={{client_id}}' --form 'client_secret={{client_secret}}' --form 'resource={{client_id}}' --form 'response_type=code' --form 'redirect_uri=https://localhost' --form 'code={{your_code}}' https://login.microsoftonline.com/common/oauth2/token

NOTE You could face such error in response - The provided authorization code or refresh token has expired due to inactivity, just send a new request for code as it's temporary and could expire if you have a delay in steps.

  • Now you can copy your token, search for it between access_token and refresh_token keys:

{"token_type":"Bearer","scope":"User.Read","expires_in":"3599","ext_expires_in":"3599","expires_on":"1621489934","not_before":"1621486034","resource":"00000000-0000-0000-0000-000000000000","access_token":"{{your_token}}","refresh_token": ... }

  • Finally, verify the access to your API with this token:

curl -vL -H 'Authorization: Bearer {{your_token}}' http://azureapi-dev-function-app.azurewebsites.net/api/v1/library/books


Feel free to suggest any changes and improvements

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.