38
38
using Microsoft . Rest . Azure . OData ;
39
39
using Microsoft . Azure . Management . Internal . Resources . Models ;
40
40
using Microsoft . WindowsAzure . Commands . Common . CustomAttributes ;
41
+ using Microsoft . Azure . Commands . Common . Exceptions ;
42
+ using Microsoft . WindowsAzure . Commands . Common ;
41
43
42
44
namespace Microsoft . Azure . Commands . Aks
43
45
{
@@ -150,7 +152,7 @@ protected virtual ManagedCluster BuildNewCluster()
150
152
new ContainerServiceLinuxProfile ( LinuxProfileAdminUserName ,
151
153
new ContainerServiceSshConfiguration ( pubKey ) ) ;
152
154
153
- var acsServicePrincipal = EnsureServicePrincipal ( ServicePrincipalIdAndSecret ? . UserName , ServicePrincipalIdAndSecret ? . Password ? . ToString ( ) ) ;
155
+ var acsServicePrincipal = EnsureServicePrincipal ( ServicePrincipalIdAndSecret ? . UserName , ServicePrincipalIdAndSecret ? . Password ? . ConvertToString ( ) ) ;
154
156
155
157
var spProfile = new ManagedClusterServicePrincipalProfile (
156
158
acsServicePrincipal . SpId ,
@@ -218,8 +220,6 @@ protected void BeforeBuildNewCluster()
218
220
/// <exception cref="ArgumentException">The SSH key or file argument was null and there was no default pub key in path.</exception>
219
221
protected string GetSshKey ( string sshKeyOrFile )
220
222
{
221
- const string helpLink = "https://docs.microsoft.com/en-us/azure/virtual-machines/linux/mac-create-ssh-keys" ;
222
-
223
223
// SSH key was specified as either a file or as key data
224
224
if ( ! string . IsNullOrEmpty ( SshKeyValue ) )
225
225
{
@@ -237,7 +237,8 @@ protected string GetSshKey(string sshKeyOrFile)
237
237
var path = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".ssh" , "id_rsa.pub" ) ;
238
238
if ( ! AzureSession . Instance . DataStore . FileExists ( path ) )
239
239
{
240
- throw new ArgumentException ( string . Format ( Resources . CouldNotFindSshPublicKeyInError , path , helpLink ) ) ;
240
+ var errorMessage = string . Format ( Resources . CouldNotFindSshPublicKeyInError , path ) ;
241
+ throw new AzPSArgumentException ( errorMessage , nameof ( SshKeyValue ) ) ;
241
242
}
242
243
243
244
WriteVerbose ( string . Format ( Resources . FetchSshPublicKeyFromFile , path ) ) ;
@@ -248,12 +249,22 @@ protected string GetSshKey(string sshKeyOrFile)
248
249
249
250
protected AcsServicePrincipal EnsureServicePrincipal ( string spId = null , string clientSecret = null )
250
251
{
252
+ //If user specifies service principal, just use it directly and no need to save to disk
253
+ if ( ! string . IsNullOrEmpty ( spId ) && ! string . IsNullOrEmpty ( clientSecret ) )
254
+ {
255
+ return new AcsServicePrincipal ( )
256
+ {
257
+ SpId = spId ,
258
+ ClientSecret = clientSecret
259
+ } ;
260
+ }
261
+
251
262
var acsServicePrincipal = LoadServicePrincipal ( ) ;
252
263
if ( acsServicePrincipal == null )
253
264
{
254
- WriteVerbose ( string . Format (
265
+ WriteWarning ( string . Format (
255
266
Resources . NoServicePrincipalFoundCreatingANewServicePrincipal ,
256
- AcsSpFilePath ) ) ;
267
+ AcsSpFilePath , DefaultContext . Subscription . Id ) ) ;
257
268
258
269
// if nothing to load, make one
259
270
if ( clientSecret == null )
@@ -296,14 +307,16 @@ private AcsServicePrincipal BuildServicePrincipal(string name, string url, strin
296
307
297
308
if ( ! success )
298
309
{
299
- throw new CmdletInvocationException ( Resources . CouldNotCreateAServicePrincipalWithTheRightPermissionsAreYouAnOwner ) ;
310
+ throw new AzPSInvalidOperationException (
311
+ Resources . CouldNotCreateAServicePrincipalWithTheRightPermissionsAreYouAnOwner ,
312
+ desensitizedMessage : Resources . CouldNotCreateAServicePrincipalWithTheRightPermissionsAreYouAnOwner ) ;
300
313
}
301
314
302
315
AddSubscriptionRoleAssignment ( "Contributor" , sp . ObjectId ) ;
303
316
return new AcsServicePrincipal { SpId = app . AppId , ClientSecret = clientSecret , ObjectId = app . ObjectId } ;
304
317
}
305
318
306
- protected void AddAcrRoleAssignment ( string acrName , AcsServicePrincipal acsServicePrincipal )
319
+ protected void AddAcrRoleAssignment ( string acrName , string acrParameterName , AcsServicePrincipal acsServicePrincipal )
307
320
{
308
321
string acrResourceId = null ;
309
322
try
@@ -313,9 +326,12 @@ protected void AddAcrRoleAssignment(string acrName, AcsServicePrincipal acsServi
313
326
var acrObjects = RmClient . Resources . List ( acrQuery ) ;
314
327
acrResourceId = acrObjects . First ( ) . Id ;
315
328
}
316
- catch ( Exception ex )
329
+ catch ( Exception )
317
330
{
318
- throw new CmdletInvocationException ( string . Format ( Resources . CouldNotFindSpecifiedAcr , acrName ) , ex ) ;
331
+ throw new AzPSArgumentException (
332
+ string . Format ( Resources . CouldNotFindSpecifiedAcr , acrName ) ,
333
+ acrParameterName ,
334
+ string . Format ( Resources . CouldNotFindSpecifiedAcr , "*" ) ) ;
319
335
}
320
336
321
337
var roleId = GetRoleId ( "acrpull" , acrResourceId ) ;
@@ -331,7 +347,10 @@ protected void AddAcrRoleAssignment(string acrName, AcsServicePrincipal acsServi
331
347
}
332
348
catch ( Exception ex )
333
349
{
334
- throw new CmdletInvocationException ( string . Format ( Resources . CouldNotFindObjectIdForServicePrincipal , acsServicePrincipal . SpId ) , ex ) ;
350
+ throw new AzPSInvalidOperationException (
351
+ string . Format ( Resources . CouldNotFindObjectIdForServicePrincipal , acsServicePrincipal . SpId ) ,
352
+ ex ,
353
+ string . Format ( Resources . CouldNotFindObjectIdForServicePrincipal , "*" ) ) ;
335
354
}
336
355
}
337
356
var success = RetryAction ( ( ) =>
@@ -342,8 +361,9 @@ protected void AddAcrRoleAssignment(string acrName, AcsServicePrincipal acsServi
342
361
343
362
if ( ! success )
344
363
{
345
- throw new CmdletInvocationException (
346
- Resources . CouldNotAddAcrRoleAssignment ) ;
364
+ throw new AzPSInvalidOperationException (
365
+ Resources . CouldNotAddAcrRoleAssignment ,
366
+ desensitizedMessage : Resources . CouldNotAddAcrRoleAssignment ) ;
347
367
}
348
368
}
349
369
@@ -374,8 +394,9 @@ protected void AddSubscriptionRoleAssignment(string role, string appId)
374
394
375
395
if ( ! success )
376
396
{
377
- throw new CmdletInvocationException (
378
- Resources . CouldNotCreateAServicePrincipalWithTheRightPermissionsAreYouAnOwner ) ;
397
+ throw new AzPSInvalidOperationException (
398
+ Resources . CouldNotAssignServicePrincipalWithSubsContributorPermission ,
399
+ desensitizedMessage : Resources . CouldNotAssignServicePrincipalWithSubsContributorPermission ) ;
379
400
}
380
401
}
381
402
@@ -407,7 +428,11 @@ protected bool RetryAction(Action action, string actionName = null)
407
428
protected AcsServicePrincipal LoadServicePrincipal ( )
408
429
{
409
430
var config = LoadServicePrincipals ( ) ;
410
- return config ? [ DefaultContext . Subscription . Id ] ;
431
+ if ( config ? . ContainsKey ( DefaultContext . Subscription . Id ) == true )
432
+ {
433
+ return config [ DefaultContext . Subscription . Id ] ;
434
+ }
435
+ return null ;
411
436
}
412
437
413
438
protected Dictionary < string , AcsServicePrincipal > LoadServicePrincipals ( )
0 commit comments