From a661f2b3c25f001afc0c551e240cd24d2f86efe1 Mon Sep 17 00:00:00 2001 From: markcowl Date: Thu, 8 Oct 2015 17:47:43 -0700 Subject: [PATCH 1/3] fixing the script for32-bit machines --- tools/AzureRM/AzureRM.psm1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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) { From 59f1a01e8b20dfa8ae864be460cdd4ced6fe09c6 Mon Sep 17 00:00:00 2001 From: markcowl Date: Thu, 8 Oct 2015 20:08:53 -0700 Subject: [PATCH 2/3] Fix issue with context changes not copying the token cache --- .../Commands.Common/GeneralUtilities.cs | 1 - .../AccessTokenExtensions.cs | 16 ++++++- .../AzureRMProfileExtensions.cs | 42 +++++++++++++++++++ .../Commands.ResourceManager.Common.csproj | 1 + .../Context/SetAzureRMContext.cs | 4 +- .../Models/RMProfileClient.cs | 8 ++-- 6 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs 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..4ebc460e8d86 --- /dev/null +++ b/src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs @@ -0,0 +1,42 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Common.Authentication.Models; + +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 + public static void SetContext(this AzureRMProfile profile, AzureContext newContext) + { + var currentContext = profile.Context; + if (currentContext != null && currentContext.TokenCache != null && currentContext.TokenCache.Length > 0) + { + newContext.TokenCache = currentContext.TokenCache; + } + + 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/Profile/Commands.Profile/Context/SetAzureRMContext.cs b/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs index b5476c8a08eb..f0b4ed071500 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.SetContext(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..d0d20267cc69 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.SetContext(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.SetContext(new AzureContext( newSubscription, _profile.Context.Account, _profile.Context.Environment, - _profile.Context.Tenant); + _profile.Context.Tenant)); } return _profile.Context; From b3c26e5ecabf80231b679b203f103fe3681dda45 Mon Sep 17 00:00:00 2001 From: markcowl Date: Thu, 8 Oct 2015 23:29:09 -0700 Subject: [PATCH 3/3] Ensure that TokenCache is transmitted to context when context is set --- .../AzureRMProfileExtensions.cs | 17 ++++++++---- .../Properties/Resources.Designer.cs | 26 ++++++++++++++++--- .../Properties/Resources.resx | 6 +++++ .../AzureRMProfileTests.cs | 14 ++++++++++ .../Context/SetAzureRMContext.cs | 2 +- .../Models/RMProfileClient.cs | 4 +-- 6 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs b/src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs index 4ebc460e8d86..e2dc5eed060c 100644 --- a/src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs +++ b/src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs @@ -17,7 +17,9 @@ 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 { @@ -27,15 +29,20 @@ 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 - public static void SetContext(this AzureRMProfile profile, AzureContext newContext) + /// The new context, with no token cache information. + public static void SetContextWithCache(this AzureRMProfile profile, AzureContext newContext) { - var currentContext = profile.Context; - if (currentContext != null && currentContext.TokenCache != null && currentContext.TokenCache.Length > 0) + if (profile == null) { - newContext.TokenCache = currentContext.TokenCache; + 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/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 f0b4ed071500..a0a4f38fb9b5 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs @@ -55,7 +55,7 @@ protected override void ProcessRecord() { if (ParameterSetName == ContextParameterSet) { - AzureRmProfileProvider.Instance.Profile.SetContext(new AzureContext(Context.Subscription, Context.Account, + 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 d0d20267cc69..dcec21991609 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs @@ -105,7 +105,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId) { if (!string.IsNullOrWhiteSpace(tenantId)) { - _profile.SetContext(new AzureContext( + _profile.SetContextWithCache(new AzureContext( _profile.Context.Subscription, _profile.Context.Account, _profile.Context.Environment, @@ -139,7 +139,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId) newSubscription.Name = null; } - _profile.SetContext(new AzureContext( + _profile.SetContextWithCache(new AzureContext( newSubscription, _profile.Context.Account, _profile.Context.Environment,