-
Notifications
You must be signed in to change notification settings - Fork 3
/
cloud.go
81 lines (70 loc) · 2.39 KB
/
cloud.go
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package client
import (
"encoding/json"
"fmt"
"strings"
"github.com/Azure/aks-tls-bootstrap-client/pkg/datamodel"
"github.com/Azure/go-autorest/autorest/azure"
)
const (
azureConfigPathLinux = "/etc/kubernetes/azure.json"
azureConfigPathWindows = "c:\\k\\azure.json"
customCloudConfigPathLinux = "/etc/kubernetes/akscustom.json"
customCloudConfigPathWindows = "c:\\k\\azurestackcloud.json"
azurePublicCloud = "AzurePublicCloud"
azureUSGovCloud = "AzureUSGovernmentCloud"
azureChinaCloud = "AzureChinaCloud"
)
func getAADAuthorityURL(reader fileReader, azureConfig *datamodel.AzureConfig) (string, error) {
cloudName := azureConfig.Cloud
if strings.EqualFold(cloudName, azurePublicCloud) || strings.EqualFold(cloudName, azureUSGovCloud) || strings.EqualFold(cloudName, azureChinaCloud) {
env, err := azure.EnvironmentFromName(cloudName)
if err != nil {
return "", fmt.Errorf("unable to determine cloud environment config from cloud name %s: %w", cloudName, err)
}
return env.ActiveDirectoryEndpoint, nil
}
// azure.EnvironmnetFromName does something similar for stack, but that relies on AZURE_ENVIRONMENT_FILENAME being set
// which isn't always the case on Windows.
var (
customEnv = &azure.Environment{}
err error
)
if isWindows() {
err = loadJSONFromPath(reader, customCloudConfigPathWindows, customEnv)
} else {
err = loadJSONFromPath(reader, customCloudConfigPathLinux, customEnv)
}
if err != nil {
return "", fmt.Errorf("unable to load custom cloud environment config: %w", err)
}
return customEnv.ActiveDirectoryEndpoint, nil
}
func (c *tlsBootstrapClientImpl) loadAzureConfig() error {
var (
azureConfig = &datamodel.AzureConfig{}
err error
)
if isWindows() {
err = loadJSONFromPath(c.reader, azureConfigPathWindows, azureConfig)
} else {
err = loadJSONFromPath(c.reader, azureConfigPathLinux, azureConfig)
}
if err != nil {
return fmt.Errorf("unable to load azure config: %w", err)
}
c.azureConfig = azureConfig
return nil
}
func loadJSONFromPath(reader fileReader, path string, out interface{}) error {
jsonBytes, err := reader.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to parse %s: %w", path, err)
}
if err = json.Unmarshal(jsonBytes, out); err != nil {
return fmt.Errorf("failed to unmarshal %s: %w", path, err)
}
return nil
}