Skip to content

Get-AccessToken's Breaking Change #27706

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

Merged
merged 8 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Accounts/Accounts.Test/AccessTokenCmdletTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void TestGetAccessTokenAsPlainText()
// Setup
cmdlet.TenantId = tenantId;
var fakeToken = "eyfaketoken.eyfaketoken";
Environment.SetEnvironmentVariable(Constants.AzPsOutputPlainTextAccessToken, bool.TrueString);

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

Environment.SetEnvironmentVariable(Constants.AzPsOutputPlainTextAccessToken, null);
AzureSession.Instance.AuthenticationFactory = previousFactory;
}

Expand Down
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->

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

Expand Down
24 changes: 17 additions & 7 deletions src/Accounts/Accounts/Token/GetAzureRmAccessToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.PowerShell.Authenticators;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using Microsoft.WindowsAzure.Commands.Utilities.Common;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Security.Cryptography;
using System.Text.Json;

namespace Microsoft.Azure.Commands.Profile
{
[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")]
[Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "AccessToken", DefaultParameterSetName = KnownResourceNameParameterSet)]
[OutputType(typeof(PSAccessToken), typeof(PSSecureAccessToken))]
[OutputType(typeof(PSSecureAccessToken))]
public class GetAzureRmAccessTokenCommand : AzureRMCmdlet
{
private const string ResourceUrlParameterSet = "ResourceUrl";
Expand Down Expand Up @@ -73,7 +72,7 @@ public class GetAzureRmAccessTokenCommand : AzureRMCmdlet
[Parameter(Mandatory = false, HelpMessage = "Optional Tenant Id. Use tenant id of default context if not specified.")]
public string TenantId { get; set; }

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

public override void ExecuteCmdlet()
Expand Down Expand Up @@ -146,14 +145,25 @@ public override void ExecuteCmdlet()
}
}

if (AsSecureString.IsPresent)
bool usePlainText = false;
try
{
WriteObject(new PSSecureAccessToken(result));
usePlainText = string.Equals(Environment.GetEnvironmentVariable(Constants.AzPsOutputPlainTextAccessToken), bool.TrueString, StringComparison.OrdinalIgnoreCase);
}
else
catch (Exception e)
{
WriteDebug("Exception occurred while checking environment variable AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN: " + e.ToString());
//Throw exception when the caller doesn't have permission.
//Use SecureString only when AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN is successfully set.
}
if (usePlainText)
{
WriteObject(result);
}
else
{
WriteObject(new PSSecureAccessToken(result));
}
}
}
}
17 changes: 6 additions & 11 deletions src/Accounts/Accounts/help/Get-AzAccessToken.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ schema: 2.0.0
# Get-AzAccessToken

## SYNOPSIS
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`.
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`.

> [!NOTE]
> For security purposes, the default output type will change from a plain text `String` to
> `SecureString`. To prepare for this change and ensure secure handling, use the **AsSecureString**
> parameter before the update takes effect.
> For security purposes, the default output type has been changed from a plain text `String` to `SecureString`.
> Please 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.

## SYNTAX

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

## DESCRIPTION
Get access token
Get secure access token

## EXAMPLES

Expand Down Expand Up @@ -58,8 +58,7 @@ Get access token of Microsoft Graph endpoint for current account
## PARAMETERS

### -AsSecureString
Specifiy to convert output token as a secure string.
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.
The parameter is no longer used but kept for backward compatibility. No matter `AsSecureString` is specified, the output token is a `SecureString`.

```yaml
Type: System.Management.Automation.SwitchParameter
Expand Down Expand Up @@ -142,11 +141,7 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable

## OUTPUTS

### Microsoft.Azure.Commands.Profile.Models.PSAccessToken
The output type is going to be deprecate.

### Microsoft.Azure.Commands.Profile.Models.PSSecureAccessToken
Use `-AsSecureString` to get the token as `SecureString`.

## NOTES

Expand Down
2 changes: 2 additions & 0 deletions src/Accounts/Authentication/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public class ConfigProviderIds
/// </summary>
public const string None = "None";
}

public const string AzPsOutputPlainTextAccessToken = "AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
"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'."
"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'."
"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."
"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'."
"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'."
"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'."