-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathARMConfiguration.cs
More file actions
178 lines (159 loc) · 9.46 KB
/
ARMConfiguration.cs
File metadata and controls
178 lines (159 loc) · 9.46 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
171
172
173
174
175
176
177
178
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
namespace ARMClient.Authentication
{
public class ARMConfiguration
{
static readonly ConcurrentDictionary<string, string> _configs = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
static readonly Dictionary<string, string> _defaults = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "Dogfood.AADGraphUrl", "https://graph.ppe.windows.net" },
{ "Dogfood.AADMSGraphUrl", "https://graph.microsoft-ppe.com" },
{ "Dogfood.AADLoginUrl", "https://login.windows-ppe.net" },
{ "Dogfood.ARMResource", "https://management.core.windows.net/" },
{ "Dogfood.ARMUrl", "https://api-dogfood.resources.windows-int.net" },
{ "Dogfood.KeyVaultResource", "https://vault-int.azure-int.net" },
{ "Dogfood.RDFEUrl", "https://umapi-preview.core.windows-int.net" },
{ "Dogfood.ScmSuffix", ".windows-int.net" },
{ "Dogfood.VsoSuffix", ".tfsallin.net" },
{ "Dogfood.AppServiceUrl", "https://appservice.azure.com" },
{ "Prod.AADGraphUrl", "https://graph.windows.net" },
{ "Prod.AADMSGraphUrl", "https://graph.microsoft.com" },
{ "Prod.AADLoginUrl", "https://login.microsoftonline.com" },
{ "Prod.ARMResource", "https://management.core.windows.net/" },
{ "Prod.ARMUrl", "https://management.azure.com" },
{ "Prod.KeyVaultResource", "https://vault.azure.net" },
{ "Prod.RDFEUrl", "https://management.core.windows.net" },
{ "Prod.ScmSuffix", ".scm.azurewebsites.net" },
{ "Prod.VsoSuffix", ".visualstudio.com" },
{ "Prod.AppServiceUrl", "https://appservice.azure.com" },
// https://docs.microsoft.com/en-us/azure/azure-government/documentation-government-developer-guide
{ "Fairfax.AADGraphUrl", "https://graph.windows.net" },
{ "Fairfax.AADMSGraphUrl", "https://graph.microsoft.us" },
{ "Fairfax.AADLoginUrl", "https://login.microsoftonline.us" },
{ "Fairfax.ARMResource", "https://management.core.usgovcloudapi.net/" },
{ "Fairfax.ARMUrl", "https://management.usgovcloudapi.net" },
{ "Fairfax.KeyVaultResource", "https://vault.usgovcloudapi.net" },
{ "Fairfax.RDFEUrl", "https://management.core.usgovcloudapi.net" },
{ "Fairfax.ScmSuffix", ".scm.azurewebsites.us" },
{ "Fairfax.AppServiceUrl", "https://appservice.azure.us" },
// https://docs.microsoft.com/en-us/azure/china/resources-developer-guide
{ "Mooncake.AADGraphUrl", "https://graph.chinacloudapi.cn" },
{ "Mooncake.AADMSGraphUrl", "https://microsoftgraph.chinacloudapi.cn" },
{ "Mooncake.AADLoginUrl", "https://login.chinacloudapi.cn" },
{ "Mooncake.ARMResource", "https://management.core.chinacloudapi.cn/" },
{ "Mooncake.ARMUrl", "https://management.chinacloudapi.cn" },
{ "Mooncake.KeyVaultResource", "https://vault.azure.cn" },
{ "Mooncake.RDFEUrl", "https://management.core.chinacloudapi.cn" },
{ "Mooncake.ScmSuffix", ".scm.chinacloudsites.cn" },
{ "Mooncake.AppServiceUrl", "https://appservice.azure.cn" },
// https://docs.microsoft.com/en-us/azure/germany/germany-developer-guide
{ "Blackforest.AADGraphUrl", "https://graph.cloudapi.de" },
{ "Blackforest.AADMSGraphUrl", "https://microsoftgraph.cloudapi.de" },
{ "Blackforest.AADLoginUrl", "https://login.microsoftonline.de" },
{ "Blackforest.ARMResource", "https://management.core.cloudapi.de/" },
{ "Blackforest.ARMUrl", "https://management.microsoftazure.de" },
{ "Blackforest.KeyVaultResource", "https://vault.microsoftazure.de" },
{ "Blackforest.RDFEUrl", "https://management.core.cloudapi.de/" },
{ "Blackforest.ScmSuffix", ".scm.azurewebsites.de" },
{ "Blackforest.AppServiceUrl", "https://appservice.azure.de" },
// Sovereign cloud - France (Bleu)
{ "Bleu.AADGraphUrl", string.Empty },
{ "Bleu.AADMSGraphUrl", "https://graph.svc.sovcloud.fr" },
{ "Bleu.AADLoginUrl", "https://login.sovcloud-identity.fr" },
{ "Bleu.ARMResource", "https://management.core.sovcloud-api.fr/" },
{ "Bleu.ARMUrl", "https://management.sovcloud-api.fr" },
{ "Bleu.KeyVaultResource", "https://vault.sovcloud-api.fr" },
{ "Bleu.RDFEUrl", "https://management.core.sovcloud-api.fr" },
{ "Bleu.ScmSuffix", ".scm.azurewebsites.fr" },
{ "Bleu.AppServiceUrl", "https://appservice.azure.sovcloud-api.fr" },
// Sovereign cloud - Germany (Delos)
{ "Delos.AADGraphUrl", string.Empty },
{ "Delos.AADMSGraphUrl", "https://graph.svc.sovcloud.de" },
{ "Delos.AADLoginUrl", "https://login.sovcloud-identity.de" },
{ "Delos.ARMResource", "https://management.core.sovcloud-api.de/" },
{ "Delos.ARMUrl", "https://management.sovcloud-api.de" },
{ "Delos.KeyVaultResource", "https://vault.sovcloud-api.de" },
{ "Delos.RDFEUrl", "https://management.core.sovcloud-api.de" },
{ "Delos.ScmSuffix", ".scm.azurewebsites.de" },
{ "Delos.AppServiceUrl", "https://appservice.azure.de" },
};
private string _aadLoginUrl;
private string _aadGraphUrl;
private string _aadMSGraphUrl;
private string _armUrl;
private string _rdfeUrl;
private string _armResource;
private string _keyVaultResource;
private string _scmSuffix;
private string _vsoSuffix;
private string _appServiceUrl;
public ARMConfiguration(string env)
{
AzureEnvironment = env;
Current = this;
}
public ARMConfiguration(Uri aadLoginUrl)
: this(_defaults.First(kv => kv.Key.EndsWith(".AADLoginUrl") && string.Equals(new Uri(kv.Value).Host, aadLoginUrl.Host, StringComparison.OrdinalIgnoreCase)).Key.Split('.')[0])
{
}
public static ARMConfiguration Current { get; private set; }
public string AzureEnvironment { get; private set; }
public string AADLoginUrl { get { return _aadLoginUrl ?? (_aadLoginUrl = _configs.GetOrAdd($"{AzureEnvironment}.AADLoginUrl", k => GetValue(k))); } }
public string AADGraphUrl { get { return _aadGraphUrl ?? (_aadGraphUrl = _configs.GetOrAdd($"{AzureEnvironment}.AADGraphUrl", k => GetValue(k))); } }
public string AADMSGraphUrl { get { return _aadMSGraphUrl ?? (_aadMSGraphUrl = _configs.GetOrAdd($"{AzureEnvironment}.AADMSGraphUrl", k => GetValue(k))); } }
public string ARMUrl { get { return _armUrl ?? (_armUrl = _configs.GetOrAdd($"{AzureEnvironment}.ARMUrl", k => GetValue(k))); } }
public string RDFEUrl { get { return _rdfeUrl ?? (_rdfeUrl = _configs.GetOrAdd($"{AzureEnvironment}.RDFEUrl", k => GetValue(k))); } }
public string ARMResource { get { return _armResource ?? (_armResource = _configs.GetOrAdd($"{AzureEnvironment}.ARMResource", k => GetValue(k))); } }
public string KeyVaultResource { get { return _keyVaultResource ?? (_keyVaultResource = _configs.GetOrAdd($"{AzureEnvironment}.KeyVaultResource", k => GetValue(k))); } }
public string ScmSuffix { get { return _scmSuffix ?? (_scmSuffix = _configs.GetOrAdd($"{AzureEnvironment}.ScmSuffix", k => GetValue(k))); } }
public string VsoSuffix { get { return _vsoSuffix ?? (_vsoSuffix = _configs.GetOrAdd($"{AzureEnvironment}.VsoSuffix", k => GetValue(k))); } }
public string AppServiceUrl { get { return _appServiceUrl ?? (_appServiceUrl = _configs.GetOrAdd($"{AzureEnvironment}.{nameof(AppServiceUrl)}", k => GetValue(k))); } }
static string GetValue(string key)
{
// env -> appSetting -> default
var value = Environment.GetEnvironmentVariable(key);
if (!string.IsNullOrEmpty(value))
{
return value;
}
value = ConfigurationManager.AppSettings[key];
if (!string.IsNullOrEmpty(value))
{
return value;
}
if (_defaults.TryGetValue(key, out value))
{
return value;
}
throw new InvalidOperationException($"Configuration for {key} is not found. Please see https://github.com/projectkudu/ARMClient/wiki/ARMConfiguration for details.");
}
public static string GetEnvironmentByRequest(Uri uri)
{
foreach (var pair in _defaults)
{
if (Uri.TryCreate(pair.Value, UriKind.Absolute, out Uri defaultUri))
{
if (uri.Host.EndsWith(defaultUri.Host, StringComparison.OrdinalIgnoreCase))
{
return pair.Key.Split('.')[0];
}
}
}
foreach (var pair in _configs)
{
if (Uri.TryCreate(pair.Value, UriKind.Absolute, out Uri defaultUri))
{
if (uri.Host.EndsWith(defaultUri.Host, StringComparison.OrdinalIgnoreCase))
{
return pair.Key.Split('.')[0];
}
}
}
return null;
}
}
}