File: __init__.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (50 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os

import azure.functions as func
from azure.identity import ManagedIdentityCredential
from azure.storage.blob import BlobServiceClient

EXPECTED_VARIABLES = (
    "IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID",
    "IDENTITY_STORAGE_NAME_1",
    "IDENTITY_STORAGE_NAME_2",
    "MSI_ENDPOINT",
)


def main(req: func.HttpRequest) -> func.HttpResponse:
    # capture interesting environment variables for debugging
    env = "\n".join(f"{var}: {os.environ.get(var)}" for var in EXPECTED_VARIABLES)
    system_success_message = ""
    try:
        credential_system_assigned = ManagedIdentityCredential()
        credential_user_assigned = ManagedIdentityCredential(
            client_id=os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID")
        )

        client = BlobServiceClient(
            account_url=f"https://{os.environ['IDENTITY_STORAGE_NAME_1']}.blob.core.windows.net",
            credential=credential_system_assigned,
        )
        client2 = BlobServiceClient(
            account_url=f"https://{os.environ['IDENTITY_STORAGE_NAME_2']}.blob.core.windows.net",
            credential=credential_user_assigned,
        )

        containers = client.list_containers()
        for container in containers:
            print(container["name"])

        system_success_message = "Successfully acquired token with system-assigned ManagedIdentityCredential"

        containers = client2.list_containers()
        for container in containers:
            print(container["name"])

        return func.HttpResponse("Successfully acquired tokens with ManagedIdentityCredential")
    except Exception as ex:
        return func.HttpResponse(f"Test Failed: {repr(ex)}\n\n{system_success_message}\n\n{env}", status_code=500)