Skip to content

Commit c4de9ec

Browse files
committed
Fix token renewal problem for rm and graph tokens passed in
1 parent c65fc0f commit c4de9ec

File tree

11 files changed

+458
-22
lines changed

11 files changed

+458
-22
lines changed

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureAccountExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,28 @@ public static void CopyFrom(this IAzureAccount account, IAzureAccount source)
197197
{
198198
account.TenantMap[item.Key] = item.Value;
199199
}
200+
200201
account.CopyPropertiesFrom(source);
201202
}
202203
}
204+
205+
/// <summary>
206+
/// Update non-null non-identity account properties from the given account
207+
/// </summary>
208+
/// <param name="account">The account to copy to (target)</param>
209+
/// <param name="other">The account to copy from (source)</param>
210+
public static void Update(this IAzureAccount account, IAzureAccount source)
211+
{
212+
if (account != null && source != null)
213+
{
214+
account.Credential = source.Credential ?? account.Credential;
215+
foreach (var item in source.TenantMap)
216+
{
217+
account.TenantMap[item.Key] = item.Value;
218+
}
219+
220+
account.UpdateProperties(source);
221+
}
222+
}
203223
}
204224
}

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureContextExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,21 @@ public static IAzureAccount GetAccount(this IAzureContextContainer container, st
128128
{
129129
return container.Accounts.FirstOrDefault((a) => string.Equals(a.Id, account, StringComparison.CurrentCultureIgnoreCase));
130130
}
131+
132+
/// <summary>
133+
/// Update the properties of the context
134+
/// </summary>
135+
/// <param name="context">The context to update</param>
136+
/// <param name="other">The context to update from</param>
137+
public static void Update(this IAzureContext context, IAzureContext other)
138+
{
139+
if (context != null && other != null)
140+
{
141+
context.Account.Update(other.Account);
142+
context.Subscription.Update(other.Subscription);
143+
context.Tenant.Update(other.Tenant);
144+
context.UpdateProperties(other);
145+
}
146+
}
131147
}
132148
}

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureEnvironmentExtensions.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,96 @@ public static void CopyFrom(this IAzureEnvironment environment, IAzureEnvironmen
451451
}
452452

453453
}
454+
455+
public static void Update(this IAzureEnvironment environment, IAzureEnvironment other)
456+
{
457+
if (environment != null && other != null)
458+
{
459+
environment.OnPremise = other.OnPremise;
460+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId))
461+
{
462+
environment.ActiveDirectoryServiceEndpointResourceId = other.ActiveDirectoryServiceEndpointResourceId;
463+
}
464+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.AdTenant))
465+
{
466+
environment.AdTenant = other.AdTenant;
467+
}
468+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.Gallery))
469+
{
470+
environment.GalleryUrl = other.GalleryUrl;
471+
}
472+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.ManagementPortalUrl))
473+
{
474+
environment.ManagementPortalUrl = other.ManagementPortalUrl;
475+
}
476+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.ServiceManagement))
477+
{
478+
environment.ServiceManagementUrl = other.ServiceManagementUrl;
479+
}
480+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.PublishSettingsFileUrl))
481+
{
482+
environment.PublishSettingsFileUrl = other.PublishSettingsFileUrl;
483+
}
484+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.ResourceManager))
485+
{
486+
environment.ResourceManagerUrl = other.ResourceManagerUrl;
487+
}
488+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix))
489+
{
490+
environment.SqlDatabaseDnsSuffix = other.SqlDatabaseDnsSuffix;
491+
}
492+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.StorageEndpointSuffix))
493+
{
494+
environment.StorageEndpointSuffix = other.StorageEndpointSuffix;
495+
}
496+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.ActiveDirectory))
497+
{
498+
environment.ActiveDirectoryAuthority = other.ActiveDirectoryAuthority;
499+
}
500+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.Graph))
501+
{
502+
environment.GraphUrl = other.GraphUrl;
503+
}
504+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.GraphEndpointResourceId))
505+
{
506+
environment.GraphEndpointResourceId = other.GraphEndpointResourceId;
507+
}
508+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.TrafficManagerDnsSuffix))
509+
{
510+
environment.TrafficManagerDnsSuffix = other.TrafficManagerDnsSuffix;
511+
}
512+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix))
513+
{
514+
environment.AzureKeyVaultDnsSuffix = other.AzureKeyVaultDnsSuffix;
515+
}
516+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.AzureDataLakeStoreFileSystemEndpointSuffix))
517+
{
518+
environment.AzureDataLakeStoreFileSystemEndpointSuffix = other.AzureDataLakeStoreFileSystemEndpointSuffix;
519+
}
520+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix))
521+
{
522+
environment.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix =
523+
other.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix;
524+
}
525+
if (other.IsEndpointSet(AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId))
526+
{
527+
environment.AzureKeyVaultServiceEndpointResourceId =
528+
other.AzureKeyVaultServiceEndpointResourceId;
529+
}
530+
531+
foreach (var profile in other.VersionProfiles)
532+
{
533+
if (!environment.VersionProfiles.Contains(profile))
534+
{
535+
environment.VersionProfiles.Add(profile);
536+
}
537+
}
538+
539+
environment.UpdateProperties(other);
540+
}
541+
542+
}
543+
454544
}
455545

