Skip to content

Commit f893a59

Browse files
authored
Az.Accounts Improvement (#13592)
* 1. Improve warning message when Interactive is unavailable; 2. Update doc for MSI customized endpoint * Register CommonUtilities if not present
1 parent bc94344 commit f893a59

File tree

6 files changed

+101
-13
lines changed

6 files changed

+101
-13
lines changed

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
125125
[Alias("MSI", "ManagedService")]
126126
public SwitchParameter Identity { get; set; }
127127

128-
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Port number for managed service login.")]
128+
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Obsolete. To use customized MSI endpoint, please set environment variable MSI_ENDPOINT, e.g. \"http://localhost:50342/oauth2/token\". Port number for managed service login.")]
129129
[PSDefaultValue(Help = "50342", Value = 50342)]
130130
public int ManagedServicePort { get; set; } = 50342;
131131

132-
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Host name for managed service login.")]
132+
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Obsolete. To use customized MSI endpoint, please set environment variable MSI_ENDPOINT, e.g. \"http://localhost:50342/oauth2/token\". Host name for managed service login.")]
133133
[PSDefaultValue(Help = "localhost", Value = "localhost")]
134134
public string ManagedServiceHostName { get; set; } = "localhost";
135135

136-
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Secret, used for some kinds of managed service login.")]
136+
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Obsolete. To use customized MSI secret, please set environment variable MSI_SECRET. Secret, used for some kinds of managed service login.")]
137137
[ValidateNotNullOrEmpty]
138138
public SecureString ManagedServiceSecret { get; set; }
139139

@@ -295,6 +295,12 @@ public override void ExecuteCmdlet()
295295
Path = "/oauth2/token"
296296
};
297297

298+
//ManagedServiceHostName/ManagedServicePort/ManagedServiceSecret are obsolete, should be removed in next major release
299+
if (this.IsBound(nameof(ManagedServiceHostName)) || this.IsBound(nameof(ManagedServicePort)) || this.IsBound(nameof(ManagedServiceSecret)))
300+
{
301+
WriteWarning(Resources.ObsoleteManagedServiceParameters);
302+
}
303+
298304
var envSecret = System.Environment.GetEnvironmentVariable(MSISecretVariable);
299305

300306
var msiSecret = this.IsBound(nameof(ManagedServiceSecret))
@@ -387,6 +393,17 @@ public override void ExecuteCmdlet()
387393
InitializeProfileProvider();
388394
}
389395

