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.3.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.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: az_container_helper-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 77df363adba9d783736396e26ebb2bc3879ba8f35a3d0dca272197a50313f8c8
MD5 a9272ca24efddd122b6cac5687404cbc
BLAKE2b-256 95d204020e8e518ca449f50c541c0d65c08cf0866e5ecf4c67f7316ff40d0df3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for az_container_helper-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d959451bd65ebf9347c24d58a97118b28b44652cdb6d3f89690b39ab13b84aa7
MD5 44f3dd2da25c9bf5f998538de25473a7
BLAKE2b-256 a1fb9619790399d9e102bc4356eecbdbb8f4884a799f7eb970aa2ea7db2f68d2

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