Skip to content

Update TokenCache to write to the current profile #910

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

Merged
merged 8 commits into from
Sep 15, 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 4 additions & 1 deletion src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@ protected void ConfirmAction(bool force, string actionMessage, string processMes

protected virtual void Dispose(bool disposing)
{
_adalListener.Dispose();
if (_adalListener != null)
{
_adalListener.Dispose();
}
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.1.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Storage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.1.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.1.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
Expand Down
36 changes: 18 additions & 18 deletions src/Common/Commands.Common.Test/Common/ProfileClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public void NewProfileWithAccessTokenReturnsProfile()
Assert.Equal("AzureCloud", newProfile.DefaultSubscription.Environment);
Assert.Equal(new Guid(csmSubscription1.SubscriptionId), newProfile.DefaultSubscription.Id);
Assert.Equal("accountId", newProfile.DefaultSubscription.Account);
Assert.Equal(AzureAccount.AccountType.AccessToken, newProfile.DefaultContext.Account.Type);
Assert.Equal("accessToken", newProfile.DefaultContext.Account.Properties[AzureAccount.Property.AccessToken]);
Assert.Equal(AzureAccount.AccountType.AccessToken, newProfile.Context.Account.Type);
Assert.Equal("accessToken", newProfile.Context.Account.Properties[AzureAccount.Property.AccessToken]);
Assert.False(newProfile.DefaultSubscription.Properties.ContainsKey(AzureSubscription.Property.StorageAccount));
}

Expand Down Expand Up @@ -703,15 +703,15 @@ public void RemoveAzureAccountRemovesInMemoryAccount()

client.RemoveAccount(azureAccount.Id);

Assert.Equal("test2", currentProfile.DefaultContext.Account.Id);
Assert.Equal("test2", currentProfile.DefaultContext.Subscription.Account);
Assert.Equal(azureSubscription1.Id, currentProfile.DefaultContext.Subscription.Id);
Assert.Equal("test2", currentProfile.Context.Account.Id);
Assert.Equal("test2", currentProfile.Context.Subscription.Account);
Assert.Equal(azureSubscription1.Id, currentProfile.Context.Subscription.Id);

client.RemoveAccount("test2");

Assert.Null(currentProfile.DefaultContext.Account);
Assert.Null(currentProfile.DefaultContext.Subscription);
Assert.Null(currentProfile.DefaultContext.Environment);
Assert.Null(currentProfile.Context.Account);
Assert.Null(currentProfile.Context.Subscription);
Assert.Null(currentProfile.Context.Environment);
}

[Fact]
Expand Down Expand Up @@ -906,9 +906,9 @@ public void AddOrSetAzureSubscriptionUpdatesInMemory()
client.AddOrSetSubscription(azureSubscription1);
currentProfile.DefaultSubscription = azureSubscription1;
azureSubscription1.Properties[AzureSubscription.Property.StorageAccount] = "testAccount";
Assert.Equal(azureSubscription1.Id, currentProfile.DefaultContext.Subscription.Id);
Assert.Equal(azureSubscription1.Id, currentProfile.Context.Subscription.Id);
Assert.Equal(azureSubscription1.Properties[AzureSubscription.Property.StorageAccount],
currentProfile.DefaultContext.Subscription.Properties[AzureSubscription.Property.StorageAccount]);
currentProfile.Context.Subscription.Properties[AzureSubscription.Property.StorageAccount]);

var newSubscription = new AzureSubscription
{
Expand All @@ -922,10 +922,10 @@ public void AddOrSetAzureSubscriptionUpdatesInMemory()
client.AddOrSetSubscription(newSubscription);
var newSubscriptionFromProfile = client.Profile.Subscriptions[newSubscription.Id];

Assert.Equal(newSubscription.Id, currentProfile.DefaultContext.Subscription.Id);
Assert.Equal(newSubscription.Id, currentProfile.Context.Subscription.Id);
Assert.Equal(newSubscription.Id, newSubscriptionFromProfile.Id);
Assert.Equal(newSubscription.Properties[AzureSubscription.Property.StorageAccount],
currentProfile.DefaultContext.Subscription.Properties[AzureSubscription.Property.StorageAccount]);
currentProfile.Context.Subscription.Properties[AzureSubscription.Property.StorageAccount]);
Assert.Equal(newSubscription.Properties[AzureSubscription.Property.StorageAccount],
newSubscriptionFromProfile.Properties[AzureSubscription.Property.StorageAccount]);
}
Expand Down Expand Up @@ -1096,9 +1096,9 @@ public void SetAzureSubscriptionAsDefaultSetsDefaultAndCurrent()
client.SetSubscriptionAsDefault(azureSubscription2.Name, azureSubscription2.Account);

