-
Notifications
You must be signed in to change notification settings - Fork 426
/
clients.go
171 lines (145 loc) · 5.66 KB
/
clients.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
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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scope
import (
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"os"
"strings"
"github.com/Azure/go-autorest/autorest"
azureautorest "github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
)
// AzureClients contains all the Azure clients used by the scopes.
type AzureClients struct {
auth.EnvironmentSettings
Authorizer autorest.Authorizer
ResourceManagerEndpoint string
ResourceManagerVMDNSSuffix string
}
// CloudEnvironment returns the Azure environment the controller runs in.
func (c *AzureClients) CloudEnvironment() string {
return c.Environment.Name
}
// TenantID returns the Azure tenant id the controller runs in.
func (c *AzureClients) TenantID() string {
return c.Values[auth.TenantID]
}
// ClientID returns the Azure client id from the controller environment.
func (c *AzureClients) ClientID() string {
return c.Values[auth.ClientID]
}
// ClientSecret returns the Azure client secret from the controller environment.
func (c *AzureClients) ClientSecret() string {
return c.Values[auth.ClientSecret]
}
// SubscriptionID returns the Azure subscription id of the cluster,
// either specified or from the environment.
func (c *AzureClients) SubscriptionID() string {
return c.Values[auth.SubscriptionID]
}
// HashKey returns a base64 url encoded sha256 hash for the Auth scope (Azure TenantID + CloudEnv + SubscriptionID +
// ClientID).
func (c *AzureClients) HashKey() string {
hasher := sha256.New()
_, _ = hasher.Write([]byte(c.TenantID() + c.CloudEnvironment() + c.SubscriptionID() + c.ClientID()))
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}
func (c *AzureClients) setCredentials(subscriptionID, environmentName string) error {
settings, err := c.getSettingsFromEnvironment(environmentName)
if err != nil {
return err
}
if subscriptionID == "" {
subscriptionID = settings.GetSubscriptionID()
if subscriptionID == "" {
return fmt.Errorf("error creating azure services. subscriptionID is not set in cluster or AZURE_SUBSCRIPTION_ID env var")
}
}
c.EnvironmentSettings = settings
c.ResourceManagerEndpoint = settings.Environment.ResourceManagerEndpoint
c.ResourceManagerVMDNSSuffix = settings.Environment.ResourceManagerVMDNSSuffix
c.Values[auth.ClientID] = strings.TrimSuffix(c.Values[auth.ClientID], "\n")
c.Values[auth.ClientSecret] = strings.TrimSuffix(c.Values[auth.ClientSecret], "\n")
c.Values[auth.SubscriptionID] = strings.TrimSuffix(subscriptionID, "\n")
c.Values[auth.TenantID] = strings.TrimSuffix(c.Values[auth.TenantID], "\n")
if c.Authorizer == nil {
c.Authorizer, err = azureutil.GetAuthorizer(settings)
if err != nil {
return err
}
}
return nil
}
func (c *AzureClients) setCredentialsWithProvider(ctx context.Context, subscriptionID, environmentName string, credentialsProvider CredentialsProvider) error {
if credentialsProvider == nil {
return fmt.Errorf("credentials provider cannot have an empty value")
}
settings, err := c.getSettingsFromEnvironment(environmentName)
if err != nil {
return err
}
if subscriptionID == "" {
subscriptionID = settings.GetSubscriptionID()
if subscriptionID == "" {
return fmt.Errorf("error creating azure services. subscriptionID is not set in cluster or AZURE_SUBSCRIPTION_ID env var")
}
}
c.EnvironmentSettings = settings
c.ResourceManagerEndpoint = settings.Environment.ResourceManagerEndpoint
c.ResourceManagerVMDNSSuffix = settings.Environment.ResourceManagerVMDNSSuffix
c.Values[auth.SubscriptionID] = strings.TrimSuffix(subscriptionID, "\n")
c.Values[auth.TenantID] = strings.TrimSuffix(credentialsProvider.GetTenantID(), "\n")
c.Values[auth.ClientID] = strings.TrimSuffix(credentialsProvider.GetClientID(), "\n")
clientSecret, err := credentialsProvider.GetClientSecret(ctx)
if err != nil {
return err
}
c.Values[auth.ClientSecret] = strings.TrimSuffix(clientSecret, "\n")
c.Authorizer, err = credentialsProvider.GetAuthorizer(ctx, c.ResourceManagerEndpoint, c.Environment.ActiveDirectoryEndpoint, c.Environment.TokenAudience)
return err
}
func (c *AzureClients) getSettingsFromEnvironment(environmentName string) (s auth.EnvironmentSettings, err error) {
s = auth.EnvironmentSettings{
Values: map[string]string{},
}
s.Values[auth.EnvironmentName] = environmentName
setValue(s, auth.SubscriptionID)
setValue(s, auth.TenantID)
setValue(s, auth.AuxiliaryTenantIDs)
setValue(s, auth.ClientID)
setValue(s, auth.ClientSecret)
setValue(s, auth.CertificatePath)
setValue(s, auth.CertificatePassword)
setValue(s, auth.Username)
setValue(s, auth.Password)
setValue(s, auth.Resource)
if v := s.Values[auth.EnvironmentName]; v == "" {
s.Environment = azureautorest.PublicCloud
} else {
s.Environment, err = azureautorest.EnvironmentFromName(v)
}
if s.Values[auth.Resource] == "" {
s.Values[auth.Resource] = s.Environment.ResourceManagerEndpoint
}
return
}
// setValue adds the specified environment variable value to the Values map if it exists.
func setValue(settings auth.EnvironmentSettings, key string) {
if v := os.Getenv(key); v != "" {
settings.Values[key] = v
}
}