Skip to content

Commit 55ccac3

Browse files
author
Hovsep Mkrtchyan
committed
Work in progress
1 parent 5b76c35 commit 55ccac3

File tree

11 files changed

+78
-26
lines changed

11 files changed

+78
-26
lines changed

src/Common/AzurePSCmdlet.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,10 @@ protected void ConfirmAction(bool force, string actionMessage, string processMes
440440

441441
protected virtual void Dispose(bool disposing)
442442
{
443-
_adalListener.Dispose();
443+
if (_adalListener != null)
444+
{
445+
_adalListener.Dispose();
446+
}
444447
}
445448

446449
public void Dispose()

src/Common/Commands.ResourceManager.Common/RMProfileClient.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Azure.Common.Authentication.Factories;
1818
using Microsoft.Azure.Common.Authentication.Models;
1919
using Microsoft.Azure.Subscriptions;
20+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2021
using System;
2122
using System.Collections.Generic;
2223
using System.Linq;
@@ -39,6 +40,11 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
3940
{
4041
AzureSubscription newSubscription = null;
4142
AzureTenant newTenant = new AzureTenant();
43+
44+
if (_profile != null && _profile.TokenCache != null && _profile.TokenCache.Length > 0)
45+
{
46+
TokenCache.DefaultShared.Deserialize(_profile.TokenCache);
47+
}
4248

4349
// (tenant and subscription are present) OR
4450
// (tenant is present and subscription is not provided)
@@ -60,7 +66,6 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
6066
break;
6167
}
6268
}
63-
6469
}
6570

6671
if (newSubscription == null)
@@ -69,6 +74,7 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
6974
}
7075

7176
_profile.DefaultContext = new AzureContext(newSubscription, account, environment, newTenant);
77+
_profile.TokenCache = TokenCache.DefaultShared.Serialize();
7278

7379
return _profile;
7480
}
@@ -87,7 +93,8 @@ private bool TryGetTenantSubscription(
8793
environment,
8894
tenantId,
8995
password,
90-
promptBehavior);
96+
promptBehavior,
97+
TokenCache.DefaultShared);
9198
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
9299
new TokenCloudCredentials(accessToken.AccessToken),
93100
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
@@ -130,6 +137,8 @@ private bool TryGetTenantSubscription(
130137
Name = subscriptionFromServer.DisplayName,
131138
Properties = new Dictionary<AzureSubscription.Property, string> { { AzureSubscription.Property.Tenants, accessToken.TenantId } }
132139
};
140+
141+
account.Properties[AzureAccount.Property.Tenants] = accessToken.TenantId;
133142
return true;
134143
}
135144