Assert.Equal(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
Assert.Equal(azureSubscription2.Id, currentProfile.DefaultContext.Subscription.Id);
Assert.Equal(azureSubscription2.Account, currentProfile.DefaultContext.Account.Id);
Assert.Equal(azureSubscription2.Environment, currentProfile.DefaultContext.Environment.Name);
Assert.Equal(azureSubscription2.Id, currentProfile.Context.Subscription.Id);
Assert.Equal(azureSubscription2.Account, currentProfile.Context.Account.Id);
Assert.Equal(azureSubscription2.Environment, currentProfile.Context.Environment.Name);
var notFoundEx = Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsDefault("bad", null));
var invalidEx = Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsDefault(null, null));
Assert.Contains("doesn't exist", notFoundEx.Message);
Expand All @@ -1123,9 +1123,9 @@ public void ClearDefaultAzureSubscriptionClearsDefault()
client.ClearDefaultSubscription();

Assert.Null(client.Profile.DefaultSubscription);
Assert.Null(client.Profile.DefaultContext.Account);
Assert.Null(client.Profile.DefaultContext.Environment);
Assert.Null(client.Profile.DefaultContext.Subscription);
Assert.Null(client.Profile.Context.Account);
Assert.Null(client.Profile.Context.Environment);
Assert.Null(client.Profile.Context.Subscription);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.1.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/AzureSMCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private get

protected static TokenCache DefaultMemoryTokenCache { get; set; }

protected override AzureContext DefaultContext { get { return CurrentProfile.DefaultContext; } }
protected override AzureContext DefaultContext { get { return CurrentProfile.Context; } }

static AzureSMCmdlet()
{
Expand Down
12 changes: 6 additions & 6 deletions src/Common/Commands.Common/CloudBaseCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ protected virtual void InitChannelCurrentSubscription(bool force)

protected void DoInitChannelCurrentSubscription(bool force)
{
if (Profile.DefaultContext.Subscription == null)
if (Profile.Context.Subscription == null)
{
throw new ArgumentException(Resources.InvalidDefaultSubscription);
}

if (Profile.DefaultContext.Account == null)
if (Profile.Context.Account == null)
{
throw new ArgumentException(Resources.AccountNeedsToBeSpecified);
}
Expand Down Expand Up @@ -127,19 +127,19 @@ protected virtual T CreateChannel()
return Channel;
}

string certificateThumbprint = Profile.DefaultContext.Account.Id;
string certificateThumbprint = Profile.Context.Account.Id;
Debug.Assert(Profile.Accounts[certificateThumbprint].Type == AzureAccount.AccountType.Certificate);

return ChannelHelper.CreateServiceManagementChannel<T>(
ServiceBinding,
Profile.DefaultContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement),
Profile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement),
AzureSession.DataStore.GetCertificate(certificateThumbprint),
new HttpRestMessageInspector(WriteDebug));
}

protected void RetryCall(Action<string> call)
{
RetryCall(Profile.DefaultContext.Subscription.Id, call);
RetryCall(Profile.Context.Subscription.Id, call);
}

protected void RetryCall(Guid subsId, Action<string> call)
Expand Down Expand Up @@ -172,7 +172,7 @@ protected void RetryCall(Guid subsId, Action<string> call)

protected TResult RetryCall<TResult>(Func<string, TResult> call)
{
return RetryCall(Profile.DefaultContext.Subscription.Id, call);
return RetryCall(Profile.Context.Subscription.Id, call);
}

