Skip to content

Commit a38fa2d

Browse files
committed
Merge pull request #90 from Azure/dev
.
2 parents ad974b7 + 4ac52ac commit a38fa2d

File tree

270 files changed

+4908
-4366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

270 files changed

+4908
-4366
lines changed

src/Common/Commands.Common.Storage/Commands.Common.Storage.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</Reference>
5959
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6060
<SpecificVersion>False</SpecificVersion>
61-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
61+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6464
<SpecificVersion>False</SpecificVersion>

src/Common/Commands.Common.Storage/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
44
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
5-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
5+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
88
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />

src/Common/Commands.Common.Test/Commands.Common.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</Reference>
5959
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6060
<SpecificVersion>False</SpecificVersion>
61-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
61+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Microsoft.Azure.Common.NetFramework">
6464
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>

src/Common/Commands.Common.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
44
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
5-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
5+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
88
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
</Reference>
6666
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6767
<SpecificVersion>False</SpecificVersion>
68-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
68+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6969
</Reference>
7070
<Reference Include="Microsoft.Azure.Common.NetFramework">
7171
<SpecificVersion>False</SpecificVersion>

src/Common/Commands.Common/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<package id="Microsoft.ApplicationInsights" version="1.1.1-beta" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
7-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
7+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
1010
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.dll</HintPath>
5555
</Reference>
5656
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
57-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
57+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
5858
<Private>True</Private>
5959
</Reference>
6060
<Reference Include="Microsoft.Azure.Common.NetFramework">

src/Common/Commands.Profile/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
44
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
5-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
5+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
88
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Azure.Common.Authentication;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.Azure.Commands.ResourceManager.Common
9+
{
10+
public static class AccessTokenExtensions
11+
{
12+
public static string GetDomain(this IAccessToken token)
13+
{
14+
if( token != null && token.UserId !=null && token.UserId.Contains('@'))
15+
{
16+
return token.UserId.Split(
17+
new[] { '@' },
18+
StringSplitOptions.RemoveEmptyEntries).Last();
19+
}
20+
21+
return null;
22+
}
23+
}
24+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
</Reference>
6363
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6464
<SpecificVersion>False</SpecificVersion>
65-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
65+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6666
</Reference>
6767
<Reference Include="Microsoft.Azure.Common.NetFramework">
6868
<SpecificVersion>False</SpecificVersion>
@@ -182,6 +182,7 @@
182182
<Compile Include="..\Commands.Common\TestMockSupport.cs">
183183
<Link>Common\TestMockSupport.cs</Link>
184184
</Compile>
185+
<Compile Include="AccessTokenExtensions.cs" />
185186
<Compile Include="AzureRMCmdlet.cs" />
186187
<Compile Include="Properties\AssemblyInfo.cs" />
187188
<Compile Include="Properties\Resources.Designer.cs">

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

Lines changed: 84 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,34 @@ public class RMProfileClient
3434
public RMProfileClient(AzureRMProfile profile)
3535
{
3636
_profile = profile;
37-
}
3837

39-
public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, SecureString password)
40-
{
41-
AzureSubscription newSubscription = null;
42-
AzureTenant newTenant = new AzureTenant();
43-
44-
if (_profile != null && _profile.Context != null &&
38+
if (_profile != null && _profile.Context != null &&
4539
_profile.Context.TokenCache != null && _profile.Context.TokenCache.Length > 0)
4640
{
4741
TokenCache.DefaultShared.Deserialize(_profile.Context.TokenCache);
4842
}
43+
}
44+
45+
public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, SecureString password)
46+
{
47+
AzureSubscription newSubscription = null;
48+
AzureTenant newTenant = null;
49+
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
4950

5051
// (tenant and subscription are present) OR
5152
// (tenant is present and subscription is not provided)
5253
if (!string.IsNullOrEmpty(tenantId))
5354
{
54-
newTenant.Id = new Guid(tenantId);
55-
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
56-
TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription);
55+
TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription, out newTenant);
5756
}
5857
// (tenant is not provided and subscription is present) OR
5958
// (tenant is not provided and subscription is not provided)
6059
else
6160
{
62-
foreach(var tenant in ListAccountTenants(account, environment, password))
61+
foreach(var tenant in ListAccountTenants(account, environment, password, promptBehavior))
6362
{
64-
if (TryGetTenantSubscription(account, environment, tenant, subscriptionId, password, ShowDialog.Auto, out newSubscription))
63+
if (TryGetTenantSubscription(account, environment, tenant.Id.ToString(), subscriptionId, password, ShowDialog.Auto, out newSubscription, out newTenant))
6564
{
66-
newTenant.Id = new Guid(tenant);
6765
break;
6866
}
6967
}
@@ -80,14 +78,77 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
8078
return _profile;
8179
}
8280

