Skip to content

Commit e664dad

Browse files
authored
Merge pull request Azure#5070 from maddieclayton/skipvalidation
Add ability to avoid validation calls in CloudShell login
2 parents 552460e + 243a343 commit e664dad

File tree

6 files changed

+176
-67
lines changed

6 files changed

+176
-67
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public void SpecifyTenantAndSubscriptionIdSucceed()
139139
DefaultSubscription.ToString(),
140140
null,
141141
null,
142+
false,
142143
null);
143144
}
144145

@@ -173,6 +174,7 @@ public void SubscriptionIdNotExist()
173174
DefaultSubscription.ToString(),
174175
null,
175176
null,
177+
false,
176178
null));
177179
}
178180

@@ -201,6 +203,7 @@ public void SpecifyTenantAndNotExistingSubscriptionId()
201203
DefaultSubscription.ToString(),
202204
null,
203205
null,
206+
false,
204207
null));
205208
}
206209

@@ -237,6 +240,7 @@ public void SubscriptionIdNotInFirstTenant()
237240
subscriptionInSecondTenant,
238241
null,
239242
null,
243+
false,
240244
null);
241245
}
242246

@@ -292,6 +296,7 @@ public void SubscriptionNameNotInFirstTenant()
292296
null,
293297
MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
294298
null,
299+
false,
295300
null);
296301
}
297302

@@ -342,6 +347,7 @@ public void TokenIdAndAccountIdMismatch()
342347
secondsubscriptionInTheFirstTenant,
343348
null,
344349
null,
350+
false,
345351
null);
346352

347353
var tenantsInAccount = azureRmProfile.DefaultContext.Account.GetPropertyAsArray(AzureAccount.Property.Tenants);
@@ -381,6 +387,7 @@ public void AdalExceptionsArePropagatedToCaller()
381387
secondsubscriptionInTheFirstTenant,
382388
null,
383389
null,
390+
false,
384391
null));
385392
}
386393

src/ResourceManager/Profile/Commands.Profile.Test/LoginCmdletTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void GetPsVersionFromUserAgent()
6565
cmdlt.CommandRuntime = commandRuntimeMock;
6666
cmdlt.Subscription = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
6767
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
68+
cmdlt.SetParameterSet("UserWithSubscriptionId");
6869

6970
cmdlt.InvokeBeginProcessing();
7071
int postProcessingUserAgentCount = AzureSession.Instance.ClientFactory.UserAgents.Length;
@@ -94,6 +95,7 @@ public void LoginWithSubscriptionAndTenant()
9495
cmdlt.CommandRuntime = commandRuntimeMock;
9596
cmdlt.Subscription = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
9697
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
98+
cmdlt.SetParameterSet("UserWithSubscriptionId");
9799

98100
// Act
99101
cmdlt.InvokeBeginProcessing();
@@ -113,6 +115,7 @@ public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
113115
cmdlt.CommandRuntime = commandRuntimeMock;
114116
cmdlt.Subscription = "2c224e7e-3ef5-431d-a57b-e71f4662e3a5";
115117
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
118+
cmdlt.SetParameterSet("UserWithSubscriptionId");
116119

117120
// Act
118121
cmdlt.InvokeBeginProcessing();
@@ -128,6 +131,7 @@ public void LoginWithSubscriptionAndNoTenant()
128131
// Setup
129132
cmdlt.CommandRuntime = commandRuntimeMock;
130133
cmdlt.Subscription = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
134+
cmdlt.SetParameterSet("UserWithSubscriptionId");
131135

132136
// Act
133137
cmdlt.InvokeBeginProcessing();
@@ -145,6 +149,7 @@ public void LoginWithNoSubscriptionAndNoTenant()
145149
var cmdlt = new AddAzureRMAccountCommand();
146150
// Setup
147151
cmdlt.CommandRuntime = commandRuntimeMock;
152+
cmdlt.SetParameterSet("UserWithSubscriptionId");
148153

149154
// Act
150155
cmdlt.InvokeBeginProcessing();
@@ -163,6 +168,7 @@ public void LoginWithNoSubscriptionAndTenant()
163168
// Setup
164169
cmdlt.CommandRuntime = commandRuntimeMock;
165170
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
171+
cmdlt.SetParameterSet("UserWithSubscriptionId");
166172

167173
// Act
168174
cmdlt.InvokeBeginProcessing();
@@ -182,6 +188,7 @@ public void LoginWithSubscriptionname()
182188
cmdlt.CommandRuntime = commandRuntimeMock;
183189
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
184190
cmdlt.Subscription = "Node CLI Test";
191+
cmdlt.SetParameterSet("UserWithSubscriptionId");
185192

186193
// Act
187194
cmdlt.InvokeBeginProcessing();
@@ -201,6 +208,7 @@ public void LoginWithRbacTenantOnly()
201208
// NOTE: Use [email protected] credentials for this test case
202209
cmdlt.CommandRuntime = commandRuntimeMock;
203210
cmdlt.TenantId = "1449d5b7-8a83-47db-ae4c-9b03e888bad0";
211+
cmdlt.SetParameterSet("UserWithSubscriptionId");
204212

205213
// Act
206214
cmdlt.InvokeBeginProcessing();
@@ -225,6 +233,7 @@ public void LoginWithRbacSPNAndCertificateOnly()
225233
cmdlt.TenantId = "54826b22-38d6-4fb2-bad9-b7b93a3e9c5a";
226234
cmdlt.ApplicationId = "99edf981-74c0-4284-bddf-3e9d092ba4e2";
227235
cmdlt.CertificateThumbprint = "F064B7C7EACC942D10662A5115E047E94FA18498";
236+
cmdlt.SetParameterSet("ServicePrincipalCertificateWithSubscriptionId");
228237

