Skip to content

Commit 40a3e1c

Browse files
msJinLeiisra-felCopilot
authored
Get-AccessToken's Breaking Change (Azure#27706)
Co-authored-by: Yeming Liu <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent d53f8a4 commit 40a3e1c

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

src/Accounts/Accounts.Test/AccessTokenCmdletTest.cs

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void TestGetAccessTokenAsPlainText()
8585
// Setup
8686
cmdlet.TenantId = tenantId;
8787
var fakeToken = "eyfaketoken.eyfaketoken";
88+
Environment.SetEnvironmentVariable(Constants.AzPsOutputPlainTextAccessToken, bool.TrueString);
8889

8990
var expected = new PSAccessToken {
9091
UserId = "[email protected]",
@@ -122,6 +123,7 @@ public void TestGetAccessTokenAsPlainText()
122123
Assert.Equal("Bearer", ((PSAccessToken)outputPipeline.First()).Type);
123124
Assert.Equal(expected.Token, ((PSAccessToken)outputPipeline.First()).Token);
124125

126+
Environment.SetEnvironmentVariable(Constants.AzPsOutputPlainTextAccessToken, null);
125127
AzureSession.Instance.AuthenticationFactory = previousFactory;
126128
}
127129

src/Accounts/Accounts/ChangeLog.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Changed the default output access token of `Get-AzAccessToken` from plain text to `SecureString`.
2223
* Removed the warning message about failing to initialize PSStyle in automation runbooks. [#26155]
2324
* Increased the timeout for tab-completion of location, resource group, etc. to 10 seconds.
2425

src/Accounts/Accounts/Token/GetAzureRmAccessToken.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,19 @@
1919
using Microsoft.Azure.Commands.ResourceManager.Common;
2020
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2121
using Microsoft.Azure.PowerShell.Authenticators;
22-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2322
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2423

2524
using System;
2625
using System.Collections.Generic;
2726
using System.Linq;
2827
using System.Management.Automation;
28+
using System.Security.Cryptography;
2929
using System.Text.Json;
3030

3131
namespace Microsoft.Azure.Commands.Profile
3232
{
33-
[SecureStringBreakingChange("The Token property of the output type will be changed from String to SecureString. Add the [-AsSecureString] switch to avoid the impact of this upcoming breaking change.", "14.0.0", "5.0.0")]
3433
[Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "AccessToken", DefaultParameterSetName = KnownResourceNameParameterSet)]
35-
[OutputType(typeof(PSAccessToken), typeof(PSSecureAccessToken))]
34+
[OutputType(typeof(PSSecureAccessToken))]
3635
public class GetAzureRmAccessTokenCommand : AzureRMCmdlet
3736
{
3837
private const string ResourceUrlParameterSet = "ResourceUrl";
@@ -73,7 +72,7 @@ public class GetAzureRmAccessTokenCommand : AzureRMCmdlet
7372
[Parameter(Mandatory = false, HelpMessage = "Optional Tenant Id. Use tenant id of default context if not specified.")]
7473
public string TenantId { get; set; }
7574

76-
[Parameter(Mandatory = false, HelpMessage = "Specify to convert output token as a secure string.")]
75+
[Parameter(Mandatory = false, HelpMessage = "The parameter is no long used but kept for backward compatibility.")]
7776
public SwitchParameter AsSecureString { get; set; }
7877

7978
public override void ExecuteCmdlet()
@@ -146,14 +145,25 @@ public override void ExecuteCmdlet()
146145
}
147146
}
148147

149-
if (AsSecureString.IsPresent)
148+
bool usePlainText = false;
149+
try
150150
{
151-
WriteObject(new PSSecureAccessToken(result));
151+
usePlainText = string.Equals(Environment.GetEnvironmentVariable(Constants.AzPsOutputPlainTextAccessToken), bool.TrueString, StringComparison.OrdinalIgnoreCase);
152152
}
153-
else
153+
catch (Exception e)
154+
{
155+
WriteDebug("Exception occurred while checking environment variable AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN: " + e.ToString());
156+
//Throw exception when the caller doesn't have permission.
157+
//Use SecureString only when AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN is successfully set.
158+
}
159+
if (usePlainText)
154160
{
155161
WriteObject(result);
156162
}
163+
else
164+
{
165+
WriteObject(new PSSecureAccessToken(result));
166+
}
157167
}
158168
}
159169
}

src/Accounts/Accounts/help/Get-AzAccessToken.md

+6-11
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ schema: 2.0.0
88
# Get-AzAccessToken
99

