-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathRegionAndMtlsDiscoveryProvider.cs
More file actions
170 lines (145 loc) · 7.29 KB
/
RegionAndMtlsDiscoveryProvider.cs
File metadata and controls
170 lines (145 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Identity.Client.Core;
using Microsoft.Identity.Client.Http;
using Microsoft.Identity.Client.Instance.Discovery;
using Microsoft.Identity.Client.Internal;
namespace Microsoft.Identity.Client.Region
{
internal class RegionAndMtlsDiscoveryProvider : IRegionDiscoveryProvider
{
private readonly IRegionManager _regionManager;
public const string PublicEnvForRegional = "login.microsoft.com";
public const string PublicEnvForRegionalMtlsAuth = "mtlsauth.microsoft.com";
// Map of unsupported sovereign cloud hosts for mTLS PoP to their error messages
private static readonly Dictionary<string, string> s_unsupportedMtlsHosts =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "login.usgovcloudapi.net", MsalErrorMessage.MtlsPopNotSupportedForUsGovCloudApiMessage },
{ "login.chinacloudapi.cn", MsalErrorMessage.MtlsPopNotSupportedForChinaCloudApiMessage }
};
public RegionAndMtlsDiscoveryProvider(IHttpManager httpManager)
{
_regionManager = new RegionManager(httpManager);
}
public async Task<InstanceDiscoveryMetadataEntry> GetMetadataAsync(Uri authority, RequestContext requestContext)
{
// Fail fast: Check for unsupported mTLS hosts before any region discovery
if (requestContext.IsMtlsRequested)
{
string host = authority.Host;
// Check if host is in the unsupported list
if (s_unsupportedMtlsHosts.TryGetValue(host, out string errorMessage))
{
requestContext.Logger.Error($"[Region discovery] mTLS PoP is not supported for host: {host}");
throw new MsalClientException(
MsalError.MtlsPopNotSupportedForEnvironment,
errorMessage);
}
// Check if host starts with "login."
if (!host.StartsWith("login.", StringComparison.OrdinalIgnoreCase))
{
requestContext.Logger.Error($"[Region discovery] mTLS PoP requires hosts to start with 'login.': {host}");
throw new MsalClientException(
MsalError.MtlsPopNotSupportedForEnvironment,
MsalErrorMessage.MtlsPopNotSupportedForNonLoginHostMessage);
}
}
string region = null;
bool isMtlsEnabled = requestContext.IsMtlsRequested;
// Always attempt region discovery for AcquireTokenForClient.
// Also attempt it for mTLS-enabled user flows when the app has opted in to
// regional endpoints (AzureRegion != null), so that OBO/RT can use a regional
// mTLS endpoint (e.g. eastus.mtlsauth.microsoft.com) when configured.
bool shouldAttemptRegionDiscovery =
requestContext.ApiEvent?.ApiId == TelemetryCore.Internal.Events.ApiEvent.ApiIds.AcquireTokenForClient ||
(isMtlsEnabled && requestContext.ServiceBundle.Config.AzureRegion != null);
if (shouldAttemptRegionDiscovery)
{
region = await _regionManager.GetAzureRegionAsync(requestContext).ConfigureAwait(false);
}
if (string.IsNullOrEmpty(region))
{
if (isMtlsEnabled)
{
// Region is not available — use the global mTLS endpoint
string globalMtlsEnv = GetGlobalMtlsEnvironment(authority, requestContext);
requestContext.Logger.Info($"[Region discovery] Region not available. Using global mTLS environment: {globalMtlsEnv}");
return CreateEntry(authority.Host, globalMtlsEnv);
}
requestContext.Logger.Info("[Region discovery] Not using a regional authority. ");
return null;
}
// already regionalized
if (authority.Host.StartsWith($"{region}."))
{
return CreateEntry(requestContext.ServiceBundle.Config.Authority.AuthorityInfo.Host, authority.Host);
}
string regionalEnv = GetRegionalizedEnvironment(authority, region, requestContext);
return CreateEntry(authority.Host, regionalEnv);
}
private static InstanceDiscoveryMetadataEntry CreateEntry(string originalEnv, string regionalEnv)
{
return new InstanceDiscoveryMetadataEntry()
{
Aliases = new[] { regionalEnv, originalEnv },
PreferredCache = originalEnv,
PreferredNetwork = regionalEnv
};
}
private static string GetGlobalMtlsEnvironment(Uri authority, RequestContext requestContext)
{
string host = authority.Host;
if (KnownMetadataProvider.IsPublicEnvironment(host))
{
return PublicEnvForRegionalMtlsAuth;
}
if (KnownMetadataProvider.TryGetKnownEnviromentPreferredNetwork(host, out var preferredNetworkEnv))
{
host = preferredNetworkEnv;
}
// Replace "login" with "mtlsauth" for mTLS scenarios
if (host.StartsWith("login.", StringComparison.OrdinalIgnoreCase))
{
host = "mtlsauth" + host.Substring("login".Length);
}
return host;
}
private static string GetRegionalizedEnvironment(Uri authority, string region, RequestContext requestContext)
{
string host = authority.Host;
if (KnownMetadataProvider.IsPublicEnvironment(host))
{
if (requestContext.IsMtlsRequested)
{
requestContext.Logger.Info(() => $"[Region discovery] Using MTLS regional environment: {region}.{PublicEnvForRegionalMtlsAuth}");
return $"{region}.{PublicEnvForRegionalMtlsAuth}";
}
else
{
requestContext.Logger.Info(() => $"[Region discovery] Regionalized Environment is : {region}.{PublicEnvForRegional}. ");
return $"{region}.{PublicEnvForRegional}";
}
}
// Regional business rule - use the PreferredNetwork value for public and sovereign clouds
// but do not do instance discovery for it - rely on cached values only
if (KnownMetadataProvider.TryGetKnownEnviromentPreferredNetwork(host, out var preferredNetworkEnv))
{
host = preferredNetworkEnv;
}
if (requestContext.IsMtlsRequested)
{
// Modify the host to replace "login" with "mtlsauth" for mTLS scenarios
if (host.StartsWith("login.", StringComparison.OrdinalIgnoreCase))
{
host = "mtlsauth" + host.Substring("login".Length);
}
}
requestContext.Logger.Info(() => $"[Region discovery] Regionalized Environment is : {region}.{host}. ");
return $"{region}.{host}";
}
}
}