-
Notifications
You must be signed in to change notification settings - Fork 16
/
azure.go
464 lines (393 loc) · 13.9 KB
/
azure.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azureauth
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/msi/armmsi"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/pluginidentityutil"
"github.com/hashicorp/vault/sdk/helper/pluginutil"
"github.com/hashicorp/vault/sdk/helper/useragent"
"github.com/hashicorp/vault/sdk/logical"
"golang.org/x/oauth2"
"github.com/hashicorp/vault-plugin-auth-azure/client"
)
// https://learn.microsoft.com/en-us/graph/sdks/national-clouds
const (
azurePublicCloudBaseURI = "https://graph.microsoft.com"
azureChinaCloudBaseURI = "https://microsoftgraph.chinacloudapi.cn"
azureUSGovCloudBaseURI = "https://graph.microsoft.us"
azurePublicCloudEnvName = "AZUREPUBLICCLOUD"
azureChinaCloudEnvName = "AZURECHINACLOUD"
azureUSGovCloudEnvName = "AZUREUSGOVERNMENTCLOUD"
)
type provider interface {
TokenVerifier() client.TokenVerifier
ComputeClient(subscriptionID string) (client.ComputeClient, error)
VMSSClient(subscriptionID string) (client.VMSSClient, error)
MSIClient(subscriptionID string) (client.MSIClient, error)
MSGraphClient() (client.MSGraphClient, error)
ResourceClient(subscriptionID string) (client.ResourceClient, error)
ProvidersClient(subscriptionID string) (client.ProvidersClient, error)
}
type azureProvider struct {
oidcVerifier *oidc.IDTokenVerifier
settings *azureSettings
httpClient *http.Client
logger hclog.Logger
systemView logical.SystemView
}
type oidcDiscoveryInfo struct {
Issuer string `json:"issuer"`
JWKSURL string `json:"jwks_uri"`
}
// transporter implements the azure exported.Transporter interface to send HTTP
// requests. This allows us to set our custom http client and user agent.
type transporter struct {
pluginEnv *logical.PluginEnvironment
sender *http.Client
}
func (tp transporter) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", useragent.PluginString(tp.pluginEnv,
userAgentPluginName))
client := tp.sender
// don't attempt redirects so we aren't acting as an unintended network proxy
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func (b *azureAuthBackend) newAzureProvider(ctx context.Context, config *azureConfig) (*azureProvider, error) {
httpClient := cleanhttp.DefaultClient()
settings, err := b.getAzureSettings(ctx, config)
if err != nil {
return nil, err
}
// In many OIDC providers, the discovery endpoint matches the issuer. For Azure AD, the discovery
// endpoint is the AD endpoint which does not match the issuer defined in the discovery payload. This
// makes a request to the discovery URL to determine the issuer and key set information to configure
// the OIDC verifier
discoveryURL := fmt.Sprintf("%s%s/.well-known/openid-configuration", settings.CloudConfig.ActiveDirectoryAuthorityHost, settings.TenantID)
req, err := http.NewRequestWithContext(ctx, "GET", discoveryURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", useragent.PluginString(settings.PluginEnv,
userAgentPluginName))
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read response body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %s", resp.Status, body)
}
var discoveryInfo oidcDiscoveryInfo
if err := json.Unmarshal(body, &discoveryInfo); err != nil {
return nil, fmt.Errorf("unable to unmarshal discovery url: %w", err)
}
// Create a remote key set from the discovery endpoint
keySetCtx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
remoteKeySet := oidc.NewRemoteKeySet(keySetCtx, discoveryInfo.JWKSURL)
verifierConfig := &oidc.Config{
ClientID: settings.Resource,
SupportedSigningAlgs: []string{oidc.RS256},
}
oidcVerifier := oidc.NewVerifier(discoveryInfo.Issuer, remoteKeySet, verifierConfig)
return &azureProvider{
settings: settings,
oidcVerifier: oidcVerifier,
httpClient: httpClient,
logger: b.Logger(),
systemView: b.System(),
}, nil
}
func (p *azureProvider) TokenVerifier() client.TokenVerifier {
return p.oidcVerifier
}
func (p *azureProvider) MSGraphClient() (client.MSGraphClient, error) {
cred, err := p.getTokenCredential()
if err != nil {
return nil, err
}
msGraphAppClient, err := client.NewMSGraphApplicationClient(p.settings.GraphURI, cred)
if err != nil {
return nil, fmt.Errorf("failed to create MS graph client: %w", err)
}
return msGraphAppClient, nil
}
func (p *azureProvider) ComputeClient(subscriptionID string) (client.ComputeClient, error) {
cred, err := p.getTokenCredential()
if err != nil {
return nil, err
}
clientOptions := p.getClientOptions()
client, err := armcompute.NewVirtualMachinesClient(subscriptionID, cred, clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to create virtual machines client: %w", err)
}
return client, nil
}
func (p *azureProvider) VMSSClient(subscriptionID string) (client.VMSSClient, error) {
cred, err := p.getTokenCredential()
if err != nil {
return nil, err
}
clientOptions := p.getClientOptions()
client, err := armcompute.NewVirtualMachineScaleSetsClient(subscriptionID, cred, clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to create virtual machine scale sets client: %w", err)
}
return client, nil
}
func (p *azureProvider) MSIClient(subscriptionID string) (client.MSIClient, error) {
cred, err := p.getTokenCredential()
if err != nil {
return nil, err
}
clientOptions := p.getClientOptions()
client, err := armmsi.NewUserAssignedIdentitiesClient(subscriptionID, cred, clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to create user assigned identity client: %w", err)
}
return client, nil
}
func (p *azureProvider) ProvidersClient(subscriptionID string) (client.ProvidersClient, error) {
cred, err := p.getTokenCredential()
if err != nil {
return nil, err
}
clientOptions := p.getClientOptions()
client, err := armresources.NewProvidersClient(subscriptionID, cred, clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to create providers client: %w", err)
}
return client, nil
}
func (p *azureProvider) ResourceClient(subscriptionID string) (client.ResourceClient, error) {
cred, err := p.getTokenCredential()
if err != nil {
return nil, err
}
clientOptions := p.getClientOptions()
client, err := armresources.NewClient(subscriptionID, cred, clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to create resource client: %w", err)
}
return client, nil
}
func (p *azureProvider) getClientOptions() *arm.ClientOptions {
return &arm.ClientOptions{
ClientOptions: policy.ClientOptions{
Cloud: p.settings.CloudConfig,
Transport: transporter{
pluginEnv: p.settings.PluginEnv,
sender: p.httpClient,
},
Retry: policy.RetryOptions{
MaxRetries: p.settings.MaxRetries,
MaxRetryDelay: p.settings.MaxRetryDelay,
RetryDelay: p.settings.RetryDelay,
},
},
}
}
func (p *azureProvider) getTokenCredential() (azcore.TokenCredential, error) {
clientCloudOpts := azcore.ClientOptions{Cloud: p.settings.CloudConfig}
if p.settings.ClientSecret != "" {
options := &azidentity.ClientSecretCredentialOptions{
ClientOptions: clientCloudOpts,
}
cred, err := azidentity.NewClientSecretCredential(p.settings.TenantID, p.settings.ClientID,
p.settings.ClientSecret, options)
if err != nil {
return nil, fmt.Errorf("failed to create client secret token credential: %w", err)
}
return cred, nil
}
if p.settings.IdentityTokenAudience != "" {
options := &azidentity.ClientAssertionCredentialOptions{
ClientOptions: clientCloudOpts,
}
getAssertion := getAssertionFunc(p.logger, p.systemView, p.settings)
cred, err := azidentity.NewClientAssertionCredential(
p.settings.TenantID,
p.settings.ClientID,
getAssertion,
options,
)
if err != nil {
return nil, fmt.Errorf("failed to create client assertion credential: %w", err)
}
return cred, nil
}
// Fall back to using managed service identity
options := &azidentity.ManagedIdentityCredentialOptions{
ClientOptions: clientCloudOpts,
}
cred, err := azidentity.NewManagedIdentityCredential(options)
if err != nil {
return nil, fmt.Errorf("failed to create managed identity token credential: %w", err)
}
return cred, nil
}
type getAssertion func(context.Context) (string, error)
func getAssertionFunc(logger hclog.Logger, sys logical.SystemView, s *azureSettings) getAssertion {
return func(ctx context.Context) (string, error) {
req := &pluginutil.IdentityTokenRequest{
Audience: s.IdentityTokenAudience,
TTL: s.IdentityTokenTTL * time.Second,
}
resp, err := sys.GenerateIdentityToken(ctx, req)
if err != nil {
return "", fmt.Errorf("failed to generate plugin identity token: %w", err)
}
logger.Info("fetched new plugin identity token")
if resp.TTL < req.TTL {
logger.Debug("generated plugin identity token has shorter TTL than requested",
"requested", req.TTL, "actual", resp.TTL)
}
return resp.Token.Token(), nil
}
}
type azureSettings struct {
pluginidentityutil.PluginIdentityTokenParams
TenantID string
ClientID string
ClientSecret string
CloudConfig cloud.Configuration
GraphURI string
Resource string
PluginEnv *logical.PluginEnvironment
MaxRetries int32
MaxRetryDelay time.Duration
RetryDelay time.Duration
}
func (b *azureAuthBackend) getAzureSettings(ctx context.Context, config *azureConfig) (*azureSettings, error) {
settings := &azureSettings{
MaxRetries: config.MaxRetries,
MaxRetryDelay: config.MaxRetryDelay,
RetryDelay: config.RetryDelay,
}
envTenantID := os.Getenv("AZURE_TENANT_ID")
switch {
case envTenantID != "":
settings.TenantID = envTenantID
case config.TenantID != "":
settings.TenantID = config.TenantID
default:
return nil, errors.New("tenant_id is required")
}
envResource := os.Getenv("AZURE_AD_RESOURCE")
switch {
case envResource != "":
settings.Resource = envResource
case config.Resource != "":
settings.Resource = config.Resource
default:
return nil, errors.New("resource is required")
}
clientID := os.Getenv("AZURE_CLIENT_ID")
if clientID == "" {
clientID = config.ClientID
}
settings.ClientID = clientID
clientSecret := os.Getenv("AZURE_CLIENT_SECRET")
if clientSecret == "" {
clientSecret = config.ClientSecret
}
settings.ClientSecret = clientSecret
settings.IdentityTokenAudience = config.IdentityTokenAudience
settings.IdentityTokenTTL = config.IdentityTokenTTL
environment := os.Getenv("AZURE_ENVIRONMENT")
if environment == "" {
// set environment from config
environment = config.Environment
}
if environment == "" {
// Default to Azure public cloud
settings.CloudConfig = cloud.AzurePublic
settings.GraphURI = azurePublicCloudBaseURI
} else {
var err error
settings.CloudConfig, err = cloudConfigFromName(environment)
if err != nil {
return nil, err
}
settings.GraphURI, err = graphURIFromName(environment)
if err != nil {
return nil, err
}
}
pluginEnv, err := b.System().PluginEnv(ctx)
if err != nil {
b.Logger().Warn("failed to read plugin environment, user-agent will not be set",
"error", err)
}
settings.PluginEnv = pluginEnv
return settings, nil
}
func cloudConfigFromName(name string) (cloud.Configuration, error) {
configs := map[string]cloud.Configuration{
azureChinaCloudEnvName: cloud.AzureChina,
azurePublicCloudEnvName: cloud.AzurePublic,
azureUSGovCloudEnvName: cloud.AzureGovernment,
}
name = strings.ToUpper(name)
c, ok := configs[name]
if !ok {
return c, fmt.Errorf("err: no cloud configuration matching the name %q", name)
}
return c, nil
}
func graphURIFromName(name string) (string, error) {
configs := map[string]string{
azureChinaCloudEnvName: azureChinaCloudBaseURI,
azurePublicCloudEnvName: azurePublicCloudBaseURI,
azureUSGovCloudEnvName: azureUSGovCloudBaseURI,
}
name = strings.ToUpper(name)
c, ok := configs[name]
if !ok {
return c, fmt.Errorf("err: no MS Graph URI matching the name %q", name)
}
return c, nil
}
// guidRx from https://learn.microsoft.com/en-us/rest/api/defenderforcloud/tasks/get-subscription-level-task
var guidRx = regexp.MustCompile(`^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$`) // just a uuid
// nameRx based on https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.VM.Name/#description
var nameRx = regexp.MustCompile(`^[a-zA-Z]$|^[a-zA-Z][a-zA-Z0-9.\-_]*[a-zA-Z0-9_]$`) // alphanumeric, doesn't start with a number, at least 1 character, doesn't end with a . or -
// https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.ResourceGroup.Name/ and https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules
// The latter documentation specifically allows characters in unicode letter/digit categories, which is wider than a-zA-Z0-9.
var rgRx = regexp.MustCompile(`^[\-_.()\pL\pN]*[\-_()\pL\pN]$`)
// verify the field provided matches Azure's requirements
// (see: https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules).
func validateAzureField(regex *regexp.Regexp, value string) bool {
return regex.MatchString(value)
}