Skip to content

Commit 671fcef

Browse files
committed
Merge pull request Azure#1094 from markcowl/installerupdate
Fixing issue with token cache copying and fixing the script for32-bit machines
2 parents 0e27fbd + b3c26e5 commit 671fcef

File tree

10 files changed

+120
-13
lines changed

10 files changed

+120
-13
lines changed

src/Common/Commands.Common/GeneralUtilities.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,5 @@ public static void ClearCurrentStorageAccount(bool clearSMContext = false)
442442
}
443443
}
444444
}
445-
446445
}
447446
}

src/ResourceManager/Common/Commands.ResourceManager.Common/AccessTokenExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
216
using System.Linq;
317
using Microsoft.Azure.Common.Authentication;
418

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
using Microsoft.Azure.Commands.ResourceManager.Common.Properties;
21+
using Microsoft.Azure.Common.Authentication.Models;
22+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
23+
24+
namespace Microsoft.Azure.Commands.ResourceManager.Common
25+
{
26+
public static class AzureRMProfileExtensions
27+
{
28+
/// <summary>
29+
/// Set the context for the current profile, preserving token cache information
30+
/// </summary>
31+
/// <param name="profile">The profile to change the context for</param>
32+
/// <param name="newContext">The new context, with no token cache information.</param>
33+
public static void SetContextWithCache(this AzureRMProfile profile, AzureContext newContext)
34+
{
35+
if (profile == null)
36+
{
37+
throw new ArgumentNullException("profile", Resources.ProfileCannotBeNull);
38+
}
39+
40+
if (newContext == null)
41+
{
42+
throw new ArgumentNullException("newContext", Resources.ContextCannotBeNull);
43+
}
44+
45+
newContext.TokenCache = TokenCache.DefaultShared.Serialize();
46+
profile.Context = newContext;
47+
}
48+
}
49+
}

src/ResourceManager/Common/Commands.ResourceManager.Common/Commands.ResourceManager.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<ItemGroup>
139139
<Compile Include="AccessTokenExtensions.cs" />
140140
<Compile Include="AzureRmCmdlet.cs" />
141+
<Compile Include="AzureRMProfileExtensions.cs" />
141142
<Compile Include="Generated\AuthorizationClient.cs" />
142143
<Compile Include="Generated\AuthorizationClientExtensions.cs" />
143144
<Compile Include="Generated\DeploymentOperationOperations.cs" />

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

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="ContextCannotBeNull" xml:space="preserve">
121+
<value>Context cannot be null. Please log in using Add-AzureRmAccount.</value>
122+
</data>
120123
<data name="DataCollectionActivity" xml:space="preserve">
121124
<value>Microsoft Azure PowerShell Data Collection Confirmation</value>
122125
</data>
@@ -151,6 +154,9 @@ Select Y to enable data collection [Y/N]:</value>
151154
<data name="NoSubscriptionFound" xml:space="preserve">
152155
<value>The provided account {0} does not have access to any subscriptions. Please try logging in with different credentials.</value>
153156
</data>
157+
<data name="ProfileCannotBeNull" xml:space="preserve">
158+
<value>Profile cannot be null. Please run Add-AzureRmAccount.</value>
159+
</data>
154160
<data name="ResourceProviderRegisterAttempt" xml:space="preserve">
155161
<value>Attempting to register resource provider '{0}'</value>
156162
</data>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System;
2020
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
2121
using System.Collections.Generic;
22+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2223
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2324

2425
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
@@ -131,5 +132,18 @@ public void NoSubscriptionsInListThrows()
131132
AzureSubscription subValue;
132133
Assert.False(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
133134
}
135+
136+
[Fact]
137+
[Trait(Category.AcceptanceType, Category.CheckIn)]
138+
public void SetContextPreservesTokenCache()
139+
{
140+
AzureRMProfile profile = null;
141+
AzureContext context = new AzureContext(null, null, null, null);
142+
Assert.Throws<ArgumentNullException>(() =>profile.SetContextWithCache(context));
143+
profile = new AzureRMProfile();
144+
Assert.Throws<ArgumentNullException>(() => profile.SetContextWithCache(null));
145+
profile.SetContextWithCache(context);
146+
Assert.Equal(TokenCache.DefaultShared.Serialize(), profile.Context.TokenCache);
147+
}
134148
}
135149
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ protected override void ProcessRecord()
5555
{
5656
if (ParameterSetName == ContextParameterSet)
5757
{
58-
AzureRmProfileProvider.Instance.Profile.Context = new AzureContext(Context.Subscription, Context.Account,
59-
Context.Environment, Context.Tenant);
58+
AzureRmProfileProvider.Instance.Profile.SetContextWithCache(new AzureContext(Context.Subscription, Context.Account,
59+
Context.Environment, Context.Tenant));
6060
}
6161
else
6262
{

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
105105
{
106106
if (!string.IsNullOrWhiteSpace(tenantId))
107107
{
108-
_profile.Context = new AzureContext(
108+
_profile.SetContextWithCache(new AzureContext(
109109
_profile.Context.Subscription,
110110
_profile.Context.Account,
111111
_profile.Context.Environment,
112-
new AzureTenant() { Id = new Guid(tenantId) });
112+
new AzureTenant() { Id = new Guid(tenantId) }));
113113

114114
if (_profile.Context.Account != null)
115115
{
@@ -139,11 +139,11 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
139139
newSubscription.Name = null;
140140
}
141141

142-
_profile.Context = new AzureContext(
142+
_profile.SetContextWithCache(new AzureContext(
143143
newSubscription,
144144
_profile.Context.Account,
145145
_profile.Context.Environment,
146-
_profile.Context.Tenant);
146+
_profile.Context.Tenant));
147147
}
148148

149149
return _profile.Context;

tools/AzureRM/AzureRM.psm1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ function Test-AdminRights([string]$Scope)
4141
function CheckIncompatibleVersion([bool]$Force)
4242
{
4343
$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'."
44-
if ( Test-Path "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureResourceManager.psd1")
44+
$path = ${env:ProgramFiles(x86)}
45+
if ($path -eq $null)
46+
{
47+
$path = ${env:ProgramFiles}
48+
}
49+
50+
if ( Test-Path "$path\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureResourceManager.psd1")
4551
{
4652
if ($Force)
4753
{

0 commit comments

Comments
 (0)