@@ -142,8 +151,13 @@ private string[] ListAccountTenants(AzureAccount account, AzureEnvironment envir
142151
{
143152
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
144153

145-
var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(account, environment,
146-
AuthenticationFactory.CommonAdTenant, password, promptBehavior);
154+
var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(
155+
account,
156+
environment,
157+
AuthenticationFactory.CommonAdTenant,
158+
password,
159+
promptBehavior,
160+
TokenCache.DefaultShared);
147161

148162
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
149163
new TokenCloudCredentials(commonTenantToken.AccessToken),

src/Common/Commands.ResourceManager.Profile.Test/ProfileCmdletTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void SelectAzureProfileInMemory()
4545
{
4646
var profile = new AzureRMProfile();
4747
profile.Environments.Add("foo", AzureEnvironment.PublicEnvironments.Values.FirstOrDefault());
48-
SelectAzureProfileCommand cmdlt = new SelectAzureProfileCommand();
48+
SelectAzureRMProfileCommand cmdlt = new SelectAzureRMProfileCommand();
4949
// Setup
5050
cmdlt.Profile = profile;
5151
cmdlt.CommandRuntime = commandRuntimeMock;
@@ -63,7 +63,7 @@ public void SelectAzureProfileInMemory()
6363
[Trait(Category.AcceptanceType, Category.CheckIn)]
6464
public void SelectAzureProfileNull()
6565
{
66-
SelectAzureProfileCommand cmdlt = new SelectAzureProfileCommand();
66+
SelectAzureRMProfileCommand cmdlt = new SelectAzureRMProfileCommand();
6767
// Setup
6868
cmdlt.CommandRuntime = commandRuntimeMock;
6969

@@ -80,7 +80,7 @@ public void SelectAzureProfileFromDisk()
8080
var profile = new AzureRMProfile();
8181
profile.Environments.Add("foo", AzureEnvironment.PublicEnvironments.Values.FirstOrDefault());
8282
profile.Save("X:\\foo.json");
83-
SelectAzureProfileCommand cmdlt = new SelectAzureProfileCommand();
83+
SelectAzureRMProfileCommand cmdlt = new SelectAzureRMProfileCommand();
8484
// Setup
8585
cmdlt.Path = "X:\\foo.json";
8686
cmdlt.CommandRuntime = commandRuntimeMock;
@@ -100,7 +100,7 @@ public void SaveAzureProfileInMemory()
100100
{
101101
var profile = new AzureRMProfile();
102102
profile.Environments.Add("foo", AzureEnvironment.PublicEnvironments.Values.FirstOrDefault());
103-
SaveAzureProfileCommand cmdlt = new SaveAzureProfileCommand();
103+
SaveAzureRMProfileCommand cmdlt = new SaveAzureRMProfileCommand();
104104
// Setup
105105
cmdlt.Profile = profile;
106106
cmdlt.Path = "X:\\foo.json";
@@ -121,7 +121,7 @@ public void SaveAzureProfileInMemory()
121121
[Trait(Category.AcceptanceType, Category.CheckIn)]
122122
public void SaveAzureProfileNull()
123123
{
124-
SaveAzureProfileCommand cmdlt = new SaveAzureProfileCommand();
124+
SaveAzureRMProfileCommand cmdlt = new SaveAzureRMProfileCommand();
125125
// Setup
126126
AzureRMCmdlet.DefaultProfile = null;
127127
cmdlt.Path = "X:\\foo.json";
@@ -139,7 +139,7 @@ public void SaveAzureProfileFromDefault()
139139
profile.Environments.Add("foo", AzureEnvironment.PublicEnvironments.Values.FirstOrDefault());
140140
profile.DefaultContext = new AzureContext(new AzureSubscription(), new AzureAccount(), profile.Environments["foo"]);
141141
AzureRMCmdlet.DefaultProfile = profile;
142-
SaveAzureProfileCommand cmdlt = new SaveAzureProfileCommand();
142+
SaveAzureRMProfileCommand cmdlt = new SaveAzureRMProfileCommand();
143143
// Setup
144144
cmdlt.Path = "X:\\foo.json";
145145
cmdlt.CommandRuntime = commandRuntimeMock;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@
133133
</ItemGroup>
134134
<ItemGroup>
135135
<Compile Include="LoginAzureRMAccount.cs" />
136-
<Compile Include="Profile\SaveAzureProfile.cs" />
137-
<Compile Include="Profile\SelectAzureProfile.cs" />
136+
<Compile Include="Profile\SaveAzureRMProfile.cs" />
137+
<Compile Include="Profile\SelectAzureRMProfile.cs" />
138138
<Compile Include="Properties\AssemblyInfo.cs" />
139139
<Compile Include="Properties\Resources.Designer.cs">
140140
<AutoGen>True</AutoGen>

src/Common/Commands.ResourceManager.Profile/LoginAzureRMAccount.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Azure.Common.Authentication.Models;
1818
using System.Security;
1919
using Microsoft.WindowsAzure.Commands.Utilities.Common;
20+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2021

2122
namespace Microsoft.Azure.Commands.Profile
2223
{

src/Common/Commands.ResourceManager.Profile/Profile/SaveAzureProfile.cs renamed to src/Common/Commands.ResourceManager.Profile/Profile/SaveAzureRMProfile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ namespace Microsoft.Azure.Commands.Profile
2323
/// <summary>
2424
/// Saves Microsoft Azure profile.
2525
/// </summary>
26-
[Cmdlet(VerbsData.Save, "AzureProfile"), OutputType(typeof(AzureRMProfile))]
27-
public class SaveAzureProfileCommand : AzureRMCmdlet
26+
[Cmdlet(VerbsData.Save, "AzureRMProfile"), OutputType(typeof(AzureRMProfile))]
27+
public class SaveAzureRMProfileCommand : AzureRMCmdlet
2828
{
2929
[Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true)]
3030
public AzureRMProfile Profile { get; set; }

src/Common/Commands.ResourceManager.Profile/Profile/SelectAzureProfile.cs renamed to src/Common/Commands.ResourceManager.Profile/Profile/SelectAzureRMProfile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ namespace Microsoft.Azure.Commands.Profile
2323
/// <summary>
2424
/// Selects Microsoft Azure profile.
2525
/// </summary>
26-
[Cmdlet(VerbsCommon.Select, "AzureProfile"), OutputType(typeof(AzureRMProfile))]
27-
public class SelectAzureProfileCommand : AzureRMCmdlet
26+
[Cmdlet(VerbsCommon.Select, "AzureRMProfile"), OutputType(typeof(AzureRMProfile))]
27+
public class SelectAzureRMProfileCommand : AzureRMCmdlet
2828
{
2929
internal const string InMemoryProfileParameterSet = "InMemoryProfile";
3030
internal const string ProfileFromDiskParameterSet = "ProfileFromDisk";

src/Common/Commands.ScenarioTests.ResourceManager.Common/Mocks/MockCertificateAuthenticationFactory.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ public MockCertificateAuthenticationFactory(string userId, X509Certificate2 cert
3434
Certificate = certificate;
3535
}
3636

37-
public IAccessToken Authenticate(AzureAccount account, AzureEnvironment environment, string tenant, SecureString password, ShowDialog promptBehavior,
37+
public IAccessToken Authenticate(
38+
AzureAccount account,
39+
AzureEnvironment environment,
40+
string tenant,
41+
SecureString password,
42+
ShowDialog promptBehavior,
43+
IdentityModel.Clients.ActiveDirectory.TokenCache tokenCache,
3844
AzureEnvironment.Endpoint resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)
3945
{
4046
if (account.Id == null)
@@ -52,6 +58,17 @@ public IAccessToken Authenticate(AzureAccount account, AzureEnvironment environm
5258
return token;
5359
}
5460

61+
public IAccessToken Authenticate(
62+
AzureAccount account,
63+
AzureEnvironment environment,
64+
string tenant,
65+
SecureString password,
66+
ShowDialog promptBehavior,
67+
AzureEnvironment.Endpoint resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)
68+
{
69+
return Authenticate(account, environment, tenant, password, promptBehavior, AzureSession.TokenCache, resourceId);
70+
}
71+
5572
public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext context)
5673
{
5774
return new CertificateCloudCredentials(context.Subscription.Id.ToString(), Certificate);

src/Common/Commands.ScenarioTests.ResourceManager.Common/Mocks/MockTokenAuthenticationFactory.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ public MockTokenAuthenticationFactory(string userId, string accessToken)
5656
TokenProvider = ((account, environment, tenant) => Token);
5757
}
5858

59-
public IAccessToken Authenticate(AzureAccount account, AzureEnvironment environment, string tenant, SecureString password, ShowDialog promptBehavior,
59+
public IAccessToken Authenticate(
60+
AzureAccount account,
61+
AzureEnvironment environment,
62+
string tenant,
63+
SecureString password,
64+
ShowDialog promptBehavior,
65+
IdentityModel.Clients.ActiveDirectory.TokenCache tokenCache,
6066
AzureEnvironment.Endpoint resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)
6167
{
6268
if (account.Id == null)
@@ -79,12 +85,22 @@ public IAccessToken Authenticate(AzureAccount account, AzureEnvironment environm
7985
}
8086
}
8187

88+
public IAccessToken Authenticate(
89+
AzureAccount account,
90+
AzureEnvironment environment,
91+
string tenant,
92+
SecureString password,
93+
ShowDialog promptBehavior,
94+
AzureEnvironment.Endpoint resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)
95+
{
96+
return Authenticate(account, environment, tenant, password, promptBehavior, AzureSession.TokenCache, resourceId);
97+
}
98+
8299
public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(AzureContext context)
83100
{
84101
return new AccessTokenCredential(context.Subscription.Id, Token);
85102
}
86-
87-
103+
88104
public Microsoft.Rest.ServiceClientCredentials GetServiceClientCredentials(AzureContext context)
89105
{
90106
return new Microsoft.Rest.TokenCredentials(Token.AccessToken);

src/ResourceManager.sln

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.ScenarioTests.Reso
119119
EndProject
120120
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Common", "Common\Commands.Common\Commands.Common.csproj", "{5EE72C53-1720-4309-B54B-5FB79703195F}"
121121
EndProject
122+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Storage", "ServiceManagement\Storage\Commands.Storage\Commands.Storage.csproj", "{08CF7DA7-0392-4A19-B79B-E1FF67CDB81A}"
123+
EndProject
122124
Global
123125
GlobalSection(SolutionConfigurationPlatforms) = preSolution
124126
Debug|Any CPU = Debug|Any CPU
@@ -341,6 +343,10 @@ Global
341343
{5EE72C53-1720-4309-B54B-5FB79703195F}.Debug|Any CPU.Build.0 = Debug|Any CPU
342344
{5EE72C53-1720-4309-B54B-5FB79703195F}.Release|Any CPU.ActiveCfg = Release|Any CPU
343345
{5EE72C53-1720-4309-B54B-5FB79703195F}.Release|Any CPU.Build.0 = Release|Any CPU
346+
{08CF7DA7-0392-4A19-B79B-E1FF67CDB81A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
347+
{08CF7DA7-0392-4A19-B79B-E1FF67CDB81A}.Debug|Any CPU.Build.0 = Debug|Any CPU
348+
{08CF7DA7-0392-4A19-B79B-E1FF67CDB81A}.Release|Any CPU.ActiveCfg = Release|Any CPU
349+
{08CF7DA7-0392-4A19-B79B-E1FF67CDB81A}.Release|Any CPU.Build.0 = Release|Any CPU
344350
EndGlobalSection
345351
GlobalSection(SolutionProperties) = preSolution
346352
HideSolutionNode = FALSE
@@ -370,6 +376,5 @@ Global
370376
{F220C306-29A3-4511-8518-A58A55C60D07} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
371377
{13E031E4-8A43-4B87-9D72-D70180C31C11} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
372378
{3436A126-EDC9-4060-8952-9A1BE34CDD95} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
373-
{5EE72C53-1720-4309-B54B-5FB79703195F} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
374379
EndGlobalSection
375380
EndGlobal

src/ResourceManager/Resources/Commands.Resources/AzureResourceManager.psd1

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ TypesToProcess = @(
6363
FormatsToProcess = @(
6464
'.\Resources\Microsoft.Azure.Commands.Resources.format.ps1xml',
6565
'.\Tags\Microsoft.Azure.Commands.Tags.format.ps1xml',
66-
# TODO: the following should go away when Azure RM Profile Cmdlets are implemented
67-
'..\..\ServiceManagement\Azure\Services\Microsoft.WindowsAzure.Commands.Profile.format.ps1xml',
6866
'.\Resources\Microsoft.Azure.Commands.Profile.format.ps1xml',
6967
'.\DataFactories\Microsoft.Azure.Commands.DataFactories.format.ps1xml',
7068
'.\RedisCache\Microsoft.Azure.Commands.RedisCache.format.ps1xml',
@@ -86,8 +84,6 @@ FormatsToProcess = @(
8684
NestedModules = @(
8785
'.\Automation\Microsoft.Azure.Commands.ResourceManager.Automation.dll',
8886
'.\Resources\Microsoft.Azure.Commands.Resources.dll',
89-
# TODO: the following should go away when Azure RM Profile Cmdlets are implemented
90-
'..\..\ServiceManagement\Azure\Services\Microsoft.WindowsAzure.Commands.Profile.dll',
9187
'.\Resources\Microsoft.Azure.Commands.Profile.dll',
9288
'.\Tags\Microsoft.Azure.Commands.Tags.dll',
9389
'.\Sql\Microsoft.Azure.Commands.Sql.dll',

0 commit comments

Comments
 (0)