Skip to main content

Helper classes and methods for using Azure services from inside a local Docker container.

Project description

az-container-helper

Helper classes and methods for using Azure services from inside a local Docker container.

Why another helper package for azure? Well, I found none that addresses the issue that it is not possible to access Azure Services from inside a local Docker container, see

This is unfortunate, because it makes running your Python app inside a local container (e.g. for testing) impossible, if it uses Azure Services. The workarounds from the github issue only work, if you install the azure cli tools inside your container, which will not only increase the container size by a huge amount, but the cli tools are also strictly unnecessary when running your container on the Azure cloud.

Therefor this package offers some minimal wrapper classes and utils around the default Python Azure SDK, so you can use Azure services in your Python app also from inside a local container.

Usage

Retrieving credentials

To retrieve credentials for your app you can use a wrapper function in your Python app

from az_container_helper import get_credentials

credentials = get_credentials()

It will first try to read an existing access token from the ACCESS_TOKEN env variable. If no existing token is found it will fall back to standard DefaultAzureCredential workflow.

To make use of that wrapper when running in a local docker container first generate an access token for your azure service (e.g. for a key vault) using the az cli tools and export it as ACCESS_TOKEN env variable.

az login
export ACCESS_TOKEN=$(az account get-access-token --resource https://vault.azure.net --output tsv)

Then you can run your app inside a local docker container and inject the token as env variable in the container

docker run -e ACCESS_TOKEN=$ACCESS_TOKEN your-image

Retrieving secrets from an Azure KeyVault

To conveniently access secret from an Azure KeyVault, this package offers a wrapper class. The usage is quite simple. Here is this example we retrieve an api key from the Azure Key Vault and use it to initialize an OpenAI client.

from pathlib import Path
from az_container_helper.keyvault import KeyVault
from openai import AzureOpenAI

kv_url = "https://<your-keyvault-name>.vault.azure.net"
secrets_path = Path.cwd() / ".secrets"

kv = KeyVault(kv_url, secrets_path = secrets_path)

apikey = kv.get_secret("<name-of-secret>")

openai_client = AzureOpenAI(api_key=apikey.get_secret_valu()...)

Our KeyVault class makes use of the get_credentials() function. So to connect to the KeyVault from your local container you can inject an ACCESS_TOKEN as shown above.

However in case you cannot create an ACCESS_TOKEN locally (e.g. because you are not allowed to install az cli tools on your developers machine) you simply can't access the KeyVault from your local machine. In that case our KeyVault class will try to read secrets from a .secrets file, so this file acts an alternative local KeyVault. The format of the file is simply

name_of_secret=value_of_secret
name_of_secret_2=value_of_secret_2
...

This file can simply be copied into your local DockerContainer in the working directory of the application. With that you can run your app inside a local container even without any local access to the Azure KeyVault.

[!WARNING] Never commit the .secrets file into any versioning system and don't put it into your production container.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

az_container_helper-0.1.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

az_container_helper-0.1.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file az_container_helper-0.1.2.tar.gz.

File metadata

  • Download URL: az_container_helper-0.1.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for az_container_helper-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ca6925f8ad4c08b77e2ccf1a2b8fa42b887df8792b18432b0e119143efaec215
MD5 da8e541efe0b554c7e5a029bec2e3086
BLAKE2b-256 b4d6d6fb0990d37f0fdcf3c80638f9ae891526ade2855c5ee7624d10507f2418

See more details on using hashes here.

File details

Details for the file az_container_helper-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for az_container_helper-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a95979e9bd7e077283da073e139be1568054edccac0848a04c2b5c4c21f771ea
MD5 193ae63098ccea9d4ec2bbd8224b1ede
BLAKE2b-256 215ae71b1e587f1f3afc9be5c1c5311bebe87d38e58be65bccaf419377f3256d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page