forked from Azure/aks-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azureclient.go
424 lines (366 loc) · 18 KB
/
azureclient.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package azurestack
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/Azure/aks-engine/pkg/engine"
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2017-03-01/apimanagement"
"github.com/Azure/azure-sdk-for-go/services/authorization/mgmt/2015-07-01/authorization"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-10-01/network"
"github.com/Azure/azure-sdk-for-go/services/preview/msi/mgmt/2015-08-31-preview/msi"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2017-10-01/storage"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const (
// ApplicationDir is the name of the dir where the token is cached
ApplicationDir = ".acsengine"
)
var (
// RequiredResourceProviders is the list of Azure Resource Providers needed for AKS Engine to function
RequiredResourceProviders = []string{"Microsoft.Compute", "Microsoft.Storage", "Microsoft.Network"}
)
// AzureClient implements the `AKSEngineClient` interface.
// This client is backed by real Azure clients talking to an ARM endpoint.
type AzureClient struct {
acceptLanguages []string
auxiliaryTokens []string
environment azure.Environment
subscriptionID string
authorizationClient authorization.RoleAssignmentsClient
deploymentsClient resources.DeploymentsClient
deploymentOperationsClient resources.DeploymentOperationsClient
msiClient msi.UserAssignedIdentitiesClient
resourcesClient apimanagement.GroupClient
storageAccountsClient storage.AccountsClient
interfacesClient network.InterfacesClient
groupsClient resources.GroupsClient
providersClient resources.ProvidersClient
virtualMachinesClient compute.VirtualMachinesClient
virtualMachineScaleSetsClient compute.VirtualMachineScaleSetsClient
virtualMachineScaleSetVMsClient compute.VirtualMachineScaleSetVMsClient
virtualMachineExtensionsClient compute.VirtualMachineExtensionsClient
disksClient compute.DisksClient
availabilitySetsClient compute.AvailabilitySetsClient
applicationsClient graphrbac.ApplicationsClient
servicePrincipalsClient graphrbac.ServicePrincipalsClient
}
// NewAzureClientWithClientSecret returns an AzureClient via client_id and client_secret
func NewAzureClientWithClientSecret(env azure.Environment, subscriptionID, clientID, clientSecret string) (*AzureClient, error) {
oauthConfig, tenantID, err := getOAuthConfig(env, subscriptionID)
if err != nil {
return nil, err
}
armSpt, err := adal.NewServicePrincipalToken(*oauthConfig, clientID, clientSecret, env.ServiceManagementEndpoint)
if err != nil {
return nil, err
}
graphSpt, err := adal.NewServicePrincipalToken(*oauthConfig, clientID, clientSecret, env.GraphEndpoint)
if err != nil {
return nil, err
}
graphSpt.Refresh()
return getClient(env, subscriptionID, tenantID, autorest.NewBearerAuthorizer(armSpt), autorest.NewBearerAuthorizer(graphSpt)), nil
}
// NewAzureClientWithClientSecretExternalTenant returns an AzureClient via client_id and client_secret from a tenant
func NewAzureClientWithClientSecretExternalTenant(env azure.Environment, subscriptionID, tenantID, clientID, clientSecret string) (*AzureClient, error) {
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, tenantID)
if err != nil {
return nil, err
}
armSpt, err := adal.NewServicePrincipalToken(*oauthConfig, clientID, clientSecret, env.ServiceManagementEndpoint)
if err != nil {
return nil, err
}
graphSpt, err := adal.NewServicePrincipalToken(*oauthConfig, clientID, clientSecret, env.GraphEndpoint)
if err != nil {
return nil, err
}
graphSpt.Refresh()
return getClient(env, subscriptionID, tenantID, autorest.NewBearerAuthorizer(armSpt), autorest.NewBearerAuthorizer(graphSpt)), nil
}
// NewAzureClientWithClientCertificateFile returns an AzureClient via client_id and jwt certificate assertion
func NewAzureClientWithClientCertificateFile(env azure.Environment, subscriptionID, clientID, certificatePath, privateKeyPath string) (*AzureClient, error) {
certificate, err := parseCertificate(certificatePath)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse certificate")
}
privateKey, err := parseRsaPrivateKey(privateKeyPath)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse rsa private key")
}
return NewAzureClientWithClientCertificate(env, subscriptionID, clientID, certificate, privateKey)
}
// NewAzureClientWithClientCertificateFileExternalTenant returns an AzureClient via client_id and jwt certificate assertion a 3rd party tenant
func NewAzureClientWithClientCertificateFileExternalTenant(env azure.Environment, subscriptionID, tenantID, clientID, certificatePath, privateKeyPath string) (*AzureClient, error) {
certificate, err := parseCertificate(certificatePath)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse certificate")
}
privateKey, err := parseRsaPrivateKey(privateKeyPath)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse rsa private key")
}
return NewAzureClientWithClientCertificateExternalTenant(env, subscriptionID, tenantID, clientID, certificate, privateKey)
}
// NewAzureClientWithClientCertificate returns an AzureClient via client_id and jwt certificate assertion
func NewAzureClientWithClientCertificate(env azure.Environment, subscriptionID, clientID string, certificate *x509.Certificate, privateKey *rsa.PrivateKey) (*AzureClient, error) {
oauthConfig, tenantID, err := getOAuthConfig(env, subscriptionID)
if err != nil {
return nil, err
}
return newAzureClientWithCertificate(env, oauthConfig, subscriptionID, clientID, tenantID, certificate, privateKey)
}
// NewAzureClientWithClientCertificateExternalTenant returns an AzureClient via client_id and jwt certificate assertion against a 3rd party tenant
func NewAzureClientWithClientCertificateExternalTenant(env azure.Environment, subscriptionID, tenantID, clientID string, certificate *x509.Certificate, privateKey *rsa.PrivateKey) (*AzureClient, error) {
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, tenantID)
if err != nil {
return nil, err
}
return newAzureClientWithCertificate(env, oauthConfig, subscriptionID, clientID, tenantID, certificate, privateKey)
}
func newAzureClientWithCertificate(env azure.Environment, oauthConfig *adal.OAuthConfig, subscriptionID, clientID, tenantID string, certificate *x509.Certificate, privateKey *rsa.PrivateKey) (*AzureClient, error) {
if certificate == nil {
return nil, errors.New("certificate should not be nil")
}
if privateKey == nil {
return nil, errors.New("privateKey should not be nil")
}
armSpt, err := adal.NewServicePrincipalTokenFromCertificate(*oauthConfig, clientID, certificate, privateKey, env.ServiceManagementEndpoint)
if err != nil {
return nil, err
}
graphSpt, err := adal.NewServicePrincipalTokenFromCertificate(*oauthConfig, clientID, certificate, privateKey, env.GraphEndpoint)
if err != nil {
return nil, err
}
graphSpt.Refresh()
return getClient(env, subscriptionID, tenantID, autorest.NewBearerAuthorizer(armSpt), autorest.NewBearerAuthorizer(graphSpt)), nil
}
func getOAuthConfig(env azure.Environment, subscriptionID string) (*adal.OAuthConfig, string, error) {
tenantID, err := engine.GetTenantID(env.ResourceManagerEndpoint, subscriptionID)
if err != nil {
return nil, "", err
}
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, tenantID)
if err != nil {
return nil, "", err
}
return oauthConfig, tenantID, nil
}
func getClient(env azure.Environment, subscriptionID, tenantID string, armAuthorizer autorest.Authorizer, graphAuthorizer autorest.Authorizer) *AzureClient {
c := &AzureClient{
environment: env,
subscriptionID: subscriptionID,
authorizationClient: authorization.NewRoleAssignmentsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
deploymentsClient: resources.NewDeploymentsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
deploymentOperationsClient: resources.NewDeploymentOperationsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
msiClient: msi.NewUserAssignedIdentitiesClient(subscriptionID),
resourcesClient: apimanagement.NewGroupClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
storageAccountsClient: storage.NewAccountsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
interfacesClient: network.NewInterfacesClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
groupsClient: resources.NewGroupsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
providersClient: resources.NewProvidersClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
virtualMachinesClient: compute.NewVirtualMachinesClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
virtualMachineScaleSetsClient: compute.NewVirtualMachineScaleSetsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
virtualMachineScaleSetVMsClient: compute.NewVirtualMachineScaleSetVMsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
virtualMachineExtensionsClient: compute.NewVirtualMachineExtensionsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
disksClient: compute.NewDisksClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
availabilitySetsClient: compute.NewAvailabilitySetsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
applicationsClient: graphrbac.NewApplicationsClientWithBaseURI(env.GraphEndpoint, tenantID),
servicePrincipalsClient: graphrbac.NewServicePrincipalsClientWithBaseURI(env.GraphEndpoint, tenantID),
}
c.authorizationClient.Authorizer = armAuthorizer
c.deploymentsClient.Authorizer = armAuthorizer
c.deploymentOperationsClient.Authorizer = armAuthorizer
c.msiClient.Authorizer = armAuthorizer
c.resourcesClient.Authorizer = armAuthorizer
c.storageAccountsClient.Authorizer = armAuthorizer
c.interfacesClient.Authorizer = armAuthorizer
c.groupsClient.Authorizer = armAuthorizer
c.providersClient.Authorizer = armAuthorizer
c.virtualMachinesClient.Authorizer = armAuthorizer
c.virtualMachineScaleSetsClient.Authorizer = armAuthorizer
c.virtualMachineScaleSetVMsClient.Authorizer = armAuthorizer
c.disksClient.Authorizer = armAuthorizer
c.availabilitySetsClient.Authorizer = armAuthorizer
c.deploymentsClient.PollingDelay = time.Second * 5
c.resourcesClient.PollingDelay = time.Second * 5
// Set permissive timeouts to accommodate long-running operations
c.deploymentsClient.PollingDuration = DefaultARMOperationTimeout
c.deploymentOperationsClient.PollingDuration = DefaultARMOperationTimeout
c.applicationsClient.PollingDuration = DefaultARMOperationTimeout
c.authorizationClient.PollingDuration = DefaultARMOperationTimeout
c.disksClient.PollingDuration = DefaultARMOperationTimeout
c.groupsClient.PollingDuration = DefaultARMOperationTimeout
c.interfacesClient.PollingDuration = DefaultARMOperationTimeout
c.providersClient.PollingDuration = DefaultARMOperationTimeout
c.resourcesClient.PollingDuration = DefaultARMOperationTimeout
c.storageAccountsClient.PollingDuration = DefaultARMOperationTimeout
c.virtualMachineScaleSetsClient.PollingDuration = DefaultARMOperationTimeout
c.virtualMachineScaleSetVMsClient.PollingDuration = DefaultARMOperationTimeout
c.virtualMachinesClient.PollingDuration = DefaultARMOperationTimeout
c.availabilitySetsClient.PollingDuration = DefaultARMOperationTimeout
c.applicationsClient.Authorizer = graphAuthorizer
c.servicePrincipalsClient.Authorizer = graphAuthorizer
return c
}
// EnsureProvidersRegistered checks if the AzureClient is registered to required resource providers and, if not, register subscription to providers
func (az *AzureClient) EnsureProvidersRegistered(subscriptionID string) error {
ctx, cancel := context.WithTimeout(context.Background(), DefaultARMOperationTimeout)
defer cancel()
registeredProviders, err := az.providersClient.List(ctx, to.Int32Ptr(100), "")
if err != nil {
return err
}
if registeredProviders.Values() == nil {
return errors.Errorf("Providers list was nil. subscription=%q", subscriptionID)
}
m := make(map[string]bool)
for _, provider := range registeredProviders.Values() {
m[strings.ToLower(to.String(provider.Namespace))] = to.String(provider.RegistrationState) == "Registered"
}
for _, provider := range RequiredResourceProviders {
registered, ok := m[strings.ToLower(provider)]
if !ok {
return errors.Errorf("Unknown resource provider %q", provider)
}
if registered {
log.Debugf("Already registered for %q", provider)
} else {
log.Infof("Registering subscription to resource provider. provider=%q subscription=%q", provider, subscriptionID)
if _, err := az.providersClient.Register(ctx, provider); err != nil {
return err
}
}
}
return nil
}
func parseCertificate(certificatePath string) (*x509.Certificate, error) {
certificateData, err := ioutil.ReadFile(certificatePath)
if err != nil {
return nil, errors.Wrap(err, "Failed to read certificate")
}
block, _ := pem.Decode(certificateData)
if block == nil {
return nil, errors.New("Failed to decode pem block from certificate")
}
certificate, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse certificate")
}
return certificate, nil
}
func parseRsaPrivateKey(path string) (*rsa.PrivateKey, error) {
privateKeyData, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
block, _ := pem.Decode(privateKeyData)
if block == nil {
return nil, errors.New("Failed to decode a pem block from private key")
}
privatePkcs1Key, errPkcs1 := x509.ParsePKCS1PrivateKey(block.Bytes)
if errPkcs1 == nil {
return privatePkcs1Key, nil
}
privatePkcs8Key, errPkcs8 := x509.ParsePKCS8PrivateKey(block.Bytes)
if errPkcs8 == nil {
privatePkcs8RsaKey, ok := privatePkcs8Key.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("pkcs8 contained non-RSA key. Expected RSA key")
}
return privatePkcs8RsaKey, nil
}
return nil, errors.Errorf("failed to parse private key as Pkcs#1 or Pkcs#8. (%s). (%s)", errPkcs1, errPkcs8)
}
//AddAcceptLanguages sets the list of languages to accept on this request
func (az *AzureClient) AddAcceptLanguages(languages []string) {
az.acceptLanguages = languages
az.authorizationClient.Client.RequestInspector = az.addAcceptLanguages()
az.deploymentOperationsClient.Client.RequestInspector = az.addAcceptLanguages()
az.deploymentsClient.Client.RequestInspector = az.addAcceptLanguages()
az.deploymentOperationsClient.Client.RequestInspector = az.addAcceptLanguages()
az.resourcesClient.Client.RequestInspector = az.addAcceptLanguages()
az.storageAccountsClient.Client.RequestInspector = az.addAcceptLanguages()
az.interfacesClient.Client.RequestInspector = az.addAcceptLanguages()
az.groupsClient.Client.RequestInspector = az.addAcceptLanguages()
az.providersClient.Client.RequestInspector = az.addAcceptLanguages()
az.virtualMachinesClient.Client.RequestInspector = az.addAcceptLanguages()
az.virtualMachineScaleSetsClient.Client.RequestInspector = az.addAcceptLanguages()
az.disksClient.Client.RequestInspector = az.addAcceptLanguages()
az.applicationsClient.Client.RequestInspector = az.addAcceptLanguages()
az.servicePrincipalsClient.Client.RequestInspector = az.addAcceptLanguages()
}
func (az *AzureClient) addAcceptLanguages() autorest.PrepareDecorator {
return func(p autorest.Preparer) autorest.Preparer {
return autorest.PreparerFunc(func(r *http.Request) (*http.Request, error) {
r, err := p.Prepare(r)
if err != nil {
return r, err
}
if az.acceptLanguages != nil {
for _, language := range az.acceptLanguages {
r.Header.Add("Accept-Language", language)
}
}
return r, nil
})
}
}
func (az *AzureClient) setAuxiliaryTokens() autorest.PrepareDecorator {
return func(p autorest.Preparer) autorest.Preparer {
return autorest.PreparerFunc(func(r *http.Request) (*http.Request, error) {
r, err := p.Prepare(r)
if err != nil {
return r, err
}
if r.Header == nil {
r.Header = make(http.Header)
}
if az.auxiliaryTokens != nil {
for _, token := range az.auxiliaryTokens {
if token == "" {
continue
}
r.Header.Set("x-ms-authorization-auxiliary", fmt.Sprintf("Bearer %s", token))
}
}
return r, nil
})
}
}
// AddAuxiliaryTokens sets the list of aux tokens to accept on this request
func (az *AzureClient) AddAuxiliaryTokens(tokens []string) {
az.auxiliaryTokens = tokens
requestWithTokens := az.setAuxiliaryTokens()
az.authorizationClient.Client.RequestInspector = requestWithTokens
az.deploymentOperationsClient.Client.RequestInspector = requestWithTokens
az.deploymentsClient.Client.RequestInspector = requestWithTokens
az.deploymentOperationsClient.Client.RequestInspector = requestWithTokens
az.resourcesClient.Client.RequestInspector = requestWithTokens
az.storageAccountsClient.Client.RequestInspector = requestWithTokens
az.interfacesClient.Client.RequestInspector = requestWithTokens
az.groupsClient.Client.RequestInspector = requestWithTokens
az.providersClient.Client.RequestInspector = requestWithTokens
az.virtualMachinesClient.Client.RequestInspector = requestWithTokens
az.virtualMachineScaleSetsClient.Client.RequestInspector = requestWithTokens
az.disksClient.Client.RequestInspector = requestWithTokens
az.applicationsClient.Client.RequestInspector = requestWithTokens
az.servicePrincipalsClient.Client.RequestInspector = requestWithTokens
}