|
19 | 19 | using Microsoft.Azure.Commands.ResourceManager.Common;
|
20 | 20 | using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
|
21 | 21 | using Microsoft.Azure.PowerShell.Authenticators;
|
22 |
| -using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; |
23 | 22 | using Microsoft.WindowsAzure.Commands.Utilities.Common;
|
24 | 23 |
|
25 | 24 | using System;
|
26 | 25 | using System.Collections.Generic;
|
27 | 26 | using System.Linq;
|
28 | 27 | using System.Management.Automation;
|
| 28 | +using System.Security.Cryptography; |
29 | 29 | using System.Text.Json;
|
30 | 30 |
|
31 | 31 | namespace Microsoft.Azure.Commands.Profile
|
32 | 32 | {
|
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")] |
34 | 33 | [Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "AccessToken", DefaultParameterSetName = KnownResourceNameParameterSet)]
|
35 |
| - [OutputType(typeof(PSAccessToken), typeof(PSSecureAccessToken))] |
| 34 | + [OutputType(typeof(PSSecureAccessToken))] |
36 | 35 | public class GetAzureRmAccessTokenCommand : AzureRMCmdlet
|
37 | 36 | {
|
38 | 37 | private const string ResourceUrlParameterSet = "ResourceUrl";
|
@@ -73,7 +72,7 @@ public class GetAzureRmAccessTokenCommand : AzureRMCmdlet
|
73 | 72 | [Parameter(Mandatory = false, HelpMessage = "Optional Tenant Id. Use tenant id of default context if not specified.")]
|
74 | 73 | public string TenantId { get; set; }
|
75 | 74 |
|
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.")] |
77 | 76 | public SwitchParameter AsSecureString { get; set; }
|
78 | 77 |
|
79 | 78 | public override void ExecuteCmdlet()
|
@@ -146,14 +145,25 @@ public override void ExecuteCmdlet()
|
146 | 145 | }
|
147 | 146 | }
|
148 | 147 |
|
149 |
| - if (AsSecureString.IsPresent) |
| 148 | + bool usePlainText = false; |
| 149 | + try |
150 | 150 | {
|
151 |
| - WriteObject(new PSSecureAccessToken(result)); |
| 151 | + usePlainText = string.Equals(Environment.GetEnvironmentVariable(Constants.AzPsOutputPlainTextAccessToken), bool.TrueString, StringComparison.OrdinalIgnoreCase); |
152 | 152 | }
|
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) |
154 | 160 | {
|
155 | 161 | WriteObject(result);
|
156 | 162 | }
|
| 163 | + else |
| 164 | + { |
| 165 | + WriteObject(new PSSecureAccessToken(result)); |
| 166 | + } |
157 | 167 | }
|
158 | 168 | }
|
159 | 169 | }
|
0 commit comments