Skip to content

Commit fa25f76

Browse files
authored
use env vars as default value (Azure#29632)
* use env vars as default value * updates * update * update
1 parent 289f3be commit fa25f76

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

sdk/identity/azure-identity/azure/identity/_credentials/workload_identity.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5+
import os
56
import time
67
from typing import Any
8+
from typing import Optional
79

810
from .client_assertion import ClientAssertionCredential
11+
from .._constants import EnvironmentVariables
912

1013

1114
class TokenFileMixin:
@@ -33,18 +36,37 @@ class WorkloadIdentityCredential(ClientAssertionCredential, TokenFileMixin):
3336
See the `workload identity overview <https://learn.microsoft.com/azure/aks/workload-identity-overview>`_
3437
for more information.
3538
36-
:param str tenant_id: ID of the application's Azure Active Directory tenant. Also called its "directory" ID.
37-
:param str client_id: The client ID of an Azure AD app registration.
38-
:param str file: The path to a file containing a Kubernetes service account token that authenticates the identity.
39+
:keyword str tenant_id: ID of the application's Azure Active Directory tenant. Also called its "directory" ID.
40+
:keyword str client_id: The client ID of an Azure AD app registration.
41+
:keyword str file: The path to a file containing a Kubernetes service account token that authenticates the identity.
3942
"""
4043

4144
def __init__(
4245
self,
43-
tenant_id: str,
44-
client_id: str,
45-
file: str,
46+
*,
47+
tenant_id: Optional[str] = None,
48+
client_id: Optional[str] = None,
49+
file: Optional[str] = None,
4650
**kwargs: Any
4751
) -> None:
52+
tenant_id = tenant_id or os.environ.get(EnvironmentVariables.AZURE_TENANT_ID)
53+
client_id = client_id or os.environ.get(EnvironmentVariables.AZURE_CLIENT_ID)
54+
file = file or os.environ.get(EnvironmentVariables.AZURE_FEDERATED_TOKEN_FILE)
55+
if not tenant_id:
56+
raise ValueError(
57+
"'tenant_id' is required. Please pass it in or set the "
58+
f"{EnvironmentVariables.AZURE_TENANT_ID} environment variable"
59+
)
60+
if not client_id:
61+
raise ValueError(
62+
"'client_id' is required. Please pass it in or set the "
63+
f"{EnvironmentVariables.AZURE_CLIENT_ID} environment variable"
64+
)
65+
if not file:
66+
raise ValueError(
67+
"'file' is required. Please pass it in or set the "
68+
f"{EnvironmentVariables.AZURE_FEDERATED_TOKEN_FILE} environment variable"
69+
)
4870
super(WorkloadIdentityCredential, self).__init__(
4971
tenant_id=tenant_id,
5072
client_id=client_id,

sdk/identity/azure-identity/azure/identity/aio/_credentials/workload_identity.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,48 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5-
from typing import Any
5+
import os
6+
from typing import Any, Optional
67
from .client_assertion import ClientAssertionCredential
78
from ..._credentials.workload_identity import TokenFileMixin
9+
from ..._constants import EnvironmentVariables
810

911

1012
class WorkloadIdentityCredential(ClientAssertionCredential, TokenFileMixin):
1113
"""WorkloadIdentityCredential supports Azure workload identity on Kubernetes.
1214
See the `workload identity overview <https://learn.microsoft.com/azure/aks/workload-identity-overview>`_
1315
for more information.
1416
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.
1820
"""
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+
)
2047
super().__init__(
2148
tenant_id=tenant_id,
2249
client_id=client_id,

0 commit comments

Comments
 (0)