From 37602e324e66d7ce5757eb09da98e04288ebb973 Mon Sep 17 00:00:00 2001 From: markcowl Date: Wed, 28 Nov 2018 18:37:23 -0800 Subject: [PATCH 1/4] Update import to use TokenCache from imported context --- .../Context/ImportAzureRMContext.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs index 2e5712ca6c80..be6f60439de3 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs @@ -23,6 +23,7 @@ #endif using Microsoft.Azure.Commands.Profile.Properties; using System; +using System.Linq; using System.Management.Automation; namespace Microsoft.Azure.Commands.Profile @@ -68,7 +69,13 @@ public override void ExecuteCmdlet() ModifyContext((profile, client) => { var newProfile = new AzureRmProfile(Path); + var cache = newProfile?.DefaultContext?.TokenCache; profile.TryCopyProfile(newProfile); + if (cache != null && cache.CacheData.Any()) + { + AzureSession.Instance.TokenCache.CacheData = cache.CacheData; + } + AzureRmProfileProvider.Instance.SetTokenCacheForProfile(newProfile); executionComplete = true; }); @@ -80,7 +87,13 @@ public override void ExecuteCmdlet() { ModifyContext((profile, client) => { + var cache = profile?.DefaultContext?.TokenCache; profile.TryCopyProfile(AzureContext); + if (cache != null && cache.CacheData.Any()) + { + AzureSession.Instance.TokenCache.CacheData = cache.CacheData; + } + AzureRmProfileProvider.Instance.SetTokenCacheForProfile(AzureContext); executionComplete = true; }); From 535e949e9572f4da43790e624552bf6a0007616a Mon Sep 17 00:00:00 2001 From: markcowl Date: Wed, 28 Nov 2018 21:43:24 -0800 Subject: [PATCH 2/4] Fix token cache and persistence issues with Import-Context --- .../Common/AzureContextModificationCmdlet.cs | 12 ++++ .../Context/ImportAzureRMContext.cs | 69 ++++++++++++++----- .../Properties/Resources.Designer.cs | 9 +++ .../Properties/Resources.resx | 3 + 4 files changed, 74 insertions(+), 19 deletions(-) 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 be6f60439de3..c81c36f53e75 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs @@ -14,7 +14,9 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Core; 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 @@ -51,6 +53,50 @@ 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) + { + 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; @@ -66,17 +112,9 @@ public override void ExecuteCmdlet() Path)); } - ModifyContext((profile, client) => + ModifyProfile((profile) => { - var newProfile = new AzureRmProfile(Path); - var cache = newProfile?.DefaultContext?.TokenCache; - profile.TryCopyProfile(newProfile); - if (cache != null && cache.CacheData.Any()) - { - AzureSession.Instance.TokenCache.CacheData = cache.CacheData; - } - - AzureRmProfileProvider.Instance.SetTokenCacheForProfile(newProfile); + CopyProfile(new AzureRmProfile(Path), profile); executionComplete = true; }); }); @@ -85,16 +123,9 @@ public override void ExecuteCmdlet() { ConfirmAction(Resources.ProcessImportContextFromObject, Resources.ImportContextTarget, () => { - ModifyContext((profile, client) => + ModifyProfile((profile) => { - var cache = profile?.DefaultContext?.TokenCache; - profile.TryCopyProfile(AzureContext); - if (cache != null && cache.CacheData.Any()) - { - AzureSession.Instance.TokenCache.CacheData = cache.CacheData; - } - - 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 From 3e1ab7e4e7059dd5dc164f0f011c71030f11bb8c Mon Sep 17 00:00:00 2001 From: markcowl Date: Thu, 29 Nov 2018 11:45:38 -0800 Subject: [PATCH 3/4] Updating test to include environment name in environment record --- .../Profile/Commands.Profile.Test/ProfileCmdletTests.cs | 4 +++- .../Commands.Profile/Context/ImportAzureRMContext.cs | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) 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/Context/ImportAzureRMContext.cs b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs index c81c36f53e75..ca618bead71b 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs @@ -14,14 +14,16 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; -using Microsoft.Azure.Commands.Common.Authentication.Core; 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; +#else +using Microsoft.Azure.Commands.Common.Authentication; #endif using Microsoft.Azure.Commands.Profile.Properties; using System; @@ -82,6 +84,11 @@ void CopyProfile(AzureRmProfile source, IProfileOperations target) 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()) From cfd2146edbfda37d0c4b9c20a744caa8118cc9a0 Mon Sep 17 00:00:00 2001 From: markcowl Date: Fri, 30 Nov 2018 11:37:22 -0800 Subject: [PATCH 4/4] Fix net45 usings for Import-Context cmdlet --- .../Profile/Commands.Profile/Context/ImportAzureRMContext.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs index ca618bead71b..5ba0e0c8658f 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs @@ -22,8 +22,6 @@ #if NETSTANDARD using Microsoft.Azure.Commands.Common.Authentication.Core; using Microsoft.Azure.Commands.Profile.Models.Core; -#else -using Microsoft.Azure.Commands.Common.Authentication; #endif using Microsoft.Azure.Commands.Profile.Properties; using System;