|
2 | 2 | # Copyright (c) Microsoft Corporation. |
3 | 3 | # Licensed under the MIT License. |
4 | 4 | # ------------------------------------ |
5 | | -from typing import Any |
| 5 | +import os |
| 6 | +from typing import Any, Optional |
6 | 7 | from .client_assertion import ClientAssertionCredential |
7 | 8 | from ..._credentials.workload_identity import TokenFileMixin |
| 9 | +from ..._constants import EnvironmentVariables |
8 | 10 |
|
9 | 11 |
|
10 | 12 | class WorkloadIdentityCredential(ClientAssertionCredential, TokenFileMixin): |
11 | 13 | """WorkloadIdentityCredential supports Azure workload identity on Kubernetes. |
12 | 14 | See the `workload identity overview <https://learn.microsoft.com/azure/aks/workload-identity-overview>`_ |
13 | 15 | for more information. |
14 | 16 |
|
15 | | - :param str tenant_id: ID of the application's Azure Active Directory tenant. Also called its "directory" ID. |
16 | | - :param str client_id: The client ID of an Azure AD app registration. |
17 | | - :param str file: The path to a file containing a Kubernetes service account token that authenticates the identity. |
| 17 | + :keyword str tenant_id: ID of the application's Azure Active Directory tenant. Also called its "directory" ID. |
| 18 | + :keyword str client_id: The client ID of an Azure AD app registration. |
| 19 | + :keyword str file: The path to a file containing a Kubernetes service account token that authenticates the identity. |
18 | 20 | """ |
19 | | - def __init__(self, tenant_id: str, client_id: str, file: str, **kwargs: Any) -> None: |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + *, |
| 24 | + tenant_id: Optional[str] = None, |
| 25 | + client_id: Optional[str] = None, |
| 26 | + file: Optional[str] = None, |
| 27 | + **kwargs: Any |
| 28 | + ) -> None: |
| 29 | + tenant_id = tenant_id or os.environ.get(EnvironmentVariables.AZURE_TENANT_ID) |
| 30 | + client_id = client_id or os.environ.get(EnvironmentVariables.AZURE_CLIENT_ID) |
| 31 | + file = file or os.environ.get(EnvironmentVariables.AZURE_FEDERATED_TOKEN_FILE) |
| 32 | + if not tenant_id: |
| 33 | + raise ValueError( |
| 34 | + "'tenant_id' is required. Please pass it in or set the " |
| 35 | + f"{EnvironmentVariables.AZURE_TENANT_ID} environment variable" |
| 36 | + ) |
| 37 | + if not client_id: |
| 38 | + raise ValueError( |
| 39 | + "'client_id' is required. Please pass it in or set the " |
| 40 | + f"{EnvironmentVariables.AZURE_CLIENT_ID} environment variable" |
| 41 | + ) |
| 42 | + if not file: |
| 43 | + raise ValueError( |
| 44 | + "'file' is required. Please pass it in or set the " |
| 45 | + f"{EnvironmentVariables.AZURE_FEDERATED_TOKEN_FILE} environment variable" |
| 46 | + ) |
20 | 47 | super().__init__( |
21 | 48 | tenant_id=tenant_id, |
22 | 49 | client_id=client_id, |
|
0 commit comments