Skip to content

Commit d1e9b73

Browse files
authored
Merge pull request #5850 from markcowl/msiagain
MSIupdates for AppService and CloudShell
2 parents ea38ff2 + 8967cf2 commit d1e9b73

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

src/Common/Commands.Common.Authentication.Abstractions/AzureAccount.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ public static class Property
128128
/// <summary>
129129
/// Backup login Uri for MSI
130130
/// </summary>
131-
MSILoginUriBackup = "MSILoginBackup";
131+
MSILoginUriBackup = "MSILoginBackup",
132132

133133

134+
/// <summary>
135+
/// Secret that may be used with MSI login
136+
/// </summary>
137+
MSILoginSecret = "MSILoginSecret";
134138
}
135139
}
136140
}

src/Common/Commands.Common.Authentication/Authentication/ManagedServiceAccessToken.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Rest.Azure;
1818
using System;
1919
using System.Collections.Generic;
20+
using System.Net.Http;
2021
using System.Text;
2122
using System.Threading;
2223

@@ -72,6 +73,10 @@ public ManagedServiceAccessToken(IAzureAccount account, IAzureEnvironment enviro
7273
}
7374

7475
_tokenGetter = factory.GetHttpOperations<ManagedServiceTokenInfo>(true).WithHeader("Metadata", new[] { "true" });
76+
if (account.IsPropertySet(AzureAccount.Property.MSILoginSecret))
77+
{
78+
_tokenGetter = _tokenGetter.WithHeader("Secret", new[] { account.GetProperty(AzureAccount.Property.MSILoginSecret) });
79+
}
7580
}
7681

7782
public string AccessToken
@@ -119,11 +124,12 @@ void GetOrRenewAuthentication()
119124
RequestUris.Clear();
120125
RequestUris.Enqueue(currentRequestUri);
121126
}
122-
catch (CloudException) when (RequestUris.Count > 0)
127+
catch (Exception e) when ( (e is CloudException || e is HttpRequestException) && RequestUris.Count > 0)
123128
{
124-
// do nothing
129+
// skip to the next uri
125130
}
126131
}
132+
127133
SetToken(info);
128134
}
129135
}

src/Common/Commands.Common/Extensions/CmdletExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ public static void SafeCopyParameterSet<T>(this T source, T target) where T : Az
202202
}
203203
}
204204

205+
/// <summary>
206+
/// Return the value of a paramater, or null if not set
207+
/// </summary>
208+
/// <typeparam name="T"></typeparam>
209+
/// <param name="cmdlet">the executing cmdlet</param>
210+
/// <param name="parameterName">The name of the parameter to return</param>
211+
/// <returns>true if the parameter was provided by the user, otherwise false</returns>
212+
public static bool IsBound(this PSCmdlet cmdlet, string parameterName)
213+
{
214+
return cmdlet.MyInvocation.BoundParameters.ContainsKey(parameterName);
215+
}
216+
205217
public static string AsAbsoluteLocation(this string realtivePath)
206218
{
207219
return Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, realtivePath));

src/ResourceManager/Aks/NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<config>
4+
<add key="repositorypath" value="..\..\packages" />
5+
</config>
6+
</configuration>

src/ResourceManager/Profile/Commands.Profile/Account/ConnectAzureRmAccount.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using Microsoft.Azure.Commands.Profile.Properties;
2525
using Microsoft.Azure.Commands.Profile.Common;
2626
using Microsoft.Azure.Commands.Common.Authentication.Factories;
27+
using Microsoft.WindowsAzure.Commands.Common;
2728

