Skip to content

Commit fa4569c

Browse files
authored
Merge pull request Azure#6528 from praries880/6356_fix
fix for issue Azure#6356
2 parents 97187bd + 5fcdee4 commit fa4569c

File tree

78 files changed

+607
-166
lines changed

Some content is hidden

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

78 files changed

+607
-166
lines changed

src/Common/Commands.Common.Authentication.Abstractions/Settings/AuthenticationFactorySettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
2222
/// </summary>
2323
public class AuthenticationFactorySettings : IExtensibleSettings
2424
{
25+
public const string CommonAdTenant = "Common";
26+
2527
/// <summary>
2628
/// The default token cache used for the authentication factory
2729
/// </summary>

src/ResourceManager/Aks/Commands.Aks/AzureRM.Aks.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\YamlDotNet.dll',

src/ResourceManager/AnalysisServices/Commands.AnalysisServices.Dataplane/Azure.AnalysisServices.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ CLRVersion='4.0'
4848
ProcessorArchitecture = 'None'
4949

5050
# Modules that must be imported into the global environment prior to importing this module
51-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
51+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5252

5353
# Assemblies that must be loaded prior to importing this module
5454
RequiredAssemblies = @()

src/ResourceManager/AnalysisServices/Commands.AnalysisServices/AzureRM.AnalysisServices.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\Microsoft.Azure.Management.Analysis.dll'

src/ResourceManager/ApiManagement/Commands.ApiManagement/AzureRM.ApiManagement.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\AutoMapper.dll',

src/ResourceManager/ApplicationInsights/Commands.ApplicationInsights/AzureRM.ApplicationInsights.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\Microsoft.Azure.Management.ApplicationInsights.dll'

src/ResourceManager/Automation/Commands.Automation/AzureRM.Automation.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\Microsoft.Azure.Management.Automation.dll'

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureRM.Backup.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\Microsoft.Azure.Management.BackupServicesManagement.dll',

src/ResourceManager/AzureBatch/Commands.Batch/AzureRM.Batch.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\Microsoft.Azure.Batch.dll',

src/ResourceManager/Billing/Commands.Billing/AzureRM.Billing.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\Microsoft.Azure.Management.Billing.dll'

src/ResourceManager/Cdn/Commands.Cdn/AzureRM.Cdn.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\Microsoft.Azure.Management.Cdn.dll'

src/ResourceManager/CognitiveServices/Commands.Management.CognitiveServices/AzureRM.CognitiveServices.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CLRVersion = '4.0'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.1'; })
54+
RequiredModules = @(@{ModuleName = 'AzureRM.Profile'; ModuleVersion = '5.3.2'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
RequiredAssemblies = '.\Microsoft.Azure.Management.CognitiveServices.dll'

src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMCmdlet.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
using Microsoft.Azure.Commands.Common.Authentication.Models;
1818
using Microsoft.Azure.Commands.ResourceManager.Common.Properties;
1919
using Microsoft.Azure.Management.Internal.Resources;
20+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
2021
using Microsoft.Rest;
2122
using Microsoft.WindowsAzure.Commands.Common;
2223
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2324
using System;
25+
using System.Collections.Generic;
2426
using System.Globalization;
2527
using System.Linq;
2628
using System.Management.Automation;
29+
using System.Security.Authentication;
30+
using System.Text;
2731

2832
namespace Microsoft.Azure.Commands.ResourceManager.Common
2933
{
@@ -35,6 +39,11 @@ public abstract class AzureRMCmdlet : AzurePSCmdlet
3539
protected ServiceClientTracingInterceptor _serviceClientTracingInterceptor;
3640
IAzureContextContainer _profile;
3741

42+
public const int MAX_NUMBER_OF_TOKENS_ALLOWED_IN_AUX_HEADER = 3;
43+
public const string AUX_HEADER_NAME = "x-ms-authorization-auxiliary";
44+
public const string AUX_TOKEN_PREFIX = "Bearer";
45+
public const string AUX_TOKEN_APPEND_CHAR = ";";
46+
3847
/// <summary>
3948
/// Creates new instance from AzureRMCmdlet and add the RPRegistration handler.
4049
/// </summary>
@@ -68,6 +77,82 @@ public IAzureContextContainer DefaultProfile
6877
}
6978
}
7079

80+
protected IDictionary<String, List<String>> GetAuxilaryAuthHeaderFromResourceIds(List<String> resourceIds)
81+
{
82+
IDictionary<String, List<String>> auxHeader = null;
83+
84+
//Get the subscriptions from the resource Ids
85+
var subscriptionIds = resourceIds.Select(rId => (new ResourceIdentifier(rId))?.Subscription)?.Distinct();
86+
87+
//Checxk if we have access to all the subscriptions
88+
var subscriptionList = CheckAccessToSubscriptions(subscriptionIds);
89+
90+
//get all the non default tenant ids for the subscriptions
91+
var nonDeafultTenantIds = subscriptionList?.Select(s => s.GetTenant())?.Distinct()?.Where(t => t != DefaultContext.Tenant.GetId().ToString());
92+
93+
if ((nonDeafultTenantIds != null) && (nonDeafultTenantIds.Count() > 0))
94+
{
95+
// WE can only fill in tokens for 3 tennats in the aux header, if tehre are more tenants fail now
96+
if (nonDeafultTenantIds.Count() > MAX_NUMBER_OF_TOKENS_ALLOWED_IN_AUX_HEADER)
97+
{
98+
throw new ArgumentException("Number of tenants (tenants other than the one in the current context), that the requested resources belongs to, exceeds maximum allowed number of " + MAX_NUMBER_OF_TOKENS_ALLOWED_IN_AUX_HEADER);
99+
}
100+
101+
//get the tokens for each tenant and prepare the string in the following format :
102+
//"Header Value :: Bearer <auxiliary token1>;EncryptedBearer <auxiliary token2>; Bearer <auxiliary token3>"
103+
104+
var tokens = nonDeafultTenantIds.Select(t => (new StringBuilder(AUX_TOKEN_PREFIX).Append(" ").Append(GetTokenForTenant(t)?.AccessToken))?.ToString())?.ConcatStrings(AUX_TOKEN_APPEND_CHAR);
105+
106+
auxHeader = new Dictionary<String, List<String>>();
107+
108+
List<string> headerValues = new List<string>(1);
109+
headerValues.Add(tokens);
110+
auxHeader.Add(AUX_HEADER_NAME, headerValues);
111+
}
112+
113+
return auxHeader;
114+
}
115+
116+
private List<IAzureSubscription> CheckAccessToSubscriptions(IEnumerable<string> subscriptions)
117+
{
118+
var subscriptionsNotInDefaultProfile = subscriptions.ToList().Except(DefaultProfile.Subscriptions.Select(s => s.GetId().ToString()).ToList());
119+
120+
List<IAzureSubscription> subscriptionObjects = DefaultProfile.Subscriptions.Where(s => subscriptions.Contains(s.GetId().ToString())).ToList();
121+
if (subscriptionsNotInDefaultProfile.Any())
122+
{
123+
//So we didnt find some subscriptions in the default profile..
124+
//this does not mean that the user does not have access to the subs, it just menas that the local context did not have them
125+
//We gotta now call into the subscription RP and see if the user really does not have access to these subscriptions
126+
127+
var result = Utilities.SubscriptionAndTenantHelper.GetTenantsForSubscriptions(subscriptionsNotInDefaultProfile.ToList(), DefaultContext);
128+
129+
if (result.Count < subscriptionsNotInDefaultProfile.Count())
130+
{
131+
var subscriptionsNotFoundAtAll = subscriptionsNotInDefaultProfile.ToList().Except(result.Keys);
132+
//Found subscription(s) the user does not have acess to... throw exception
133+
StringBuilder message = new StringBuilder();
134+
135+
message.Append(" The user does not have access to the following subscription(s) : ");
136+
subscriptionsNotFoundAtAll.ForEach(s => message.Append(" " + s));
137+
throw new AuthenticationException(message.ToString());
138+
}
139+
else
140+
{
141+
subscriptionObjects.AddRange(result.Values);
142+
}
143+
}
144+
145+
return subscriptionObjects;
146+
}
147+
148+
149+
private IAccessToken GetTokenForTenant(string tenantId)
150+
{
151+
return Utilities.SubscriptionAndTenantHelper.AcquireAccessToken(DefaultContext.Account,
152+
DefaultContext.Environment,
153+
tenantId);
154+
}
155+
71156
protected override string DataCollectionWarning
72157
{
73158
get

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
<Compile Include="Utilities\Models\ResourceIdentifier.cs" />
205205
<Compile Include="Utilities\ResourceManagementClientExtensions.cs" />
206206
<Compile Include="Utilities\ResourcesExtensions.cs" />
207+
<Compile Include="Utilities\SubscriptionAndTenantHelper.cs" />
207208
<None Include="MSSharedLibKey.snk" />
208209
<None Include="packages.config">
209210
<SubType>Designer</SubType>

0 commit comments

Comments
 (0)