1package azure
2
3// Copyright 2017 Microsoft Corporation
4//
5//  Licensed under the Apache License, Version 2.0 (the "License");
6//  you may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at
8//
9//      http://www.apache.org/licenses/LICENSE-2.0
10//
11//  Unless required by applicable law or agreed to in writing, software
12//  distributed under the License is distributed on an "AS IS" BASIS,
13//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14//  See the License for the specific language governing permissions and
15//  limitations under the License.
16
17import (
18	"encoding/json"
19	"fmt"
20	"io/ioutil"
21	"os"
22	"strings"
23)
24
25// EnvironmentFilepathName captures the name of the environment variable containing the path to the file
26// to be used while populating the Azure Environment.
27const EnvironmentFilepathName = "AZURE_ENVIRONMENT_FILEPATH"
28
29var environments = map[string]Environment{
30	"AZURECHINACLOUD":        ChinaCloud,
31	"AZUREGERMANCLOUD":       GermanCloud,
32	"AZUREPUBLICCLOUD":       PublicCloud,
33	"AZUREUSGOVERNMENTCLOUD": USGovernmentCloud,
34}
35
36// Environment represents a set of endpoints for each of Azure's Clouds.
37type Environment struct {
38	Name                         string `json:"name"`
39	ManagementPortalURL          string `json:"managementPortalURL"`
40	PublishSettingsURL           string `json:"publishSettingsURL"`
41	ServiceManagementEndpoint    string `json:"serviceManagementEndpoint"`
42	ResourceManagerEndpoint      string `json:"resourceManagerEndpoint"`
43	ActiveDirectoryEndpoint      string `json:"activeDirectoryEndpoint"`
44	GalleryEndpoint              string `json:"galleryEndpoint"`
45	KeyVaultEndpoint             string `json:"keyVaultEndpoint"`
46	GraphEndpoint                string `json:"graphEndpoint"`
47	ServiceBusEndpoint           string `json:"serviceBusEndpoint"`
48	BatchManagementEndpoint      string `json:"batchManagementEndpoint"`
49	StorageEndpointSuffix        string `json:"storageEndpointSuffix"`
50	SQLDatabaseDNSSuffix         string `json:"sqlDatabaseDNSSuffix"`
51	TrafficManagerDNSSuffix      string `json:"trafficManagerDNSSuffix"`
52	KeyVaultDNSSuffix            string `json:"keyVaultDNSSuffix"`
53	ServiceBusEndpointSuffix     string `json:"serviceBusEndpointSuffix"`
54	ServiceManagementVMDNSSuffix string `json:"serviceManagementVMDNSSuffix"`
55	ResourceManagerVMDNSSuffix   string `json:"resourceManagerVMDNSSuffix"`
56	ContainerRegistryDNSSuffix   string `json:"containerRegistryDNSSuffix"`
57	CosmosDBDNSSuffix            string `json:"cosmosDBDNSSuffix"`
58	TokenAudience                string `json:"tokenAudience"`
59}
60
61var (
62	// PublicCloud is the default public Azure cloud environment
63	PublicCloud = Environment{
64		Name:                         "AzurePublicCloud",
65		ManagementPortalURL:          "https://manage.windowsazure.com/",
66		PublishSettingsURL:           "https://manage.windowsazure.com/publishsettings/index",
67		ServiceManagementEndpoint:    "https://management.core.windows.net/",
68		ResourceManagerEndpoint:      "https://management.azure.com/",
69		ActiveDirectoryEndpoint:      "https://login.microsoftonline.com/",
70		GalleryEndpoint:              "https://gallery.azure.com/",
71		KeyVaultEndpoint:             "https://vault.azure.net/",
72		GraphEndpoint:                "https://graph.windows.net/",
73		ServiceBusEndpoint:           "https://servicebus.windows.net/",
74		BatchManagementEndpoint:      "https://batch.core.windows.net/",
75		StorageEndpointSuffix:        "core.windows.net",
76		SQLDatabaseDNSSuffix:         "database.windows.net",
77		TrafficManagerDNSSuffix:      "trafficmanager.net",
78		KeyVaultDNSSuffix:            "vault.azure.net",
79		ServiceBusEndpointSuffix:     "servicebus.windows.net",
80		ServiceManagementVMDNSSuffix: "cloudapp.net",
81		ResourceManagerVMDNSSuffix:   "cloudapp.azure.com",
82		ContainerRegistryDNSSuffix:   "azurecr.io",
83		CosmosDBDNSSuffix:            "documents.azure.com",
84		TokenAudience:                "https://management.azure.com/",
85	}
86
87	// USGovernmentCloud is the cloud environment for the US Government
88	USGovernmentCloud = Environment{
89		Name:                         "AzureUSGovernmentCloud",
90		ManagementPortalURL:          "https://manage.windowsazure.us/",
91		PublishSettingsURL:           "https://manage.windowsazure.us/publishsettings/index",
92		ServiceManagementEndpoint:    "https://management.core.usgovcloudapi.net/",
93		ResourceManagerEndpoint:      "https://management.usgovcloudapi.net/",
94		ActiveDirectoryEndpoint:      "https://login.microsoftonline.us/",
95		GalleryEndpoint:              "https://gallery.usgovcloudapi.net/",
96		KeyVaultEndpoint:             "https://vault.usgovcloudapi.net/",
97		GraphEndpoint:                "https://graph.windows.net/",
98		ServiceBusEndpoint:           "https://servicebus.usgovcloudapi.net/",
99		BatchManagementEndpoint:      "https://batch.core.usgovcloudapi.net/",
100		StorageEndpointSuffix:        "core.usgovcloudapi.net",
101		SQLDatabaseDNSSuffix:         "database.usgovcloudapi.net",
102		TrafficManagerDNSSuffix:      "usgovtrafficmanager.net",
103		KeyVaultDNSSuffix:            "vault.usgovcloudapi.net",
104		ServiceBusEndpointSuffix:     "servicebus.usgovcloudapi.net",
105		ServiceManagementVMDNSSuffix: "usgovcloudapp.net",
106		ResourceManagerVMDNSSuffix:   "cloudapp.windowsazure.us",
107		ContainerRegistryDNSSuffix:   "azurecr.us",
108		CosmosDBDNSSuffix:            "documents.azure.us",
109		TokenAudience:                "https://management.usgovcloudapi.net/",
110	}
111
112	// ChinaCloud is the cloud environment operated in China
113	ChinaCloud = Environment{
114		Name:                         "AzureChinaCloud",
115		ManagementPortalURL:          "https://manage.chinacloudapi.com/",
116		PublishSettingsURL:           "https://manage.chinacloudapi.com/publishsettings/index",
117		ServiceManagementEndpoint:    "https://management.core.chinacloudapi.cn/",
118		ResourceManagerEndpoint:      "https://management.chinacloudapi.cn/",
119		ActiveDirectoryEndpoint:      "https://login.chinacloudapi.cn/",
120		GalleryEndpoint:              "https://gallery.chinacloudapi.cn/",
121		KeyVaultEndpoint:             "https://vault.azure.cn/",
122		GraphEndpoint:                "https://graph.chinacloudapi.cn/",
123		ServiceBusEndpoint:           "https://servicebus.chinacloudapi.cn/",
124		BatchManagementEndpoint:      "https://batch.chinacloudapi.cn/",
125		StorageEndpointSuffix:        "core.chinacloudapi.cn",
126		SQLDatabaseDNSSuffix:         "database.chinacloudapi.cn",
127		TrafficManagerDNSSuffix:      "trafficmanager.cn",
128		KeyVaultDNSSuffix:            "vault.azure.cn",
129		ServiceBusEndpointSuffix:     "servicebus.chinacloudapi.cn",
130		ServiceManagementVMDNSSuffix: "chinacloudapp.cn",
131		ResourceManagerVMDNSSuffix:   "cloudapp.azure.cn",
132		ContainerRegistryDNSSuffix:   "azurecr.cn",
133		CosmosDBDNSSuffix:            "documents.azure.cn",
134		TokenAudience:                "https://management.chinacloudapi.cn/",
135	}
136
137	// GermanCloud is the cloud environment operated in Germany
138	GermanCloud = Environment{
139		Name:                         "AzureGermanCloud",
140		ManagementPortalURL:          "http://portal.microsoftazure.de/",
141		PublishSettingsURL:           "https://manage.microsoftazure.de/publishsettings/index",
142		ServiceManagementEndpoint:    "https://management.core.cloudapi.de/",
143		ResourceManagerEndpoint:      "https://management.microsoftazure.de/",
144		ActiveDirectoryEndpoint:      "https://login.microsoftonline.de/",
145		GalleryEndpoint:              "https://gallery.cloudapi.de/",
146		KeyVaultEndpoint:             "https://vault.microsoftazure.de/",
147		GraphEndpoint:                "https://graph.cloudapi.de/",
148		ServiceBusEndpoint:           "https://servicebus.cloudapi.de/",
149		BatchManagementEndpoint:      "https://batch.cloudapi.de/",
150		StorageEndpointSuffix:        "core.cloudapi.de",
151		SQLDatabaseDNSSuffix:         "database.cloudapi.de",
152		TrafficManagerDNSSuffix:      "azuretrafficmanager.de",
153		KeyVaultDNSSuffix:            "vault.microsoftazure.de",
154		ServiceBusEndpointSuffix:     "servicebus.cloudapi.de",
155		ServiceManagementVMDNSSuffix: "azurecloudapp.de",
156		ResourceManagerVMDNSSuffix:   "cloudapp.microsoftazure.de",
157		// ContainerRegistryDNSSuffix:   "", ACR not present yet in the German Cloud
158		CosmosDBDNSSuffix: "documents.microsoftazure.de",
159		TokenAudience:     "https://management.microsoftazure.de/",
160	}
161)
162
163// EnvironmentFromName returns an Environment based on the common name specified.
164func EnvironmentFromName(name string) (Environment, error) {
165	// IMPORTANT
166	// As per @radhikagupta5:
167	// This is technical debt, fundamentally here because Kubernetes is not currently accepting
168	// contributions to the providers. Once that is an option, the provider should be updated to
169	// directly call `EnvironmentFromFile`. Until then, we rely on dispatching Azure Stack environment creation
170	// from this method based on the name that is provided to us.
171	if strings.EqualFold(name, "AZURESTACKCLOUD") {
172		return EnvironmentFromFile(os.Getenv(EnvironmentFilepathName))
173	}
174
175	name = strings.ToUpper(name)
176	env, ok := environments[name]
177	if !ok {
178		return env, fmt.Errorf("autorest/azure: There is no cloud environment matching the name %q", name)
179	}
180
181	return env, nil
182}
183
184// EnvironmentFromFile loads an Environment from a configuration file available on disk.
185// This function is particularly useful in the Hybrid Cloud model, where one must define their own
186// endpoints.
187func EnvironmentFromFile(location string) (unmarshaled Environment, err error) {
188	fileContents, err := ioutil.ReadFile(location)
189	if err != nil {
190		return
191	}
192
193	err = json.Unmarshal(fileContents, &unmarshaled)
194
195	return
196}
197