2829
namespace Microsoft.Azure.Commands.Profile
2930
{
@@ -40,6 +41,8 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
4041
public const string ServicePrincipalCertificateParameterSet= "ServicePrincipalCertificateWithSubscriptionId";
4142
public const string AccessTokenParameterSet = "AccessTokenWithSubscriptionId";
4243
public const string ManagedServiceParameterSet = "ManagedServiceLogin";
44+
public const string MSIEndpointVariable = "MSI_ENDPOINT";
45+
public const string MSISecretVariable = "MSI_SECRET";
4346

4447
protected IAzureEnvironment _environment =AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
4548

@@ -116,7 +119,12 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
116119
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Host name for managed service login.")]
117120
[PSDefaultValue(Help = "localhost", Value = "localhost")]
118121
public string ManagedServiceHostName { get; set; } = "localhost";
119-
122+
123+
[Parameter(ParameterSetName = ManagedServiceParameterSet, Mandatory = false, HelpMessage = "Secret, used for some kinds of managed service login.")]
124+
[ValidateNotNullOrEmpty]
125+
public SecureString ManagedServiceSecret { get; set; }
126+
127+
120128
[Alias("SubscriptionName", "SubscriptionId")]
121129
[Parameter(ParameterSetName = UserParameterSet,
122130
Mandatory = false, HelpMessage = "Subscription Name or ID", ValueFromPipeline = true)]
@@ -199,14 +207,36 @@ public override void ExecuteCmdlet()
199207
break;
200208
case ManagedServiceParameterSet:
201209
azureAccount.Type = AzureAccount.AccountType.ManagedService;
202-
azureAccount.Id = MyInvocation.BoundParameters.ContainsKey(nameof(AccountId))? AccountId : string.Format("MSI@{0}", ManagedServicePort);
203210
var builder = new UriBuilder();
204211
builder.Scheme = "http";
205212
builder.Host = ManagedServiceHostName;
206213
builder.Port = ManagedServicePort;
207214
builder.Path = "/oauth2/token";
208-
azureAccount.SetProperty(AzureAccount.Property.MSILoginUriBackup, builder.Uri.ToString());
209-
azureAccount.SetProperty(AzureAccount.Property.MSILoginUri, AuthenticationFactory.DefaultMSILoginUri);
215+
216+
string msiSecret = this.IsBound(nameof(ManagedServiceSecret))
217+
? ManagedServiceSecret.ConvertToString()
218+
: System.Environment.GetEnvironmentVariable(MSISecretVariable);
219+
220+
string suppliedUri = this.IsBound(nameof(ManagedServiceHostName))
221+
? builder.Uri.ToString()
222+
: System.Environment.GetEnvironmentVariable(MSIEndpointVariable);
223+
224+
if (!string.IsNullOrWhiteSpace(msiSecret))
225+
{
226+
azureAccount.SetProperty(AzureAccount.Property.MSILoginSecret, msiSecret);
227+
}
228+
229+
if (!string.IsNullOrWhiteSpace(suppliedUri))
230+
{
231+
azureAccount.SetProperty(AzureAccount.Property.MSILoginUri, suppliedUri);
232+
}
233+
else
234+
{
235+
azureAccount.SetProperty(AzureAccount.Property.MSILoginUriBackup, builder.Uri.ToString());
236+
azureAccount.SetProperty(AzureAccount.Property.MSILoginUri, AuthenticationFactory.DefaultMSILoginUri);
237+
}
238+
239+
azureAccount.Id = this.IsBound(nameof(AccountId)) ? AccountId : string.Format("MSI@{0}", ManagedServicePort);
210240
break;
211241
default:
212242
azureAccount.Type = AzureAccount.AccountType.User;

tools/CheckAssemblies.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ foreach ($ModuleManifest in $ModuleManifestFiles)
4747
$RequiredModules = $ModuleMetadata.RequiredModules | % { $_["ModuleName"] }
4848
foreach ($RequiredModule in $RequiredModules)
4949
{
50-
$RequiredModuleManifest = $ModuleManifestFiles | where { $_.Name.Replace(".psd1", "") -eq $RequiredModule }
50+
Write-Output ("ModuleManifest: " + $RequiredModuleManifest)
51+
Write-Output ("Required Module: " + $RequiredModule)
52+
$RequiredModuleManifest = $ModuleManifestFiles | where { $_.Name.Replace(".psd1", "") -eq $RequiredModule } | Select-Object -First 1
5153
if (-not $RequiredModuleManifest)
5254
{
5355
continue

0 commit comments

Comments
 (0)