396+
if(!AzureSession.Instance.TryGetComponent(nameof(CommonUtilities), out CommonUtilities commonUtitilies))
397+
{
398+
commonUtitilies = new CommonUtilities();
399+
AzureSession.Instance.RegisterComponent(nameof(CommonUtilities), () => commonUtitilies);
400+
}
401+
if(!commonUtitilies.IsDesktopSession() && IsUsingInteractiveAuthentication())
402+
{
403+
WriteWarning(Resources.InteractiveAuthNotSupported);
404+
return;
405+
}
406+
390407
SetContextWithOverwritePrompt((localProfile, profileClient, name) =>
391408
{
392409
bool shouldPopulateContextList = true;
@@ -425,14 +442,14 @@ public override void ExecuteCmdlet()
425442
}
426443
catch (AuthenticationFailedException ex)
427444
{
428-
if(IsUnableToOpenWebPageError(ex))
445+
if (IsUnableToOpenWebPageError(ex))
429446
{
430447
WriteWarning(Resources.InteractiveAuthNotSupported);
431448
WriteDebug(ex.ToString());
432449
}
433450
else
434451
{
435-
if (ParameterSetName == UserParameterSet && UseDeviceAuthentication == false)
452+
if (IsUsingInteractiveAuthentication())
436453
{
437454
//Display only if user is using Interactive auth
438455
WriteWarning(Resources.SuggestToUseDeviceCodeAuth);
@@ -445,6 +462,11 @@ public override void ExecuteCmdlet()
445462
}
446463
}
447464

465+
private bool IsUsingInteractiveAuthentication()
466+
{
467+
return ParameterSetName == UserParameterSet && UseDeviceAuthentication == false;
468+
}
469+
448470
private bool IsUnableToOpenWebPageError(AuthenticationFailedException exception)
449471
{
450472
return exception.InnerException is MsalClientException && ((MsalClientException)exception.InnerException)?.ErrorCode == MsalError.LinuxXdgOpen
@@ -591,6 +613,7 @@ public void OnImport()
591613
}
592614
var tokenCache = provider.GetTokenCache();
593615
IAzureEventListenerFactory azureEventListenerFactory = new AzureEventListenerFactory();
616+
AzureSession.Instance.RegisterComponent(nameof(CommonUtilities), () => new CommonUtilities());
594617
AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => provider);
595618
AzureSession.Instance.RegisterComponent(nameof(IAzureEventListenerFactory), () => azureEventListenerFactory);
596619
AzureSession.Instance.RegisterComponent(nameof(PowerShellTokenCache), () => tokenCache);

src/Accounts/Accounts/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Accounts/Accounts/Properties/Resources.resx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,6 @@
513513
<data name="SendFeedbackOpenLinkManually" xml:space="preserve">
514514
<value>Use a web browser to open the page {0}.</value>
515515
</data>
516-
<data name="InteractiveAuthNotSupported" xml:space="preserve">
517-
<value>Interactive authentication is not supported in this session, please run Connect-AzAccount using switch -DeviceCode.</value>
518-
</data>
519516
<data name="SuggestToUseDeviceCodeAuth" xml:space="preserve">
520517
<value>Please run 'Connect-AzAccount -DeviceCode' if browser is not supported in this session.</value>
521518
</data>
@@ -525,4 +522,10 @@
525522
<data name="FallbackContextSaveModeDueCacheCheckError" xml:space="preserve">
526523
<value>INITIALIZATION: Fallback context save mode to process because of error during checking token cache persistence: {0}.</value>
527524
</data>
525+
<data name="ObsoleteManagedServiceParameters" xml:space="preserve">
526+
<value>The parameters ManagedServiceHostName/ManagedServicePort/ManagedServiceSecret are obsolete. For ManagedServiceHostName/ManagedServicePort, please set environment variable MSI_ENDPOINT, e.g. "http://localhost:50342/oauth2/token". For ManagedServiceSecret, please set environment variable MSI_SECRET.</value>
527+
</data>
528+
<data name="InteractiveAuthNotSupported" xml:space="preserve">
529+
<value>Interactive authentication is not supported in this session, please run cmdlet 'Connect-AzAccount -UseDeviceAuthentication'.</value>
530+
</data>
528531
</root>

src/Accounts/Accounts/help/Connect-AzAccount.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ Accept wildcard characters: False
420420

421421
### -ManagedServiceHostName
422422

423-
Host name for the managed service.
423+
Obsolete. To use customized MSI endpoint, please set environment variable MSI_ENDPOINT, e.g. "http://localhost:50342/oauth2/token". Host name for the managed service.
424424

425425
```yaml
426426
Type: System.String
@@ -436,7 +436,7 @@ Accept wildcard characters: False
436436

437437
### -ManagedServicePort
438438

439-
Port number for the managed service.
439+
Obsolete. To use customized MSI endpoint, please set environment variable MSI_ENDPOINT, e.g. "http://localhost:50342/oauth2/token".Port number for the managed service.
440440

441441
```yaml
442442
Type: System.Int32
@@ -452,7 +452,7 @@ Accept wildcard characters: False
452452

453453
### -ManagedServiceSecret
454454

455-
Token for the managed service login.
455+
Obsolete. To use customized MSI secret, please set environment variable MSI_SECRET. Token for the managed service login.
456456

457457
```yaml
458458
Type: System.Security.SecureString
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Runtime.InteropServices;
17+
18+
namespace Microsoft.Azure.Commands.Common.Authentication
19+
{
20+
public class CommonUtilities
21+
{
22+
/// <summary>
23+
/// Check if it is in desktop session
24+
/// </summary>
25+
/// <returns></returns>
26+
public bool IsDesktopSession()
27+
{
28+
//Check only for Linux platform
29+
//Linux: https://github.com/microsoft/Git-Credential-Manager-Core/blob/master/src/shared/Microsoft.Git.CredentialManager/Interop/Posix/PosixSessionManager.cs
30+
//MacOS: https://github.com/microsoft/Git-Credential-Manager-Core/blob/master/src/shared/Microsoft.Git.CredentialManager/Interop/MacOS/MacOSSessionManager.cs
31+
//Windows: https://github.com/microsoft/Git-Credential-Manager-Core/blob/master/src/shared/Microsoft.Git.CredentialManager/Interop/Windows/WindowsSessionManager.cs
32+
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
33+
{
34+
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DISPLAY"));
35+
}
36+
return true;
37+
}
38+
}
39+
}

src/Accounts/Authentication/Utilities/CustomAssemblyResolver.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
216
using System.Collections.Generic;
317
using System.IO;
418
using System.Reflection;

0 commit comments

Comments
 (0)