81+
public AzureContext UpdateCurrentContext(string subscriptionId, string tenantId)
82+
{
83+
AzureSubscription newSubscription = null;
84+
AzureTenant newTenant = null;
85+
AzureAccount account = _profile.Context.Account;
86+
AzureEnvironment envrionment = _profile.Context.Environment;
87+
ShowDialog promptBehavior = ShowDialog.Auto;
88+
89+
if (!string.IsNullOrWhiteSpace(tenantId) &&
90+
!string.IsNullOrWhiteSpace(subscriptionId))
91+
{
92+
if(TryGetTenantSubscription(account, envrionment, tenantId, subscriptionId, null, promptBehavior, out newSubscription, out newTenant))
93+
{
94+
_profile.Context = new AzureContext(newSubscription, account, envrionment, newTenant);
95+
}
96+
}
97+
else if (!string.IsNullOrWhiteSpace(tenantId))
98+
{
99+
var accessToken = AzureSession.AuthenticationFactory.Authenticate(
100+
account,
101+
envrionment,
102+
tenantId,
103+
null,
104+
promptBehavior,
105+
TokenCache.DefaultShared);
106+
107+
account.Properties[AzureAccount.Property.Tenants] = accessToken.TenantId;
108+
newTenant = new AzureTenant();
109+
newTenant.Id = new Guid(accessToken.TenantId);
110+
newTenant.Domain = accessToken.GetDomain();
111+
_profile.Context = new AzureContext(account, envrionment, newTenant);
112+
}
113+
else if(!string.IsNullOrWhiteSpace(subscriptionId))
114+
{
115+
foreach (var tenant in ListAccountTenants(account, envrionment, null, promptBehavior))
116+
{
117+
if (TryGetTenantSubscription(account, envrionment, tenant.Id.ToString(), subscriptionId, null, promptBehavior, out newSubscription, out newTenant))
118+
{
119+
_profile.Context = new AzureContext(newSubscription, account, envrionment, newTenant);
120+
break;
121+
}
122+
}
123+
}
124+
else
125+
{
126+
throw new PSNotSupportedException();
127+
}
128+
129+
_profile.Context.TokenCache = TokenCache.DefaultShared.Serialize();
130+
131+
return _profile.Context;
132+
}
133+
134+
public List<AzureTenant> ListTenants(string tenant)
135+
{
136+
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Auto)
137+
.Where(t => tenant == null ||
138+
tenant.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase) ||
139+
tenant.Equals(t.Domain, StringComparison.OrdinalIgnoreCase))
140+
.ToList();
141+
}
142+
83143
private bool TryGetTenantSubscription(
84144
AzureAccount account,
85145
AzureEnvironment environment,
86146
string tenantId,
87147
string subscriptionId,
88148
SecureString password,
89149
ShowDialog promptBehavior,
90-
out AzureSubscription subscription)
150+
out AzureSubscription subscription,
151+
out AzureTenant tenant)
91152
{
92153
var accessToken = AzureSession.AuthenticationFactory.Authenticate(
93154
account,
@@ -111,7 +172,7 @@ private bool TryGetTenantSubscription(
111172
else
112173
{
113174
var subscriptions = subscriptionClient.Subscriptions.List().Subscriptions;
114-
if (subscriptions != null)
175+
if (subscriptions != null && subscriptions.Any())
115176
{
116177
if (subscriptions.Count > 1)
117178
{
@@ -140,18 +201,20 @@ private bool TryGetTenantSubscription(
140201
};
141202

142203
account.Properties[AzureAccount.Property.Tenants] = accessToken.TenantId;
204+
tenant = new AzureTenant();
205+
tenant.Id = new Guid(accessToken.TenantId);
206+
tenant.Domain = accessToken.GetDomain();
143207
return true;
144208
}
145209

146210
subscription = null;
211+
tenant = null;
147212
return false;
148213
}
149214
}
150215

151-
private string[] ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password)
216+
private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
152217
{
153-
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
154-
155218
var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(
156219
account,
157220
environment,
@@ -164,7 +227,9 @@ private string[] ListAccountTenants(AzureAccount account, AzureEnvironment envir
164227
new TokenCloudCredentials(commonTenantToken.AccessToken),
165228
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
166229
{
167-
return subscriptionClient.Tenants.List().TenantIds.Select(ti => ti.TenantId).ToArray();
230+
return subscriptionClient.Tenants.List().TenantIds
231+
.Select(ti => new AzureTenant() { Id = new Guid(ti.TenantId), Domain = commonTenantToken.GetDomain() } )
232+
.ToList();
168233
}
169234
}
170235

src/Common/Commands.ResourceManager.Common/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<package id="Microsoft.ApplicationInsights" version="1.1.1-beta" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
7-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
7+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
1010
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
</Reference>
6060
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6161
<SpecificVersion>False</SpecificVersion>
62-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
62+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6363
</Reference>
6464
<Reference Include="Microsoft.Azure.Common.NetFramework">
6565
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
@@ -178,7 +178,10 @@
178178
</Reference>
179179
</ItemGroup>
180180
<ItemGroup>
181+
<Compile Include="ContextCmdletTests.Live.cs" />
182+
<Compile Include="TenantCmdletTests.cs" />
181183
<Compile Include="LoginCmdletTests.cs" />
184+
<Compile Include="ContextCmdletTests.Mocked.cs" />
182185
<Compile Include="ProfileCmdletTests.cs" />
183186
<Compile Include="Properties\AssemblyInfo.cs" />
184187
</ItemGroup>

0 commit comments

Comments
 (0)