Skip to content

Commit 5b57461

Browse files
[Identity] Update AzurePowerShellCredential script (#41675)
- Only add the `-AsSecureString` argument when available and needed. This argument is no longer needed for `Az.Accounts` versions 5.0.0 and above. - Update secure string parsing logic to allow it to work if a user is using Windows PowerShell instead of PowerShell 7+. Signed-off-by: Paul Van Eck <[email protected]> Co-authored-by: Minh-Anh Phan <[email protected]>
1 parent 45a976b commit 5b57461

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

sdk/identity/azure-identity/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed an issue with `AzurePowerShellCredential` not working correctly for users still using older versions of PowerShell (e.g., Windows PowerShell 5.1) where `-AsPlainText` is not supported in the `ConvertFrom-SecureString` cmdlet. ([#41675](https://github.com/Azure/azure-sdk-for-python/pull/41675))
12+
1113
### Other Changes
1214

1315
## 1.23.0 (2025-05-13)

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,25 @@
4141
$params['TenantId'] = $tenantId
4242
}}
4343
44-
$useSecureString = $m.Version -ge [version]'2.17.0'
45-
if ($useSecureString) {{
44+
if ($m.Version -ge [version]'2.17.0' -and $m.Version -lt [version]'5.0.0') {{
4645
$params['AsSecureString'] = $true
4746
}}
4847
4948
$token = Get-AzAccessToken @params
5049
$tokenValue = $token.Token
51-
if ($useSecureString) {{
52-
$tokenValue = $tokenValue | ConvertFrom-SecureString -AsPlainText
50+
if ($tokenValue -is [System.Security.SecureString]) {{
51+
if ($PSVersionTable.PSVersion.Major -lt 7) {{
52+
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($tokenValue)
53+
try {{
54+
$tokenValue = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
55+
}}
56+
finally {{
57+
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
58+
}}
59+
}}
60+
else {{
61+
$tokenValue = $tokenValue | ConvertFrom-SecureString -AsPlainText
62+
}}
5363
}}
5464
Write-Output "`nazsdk%$($tokenValue)%$($token.ExpiresOn.ToUnixTimeSeconds())`n"
5565
"""

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import base64
66
from itertools import product
77
import logging
8-
from platform import python_version
98
import re
109
import subprocess
1110
import sys
@@ -121,11 +120,10 @@ def test_get_token(stderr, get_token_method):
121120
decoded_script = base64.b64decode(encoded_script).decode("utf-16-le")
122121
assert "tenantId = ''" in decoded_script
123122
assert f"'ResourceUrl' = '{scope}'" in decoded_script
123+
assert "-is [System.Security.SecureString]" in decoded_script
124124

125125
assert Popen().communicate.call_count == 1
126126
args, kwargs = Popen().communicate.call_args
127-
if python_version() >= "3.3":
128-
assert "timeout" in kwargs
129127

130128

131129
@pytest.mark.parametrize("stderr,get_token_method", product(("", PREPARING_MODULES), GET_TOKEN_METHODS))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ async def test_get_token(stderr, get_token_method):
115115
decoded_script = base64.b64decode(encoded_script).decode("utf-16-le")
116116
assert "tenantId = ''" in decoded_script
117117
assert f"'ResourceUrl' = '{scope}'" in decoded_script
118+
assert "-is [System.Security.SecureString]" in decoded_script
118119

119120
assert mock_exec().result().communicate.call_count == 1
120121

0 commit comments

Comments
 (0)