Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue with token cache copying and fixing the script for32-bit machines #1094

Merged
merged 3 commits into from
Oct 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Common/Commands.Common/GeneralUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,5 @@ public static void ClearCurrentStorageAccount(bool clearSMContext = false)
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Set the context for the current profile, preserving token cache information
/// </summary>
/// <param name="profile">The profile to change the context for</param>
/// <param name="newContext">The new context, with no token cache information.</param>
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<ItemGroup>
<Compile Include="AccessTokenExtensions.cs" />
<Compile Include="AzureRmCmdlet.cs" />
<Compile Include="AzureRMProfileExtensions.cs" />
<Compile Include="Generated\AuthorizationClient.cs" />
<Compile Include="Generated\AuthorizationClientExtensions.cs" />
<Compile Include="Generated\DeploymentOperationOperations.cs" />
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ContextCannotBeNull" xml:space="preserve">
<value>Context cannot be null. Please log in using Add-AzureRmAccount.</value>
</data>
<data name="DataCollectionActivity" xml:space="preserve">
<value>Microsoft Azure PowerShell Data Collection Confirmation</value>
</data>
Expand Down Expand Up @@ -151,6 +154,9 @@ Select Y to enable data collection [Y/N]:</value>
<data name="NoSubscriptionFound" xml:space="preserve">
<value>The provided account {0} does not have access to any subscriptions. Please try logging in with different credentials.</value>
</data>
<data name="ProfileCannotBeNull" xml:space="preserve">
<value>Profile cannot be null. Please run Add-AzureRmAccount.</value>
</data>
<data name="ResourceProviderRegisterAttempt" xml:space="preserve">
<value>Attempting to register resource provider '{0}'</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ArgumentNullException>(() =>profile.SetContextWithCache(context));
profile = new AzureRMProfile();
Assert.Throws<ArgumentNullException>(() => profile.SetContextWithCache(null));
profile.SetContextWithCache(context);
Assert.Equal(TokenCache.DefaultShared.Serialize(), profile.Context.TokenCache);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion tools/AzureRM/AzureRM.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down