Skip to content

Commit 3961eba

Browse files
authored
Merge branch 'preview' into fix_reg
2 parents b68ce7d + 65f18f8 commit 3961eba

File tree

161 files changed

+19453
-38672
lines changed

Some content is hidden

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

161 files changed

+19453
-38672
lines changed

TestMappings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@
113113
".\\src\\ResourceManager\\Network\\Commands.Network.Test\\bin\\Debug\\Microsoft.Azure.Commands.Network.Test.dll",
114114
".\\src\\ResourceManager\\RecoveryServices.Backup\\Commands.RecoveryServices.Backup.Test\\bin\\Debug\\Microsoft.Azure.Commands.RecoveryServices.Backup.Test.dll",
115115
".\\src\\ResourceManager\\ServiceFabric\\Commands.ServiceFabric.Test\\bin\\Debug\\Microsoft.Azure.Commands.ServiceFabric.Test.dll",
116-
".\\src\\ResourceManager\\Sql\\Commands.Sql.Test\\bin\\Debug\\Microsoft.Azure.Commands.Sql.Test.dll"
116+
".\\src\\ResourceManager\\Sql\\Commands.Sql.Test\\bin\\Debug\\Microsoft.Azure.Commands.Sql.Test.dll",
117+
".\\src\\ResourceManager\\Dns\\Commands.Dns.Test\\bin\\Debug\\Microsoft.Azure.Commands.Dns.Test.dll"
117118
],
118119
"src/ResourceManager/NotificationHubs /": [
119120
".\\src\\ResourceManager\\NotificationHubs\\Commands.NotificationHubs.Test\\bin\\Debug\\Microsoft.Azure.Commands.NotificationHubs.Test.dll"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 Microsoft.Rest;
16+
using System.Threading.Tasks;
17+
using System.Net.Http.Headers;
18+
using System.Threading;
19+
using System.Net.Http;
20+
21+
namespace Microsoft.Azure.Commands.Common.Authentication
22+
{
23+
public class RenewingTokenCredential : ServiceClientCredentials
24+
{
25+
private IAccessToken _token;
26+
27+
28+
public RenewingTokenCredential(IAccessToken token)
29+
{
30+
_token = token;
31+
}
32+
33+
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
34+
{
35+
return Task.Run( () => _token.AuthorizeRequest((type, token) => request.Headers.Authorization = new AuthenticationHeaderValue(type, token)));
36+
}
37+
38+
}
39+
}

src/Common/Commands.Common.Authentication/Authentication/ServicePrincipalTokenProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
using Hyak.Common;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1717
using Microsoft.IdentityModel.Clients.ActiveDirectory;
18-
using Microsoft.Rest.Azure.Authentication;
1918
#if NETSTANDARD
19+
using Microsoft.Rest.Azure.Authentication;
2020
using Microsoft.WindowsAzure.Commands.Common;
2121
#endif
2222
using System;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<Compile Include="Authentication\ManagedServiceTokenInfo.cs" />
130130
<Compile Include="Authentication\ProtectedFileTokenCache.cs" />
131131
<Compile Include="Authentication\RawAccessToken.cs" />
132+
<Compile Include="Authentication\RenewingTokenCredential.cs" />
132133
<Compile Include="Authentication\ServicePrincipalKeyStore.cs" />
133134
<Compile Include="Authentication\ServicePrincipalTokenProvider.cs" />
134135
<Compile Include="Authentication\UserTokenProvider.cs" />

src/Common/Commands.Common.Authentication/Factories/AuthenticationFactory.cs

Lines changed: 120 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,7 @@ public IAccessToken Authenticate(
6464
configuration.ValidateAuthority);
6565
if (account != null && account.Type == AzureAccount.AccountType.ManagedService)
6666
{
67-
if (environment == null)
68-
{
69-
throw new InvalidOperationException("Environment is required for MSI Login");
70-
}
71-
72-
if (!account.IsPropertySet(AzureAccount.Property.MSILoginUri))
73-
{
74-
account.SetProperty(AzureAccount.Property.MSILoginUri, DefaultMSILoginUri);
75-
}
76-
77-
if (string.IsNullOrWhiteSpace(tenant))
78-
{
79-
tenant = environment.AdTenant ?? "Common";
80-
}
81-
82-
token = new ManagedServiceAccessToken(account, environment, GetResourceId(resourceId, environment), tenant);
67+
token = GetManagedServiceToken(account, environment, tenant, resourceId);
8368
}
8469
else if (account != null && environment != null
8570
&& account.Type == AzureAccount.AccountType.AccessToken)
@@ -144,12 +129,12 @@ public IAccessToken Authenticate(
144129
string resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)
145130
{
146131
return Authenticate(
147-
account,
148-
environment,
149-
tenant, password,
150-
promptBehavior,
151-
promptAction,
152-
AzureSession.Instance.TokenCache,
132+
account,
133+
environment,
134+
tenant, password,
135+
promptBehavior,
136+
promptAction,
137+
AzureSession.Instance.TokenCache,
153138
resourceId);
154139
}
155140

@@ -176,19 +161,16 @@ public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(IAzureContex
176161
throw new ArgumentException(exceptionMessage);
177162
}
178163

179-
if (context.Account.Type == AzureAccount.AccountType.Certificate)
164+
switch (context.Account.Type)
180165
{
181-
var certificate = AzureSession.Instance.DataStore.GetCertificate(context.Account.Id);
182-
return new CertificateCloudCredentials(context.Subscription.Id.ToString(), certificate);
183-
}
184-
185-
if (context.Account.Type == AzureAccount.AccountType.AccessToken)
186-
{
187-
return new TokenCloudCredentials(context.Subscription.Id.ToString(), GetEndpointToken(context.Account, targetEndpoint));
166+
case AzureAccount.AccountType.Certificate:
167+
var certificate = AzureSession.Instance.DataStore.GetCertificate(context.Account.Id);
168+
return new CertificateCloudCredentials(context.Subscription.Id.ToString(), certificate);
169+
case AzureAccount.AccountType.AccessToken:
170+
return new TokenCloudCredentials(context.Subscription.Id.ToString(), GetEndpointToken(context.Account, targetEndpoint));
188171
}
189172

190173
string tenant = null;
191-
192174
if (context.Subscription != null && context.Account != null)
193175
{
194176
tenant = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
@@ -251,6 +233,7 @@ public SubscriptionCloudCredentials GetSubscriptionCloudCredentials(IAzureContex
251233
}
252234
}
253235

236+
254237
public ServiceClientCredentials GetServiceClientCredentials(IAzureContext context)
255238
{
256239
return GetServiceClientCredentials(context,
@@ -263,16 +246,14 @@ public ServiceClientCredentials GetServiceClientCredentials(IAzureContext contex
263246
{
264247
throw new ArgumentException(Resources.ArmAccountNotFound);
265248
}
266-
267-
if (context.Account.Type == AzureAccount.AccountType.Certificate)
249+
switch (context.Account.Type)
268250
{
269-
throw new NotSupportedException(AzureAccount.AccountType.Certificate.ToString());
251+
case AzureAccount.AccountType.Certificate:
252+
throw new NotSupportedException(AzureAccount.AccountType.Certificate.ToString());
253+
case AzureAccount.AccountType.AccessToken:
254+
return new TokenCredentials(GetEndpointToken(context.Account, targetEndpoint));
270255
}
271256

272-
if (context.Account.Type == AzureAccount.AccountType.AccessToken)
273-
{
274-
return new TokenCredentials(GetEndpointToken(context.Account, targetEndpoint));
275-
}
276257

277258
string tenant = null;
278259

@@ -317,41 +298,47 @@ public ServiceClientCredentials GetServiceClientCredentials(IAzureContext contex
317298
}
318299

319300
ServiceClientCredentials result = null;
320-
321-
if (context.Account.Type == AzureAccount.AccountType.User)
322-
{
323-
result = Rest.Azure.Authentication.UserTokenProvider.CreateCredentialsFromCache(
324-
AdalConfiguration.PowerShellClientId,
325-
tenant,
326-
context.Account.Id,
327-
env,
328-
tokenCache as TokenCache).ConfigureAwait(false).GetAwaiter().GetResult();
329-
}
330-
else if (context.Account.Type == AzureAccount.AccountType.ServicePrincipal)
331-
{
332-
if (context.Account.IsPropertySet(AzureAccount.Property.CertificateThumbprint))
333-
{
334-
result = ApplicationTokenProvider.LoginSilentAsync(
335-
tenant,
336-
context.Account.Id,
337-
new CertificateApplicationCredentialProvider(
338-
context.Account.GetThumbprint()),
339-
env,
340-
tokenCache as TokenCache).ConfigureAwait(false).GetAwaiter().GetResult();
341-
}
342-
else
343-
{
344-
result = ApplicationTokenProvider.LoginSilentAsync(
345-
tenant,
346-
context.Account.Id,
347-
new KeyStoreApplicationCredentialProvider(tenant),
348-
env,
349-
tokenCache as TokenCache).ConfigureAwait(false).GetAwaiter().GetResult();
350-
}
351-
}
352-
else
301+
switch (context.Account.Type)
353302
{
354-
throw new NotSupportedException(context.Account.Type.ToString());
303+
case AzureAccount.AccountType.ManagedService:
304+
result = new RenewingTokenCredential(
305+
GetManagedServiceToken(
306+
context.Account,
307+
context.Environment,
308+
tenant,
309+
context.Environment.GetTokenAudience(targetEndpoint)));
310+
break;
311+
case AzureAccount.AccountType.User:
312+
result = Rest.Azure.Authentication.UserTokenProvider.CreateCredentialsFromCache(
313+
AdalConfiguration.PowerShellClientId,
314+
tenant,
315+
context.Account.Id,
316+
env,
317+
tokenCache as TokenCache).ConfigureAwait(false).GetAwaiter().GetResult();
318+
break;
319+
case AzureAccount.AccountType.ServicePrincipal:
320+
if (context.Account.IsPropertySet(AzureAccount.Property.CertificateThumbprint))
321+
{
322+
result = ApplicationTokenProvider.LoginSilentAsync(
323+
tenant,
324+
context.Account.Id,
325+
new CertificateApplicationCredentialProvider(
326+
context.Account.GetThumbprint()),
327+
env,
328+
tokenCache as TokenCache).ConfigureAwait(false).GetAwaiter().GetResult();
329+
}
330+
else
331+
{
332+
result = ApplicationTokenProvider.LoginSilentAsync(
333+
tenant,
334+
context.Account.Id,
335+
new KeyStoreApplicationCredentialProvider(tenant),
336+
env,
337+
tokenCache as TokenCache).ConfigureAwait(false).GetAwaiter().GetResult();
338+
}
339+
break;
340+
default:
341+
throw new NotSupportedException(context.Account.Type.ToString());
355342
}
356343

357344
return result;
@@ -363,6 +350,60 @@ public ServiceClientCredentials GetServiceClientCredentials(IAzureContext contex
363350
}
364351
}
365352

353+
public void RemoveUser(IAzureAccount account, IAzureTokenCache tokenCache)
354+
{
355+
TokenCache cache = tokenCache as TokenCache;
356+
if (cache != null && account != null && !string.IsNullOrEmpty(account.Id) && !string.IsNullOrWhiteSpace(account.Type))
357+
{
358+
switch (account.Type)
359+
{
360+
case AzureAccount.AccountType.AccessToken:
361+
account.SetProperty(AzureAccount.Property.AccessToken, null);
362+
account.SetProperty(AzureAccount.Property.GraphAccessToken, null);
363+
account.SetProperty(AzureAccount.Property.KeyVaultAccessToken, null);
364+
break;
365+
case AzureAccount.AccountType.ManagedService:
366+
account.SetProperty(AzureAccount.Property.MSILoginUri, null);
367+
break;
368+
case AzureAccount.AccountType.ServicePrincipal:
369+
try
370+
{
371+
ServicePrincipalKeyStore.DeleteKey(account.Id, account.GetTenants().FirstOrDefault());
372+
}
373+
catch
374+
{
375+
// make best effort to remove credentials
376+
}
377+
378+
RemoveFromTokenCache(cache, account);
379+
break;
380+
case AzureAccount.AccountType.User:
381+
RemoveFromTokenCache(cache, account);
382+
break;
383+
}
384+
}
385+
}
386+
387+
private IAccessToken GetManagedServiceToken(IAzureAccount account, IAzureEnvironment environment, string tenant, string resourceId)
388+
{
389+
if (environment == null)
390+
{
391+
throw new InvalidOperationException("Environment is required for MSI Login");
392+
}
393+
394+
if (!account.IsPropertySet(AzureAccount.Property.MSILoginUri))
395+
{
396+
account.SetProperty(AzureAccount.Property.MSILoginUri, DefaultMSILoginUri);
397+
}
398+
399+
if (string.IsNullOrWhiteSpace(tenant))
400+
{
401+
tenant = environment.AdTenant ?? "Common";
402+
}
403+
404+
return new ManagedServiceAccessToken(account, environment, GetResourceId(resourceId, environment), tenant);
405+
}
406+
366407
private string GetResourceId(string resourceIdorEndpointName, IAzureEnvironment environment)
367408
{
368409
return environment.GetEndpoint(resourceIdorEndpointName) ?? resourceIdorEndpointName;
@@ -384,7 +425,7 @@ private AdalConfiguration GetAdalConfiguration(IAzureEnvironment environment, st
384425
string.Format("No Active Directory endpoint specified for environment '{0}'", environment.Name));
385426
}
386427

387-
var audience = environment.GetEndpoint(resourceId)?? resourceId;
428+
var audience = environment.GetEndpoint(resourceId) ?? resourceId;
388429
if (string.IsNullOrWhiteSpace(audience))
389430
{
390431
string message = Resources.InvalidManagementTokenAudience;
@@ -410,45 +451,14 @@ private string GetEndpointToken(IAzureAccount account, string targetEndpoint)
410451
{
411452
string tokenKey = AzureAccount.Property.AccessToken;
412453
if (targetEndpoint == AzureEnvironment.Endpoint.Graph)
413-
{
454+
{
414455
tokenKey = AzureAccount.Property.GraphAccessToken;
415456
}
416457

417458
return account.GetProperty(tokenKey);
418459
}
419460

420-
public void RemoveUser(IAzureAccount account, IAzureTokenCache tokenCache)
421-
{
422-
TokenCache cache = tokenCache as TokenCache;
423-
if (cache!= null && account != null && !string.IsNullOrEmpty(account.Id) && !string.IsNullOrWhiteSpace(account.Type))
424-
{
425-
switch (account.Type)
426-
{
427-
case AzureAccount.AccountType.AccessToken:
428-
account.SetProperty(AzureAccount.Property.AccessToken, null);
429-
account.SetProperty(AzureAccount.Property.GraphAccessToken, null);
430-
account.SetProperty(AzureAccount.Property.KeyVaultAccessToken, null);
431-
break;
432-
case AzureAccount.AccountType.ServicePrincipal:
433-
try
434-
{
435-
ServicePrincipalKeyStore.DeleteKey(account.Id, account.GetTenants().FirstOrDefault());
436-
}
437-
catch
438-
{
439-
// make best effort to remove credentials
440-
}
441-
442-
RemoveFromTokenCache(cache, account);
443-
break;
444-
case AzureAccount.AccountType.User:
445-
RemoveFromTokenCache(cache, account);
446-
break;
447-
}
448-
}
449-
}
450-
451-
void RemoveFromTokenCache(TokenCache cache, IAzureAccount account )
461+
private void RemoveFromTokenCache(TokenCache cache, IAzureAccount account)
452462
{
453463
if (cache != null && cache.Count > 0 && account != null && !string.IsNullOrWhiteSpace(account.Id) && !string.IsNullOrWhiteSpace(account.Type))
454464
{
@@ -460,20 +470,20 @@ void RemoveFromTokenCache(TokenCache cache, IAzureAccount account )
460470
}
461471
}
462472

463-
bool MatchCacheItem(IAzureAccount account, TokenCacheItem item)
473+
private bool MatchCacheItem(IAzureAccount account, TokenCacheItem item)
464474
{
465475
bool result = false;
466476
if (account != null && !string.IsNullOrWhiteSpace(account.Type) && item != null)
467477
{
468-
switch(account.Type)
478+
switch (account.Type)
469479
{
470480
case AzureAccount.AccountType.ServicePrincipal:
471481
result = string.Equals(account.Id, item.ClientId, StringComparison.OrdinalIgnoreCase);
472482
break;
473483
case AzureAccount.AccountType.User:
474-
result = string.Equals(account.Id, item.DisplayableId, StringComparison.OrdinalIgnoreCase)
484+
result = string.Equals(account.Id, item.DisplayableId, StringComparison.OrdinalIgnoreCase)
475485
|| (account.TenantMap != null && account.TenantMap.Any(
476-
(m) => string.Equals(m.Key, item.TenantId, StringComparison.OrdinalIgnoreCase)
486+
(m) => string.Equals(m.Key, item.TenantId, StringComparison.OrdinalIgnoreCase)
477487
&& string.Equals(m.Value, item.UniqueId, StringComparison.OrdinalIgnoreCase)));
478488
break;
479489
}

0 commit comments

Comments
 (0)