protected TResult RetryCall<TResult>(Guid subsId, Func<string, TResult> call)
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.1.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<SpecificVersion>False</SpecificVersion>
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Commands.Common/ProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public AzureAccount GetAccountOrDefault(string accountName)
{
if (string.IsNullOrEmpty(accountName))
{
return Profile.DefaultContext.Account;
return Profile.Context.Account;
}
else if (Profile.Accounts.ContainsKey(accountName))
{
Expand Down Expand Up @@ -1040,7 +1040,7 @@ public AzureEnvironment GetEnvironmentOrDefault(string name)
else if (string.IsNullOrEmpty(name) &&
Profile.DefaultSubscription != null)
{
return Profile.DefaultContext.Environment;
return Profile.Context.Environment;
}
else if (Profile.Environments.ContainsKey(name))
{
Expand Down
14 changes: 7 additions & 7 deletions src/Common/Commands.Common/RPRegistrationAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public class RPRegistrationAction : IClientAction
private void RegisterResourceManagerProviders<T>(IAzureProfile profile)
{
var providersToRegister = RequiredResourceLookup.RequiredProvidersForResourceManager<T>();
var registeredProviders = profile.DefaultContext.Subscription.GetPropertyAsArray(AzureSubscription.Property.RegisteredResourceProviders);
var registeredProviders = profile.Context.Subscription.GetPropertyAsArray(AzureSubscription.Property.RegisteredResourceProviders);
var unregisteredProviders = providersToRegister.Where(p => !registeredProviders.Contains(p)).ToList();
var successfullyRegisteredProvider = new List<string>();
SubscriptionCloudCredentials creds = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(profile.DefaultContext);
SubscriptionCloudCredentials creds = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(profile.Context);

if (unregisteredProviders.Count > 0)
{
using (var client = ClientFactory.CreateCustomClient<ResourceManagementClient>(
creds,
profile.DefaultContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
profile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
{
foreach (string provider in unregisteredProviders)
{
Expand All @@ -64,17 +64,17 @@ private void RegisterResourceManagerProviders<T>(IAzureProfile profile)
/// <typeparam name="T">The client type</typeparam>
private void RegisterServiceManagementProviders<T>(AzureSMProfile profile)
{
var credentials = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(profile.DefaultContext);
var credentials = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(profile.Context);
var providersToRegister = RequiredResourceLookup.RequiredProvidersForServiceManagement<T>();
var registeredProviders = profile.DefaultContext.Subscription.GetPropertyAsArray(AzureSubscription.Property.RegisteredResourceProviders);
var registeredProviders = profile.Context.Subscription.GetPropertyAsArray(AzureSubscription.Property.RegisteredResourceProviders);
var unregisteredProviders = providersToRegister.Where(p => !registeredProviders.Contains(p)).ToList();
var successfullyRegisteredProvider = new List<string>();

if (unregisteredProviders.Count > 0)
{
using (var client = new ManagementClient(
credentials,
profile.DefaultContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement)))
profile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement)))
{
foreach (var provider in unregisteredProviders)
{
Expand All @@ -97,7 +97,7 @@ private void RegisterServiceManagementProviders<T>(AzureSMProfile profile)
}

Debug.Assert(profile is AzureSMProfile);
UpdateSubscriptionRegisteredProviders((AzureSMProfile)profile, profile.DefaultContext.Subscription, successfullyRegisteredProvider);
UpdateSubscriptionRegisteredProviders((AzureSMProfile)profile, profile.Context.Subscription, successfullyRegisteredProvider);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="Microsoft.ApplicationInsights" version="1.1.1-beta" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.1.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Profile/Commands.Profile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.1.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Profile/Models/PsAzureSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public PSAzureSubscription(AzureSubscription subscription, AzureSMProfile profil
DefaultAccount = subscription.Account;
Accounts = profile.Accounts.Values.Where(a => a.HasSubscription(subscription.Id)).ToArray();
IsDefault = subscription.IsPropertySet(AzureSubscription.Property.Default);
IsCurrent = profile.DefaultContext != null && profile.DefaultContext.Subscription.Id == subscription.Id;
IsCurrent = profile.Context != null && profile.Context.Subscription.Id == subscription.Id;
CurrentStorageAccountName = subscription.GetProperty(AzureSubscription.Property.StorageAccount);
TenantId = subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants).FirstOrDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public override void ExecuteCmdlet()
{
case "ByName":
IEnumerable<AzureSubscription> subscriptions = new AzureSubscription[0];
if (Profile.DefaultContext != null && Profile.DefaultContext.Environment != null)
if (Profile.Context != null && Profile.Context.Environment != null)
{
subscriptions = ProfileClient.RefreshSubscriptions(Profile.DefaultContext.Environment)
subscriptions = ProfileClient.RefreshSubscriptions(Profile.Context.Environment)
.Where(
s =>
SubscriptionName == null ||
Expand Down Expand Up @@ -113,7 +113,7 @@ public void GetCurrent()
// since current is strictly in-memory and we want the real
// current subscription.
//
if (Profile.DefaultContext.Subscription == null)
if (Profile.Context.Subscription == null)
{
WriteError(new ErrorRecord(
new InvalidOperationException(Resources.InvalidSelectedSubscription),
Expand All @@ -122,7 +122,7 @@ public void GetCurrent()
}
else
{
WriteSubscriptions(Profile.DefaultContext.Subscription);
WriteSubscriptions(Profile.Context.Subscription);
}
}

Expand Down
Loading