forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azureclient.go
395 lines (337 loc) · 14.6 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
package armhelpers
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/arm/authorization"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/graphrbac"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"github.com/Azure/azure-sdk-for-go/arm/resources/subscriptions"
"github.com/Azure/azure-sdk-for-go/arm/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/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/Azure/acs-engine/pkg/acsengine"
"github.com/Azure/azure-sdk-for-go/arm/disk"
)
const (
// AcsEngineClientID is the AAD ClientID for the CLI native application
AcsEngineClientID = "76e0feec-6b7f-41f0-81a7-b1b944520261"
// 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 ACS-Engine to function
RequiredResourceProviders = []string{"Microsoft.Compute", "Microsoft.Storage", "Microsoft.Network"}
)
// AzureClient implements the `ACSEngineClient` interface.
// This client is backed by real Azure clients talking to an ARM endpoint.
type AzureClient struct {
acceptLanguages []string
environment azure.Environment
subscriptionID string
authorizationClient authorization.RoleAssignmentsClient
deploymentsClient resources.DeploymentsClient
deploymentOperationsClient resources.DeploymentOperationsClient
resourcesClient resources.GroupClient
storageAccountsClient storage.AccountsClient
interfacesClient network.InterfacesClient
groupsClient resources.GroupsClient
providersClient resources.ProvidersClient
subscriptionsClient subscriptions.GroupClient
virtualMachinesClient compute.VirtualMachinesClient
virtualMachineScaleSetsClient compute.VirtualMachineScaleSetsClient
disksClient disk.DisksClient
applicationsClient graphrbac.ApplicationsClient
servicePrincipalsClient graphrbac.ServicePrincipalsClient
}
// NewAzureClientWithDeviceAuth returns an AzureClient by having a user complete a device authentication flow
func NewAzureClientWithDeviceAuth(env azure.Environment, subscriptionID string) (*AzureClient, error) {
oauthConfig, tenantID, err := getOAuthConfig(env, subscriptionID)
if err != nil {
return nil, err
}
home, err := homedir.Dir()
if err != nil {
return nil, fmt.Errorf("Failed to get user home directory to look for cached token: %q", err)
}
cachePath := filepath.Join(home, ApplicationDir, "cache", fmt.Sprintf("%s_%s.token.json", tenantID, AcsEngineClientID))
rawToken, err := tryLoadCachedToken(cachePath)
if err != nil {
return nil, err
}
var armSpt *adal.ServicePrincipalToken
if rawToken != nil {
armSpt, err = adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, AcsEngineClientID, env.ServiceManagementEndpoint, *rawToken, tokenCallback(cachePath))
if err != nil {
return nil, err
}
err = armSpt.Refresh()
if err != nil {
log.Warnf("Refresh token failed. Will fallback to device auth. %q", err)
} else {
graphSpt, err := adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, AcsEngineClientID, env.GraphEndpoint, armSpt.Token)
if err != nil {
return nil, err
}
graphSpt.Refresh()
return getClient(env, subscriptionID, tenantID, armSpt, graphSpt), nil
}
}
client := &autorest.Client{}
deviceCode, err := adal.InitiateDeviceAuth(client, *oauthConfig, AcsEngineClientID, env.ServiceManagementEndpoint)
if err != nil {
return nil, err
}
log.Warnln(*deviceCode.Message)
deviceToken, err := adal.WaitForUserCompletion(client, deviceCode)
if err != nil {
return nil, err
}
armSpt, err = adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, AcsEngineClientID, env.ServiceManagementEndpoint, *deviceToken, tokenCallback(cachePath))
if err != nil {
return nil, err
}
armSpt.Refresh()
adRawToken := armSpt.Token
adRawToken.Resource = env.GraphEndpoint
graphSpt, err := adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, AcsEngineClientID, env.GraphEndpoint, adRawToken)
if err != nil {
return nil, err
}
graphSpt.Refresh()
return getClient(env, subscriptionID, tenantID, armSpt, graphSpt), nil
}
// 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, armSpt, 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) {
certificateData, err := ioutil.ReadFile(certificatePath)
if err != nil {
return nil, fmt.Errorf("Failed to read certificate: %q", err)
}
block, _ := pem.Decode(certificateData)
if block == nil {
return nil, fmt.Errorf("Failed to decode pem block from certificate")
}
certificate, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Failed to parse certificate: %q", err)
}
privateKey, err := parseRsaPrivateKey(privateKeyPath)
if err != nil {
return nil, fmt.Errorf("Failed to parse rsa private key: %q", err)
}
return NewAzureClientWithClientCertificate(env, subscriptionID, 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
}
if certificate == nil {
return nil, fmt.Errorf("certificate should not be nil")
}
if privateKey == nil {
return nil, fmt.Errorf("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, armSpt, graphSpt), nil
}
func tokenCallback(path string) func(t adal.Token) error {
return func(token adal.Token) error {
err := adal.SaveToken(path, 0600, token)
if err != nil {
return err
}
log.Debugf("Saved token to cache. path=%q", path)
return nil
}
}
func tryLoadCachedToken(cachePath string) (*adal.Token, error) {
log.Debugf("Attempting to load token from cache. path=%q", cachePath)
// Check for file not found so we can suppress the file not found error
// LoadToken doesn't discern and returns error either way
if _, err := os.Stat(cachePath); err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
token, err := adal.LoadToken(cachePath)
if err != nil {
return nil, fmt.Errorf("Failed to load token from file: %v", err)
}
return token, nil
}
func getOAuthConfig(env azure.Environment, subscriptionID string) (*adal.OAuthConfig, string, error) {
tenantID, err := acsengine.GetTenantID(env, 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, armSpt *adal.ServicePrincipalToken, graphSpt *adal.ServicePrincipalToken) *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),
resourcesClient: resources.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),
disksClient: disk.NewDisksClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
applicationsClient: graphrbac.NewApplicationsClientWithBaseURI(env.GraphEndpoint, tenantID),
servicePrincipalsClient: graphrbac.NewServicePrincipalsClientWithBaseURI(env.GraphEndpoint, tenantID),
}
authorizer := autorest.NewBearerAuthorizer(armSpt)
c.authorizationClient.Authorizer = authorizer
c.deploymentsClient.Authorizer = authorizer
c.deploymentOperationsClient.Authorizer = authorizer
c.resourcesClient.Authorizer = authorizer
c.storageAccountsClient.Authorizer = authorizer
c.interfacesClient.Authorizer = authorizer
c.groupsClient.Authorizer = authorizer
c.providersClient.Authorizer = authorizer
c.virtualMachinesClient.Authorizer = authorizer
c.virtualMachineScaleSetsClient.Authorizer = authorizer
c.disksClient.Authorizer = authorizer
c.deploymentsClient.PollingDelay = time.Second * 5
c.resourcesClient.PollingDelay = time.Second * 5
graphAuthorizer := autorest.NewBearerAuthorizer(graphSpt)
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 {
registeredProviders, err := az.providersClient.List(to.Int32Ptr(100), "")
if err != nil {
return err
}
if registeredProviders.Value == nil {
return fmt.Errorf("Providers list was nil. subscription=%q", subscriptionID)
}
m := make(map[string]bool)
for _, provider := range *registeredProviders.Value {
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 fmt.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(provider); err != nil {
return err
}
}
}
return 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, fmt.Errorf("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, fmt.Errorf("pkcs8 contained non-RSA key. Expected RSA key")
}
return privatePkcs8RsaKey, nil
}
return nil, fmt.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.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.deploymentOperationsClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.deploymentsClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.deploymentsClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.deploymentOperationsClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.resourcesClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.storageAccountsClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.interfacesClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.groupsClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.providersClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.virtualMachinesClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.virtualMachineScaleSetsClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.disksClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.applicationsClient.ManagementClient.Client.RequestInspector = az.addAcceptLanguages()
az.servicePrincipalsClient.ManagementClient.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
})
}
}