Skip to content

Commit 823e639

Browse files
authored
[Identity] Disable loading profiles in powershell (#31682)
Passing `-NoProfile` is generally recommended to ensure the powershell script is run in the default environment without user profile side effects interfering. Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
1 parent 4f8dc00 commit 823e639

4 files changed

Lines changed: 9 additions & 7 deletions

File tree

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+
- Ensure `AzurePowershellCredential` calls PowerShell with the `-NoProfile` flag to avoid loading user profiles for more consistent behavior. ([#31682](https://github.com/Azure/azure-sdk-for-python/pull/31682))
12+
1113
### Other Changes
1214

1315
## 1.14.0 (2023-08-08)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def get_command_line(scopes: Tuple[str, ...], tenant_id: str) -> List[str]:
183183
script = SCRIPT.format(NO_AZ_ACCOUNT_MODULE, resource, tenant_argument)
184184
encoded_script = base64.b64encode(script.encode("utf-16-le")).decode()
185185

186-
command = "pwsh -NonInteractive -EncodedCommand " + encoded_script
186+
command = "pwsh -NoProfile -NonInteractive -EncodedCommand " + encoded_script
187187
if sys.platform.startswith("win"):
188188
return ["cmd", "/c", command]
189189
return ["/bin/sh", "-c", command]

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_get_token(stderr):
8888
assert Popen.call_count == 1
8989
args, kwargs = Popen.call_args
9090
command = args[0][-1]
91-
assert command.startswith("pwsh -NonInteractive -EncodedCommand ")
91+
assert command.startswith("pwsh -NoProfile -NonInteractive -EncodedCommand ")
9292

9393
encoded_script = command.split()[-1]
9494
decoded_script = base64.b64decode(encoded_script).decode("utf-16-le")
@@ -267,7 +267,7 @@ def test_multitenant_authentication():
267267
second_token = first_token * 2
268268

269269
def fake_Popen(command, **_):
270-
assert command[-1].startswith("pwsh -NonInteractive -EncodedCommand ")
270+
assert command[-1].startswith("pwsh -NoProfile -NonInteractive -EncodedCommand ")
271271
encoded_script = command[-1].split()[-1]
272272
decoded_script = base64.b64decode(encoded_script).decode("utf-16-le")
273273
match = re.search(r"Get-AzAccessToken -ResourceUrl '(\S+)'(?: -TenantId (\S+))?", decoded_script)
@@ -297,7 +297,7 @@ def test_multitenant_authentication_not_allowed():
297297
expected_token = "***"
298298

299299
def fake_Popen(command, **_):
300-
assert command[-1].startswith("pwsh -NonInteractive -EncodedCommand ")
300+
assert command[-1].startswith("pwsh -NoProfile -NonInteractive -EncodedCommand ")
301301
encoded_script = command[-1].split()[-1]
302302
decoded_script = base64.b64decode(encoded_script).decode("utf-16-le")
303303
match = re.search(r"Get-AzAccessToken -ResourceUrl '(\S+)'(?: -TenantId (\S+))?", decoded_script)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async def test_get_token(stderr):
7878
assert mock_exec.call_count == 1
7979
args, kwargs = mock_exec.call_args
8080
command = args[-1]
81-
assert command.startswith("pwsh -NonInteractive -EncodedCommand ")
81+
assert command.startswith("pwsh -NoProfile -NonInteractive -EncodedCommand ")
8282

8383
encoded_script = command.split()[-1]
8484
decoded_script = base64.b64decode(encoded_script).decode("utf-16-le")
@@ -265,7 +265,7 @@ async def test_multitenant_authentication():
265265

266266
async def fake_exec(*args, **_):
267267
command = args[2]
268-
assert command.startswith("pwsh -NonInteractive -EncodedCommand ")
268+
assert command.startswith("pwsh -NoProfile -NonInteractive -EncodedCommand ")
269269
encoded_script = command.split()[-1]
270270
decoded_script = base64.b64decode(encoded_script).decode("utf-16-le")
271271
match = re.search(r"Get-AzAccessToken -ResourceUrl '(\S+)'(?: -TenantId (\S+))?", decoded_script)
@@ -296,7 +296,7 @@ async def test_multitenant_authentication_not_allowed():
296296

297297
async def fake_exec(*args, **_):
298298
command = args[2]
299-
assert command.startswith("pwsh -NonInteractive -EncodedCommand ")
299+
assert command.startswith("pwsh -NoProfile -NonInteractive -EncodedCommand ")
300300
encoded_script = command.split()[-1]
301301
decoded_script = base64.b64decode(encoded_script).decode("utf-16-le")
302302
match = re.search(r"Get-AzAccessToken -ResourceUrl '(\S+)'(?: -TenantId (\S+))?", decoded_script)

0 commit comments

Comments
 (0)