Skip to content

Commit e8d1498

Browse files
authored
Merge pull request #7949 from markcowl/import-fix
Fix persistence and cache issues with import-context cmdlet
2 parents 0f22fad + cfd2146 commit e8d1498

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public ProfileCmdletTests(ITestOutputHelper output)
5252
public void SelectAzureProfileInMemory()
5353
{
5454
var profile = new AzureRmProfile { DefaultContext = new AzureContext() };
55-
profile.EnvironmentTable.Add("foo", new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault()));
55+
var env = new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault());
56+
env.Name = "foo";
57+
profile.EnvironmentTable.Add("foo", env);
5658
ImportAzureRMContextCommand cmdlt = new ImportAzureRMContextCommand();
5759
// Setup
5860
cmdlt.AzureContext = profile;

src/ResourceManager/Profile/Commands.Profile/Common/AzureContextModificationCmdlet.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ protected virtual void ModifyContext(Action<AzureRmProfile, RMProfileClient> con
4747
}
4848
}
4949

50+
/// <summary>
51+
/// Modify the Profile according to the selected scope for thsi invocation
52+
/// </summary>
53+
/// <param name="profileAction">The action to take over the given profile</param>
54+
protected virtual void ModifyProfile(Action<IProfileOperations> profileAction)
55+
{
56+
using (var profile = GetDefaultProfile())
57+
{
58+
profileAction(profile);
59+
}
60+
}
61+
5062
/// <summary>
5163
/// Get the default profile for the current cmdlet invocation
5264
/// </summary>

src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1717
using Microsoft.Azure.Commands.Common.Authentication.Models;
18+
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager;
1819
using Microsoft.Azure.Commands.Profile.Common;
1920
using Microsoft.Azure.Commands.Profile.Models;
2021
// TODO: Remove IfDef
2122
#if NETSTANDARD
23+
using Microsoft.Azure.Commands.Common.Authentication.Core;
2224
using Microsoft.Azure.Commands.Profile.Models.Core;
2325
#endif
2426
using Microsoft.Azure.Commands.Profile.Properties;
2527
using System;
28+
using System.Linq;
2629
using System.Management.Automation;
2730

2831
namespace Microsoft.Azure.Commands.Profile
@@ -50,6 +53,55 @@ protected override void BeginProcessing()
5053
// Do not access the DefaultContext when loading a profile
5154
}
5255

56+
void CopyProfile(AzureRmProfile source, IProfileOperations target)
57+
{
58+
if (source == null || target == null)
59+
{
60+
return;
61+
}
62+
63+
foreach (var environment in source.Environments)
64+
{
65+
IAzureEnvironment merged;
66+
target.TrySetEnvironment(environment, out merged);
67+
}
68+
69+
foreach (var context in source.Contexts)
70+
{
71+
target.TrySetContext(context.Key, context.Value);
72+
}
73+
74+
if (!string.IsNullOrWhiteSpace(source.DefaultContextKey))
75+
{
76+
target.TrySetDefaultContext(source.DefaultContextKey);
77+
}
78+
79+
AzureRmProfileProvider.Instance.SetTokenCacheForProfile(target.ToProfile());
80+
EnsureProtectedCache(target, source.DefaultContext?.TokenCache?.CacheData);
81+
}
82+
83+
void EnsureProtectedCache(IProfileOperations profile, byte[] cacheData)
84+
{
85+
if (profile == null || cacheData == null)
86+
{
87+
return;
88+
}
89+
90+
AzureRmAutosaveProfile autosave = profile as AzureRmAutosaveProfile;
91+
var protectedcache = AzureSession.Instance.TokenCache as ProtectedFileTokenCache;
92+
if (autosave != null && protectedcache == null && cacheData.Any())
93+
{
94+
try
95+
{
96+
var cache = new ProtectedFileTokenCache(cacheData, AzureSession.Instance.DataStore);
97+
}
98+
catch
99+
{
100+
WriteWarning(Resources.ImportAuthenticationFailure);
101+
}
102+
}
103+
}
104+
53105
public override void ExecuteCmdlet()
54106
{
55107
bool executionComplete = false;
@@ -65,11 +117,9 @@ public override void ExecuteCmdlet()
65117
Path));
66118
}
67119

68-
ModifyContext((profile, client) =>
120+
ModifyProfile((profile) =>
69121
{
70-
var newProfile = new AzureRmProfile(Path);
71-
profile.TryCopyProfile(newProfile);
72-
AzureRmProfileProvider.Instance.SetTokenCacheForProfile(newProfile);
122+
CopyProfile(new AzureRmProfile(Path), profile);
73123
executionComplete = true;
74124
});
75125
});
@@ -78,10 +128,9 @@ public override void ExecuteCmdlet()
78128
{
79129
ConfirmAction(Resources.ProcessImportContextFromObject, Resources.ImportContextTarget, () =>
80130
{
81-
ModifyContext((profile, client) =>
131+
ModifyProfile((profile) =>
82132
{
83-
profile.TryCopyProfile(AzureContext);
84-
AzureRmProfileProvider.Instance.SetTokenCacheForProfile(AzureContext);
133+
CopyProfile(AzureContext, profile);
85134
executionComplete = true;
86135
});
87136
});

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,7 @@
444444
<data name="ShouldRemoveModule" xml:space="preserve">
445445
<value>Removing module '{0}' from your machine</value>
446446
</data>
447+
<data name="ImportAuthenticationFailure" xml:space="preserve">
448+
<value>Unable to import authentication information into the global cache. Please try executing the command again.</value>
449+
</data>
447450
</root>

0 commit comments

Comments
 (0)