-
Notifications
You must be signed in to change notification settings - Fork 4k
Force Get-AzAccessToken to always return SecureString as long as AsSecureString is set #27817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…cureString is set
Thanks for your contribution! The pull request validation has started. Please revisit this comment for updated status. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR ensures that Get-AzAccessToken
will always output a SecureString
when the AsSecureString
flag is set, regardless of the plaintext environment variable, and updates both the changelog and tests to reflect this behavior.
- Respect the
AsSecureString
parameter before writing plaintext output - Add a changelog entry for the upcoming release
- Refactor and extend tests to cover secure-string scenarios
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
src/Accounts/Accounts/Token/GetAzureRmAccessToken.cs | Modify condition to only write plaintext when AsSecureString is false |
src/Accounts/Accounts/ChangeLog.md | Add an upcoming release note about forcing secure-string output |
src/Accounts/Accounts.Test/AccessTokenCmdletTest.cs | Introduce CreateCommand() helper, remove unused fields, and add secure-string tests |
Comments suppressed due to low confidence (1)
src/Accounts/Accounts.Test/AccessTokenCmdletTest.cs:87
- This test invokes the cmdlet without mocking the authentication factory, which may call real authentication logic. You should override
AzureSession.Instance.AuthenticationFactory
withfactoryMock.Object
in this test to ensure it returns the expected fake token.
public void TestGetAccessTokenAsPlainText()
cmdlet.TenantId = tenantId; | ||
cmdlet.AsSecureString = true; | ||
var fakeToken = "eyfaketoken.eyfaketoken"; | ||
Environment.SetEnvironmentVariable(Constants.AzPsOutputPlainTextAccessToken, bool.TrueString); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first test sets the plaintext env var but never clears it, causing leakage into subsequent tests. Consider resetting or scoping the environment variable (e.g., clear it at the start or end of each test).
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR modifies the Get-AzAccessToken cmdlet to ensure that when the AsSecureString parameter is set, the returned token is a SecureString even if the environment variable for plain text output is present.
- Updated logic in GetAzureRmAccessToken.cs to conditionally bypass plain text output when AsSecureString is specified.
- Refactored test code by introducing a helper method to create a consistent cmdlet instance and removed unused fields.
- Updated ChangeLog.md to reflect the behavioral change.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/Accounts/Accounts/Token/GetAzureRmAccessToken.cs | Changed condition in token output logic to check for AsSecureString before outputting. |
src/Accounts/Accounts/ChangeLog.md | Added a changelog entry reflecting the update to SecureString token output behavior. |
src/Accounts/Accounts.Test/AccessTokenCmdletTest.cs | Refactored test setup by introducing a CreateCommand helper and removed an unused field. |
@@ -156,7 +156,7 @@ public override void ExecuteCmdlet() | |||
//Throw exception when the caller doesn't have permission. | |||
//Use SecureString only when AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN is successfully set. | |||
} | |||
if (usePlainText) | |||
if (usePlainText && !AsSecureString) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider adding an inline comment explaining that the plain text output is intentionally bypassed when AsSecureString is set.
Copilot uses AI. Check for mistakes.
AzureSession.Instance.AuthenticationFactory = factoryMock.Object; | ||
|
||
// Act | ||
cmdlet.InvokeBeginProcessing(); | ||
cmdlet.ExecuteCmdlet(); | ||
cmdlet.InvokeEndProcessing(); | ||
|
||
//Verify | ||
Assert.Single(mockedCommandRuntime.OutputPipeline); | ||
var outputPipeline = mockedCommandRuntime.OutputPipeline; | ||
Assert.Equal(expected.TenantId, ((PSSecureAccessToken)outputPipeline.First()).TenantId); | ||
Assert.Equal(expected.UserId, ((PSSecureAccessToken)outputPipeline.First()).UserId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider wrapping the assignment and restoration of AzureSession.Instance.AuthenticationFactory in a try–finally block to ensure test isolation even if exceptions occur.
AzureSession.Instance.AuthenticationFactory = factoryMock.Object; | |
// Act | |
cmdlet.InvokeBeginProcessing(); | |
cmdlet.ExecuteCmdlet(); | |
cmdlet.InvokeEndProcessing(); | |
//Verify | |
Assert.Single(mockedCommandRuntime.OutputPipeline); | |
var outputPipeline = mockedCommandRuntime.OutputPipeline; | |
Assert.Equal(expected.TenantId, ((PSSecureAccessToken)outputPipeline.First()).TenantId); | |
Assert.Equal(expected.UserId, ((PSSecureAccessToken)outputPipeline.First()).UserId); | |
try | |
{ | |
AzureSession.Instance.AuthenticationFactory = factoryMock.Object; | |
// Act | |
cmdlet.InvokeBeginProcessing(); | |
cmdlet.ExecuteCmdlet(); | |
cmdlet.InvokeEndProcessing(); | |
//Verify | |
Assert.Single(mockedCommandRuntime.OutputPipeline); | |
var outputPipeline = mockedCommandRuntime.OutputPipeline; | |
Assert.Equal(expected.TenantId, ((PSSecureAccessToken)outputPipeline.First()).TenantId); | |
Assert.Equal(expected.UserId, ((PSSecureAccessToken)outputPipeline.First()).UserId); | |
} | |
finally | |
{ | |
AzureSession.Instance.AuthenticationFactory = previousFactory; | |
} |
Copilot uses AI. Check for mistakes.
…cureString is set (Azure#27817)
Description
Mandatory Checklist
Please choose the target release of Azure PowerShell. (⚠️ Target release is a different concept from API readiness. Please click below links for details.)
Check this box to confirm: I have read the Submitting Changes section of
CONTRIBUTING.md
and reviewed the following information:ChangeLog.md
file(s) appropriatelysrc/{{SERVICE}}/{{SERVICE}}/ChangeLog.md
.## Upcoming Release
header in the past tense.ChangeLog.md
if no new release is required, such as fixing test case only.