aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/go-autorest/autorest/azure/environments.go
blob: 4701b437641927724d87b033a2d4331b70a46250 (plain) (blame)
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
package azure

import (
    "fmt"
    "net/url"
    "strings"
)

const (
    activeDirectoryAPIVersion = "1.0"
)

var environments = map[string]Environment{
    "AZURECHINACLOUD":        ChinaCloud,
    "AZUREGERMANCLOUD":       GermanCloud,
    "AZUREPUBLICCLOUD":       PublicCloud,
    "AZUREUSGOVERNMENTCLOUD": USGovernmentCloud,
}

// Environment represents a set of endpoints for each of Azure's Clouds.
type Environment struct {
    Name                         string `json:"name"`
    ManagementPortalURL          string `json:"managementPortalURL"`
    PublishSettingsURL           string `json:"publishSettingsURL"`
    ServiceManagementEndpoint    string `json:"serviceManagementEndpoint"`
    ResourceManagerEndpoint      string `json:"resourceManagerEndpoint"`
    ActiveDirectoryEndpoint      string `json:"activeDirectoryEndpoint"`
    GalleryEndpoint              string `json:"galleryEndpoint"`
    KeyVaultEndpoint             string `json:"keyVaultEndpoint"`
    GraphEndpoint                string `json:"graphEndpoint"`
    StorageEndpointSuffix        string `json:"storageEndpointSuffix"`
    SQLDatabaseDNSSuffix         string `json:"sqlDatabaseDNSSuffix"`
    TrafficManagerDNSSuffix      string `json:"trafficManagerDNSSuffix"`
    KeyVaultDNSSuffix            string `json:"keyVaultDNSSuffix"`
    ServiceBusEndpointSuffix     string `json:"serviceBusEndpointSuffix"`
    ServiceManagementVMDNSSuffix string `json:"serviceManagementVMDNSSuffix"`
    ResourceManagerVMDNSSuffix   string `json:"resourceManagerVMDNSSuffix"`
    ContainerRegistryDNSSuffix   string `json:"containerRegistryDNSSuffix"`
}