1010
## SYNOPSIS
11-
Get secure raw access token. When using -ResourceUrl, please make sure the value does match current Azure environment. You may refer to the value of `(Get-AzContext).Environment`.
11+
Get secure access token. When using -ResourceUrl, please make sure the value does match current Azure environment. You may refer to the value of `(Get-AzContext).Environment`.
1212

1313
> [!NOTE]
14-
> For security purposes, the default output type will change from a plain text `String` to
15-
> `SecureString`. To prepare for this change and ensure secure handling, use the **AsSecureString**
16-
> parameter before the update takes effect.
14+
> For security purposes, the default output type has been changed from a plain text `String` to `SecureString`.
15+
> Please refer to [Frequently asked questions about Azure PowerShell](https://learn.microsoft.com/en-us/powershell/azure/faq)
16+
> for how to convert from `SecureString` to plain text.
1717
1818
## SYNTAX
1919

@@ -30,7 +30,7 @@ Get-AzAccessToken -ResourceUrl <String> [-TenantId <String>] [-AsSecureString]
3030
```
3131

3232
## DESCRIPTION
33-
Get access token
33+
Get secure access token
3434

3535
## EXAMPLES
3636

@@ -58,8 +58,7 @@ Get access token of Microsoft Graph endpoint for current account
5858
## PARAMETERS
5959

6060
### -AsSecureString
61-
Specifiy to convert output token as a secure string.
62-
Please always use the parameter for security purpose and to avoid the upcoming breaking change and refer to [Frequently asked questions about Azure PowerShell](https://learn.microsoft.com/en-us/powershell/azure/faq) for how to convert from `SecureString` to plain text.
61+
The parameter is no longer used but kept for backward compatibility. No matter `AsSecureString` is specified, the output token is a `SecureString`.
6362

6463
```yaml
6564
Type: System.Management.Automation.SwitchParameter
@@ -142,11 +141,7 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
142141
143142
## OUTPUTS
144143
145-
### Microsoft.Azure.Commands.Profile.Models.PSAccessToken
146-
The output type is going to be deprecate.
147-
148144
### Microsoft.Azure.Commands.Profile.Models.PSSecureAccessToken
149-
Use `-AsSecureString` to get the token as `SecureString`.
150145
151146
## NOTES
152147

src/Accounts/Authentication/Constants.cs

+2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ public class ConfigProviderIds
3737
/// </summary>
3838
public const string None = "None";
3939
}
40+
41+
public const string AzPsOutputPlainTextAccessToken = "AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN";
4042
}
4143
}

tools/StaticAnalysis/Exceptions/Az.Accounts/BreakingChangeIssues.csv

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
"Az.Accounts","Microsoft.Azure.Commands.Profile.Context.RenameAzureRmContext","Rename-AzContext","0","1050","The parameter set '__AllParameterSets' for cmdlet 'Rename-AzContext' has been removed.","Add parameter set '__AllParameterSets' back to cmdlet 'Rename-AzContext'."
88
"Az.Accounts","Microsoft.Azure.Commands.Profile.Context.RenameAzureRmContext","Rename-AzContext","0","1050","The parameter set 'RenameByName' for cmdlet 'Rename-AzContext' has been removed.","Add parameter set 'RenameByName' back to cmdlet 'Rename-AzContext'."
99
"Az.Accounts","Microsoft.Azure.Commands.Profile.Context.SelectAzureRmContext","Select-AzContext","0","2000","The cmdlet 'Select-AzContext' no longer supports the parameter 'Name' and no alias was found for the original parameter name.","Add the parameter 'Name' back to the cmdlet 'Select-AzContext', or add an alias to the original parameter name."
10-
"Az.Accounts","Microsoft.Azure.Commands.Profile.Context.SelectAzureRmContext","Select-AzContext","0","1050","The parameter set 'SelectByName' for cmdlet 'Select-AzContext' has been removed.","Add parameter set 'SelectByName' back to cmdlet 'Select-AzContext'."
10+
"Az.Accounts","Microsoft.Azure.Commands.Profile.Context.SelectAzureRmContext","Select-AzContext","0","1050","The parameter set 'SelectByName' for cmdlet 'Select-AzContext' has been removed.","Add parameter set 'SelectByName' back to cmdlet 'Select-AzContext'."
11+
"Az.Accounts","Microsoft.Azure.Commands.Profile.GetAzureRmAccessTokenCommand","Get-AzAccessToken","0","1020","The cmdlet 'Get-AzAccessToken' no longer has output type 'Microsoft.Azure.Commands.Profile.Models.PSAccessToken'.","Make cmdlet 'Get-AzAccessToken' return type 'Microsoft.Azure.Commands.Profile.Models.PSAccessToken'."

0 commit comments

Comments
 (0)