Skip to content

Commit 5c9bddb

Browse files
committed
Handle null contexts in profile context operations
1 parent 4c0f92a commit 5c9bddb

File tree

1 file changed

+53
-43
lines changed
  • src/ResourceManager/Common/Commands.Common.Authentication.ResourceManager

1 file changed

+53
-43
lines changed

src/ResourceManager/Common/Commands.Common.Authentication.ResourceManager/AzureRmProfile.cs

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -356,55 +356,57 @@ public bool TryFindContext(IAzureContext context, out string name)
356356
{
357357
bool result = false;
358358
name = null;
359-
var foundContext = Contexts.FirstOrDefault((c) =>
360-
c.Value != null
361-
&& (
362-
(c.Value.Account != null && context.Account != null && string.Equals(c.Value.Account.Id, context.Account.Id, StringComparison.OrdinalIgnoreCase))
363-
|| (c.Value.Account == context.Account))
364-
&& (
365-
(c.Value.Tenant != null && context.Tenant != null && c.Value.Tenant.GetId() == context.Tenant.GetId())
366-
|| (c.Value.Tenant == context.Tenant))
367-
&& (
368-
(c.Value.Subscription != null && context.Subscription != null && c.Value.Subscription.GetId() == context.Subscription.GetId())
369-
|| (c.Value.Subscription == context.Subscription)));
370-
if (!string.IsNullOrEmpty(foundContext.Key))
371-
{
372-
name = foundContext.Key;
373-
result = true;
359+
if (context != null)
360+
{
361+
var foundContext = Contexts.FirstOrDefault((c) =>
362+
c.Value != null
363+
&& (
364+
(c.Value.Account != null && context.Account != null && string.Equals(c.Value.Account.Id, context.Account.Id, StringComparison.OrdinalIgnoreCase))
365+
|| (c.Value.Account == context.Account))
366+
&& (
367+
(c.Value.Tenant != null && context.Tenant != null && c.Value.Tenant.GetId() == context.Tenant.GetId())
368+
|| (c.Value.Tenant == context.Tenant))
369+
&& (
370+
(c.Value.Subscription != null && context.Subscription != null && c.Value.Subscription.GetId() == context.Subscription.GetId())
371+
|| (c.Value.Subscription == context.Subscription)));
372+
if (!string.IsNullOrEmpty(foundContext.Key))
373+
{
374+
name = foundContext.Key;
375+
result = true;
376+
}
374377
}
375378

376379
return result;
377380
}
378381

379382
public bool TryGetContextName(IAzureContext context, out string name)
380383
{
381-
if (context == null)
382-
{
383-
throw new ArgumentNullException(nameof(context));
384-
}
385-
386384
bool result = false;
387385
name = null;
388-
if ((context.Account != null && !string.IsNullOrWhiteSpace(context.Account.Id)) || context.Subscription != null)
386+
if (context != null)
389387
{
390-
List<string> components = new List<string>();
391-
if (context.Account != null && !string.IsNullOrWhiteSpace(context.Account.Id))
388+
389+
if ((context.Account != null && !string.IsNullOrWhiteSpace(context.Account.Id)) || context.Subscription != null)
392390
{
393-
components.Add(context.Account.Id);
391+
List<string> components = new List<string>();
392+
if (context.Account != null && !string.IsNullOrWhiteSpace(context.Account.Id))
393+
{
394+
components.Add(context.Account.Id);
395+
}
396+
397+
if (context.Subscription != null)
398+
{
399+
components.Add(context.Subscription.GetId().ToString());
400+
}
401+
402+
name = string.Format("[{0}]", string.Join(", ", components));
403+
result = true;
394404
}
395-
396-
if (context.Subscription != null)
405+
else
397406
{
398-
components.Add(context.Subscription.GetId().ToString());
407+
name = "Default";
408+
result = true;
399409
}
400-
401-
name = string.Format("[{0}]", string.Join(", ", components));
402-
result = true;
403-
}
404-
else
405-
{
406-
name = "Default";
407-
result = true;
408410
}
409411

410412
return result;
@@ -478,7 +480,8 @@ public bool TrySetDefaultContext(IAzureContext context)
478480
{
479481
bool result = false;
480482
string contextName;
481-
if (TryFindContext(context, out contextName) || TryAddContext(context, out contextName))
483+
484+
if (context != null && (TryFindContext(context, out contextName) || TryAddContext(context, out contextName)))
482485
{
483486
result = TrySetDefaultContext(contextName);
484487
}
@@ -489,13 +492,16 @@ public bool TrySetDefaultContext(IAzureContext context)
489492
public bool TrySetDefaultContext(string name, IAzureContext context)
490493
{
491494
bool result = false;
492-
if (string.IsNullOrWhiteSpace(name))
495+
if (context != null)
493496
{
494-
result = TrySetDefaultContext(context);
495-
}
496-
else if (TrySetContext(name, context))
497-
{
498-
result = TrySetDefaultContext(name);
497+
if (string.IsNullOrWhiteSpace(name))
498+
{
499+
result = TrySetDefaultContext(context);
500+
}
501+
else if (TrySetContext(name, context))
502+
{
503+
result = TrySetDefaultContext(name);
504+
}
499505
}
500506

501507
return result;
@@ -562,7 +568,11 @@ public bool TryCopyProfile(AzureRmProfile other)
562568
TrySetContext(context.Key, context.Value);
563569
}
564570

565-
this.TrySetDefaultContext(other.DefaultContext);
571+
if (other.DefaultContext != null)
572+
{
573+
this.TrySetDefaultContext(other.DefaultContext);
574+
}
575+
566576
this.CopyPropertiesFrom(other);
567577
return true;
568578
}

0 commit comments

Comments
 (0)