456546
}

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureSubscriptionExtensions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static void SetTenant(this IAzureSubscription subscription, string tenant
142142
}
143143

144144
/// <summary>
145-
///
145+
/// Copy the properties from the given subscription
146146
/// </summary>
147147
/// <param name="subscription"></param>
148148
/// <param name="other"></param>
@@ -156,5 +156,20 @@ public static void CopyFrom(this IAzureSubscription subscription, IAzureSubscrip
156156
subscription.CopyPropertiesFrom(other);
157157
}
158158
}
159+
160+
/// <summary>
161+
/// Update the non-identity properties from the given subscription
162+
/// </summary>
163+
/// <param name="subscription"></param>
164+
/// <param name="other"></param>
165+
public static void Update(this IAzureSubscription subscription, IAzureSubscription other)
166+
{
167+
if (subscription != null && other != null)
168+
{
169+
subscription.Name = other.Name?? subscription.Name;
170+
subscription.State = other.State?? subscription.State;
171+
subscription.UpdateProperties(other);
172+
}
173+
}
159174
}
160175
}

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureTenantExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,19 @@ public static void CopyFrom(this IAzureTenant tenant, IAzureTenant other)
4242
tenant.CopyPropertiesFrom(other);
4343
}
4444
}
45+
46+
/// <summary>
47+
/// Update the non-identity properties of this tenant, using another tenant
48+
/// </summary>
49+
/// <param name="tenant"></param>
50+
/// <param name="other"></param>
51+
public static void Update(this IAzureTenant tenant, IAzureTenant other)
52+
{
53+
if (tenant != null && other != null)
54+
{
55+
tenant.Directory = other.Directory?? tenant.Directory;
56+
tenant.UpdateProperties(other);
57+
}
58+
}
4559
}
4660
}

src/Common/Commands.Common.Authentication.Abstractions/Extensions/ModelExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,21 @@ public static void CopyPropertiesFrom(this IExtensibleModel model, IExtensibleMo
115115
}
116116
}
117117
}
118+
119+
/// <summary>
120+
/// Copy Unset propeties from another extensible model to this one
121+
/// </summary>
122+
/// <param name="model"></param>
123+
/// <param name="newModel"></param>
124+
public static void UpdateProperties(this IExtensibleModel model, IExtensibleModel newModel)
125+
{
126+
if (model != null && newModel != null)
127+
{
128+
foreach (var item in newModel.ExtendedProperties)
129+
{
130+
model.SetProperty(item.Key, item.Value);
131+
}
132+
}
133+
}
118134
}
119135
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,12 @@ public bool TrySetContext(string name, IAzureContext context)
456456
public bool TrySetContext(IAzureContext context, out string name)
457457
{
458458
bool result = false;
459-
if (!TryFindContext(context, out name))
459+
if (TryFindContext(context, out name) && Contexts != null && Contexts.ContainsKey(name))
460+
{
461+
Contexts[name].Update(context);
462+
result = true;
463+
}
464+
else
460465
{
461466
result = TryAddContext(context, out name);
462467
}
@@ -483,7 +488,11 @@ public bool TrySetDefaultContext(IAzureContext context)
483488

484489
if (context != null && (TryFindContext(context, out contextName) || TryAddContext(context, out contextName)))
485490
{
486-
result = TrySetDefaultContext(contextName);
491+
if (TrySetDefaultContext(contextName) && DefaultContext != null)
492+
{
493+
DefaultContext.Update(context);
494+
result = true;
495+
}
487496
}
488497

489498
return result;

0 commit comments

Comments
 (0)