diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/ProfileCmdletTests.cs b/src/ResourceManager/Profile/Commands.Profile.Test/ProfileCmdletTests.cs index 5718df5efd76..1c5390852c52 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/ProfileCmdletTests.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/ProfileCmdletTests.cs @@ -52,7 +52,9 @@ public ProfileCmdletTests(ITestOutputHelper output) public void SelectAzureProfileInMemory() { var profile = new AzureRmProfile { DefaultContext = new AzureContext() }; - profile.EnvironmentTable.Add("foo", new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault())); + var env = new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault()); + env.Name = "foo"; + profile.EnvironmentTable.Add("foo", env); ImportAzureRMContextCommand cmdlt = new ImportAzureRMContextCommand(); // Setup cmdlt.AzureContext = profile; diff --git a/src/ResourceManager/Profile/Commands.Profile/Common/AzureContextModificationCmdlet.cs b/src/ResourceManager/Profile/Commands.Profile/Common/AzureContextModificationCmdlet.cs index 5e8b2d2cd117..3cfd34090690 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Common/AzureContextModificationCmdlet.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Common/AzureContextModificationCmdlet.cs @@ -47,6 +47,18 @@ protected virtual void ModifyContext(Action con } } + /// + /// Modify the Profile according to the selected scope for thsi invocation + /// + /// The action to take over the given profile + protected virtual void ModifyProfile(Action profileAction) + { + using (var profile = GetDefaultProfile()) + { + profileAction(profile); + } + } + /// /// Get the default profile for the current cmdlet invocation /// diff --git a/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs index 2e5712ca6c80..5ba0e0c8658f 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs @@ -15,14 +15,17 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; using Microsoft.Azure.Commands.Common.Authentication.Models; +using Microsoft.Azure.Commands.Common.Authentication.ResourceManager; using Microsoft.Azure.Commands.Profile.Common; using Microsoft.Azure.Commands.Profile.Models; // TODO: Remove IfDef #if NETSTANDARD +using Microsoft.Azure.Commands.Common.Authentication.Core; using Microsoft.Azure.Commands.Profile.Models.Core; #endif using Microsoft.Azure.Commands.Profile.Properties; using System; +using System.Linq; using System.Management.Automation; namespace Microsoft.Azure.Commands.Profile @@ -50,6 +53,55 @@ protected override void BeginProcessing() // Do not access the DefaultContext when loading a profile } + void CopyProfile(AzureRmProfile source, IProfileOperations target) + { + if (source == null || target == null) + { + return; + } + + foreach (var environment in source.Environments) + { + IAzureEnvironment merged; + target.TrySetEnvironment(environment, out merged); + } + + foreach (var context in source.Contexts) + { + target.TrySetContext(context.Key, context.Value); + } + + if (!string.IsNullOrWhiteSpace(source.DefaultContextKey)) + { + target.TrySetDefaultContext(source.DefaultContextKey); + } + + AzureRmProfileProvider.Instance.SetTokenCacheForProfile(target.ToProfile()); + EnsureProtectedCache(target, source.DefaultContext?.TokenCache?.CacheData); + } + + void EnsureProtectedCache(IProfileOperations profile, byte[] cacheData) + { + if (profile == null || cacheData == null) + { + return; + } + + AzureRmAutosaveProfile autosave = profile as AzureRmAutosaveProfile; + var protectedcache = AzureSession.Instance.TokenCache as ProtectedFileTokenCache; + if (autosave != null && protectedcache == null && cacheData.Any()) + { + try + { + var cache = new ProtectedFileTokenCache(cacheData, AzureSession.Instance.DataStore); + } + catch + { + WriteWarning(Resources.ImportAuthenticationFailure); + } + } + } + public override void ExecuteCmdlet() { bool executionComplete = false; @@ -65,11 +117,9 @@ public override void ExecuteCmdlet() Path)); } - ModifyContext((profile, client) => + ModifyProfile((profile) => { - var newProfile = new AzureRmProfile(Path); - profile.TryCopyProfile(newProfile); - AzureRmProfileProvider.Instance.SetTokenCacheForProfile(newProfile); + CopyProfile(new AzureRmProfile(Path), profile); executionComplete = true; }); }); @@ -78,10 +128,9 @@ public override void ExecuteCmdlet() { ConfirmAction(Resources.ProcessImportContextFromObject, Resources.ImportContextTarget, () => { - ModifyContext((profile, client) => + ModifyProfile((profile) => { - profile.TryCopyProfile(AzureContext); - AzureRmProfileProvider.Instance.SetTokenCacheForProfile(AzureContext); + CopyProfile(AzureContext, profile); executionComplete = true; }); }); diff --git a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs index e3cec7a94e3e..4303defedd97 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs @@ -456,6 +456,15 @@ internal static string FileOverwriteMessage { } } + /// + /// Looks up a localized string similar to Unable to import authentication information into the global cache. Please try executing the command again.. + /// + internal static string ImportAuthenticationFailure { + get { + return ResourceManager.GetString("ImportAuthenticationFailure", resourceCulture); + } + } + /// /// Looks up a localized string similar to Current context. /// diff --git a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx index 60f3a87c83a4..91e3850ccfb2 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx +++ b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx @@ -444,4 +444,7 @@ Removing module '{0}' from your machine + + Unable to import authentication information into the global cache. Please try executing the command again. + \ No newline at end of file