var (
    // PublicCloud is the default public Azure cloud environment
    PublicCloud = Environment{
        Name:                         "AzurePublicCloud",
        ManagementPortalURL:          "https://manage.windowsazure.com/",
        PublishSettingsURL:           "https://manage.windowsazure.com/publishsettings/index",
        ServiceManagementEndpoint:    "https://management.core.windows.net/",
        ResourceManagerEndpoint:      "https://management.azure.com/",
        ActiveDirectoryEndpoint:      "https://login.microsoftonline.com/",
        GalleryEndpoint:              "https://gallery.azure.com/",
        KeyVaultEndpoint:             "https://vault.azure.net/",
        GraphEndpoint:                "https://graph.windows.net/",
        StorageEndpointSuffix:        "core.windows.net",
        SQLDatabaseDNSSuffix:         "database.windows.net",
        TrafficManagerDNSSuffix:      "trafficmanager.net",
        KeyVaultDNSSuffix:            "vault.azure.net",
        ServiceBusEndpointSuffix:     "servicebus.azure.com",
        ServiceManagementVMDNSSuffix: "cloudapp.net",
        ResourceManagerVMDNSSuffix:   "cloudapp.azure.com",
        ContainerRegistryDNSSuffix:   "azurecr.io",
    }

    // USGovernmentCloud is the cloud environment for the US Government
    USGovernmentCloud = Environment{
        Name:                         "AzureUSGovernmentCloud",
        ManagementPortalURL:          "https://manage.windowsazure.us/",
        PublishSettingsURL:           "https://manage.windowsazure.us/publishsettings/index",
        ServiceManagementEndpoint:    "https://management.core.usgovcloudapi.net/",
        ResourceManagerEndpoint:      "https://management.usgovcloudapi.net/",
        ActiveDirectoryEndpoint:      "https://login.microsoftonline.com/",
        GalleryEndpoint:              "https://gallery.usgovcloudapi.net/",
        KeyVaultEndpoint:             "https://vault.usgovcloudapi.net/",
        GraphEndpoint:                "https://graph.usgovcloudapi.net/",
        StorageEndpointSuffix:        "core.usgovcloudapi.net",
        SQLDatabaseDNSSuffix:         "database.usgovcloudapi.net",
        TrafficManagerDNSSuffix:      "usgovtrafficmanager.net",
        KeyVaultDNSSuffix:            "vault.usgovcloudapi.net",
        ServiceBusEndpointSuffix:     "servicebus.usgovcloudapi.net",
        ServiceManagementVMDNSSuffix: "usgovcloudapp.net",
        ResourceManagerVMDNSSuffix:   "cloudapp.windowsazure.us",
        ContainerRegistryDNSSuffix:   "azurecr.io",
    }

    // ChinaCloud is the cloud environment operated in China
    ChinaCloud = Environment{
        Name:                         "AzureChinaCloud",
        ManagementPortalURL:          "https://manage.chinacloudapi.com/",
        PublishSettingsURL:           "https://manage.chinacloudapi.com/publishsettings/index",
        ServiceManagementEndpoint:    "https://management.core.chinacloudapi.cn/",
        ResourceManagerEndpoint:      "https://management.chinacloudapi.cn/",
        ActiveDirectoryEndpoint:      "https://login.chinacloudapi.cn/",
        GalleryEndpoint:              "https://gallery.chinacloudapi.cn/",
        KeyVaultEndpoint:             "https://vault.azure.cn/",
        GraphEndpoint:                "https://graph.chinacloudapi.cn/",
        StorageEndpointSuffix:        "core.chinacloudapi.cn",
        SQLDatabaseDNSSuffix:         "database.chinacloudapi.cn",
        TrafficManagerDNSSuffix:      "trafficmanager.cn",
        KeyVaultDNSSuffix:            "vault.azure.cn",
        ServiceBusEndpointSuffix:     "servicebus.chinacloudapi.net",
        ServiceManagementVMDNSSuffix: "chinacloudapp.cn",
        ResourceManagerVMDNSSuffix:   "cloudapp.azure.cn",
        ContainerRegistryDNSSuffix:   "azurecr.io",
    }

    // GermanCloud is the cloud environment operated in Germany
    GermanCloud = Environment{
        Name:                         "AzureGermanCloud",
        ManagementPortalURL:          "http://portal.microsoftazure.de/",
        PublishSettingsURL:           "https://manage.microsoftazure.de/publishsettings/index",
        ServiceManagementEndpoint:    "https://management.core.cloudapi.de/",
        ResourceManagerEndpoint:      "https://management.microsoftazure.de/",
        ActiveDirectoryEndpoint:      "https://login.microsoftonline.de/",
        GalleryEndpoint:              "https://gallery.cloudapi.de/",
        KeyVaultEndpoint:             "https://vault.microsoftazure.de/",
        GraphEndpoint:                "https://graph.cloudapi.de/",
        StorageEndpointSuffix:        "core.cloudapi.de",
        SQLDatabaseDNSSuffix:         "database.cloudapi.de",
        TrafficManagerDNSSuffix:      "azuretrafficmanager.de",
        KeyVaultDNSSuffix:            "vault.microsoftazure.de",
        ServiceBusEndpointSuffix:     "servicebus.cloudapi.de",
        ServiceManagementVMDNSSuffix: "azurecloudapp.de",
        ResourceManagerVMDNSSuffix:   "cloudapp.microsoftazure.de",
        ContainerRegistryDNSSuffix:   "azurecr.io",
    }
)

// EnvironmentFromName returns an Environment based on the common name specified
func EnvironmentFromName(name string) (Environment, error) {
    name = strings.ToUpper(name)
    env, ok := environments[name]
    if !ok {
        return env, fmt.Errorf("autorest/azure: There is no cloud environment matching the name %q", name)
    }
    return env, nil
}

// OAuthConfigForTenant returns an OAuthConfig with tenant specific urls
func (env Environment) OAuthConfigForTenant(tenantID string) (*OAuthConfig, error) {
    return OAuthConfigForTenant(env.ActiveDirectoryEndpoint, tenantID)
}

// OAuthConfigForTenant returns an OAuthConfig with tenant specific urls for target cloud auth endpoint
func OAuthConfigForTenant(activeDirectoryEndpoint, tenantID string) (*OAuthConfig, error) {
    template := "%s/oauth2/%s?api-version=%s"
    u, err := url.Parse(activeDirectoryEndpoint)
    if err != nil {
        return nil, err
    }
    authorizeURL, err := u.Parse(fmt.Sprintf(template, tenantID, "authorize", activeDirectoryAPIVersion))
    if err != nil {
        return nil, err
    }
    tokenURL, err := u.Parse(fmt.Sprintf(template, tenantID, "token", activeDirectoryAPIVersion))
    if err != nil {
        return nil, err
    }
    deviceCodeURL, err := u.Parse(fmt.Sprintf(template, tenantID, "devicecode", activeDirectoryAPIVersion))
    if err != nil {
        return nil, err
    }

    return &OAuthConfig{
        AuthorizeEndpoint:  *authorizeURL,
        TokenEndpoint:      *tokenURL,
        DeviceCodeEndpoint: *deviceCodeURL,
    }, nil
}