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.1.tar.gz (4.9 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.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for az_container_helper-0.1.1.tar.gz
Algorithm Hash digest
SHA256 34168a5b93e2c730d6b2716bf743c8a992b8a5addf53592a7eef6368f0b328bf
MD5 a98bc62273d4c84160ea56c95631cf7b
BLAKE2b-256 51ea8b046c4fdd4dfa960dda1061c922d65cac1d7021c90436d13599a7bc1eca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for az_container_helper-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 87a3df2bdc50d74f5fe48357e5eec244fba7dbcf4ee4ad0474f68cb435b7dbe6
MD5 5073fee018c72f15058e63c67290e11c
BLAKE2b-256 81a2c4a41c53a18f8104346adddf7843f5b5c1fbfa4ff710ad9b93f5f9cda084

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