Skip to content

Commit 68a80bf

Browse files
authored
[Identity] Add env var enforcement flag to DAC (Azure#42660)
Signed-off-by: Paul Van Eck <[email protected]>
1 parent 28a0c2b commit 68a80bf

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

sdk/identity/azure-identity/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
### Features Added
66

7-
- `AzureDeveloperCliCredential` now supports `claims` in `get_token` and `get_token_info`. ([#42568](https://github.com/Azure/azure-sdk-for-python/pull/42568))
7+
- `AzureDeveloperCliCredential` now supports `claims` in `get_token` and `get_token_info`. ([#42568](https://github.com/Azure/azure-sdk-for-python/pull/42568))
8+
- Added new keyword argument `require_envvar` to `DefaultAzureCredential` to enforce the presence of the `AZURE_TOKEN_CREDENTIALS` environment variable. ([#42660](https://github.com/Azure/azure-sdk-for-python/pull/42660))
89

910
### Breaking Changes
1011

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class DefaultAzureCredential(ChainedTokenCredential):
126126
record file used by the Azure Resources extension.
127127
:keyword int process_timeout: The timeout in seconds to use for developer credentials that run
128128
subprocesses (e.g. AzureCliCredential, AzurePowerShellCredential). Defaults to **10** seconds.
129+
:keyword bool require_envvar: If **True**, require that the AZURE_TOKEN_CREDENTIALS environment variable be set
130+
to a value denoting the credential type or credential group to use. If unset or empty, DefaultAzureCredential
131+
will raise a `ValueError`. Defaults to **False**.
129132
130133
.. admonition:: Example:
131134
@@ -168,6 +171,12 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
168171
)
169172

170173
process_timeout = kwargs.pop("process_timeout", 10)
174+
require_envvar = kwargs.pop("require_envvar", False)
175+
if require_envvar and not os.environ.get(EnvironmentVariables.AZURE_TOKEN_CREDENTIALS):
176+
raise ValueError(
177+
"AZURE_TOKEN_CREDENTIALS environment variable is required but is not set or is empty. "
178+
"Set it to 'dev', 'prod', or a specific credential name."
179+
)
171180

172181
# Define credential configuration mapping
173182
credential_config = {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class DefaultAzureCredential(ChainedTokenCredential):
106106
record file used by the Azure Resources extension.
107107
:keyword int process_timeout: The timeout in seconds to use for developer credentials that run
108108
subprocesses (e.g. AzureCliCredential, AzurePowerShellCredential). Defaults to **10** seconds.
109+
:keyword bool require_envvar: If **True**, require that the AZURE_TOKEN_CREDENTIALS environment variable be set
110+
to a value denoting the credential type or credential group to use. If unset or empty, DefaultAzureCredential
111+
will raise a `ValueError`. Defaults to **False**.
109112
110113
.. admonition:: Example:
111114
@@ -140,6 +143,12 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
140143
)
141144

142145
process_timeout = kwargs.pop("process_timeout", 10)
146+
require_envvar = kwargs.pop("require_envvar", False)
147+
if require_envvar and not os.environ.get(EnvironmentVariables.AZURE_TOKEN_CREDENTIALS):
148+
raise ValueError(
149+
"AZURE_TOKEN_CREDENTIALS environment variable is required but is not set or is empty. "
150+
"Set it to 'dev', 'prod', or a specific credential name."
151+
)
143152

144153
# Define credential configuration mapping (async version)
145154
credential_config = {

sdk/identity/azure-identity/tests/test_default.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,10 @@ def test_failed_dac_credential_in_chain():
570570
# The error should mention the failed credentials
571571
error_str = str(exc_info.value)
572572
assert "workload identity error" in error_str or "test credential error" in error_str
573+
574+
575+
def test_require_envvar_raises_error_when_envvar_missing():
576+
with patch.dict("os.environ", {}, clear=True):
577+
with pytest.raises(ValueError) as exc_info:
578+
DefaultAzureCredential(require_envvar=True)
579+
assert "AZURE_TOKEN_CREDENTIALS" in str(exc_info.value)

sdk/identity/azure-identity/tests/test_default_async.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,11 @@ async def test_failed_dac_credential_in_chain():
412412
# The error should mention the failed credentials
413413
error_str = str(exc_info.value)
414414
assert "workload identity error" in error_str or "test credential error" in error_str
415+
416+
417+
@pytest.mark.asyncio
418+
async def test_require_envvar_raises_error_when_envvar_missing():
419+
with patch.dict("os.environ", {}, clear=True):
420+
with pytest.raises(ValueError) as exc_info:
421+
DefaultAzureCredential(require_envvar=True)
422+
assert "AZURE_TOKEN_CREDENTIALS" in str(exc_info.value)

0 commit comments

Comments
 (0)