229238
// Act
230239
cmdlt.InvokeBeginProcessing();
@@ -249,6 +258,7 @@ public void GetMultipleTenantsOnLogin()
249258
// Setup
250259
// NOTE: Use account that has at exactly two tenants
251260
cmdlt.CommandRuntime = commandRuntimeMock;
261+
cmdlt.SetParameterSet("UserWithSubscriptionId");
252262

253263
// Act
254264
cmdlt.InvokeBeginProcessing();
@@ -270,6 +280,7 @@ public void LoginWithEnvironementName()
270280
// Setup
271281
cmdlt.CommandRuntime = commandRuntimeMock;
272282
cmdlt.Environment = "AzureUSGovernment";
283+
cmdlt.SetParameterSet("UserWithSubscriptionId");
273284

274285
// Act
275286
cmdlt.InvokeBeginProcessing();
@@ -300,6 +311,7 @@ public void LoginWithCredentialParameterAndMSA()
300311
Array.ForEach(password.ToCharArray(), securePassword.AppendChar);
301312

302313
cmdlt.Credential = new PSCredential(userName, securePassword);
314+
cmdlt.SetParameterSet("UserWithSubscriptionId");
303315

304316
// Act
305317
try
@@ -318,6 +330,29 @@ public void LoginWithCredentialParameterAndMSA()
318330
}
319331
}
320332

333+
[Fact]
334+
[Trait(Category.RunType, Category.LiveOnly)]
335+
public void LoginWithAccessToken()
336+
{
337+
var cmdlt = new AddAzureRMAccountCommand();
338+
// Setup
339+
cmdlt.CommandRuntime = commandRuntimeMock;
340+
341+
// Obtain an access token by using [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache.ReadItems() in powershell after logging in.
342+
// Ensure you are using the token with Resource: https://management.core.windows.net/
343+
string accessTokenEnvironmentVariable = Environment.GetEnvironmentVariable("AZURE_TEST_ACCESS_TOKEN");
344+
345+
cmdlt.AccessToken = accessTokenEnvironmentVariable;
346+
cmdlt.AccountId = "testAccount";
347+
cmdlt.SetParameterSet("AccessTokenWithSubscriptionId");
348+
349+
cmdlt.InvokeBeginProcessing();
350+
cmdlt.ExecuteCmdlet();
351+
cmdlt.InvokeEndProcessing();
352+
353+
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
354+
}
355+
321356
[Fact]
322357
[Trait(Category.AcceptanceType, Category.CheckIn)]
323358
public void ThrowOnUnknownEnvironment()
@@ -328,6 +363,7 @@ public void ThrowOnUnknownEnvironment()
328363
cmdlt.Environment = "unknown";
329364
var testPassed = false;
330365
cmdlt.SetBoundParameters(new Dictionary<string, object>() { { "Environment", "unknown" } });
366+
cmdlt.SetParameterSet("UserWithSubscriptionId");
331367

332368
// Act
333369
try
@@ -344,5 +380,30 @@ public void ThrowOnUnknownEnvironment()
344380

345381
Assert.True(testPassed);
346382
}
383+
384+
[Fact]
385+
[Trait(Category.AcceptanceType, Category.CheckIn)]
386+
public void LoginUsingSkipValidation()
387+
{
388+
var cmdlt = new AddAzureRMAccountCommand();
389+
// Setup
390+
cmdlt.CommandRuntime = commandRuntimeMock;
391+
392+
cmdlt.AccessToken = "test";
393+
cmdlt.AccessToken = "[email protected]";
394+
cmdlt.SkipValidation = true;
395+
cmdlt.TenantId = Guid.NewGuid().ToString();
396+
cmdlt.Subscription = Guid.NewGuid().ToString();
397+
cmdlt.SetBoundParameters(new Dictionary<string, object>() { { "Subscription", cmdlt.Subscription } });
398+
cmdlt.SetParameterSet("AccessTokenWithSubscriptionId");
399+
400+
cmdlt.InvokeBeginProcessing();
401+
cmdlt.ExecuteCmdlet();
402+
cmdlt.InvokeEndProcessing();
403+
404+
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
405+
Assert.Equal(AzureRmProfileProvider.Instance.Profile.DefaultContext.Subscription.Id, cmdlt.Subscription);
406+
Assert.Equal(AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Id, cmdlt.TenantId);
407+
}
347408
}
348409
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public class AddAzureRMAccountCommand : AzureContextModificationCmdlet, IModuleA
118118
[ValidateNotNullOrEmpty]
119119
public string ContextName { get; set; }
120120

121+
[Parameter(ParameterSetName = AccessTokenParameterSet,
122+
Mandatory = false, HelpMessage = "Skip validation for access token")]
123+
public SwitchParameter SkipValidation { get; set; }
124+
121125
[Parameter(Mandatory = false, HelpMessage = "Overwrite the existing context with the same name, if any.")]
122126
public SwitchParameter Force { get; set; }
123127

@@ -163,7 +167,7 @@ public override void ExecuteCmdlet()
163167

164168
AzureAccount azureAccount = new AzureAccount();
165169

166-
switch(ParameterSetName)
170+
switch (ParameterSetName)
167171
{
168172
case AccessTokenParameterSet:
169173
azureAccount.Type = AzureAccount.AccountType.AccessToken;
@@ -219,6 +223,7 @@ public override void ExecuteCmdlet()
219223
subscriptionId,
220224
subscriptionName,
221225
password,
226+
SkipValidation,
222227
(s) => WriteWarning(s),
223228
name));
224229
});

0 commit comments

Comments
 (0)