diff --git a/src/Common/Commands.Common/GeneralUtilities.cs b/src/Common/Commands.Common/GeneralUtilities.cs index d76ad5fa229a..ccf454082b35 100644 --- a/src/Common/Commands.Common/GeneralUtilities.cs +++ b/src/Common/Commands.Common/GeneralUtilities.cs @@ -442,6 +442,5 @@ public static void ClearCurrentStorageAccount(bool clearSMContext = false) } } } - } } \ No newline at end of file diff --git a/src/ResourceManager/Common/Commands.ResourceManager.Common/AccessTokenExtensions.cs b/src/ResourceManager/Common/Commands.ResourceManager.Common/AccessTokenExtensions.cs index b0d2563c5d43..a546769f5940 100644 --- a/src/ResourceManager/Common/Commands.ResourceManager.Common/AccessTokenExtensions.cs +++ b/src/ResourceManager/Common/Commands.ResourceManager.Common/AccessTokenExtensions.cs @@ -1,4 +1,18 @@ -using System; +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; using System.Linq; using Microsoft.Azure.Common.Authentication; diff --git a/src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs b/src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs new file mode 100644 index 000000000000..e2dc5eed060c --- /dev/null +++ b/src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs @@ -0,0 +1,49 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Azure.Commands.ResourceManager.Common.Properties; +using Microsoft.Azure.Common.Authentication.Models; +using Microsoft.IdentityModel.Clients.ActiveDirectory; + +namespace Microsoft.Azure.Commands.ResourceManager.Common +{ + public static class AzureRMProfileExtensions + { + /// + /// Set the context for the current profile, preserving token cache information + /// + /// The profile to change the context for + /// The new context, with no token cache information. + public static void SetContextWithCache(this AzureRMProfile profile, AzureContext newContext) + { + if (profile == null) + { + throw new ArgumentNullException("profile", Resources.ProfileCannotBeNull); + } + + if (newContext == null) + { + throw new ArgumentNullException("newContext", Resources.ContextCannotBeNull); + } + + newContext.TokenCache = TokenCache.DefaultShared.Serialize(); + profile.Context = newContext; + } + } +} diff --git a/src/ResourceManager/Common/Commands.ResourceManager.Common/Commands.ResourceManager.Common.csproj b/src/ResourceManager/Common/Commands.ResourceManager.Common/Commands.ResourceManager.Common.csproj index 71534f031656..57f80edf2d29 100644 --- a/src/ResourceManager/Common/Commands.ResourceManager.Common/Commands.ResourceManager.Common.csproj +++ b/src/ResourceManager/Common/Commands.ResourceManager.Common/Commands.ResourceManager.Common.csproj @@ -138,6 +138,7 @@ + diff --git a/src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.Designer.cs b/src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.Designer.cs index 3622d8e6e313..e8836fe4a665 100644 --- a/src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.Designer.cs @@ -8,10 +8,10 @@ // //------------------------------------------------------------------------------ -namespace Microsoft.Azure.Commands.ResourceManager.Common.Properties -{ - - +namespace Microsoft.Azure.Commands.ResourceManager.Common.Properties { + using System; + + /// /// A strongly-typed resource class, for looking up localized strings, etc. /// @@ -60,6 +60,15 @@ internal Resources() { } } + /// + /// Looks up a localized string similar to Context cannot be null. Please log in using Add-AzureRmAccount.. + /// + public static string ContextCannotBeNull { + get { + return ResourceManager.GetString("ContextCannotBeNull", resourceCulture); + } + } + /// /// Looks up a localized string similar to Microsoft Azure PowerShell Data Collection Confirmation. /// @@ -135,6 +144,15 @@ public static string NoSubscriptionFound { } } + /// + /// Looks up a localized string similar to Profile cannot be null. Please run Add-AzureRmAccount.. + /// + public static string ProfileCannotBeNull { + get { + return ResourceManager.GetString("ProfileCannotBeNull", resourceCulture); + } + } + /// /// Looks up a localized string similar to Attempting to register resource provider '{0}'. /// diff --git a/src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.resx b/src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.resx index f1be5d7daeec..42973da7336d 100644 --- a/src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.resx +++ b/src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.resx @@ -117,6 +117,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Context cannot be null. Please log in using Add-AzureRmAccount. + Microsoft Azure PowerShell Data Collection Confirmation @@ -151,6 +154,9 @@ Select Y to enable data collection [Y/N]: The provided account {0} does not have access to any subscriptions. Please try logging in with different credentials. + + Profile cannot be null. Please run Add-AzureRmAccount. + Attempting to register resource provider '{0}' diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs index 8ca2d32b944a..8388f684e677 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs @@ -19,6 +19,7 @@ using System; using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; using System.Collections.Generic; +using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.WindowsAzure.Commands.ScenarioTest; namespace Microsoft.Azure.Commands.ResourceManager.Common.Test @@ -131,5 +132,18 @@ public void NoSubscriptionsInListThrows() AzureSubscription subValue; Assert.False(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue)); } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void SetContextPreservesTokenCache() + { + AzureRMProfile profile = null; + AzureContext context = new AzureContext(null, null, null, null); + Assert.Throws(() =>profile.SetContextWithCache(context)); + profile = new AzureRMProfile(); + Assert.Throws(() => profile.SetContextWithCache(null)); + profile.SetContextWithCache(context); + Assert.Equal(TokenCache.DefaultShared.Serialize(), profile.Context.TokenCache); + } } } diff --git a/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs b/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs index b5476c8a08eb..a0a4f38fb9b5 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs @@ -55,8 +55,8 @@ protected override void ProcessRecord() { if (ParameterSetName == ContextParameterSet) { - AzureRmProfileProvider.Instance.Profile.Context = new AzureContext(Context.Subscription, Context.Account, - Context.Environment, Context.Tenant); + AzureRmProfileProvider.Instance.Profile.SetContextWithCache(new AzureContext(Context.Subscription, Context.Account, + Context.Environment, Context.Tenant)); } else { diff --git a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs index 3d553a1abcb0..dcec21991609 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs @@ -105,11 +105,11 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId) { if (!string.IsNullOrWhiteSpace(tenantId)) { - _profile.Context = new AzureContext( + _profile.SetContextWithCache(new AzureContext( _profile.Context.Subscription, _profile.Context.Account, _profile.Context.Environment, - new AzureTenant() { Id = new Guid(tenantId) }); + new AzureTenant() { Id = new Guid(tenantId) })); if (_profile.Context.Account != null) { @@ -139,11 +139,11 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId) newSubscription.Name = null; } - _profile.Context = new AzureContext( + _profile.SetContextWithCache(new AzureContext( newSubscription, _profile.Context.Account, _profile.Context.Environment, - _profile.Context.Tenant); + _profile.Context.Tenant)); } return _profile.Context; diff --git a/tools/AzureRM/AzureRM.psm1 b/tools/AzureRM/AzureRM.psm1 index 9f2c73a34b1b..61033d81cf6c 100644 --- a/tools/AzureRM/AzureRM.psm1 +++ b/tools/AzureRM/AzureRM.psm1 @@ -41,7 +41,13 @@ function Test-AdminRights([string]$Scope) function CheckIncompatibleVersion([bool]$Force) { $message = "An incompatible version of Azure Resource Manager PowerShell cmdlets is installed. Please uninstall Microsoft Azure PowerShell using the 'Control Panel' before installing these cmdlets. To install these cmdlets regardless of compatibility issues, execute 'Install-AzureRM -Force'." - if ( Test-Path "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureResourceManager.psd1") + $path = ${env:ProgramFiles(x86)} + if ($path -eq $null) + { + $path = ${env:ProgramFiles} + } + + if ( Test-Path "$path\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureResourceManager.psd1") { if ($Force) {