24
24
using Microsoft . Azure . Commands . Profile . Properties ;
25
25
using Microsoft . Azure . Commands . Profile . Common ;
26
26
using Microsoft . Azure . Commands . Common . Authentication . Factories ;
27
+ using Microsoft . WindowsAzure . Commands . Common ;
27
28
28
29
namespace Microsoft . Azure . Commands . Profile
29
30
{
@@ -40,6 +41,8 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
40
41
public const string ServicePrincipalCertificateParameterSet = "ServicePrincipalCertificateWithSubscriptionId" ;
41
42
public const string AccessTokenParameterSet = "AccessTokenWithSubscriptionId" ;
42
43
public const string ManagedServiceParameterSet = "ManagedServiceLogin" ;
44
+ public const string MSIEndpointVariable = "MSI_ENDPOINT" ;
45
+ public const string MSISecretVariable = "MSI_SECRET" ;
43
46
44
47
protected IAzureEnvironment _environment = AzureEnvironment . PublicEnvironments [ EnvironmentName . AzureCloud ] ;
45
48
@@ -116,7 +119,12 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
116
119
[ Parameter ( ParameterSetName = ManagedServiceParameterSet , Mandatory = false , HelpMessage = "Host name for managed service login." ) ]
117
120
[ PSDefaultValue ( Help = "localhost" , Value = "localhost" ) ]
118
121
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
+
120
128
[ Alias ( "SubscriptionName" , "SubscriptionId" ) ]
121
129
[ Parameter ( ParameterSetName = UserParameterSet ,
122
130
Mandatory = false , HelpMessage = "Subscription Name or ID" , ValueFromPipeline = true ) ]
@@ -199,14 +207,36 @@ public override void ExecuteCmdlet()
199
207
break ;
200
208
case ManagedServiceParameterSet :
201
209
azureAccount . Type = AzureAccount . AccountType . ManagedService ;
202
- azureAccount . Id = MyInvocation . BoundParameters . ContainsKey ( nameof ( AccountId ) ) ? AccountId : string . Format ( "MSI@{0}" , ManagedServicePort ) ;
203
210
var builder = new UriBuilder ( ) ;
204
211
builder . Scheme = "http" ;
205
212
builder . Host = ManagedServiceHostName ;
206
213
builder . Port = ManagedServicePort ;
207
214
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 ) ;
210
240
break ;
211
241
default :
212
242
azureAccount . Type = AzureAccount . AccountType . User ;
0 commit comments