-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathprovider.go
752 lines (691 loc) · 27.9 KB
/
provider.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
package es
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
awsstscreds "github.com/aws/aws-sdk-go/aws/credentials/stscreds"
awssession "github.com/aws/aws-sdk-go/aws/session"
awssigv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
awssts "github.com/aws/aws-sdk-go/service/sts"
"github.com/deoxxa/aws_signing_client"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
elastic7 "github.com/olivere/elastic/v7"
elastic6 "gopkg.in/olivere/elastic.v6"
)
type ServerFlavor int64
// e.g. elasticsearch, opensearch, elasticsearch-oss, etc.
const (
Unknown ServerFlavor = iota
Elasticsearch
ElasticsearchOpenSource
OpenSearch
)
var awsUrlRegexp = regexp.MustCompile(`([a-z0-9-]+).es.amazonaws.com$`)
type ProviderConf struct {
rawUrl string
insecure bool
sniffing bool
healthchecking bool
cacertFile string
username string
password string
token string
tokenName string
parsedUrl *url.URL
signAWSRequests bool
esVersion string
pingTimeoutSeconds int
awsRegion string
awsAssumeRoleArn string
awsAssumeRoleExternalID string
awsAssumeRoleSessionName string
awsAccessKeyId string
awsSecretAccessKey string
awsSessionToken string
awsSig4Service string
awsProfile string
certPemPath string
keyPemPath string
kibanaUrl string
hostOverride string
// determined after connecting to the server
flavor ServerFlavor
}
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_URL", nil),
Description: "Elasticsearch URL",
},
"kibana_url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KIBANA_URL", nil),
Description: "URL to reach the Kibana API",
},
"sniff": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_SNIFF", false),
Description: "Set the node sniffing option for the elastic client. Client won't work with sniffing if nodes are not routable.",
},
"healthcheck": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_HEALTH", true),
Description: "Set the client healthcheck option for the elastic client. Healthchecking is designed for direct access to the cluster.",
},
"username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_USERNAME", nil),
Description: "Username to use to connect to elasticsearch using basic auth",
},
"password": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_PASSWORD", nil),
Description: "Password to use to connect to elasticsearch using basic auth",
},
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_TOKEN", nil),
Description: "A bearer token or ApiKey for an Authorization header, e.g. Active Directory API key.",
},
"token_name": {
Type: schema.TypeString,
Optional: true,
Default: "ApiKey",
Description: "The type of token, usually ApiKey or Bearer",
},
"aws_assume_role_arn": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Amazon Resource Name of an IAM Role to assume prior to making AWS API calls.",
},
"aws_assume_role_external_id": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "External ID configured in the IAM policy of the IAM Role to assume prior to making AWS API calls.",
},
"aws_assume_role_session_name": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "AWS IAM session name to use when assuming a role.",
},
"aws_access_key": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The access key for use with AWS Elasticsearch Service domains",
},
"aws_secret_key": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The secret key for use with AWS Elasticsearch Service domains",
},
"aws_token": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The session token for use with AWS Elasticsearch Service domains",
},
"aws_profile": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The AWS profile for use with AWS Elasticsearch Service domains",
},
"aws_region": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The AWS region for use in signing of AWS elasticsearch requests. Must be specified in order to use AWS URL signing with AWS ElasticSearch endpoint exposed on a custom DNS domain.",
},
"cacert_file": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A Custom CA certificate",
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Disable SSL verification of API calls",
},
"client_cert_path": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A X509 certificate to connect to elasticsearch",
DefaultFunc: schema.EnvDefaultFunc("ES_CLIENT_CERTIFICATE_PATH", ""),
},
"client_key_path": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A X509 key to connect to elasticsearch",
DefaultFunc: schema.EnvDefaultFunc("ES_CLIENT_KEY_PATH", ""),
},
"sign_aws_requests": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Enable signing of AWS elasticsearch requests. The `url` must refer to AWS ES domain (`*.<region>.es.amazonaws.com`), or `aws_region` must be specified explicitly.",
},
"aws_signature_service": {
Type: schema.TypeString,
Optional: true,
Default: "es",
Description: "AWS service name used in the credential scope of signed requests to ElasticSearch.",
},
"elasticsearch_version": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "ElasticSearch Version",
},
// version_ping_timeout is the time the ping to check the cluster
// version waits for a response from Elasticsearch on startup, e.g. when
// creating a provider.
"version_ping_timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 5,
Description: "Version ping timeout in seconds",
},
"host_override": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "If provided, sets the 'Host' header of requests and the 'ServerName' for certificate validation to this value. See the documentation on connecting to Elasticsearch via an SSH tunnel.",
},
},
ResourcesMap: map[string]*schema.Resource{
"elasticsearch_cluster_settings": resourceElasticsearchClusterSettings(),
"elasticsearch_component_template": resourceElasticsearchComponentTemplate(),
"elasticsearch_composable_index_template": resourceElasticsearchComposableIndexTemplate(),
"elasticsearch_data_stream": resourceElasticsearchDataStream(),
"elasticsearch_index_template": resourceElasticsearchIndexTemplate(),
"elasticsearch_index": resourceElasticsearchIndex(),
"elasticsearch_ingest_pipeline": resourceElasticsearchIngestPipeline(),
"elasticsearch_kibana_alert": resourceElasticsearchKibanaAlert(),
"elasticsearch_kibana_object": resourceElasticsearchKibanaObject(),
"elasticsearch_opendistro_destination": resourceElasticsearchOpenDistroDestination(),
"elasticsearch_opendistro_ism_policy_mapping": resourceElasticsearchOpenDistroISMPolicyMapping(),
"elasticsearch_opendistro_ism_policy": resourceElasticsearchOpenDistroISMPolicy(),
"elasticsearch_opendistro_kibana_tenant": resourceElasticsearchOpenDistroKibanaTenant(),
"elasticsearch_opendistro_monitor": resourceElasticsearchOpenDistroMonitor(),
"elasticsearch_opendistro_role": resourceElasticsearchOpenDistroRole(),
"elasticsearch_opendistro_roles_mapping": resourceElasticsearchOpenDistroRolesMapping(),
"elasticsearch_opendistro_user": resourceElasticsearchOpenDistroUser(),
"elasticsearch_opensearch_audit_config": resourceOpenSearchAuditConfig(),
"elasticsearch_opensearch_destination": resourceOpenSearchDestination(),
"elasticsearch_opensearch_ism_policy_mapping": resourceOpenSearchISMPolicyMapping(),
"elasticsearch_opensearch_ism_policy": resourceOpenSearchISMPolicy(),
"elasticsearch_opensearch_kibana_tenant": resourceOpenSearchKibanaTenant(),
"elasticsearch_opensearch_monitor": resourceOpenSearchMonitor(),
"elasticsearch_opensearch_role": resourceOpenSearchRole(),
"elasticsearch_opensearch_roles_mapping": resourceOpenSearchRolesMapping(),
"elasticsearch_opensearch_user": resourceOpenSearchUser(),
"elasticsearch_script": resourceElasticsearchScript(),
"elasticsearch_snapshot_repository": resourceElasticsearchSnapshotRepository(),
"elasticsearch_xpack_index_lifecycle_policy": resourceElasticsearchXpackIndexLifecyclePolicy(),
"elasticsearch_xpack_license": resourceElasticsearchXpackLicense(),
"elasticsearch_xpack_role_mapping": resourceElasticsearchXpackRoleMapping(),
"elasticsearch_xpack_role": resourceElasticsearchXpackRole(),
"elasticsearch_xpack_snapshot_lifecycle_policy": resourceElasticsearchXpackSnapshotLifecyclePolicy(),
"elasticsearch_xpack_user": resourceElasticsearchXpackUser(),
"elasticsearch_xpack_watch": resourceElasticsearchXpackWatch(),
},
DataSourcesMap: map[string]*schema.Resource{
"elasticsearch_host": dataSourceElasticsearchHost(),
"elasticsearch_opendistro_destination": dataSourceElasticsearchOpenDistroDestination(),
"elasticsearch_opensearch_destination": dataSourceOpenSearchDestination(),
},
ConfigureContextFunc: providerConfigure,
}
}
func providerConfigure(c context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
rawUrl := d.Get("url").(string)
parsedUrl, err := url.Parse(rawUrl)
if err != nil {
return nil, diag.FromErr(err)
}
return &ProviderConf{
rawUrl: rawUrl,
kibanaUrl: d.Get("kibana_url").(string),
insecure: d.Get("insecure").(bool),
sniffing: d.Get("sniff").(bool),
healthchecking: d.Get("healthcheck").(bool),
cacertFile: d.Get("cacert_file").(string),
username: d.Get("username").(string),
password: d.Get("password").(string),
token: d.Get("token").(string),
tokenName: d.Get("token_name").(string),
parsedUrl: parsedUrl,
signAWSRequests: d.Get("sign_aws_requests").(bool),
awsSig4Service: d.Get("aws_signature_service").(string),
esVersion: d.Get("elasticsearch_version").(string),
pingTimeoutSeconds: d.Get("version_ping_timeout").(int),
awsRegion: d.Get("aws_region").(string),
awsAssumeRoleArn: d.Get("aws_assume_role_arn").(string),
awsAssumeRoleExternalID: d.Get("aws_assume_role_external_id").(string),
awsAssumeRoleSessionName: d.Get("aws_assume_role_session_name").(string),
awsAccessKeyId: d.Get("aws_access_key").(string),
awsSecretAccessKey: d.Get("aws_secret_key").(string),
awsSessionToken: d.Get("aws_token").(string),
awsProfile: d.Get("aws_profile").(string),
certPemPath: d.Get("client_cert_path").(string),
keyPemPath: d.Get("client_key_path").(string),
hostOverride: d.Get("host_override").(string),
}, nil
}
func getClient(conf *ProviderConf) (interface{}, error) {
opts := []elastic7.ClientOptionFunc{
elastic7.SetURL(conf.rawUrl),
elastic7.SetScheme(conf.parsedUrl.Scheme),
elastic7.SetSniff(conf.sniffing),
elastic7.SetHealthcheck(conf.healthchecking),
}
if conf.parsedUrl.User.Username() != "" {
p, _ := conf.parsedUrl.User.Password()
opts = append(opts, elastic7.SetBasicAuth(conf.parsedUrl.User.Username(), p))
}
if conf.username != "" && conf.password != "" {
opts = append(opts, elastic7.SetBasicAuth(conf.username, conf.password))
}
if m := awsUrlRegexp.FindStringSubmatch(conf.parsedUrl.Hostname()); m != nil && conf.signAWSRequests {
log.Printf("[INFO] Using AWS: %+v", m[1])
client, err := awsHttpClient(m[1], conf, map[string]string{})
if err != nil {
return nil, err
}
opts = append(opts, elastic7.SetHttpClient(client), elastic7.SetSniff(false))
} else if awsRegion := conf.awsRegion; conf.awsRegion != "" && conf.signAWSRequests {
log.Printf("[INFO] Using AWS: %+v", awsRegion)
client, err := awsHttpClient(awsRegion, conf, map[string]string{})
if err != nil {
return nil, err
}
opts = append(opts, elastic7.SetHttpClient(client), elastic7.SetSniff(false))
} else if conf.insecure || conf.cacertFile != "" {
opts = append(opts, elastic7.SetHttpClient(tlsHttpClient(conf, map[string]string{})), elastic7.SetSniff(false))
} else if conf.token != "" {
opts = append(opts, elastic7.SetHttpClient(tokenHttpClient(conf, map[string]string{})), elastic7.SetSniff(false))
} else {
opts = append(opts, elastic7.SetHttpClient(defaultHttpClient(conf, map[string]string{})))
}
logProviderLevel, ok := os.LookupEnv("TF_LOG_PROVIDER")
if !ok {
logProviderLevel = "ERROR"
}
logProviderLevel = strings.ToUpper(logProviderLevel)
esLogger := hclog.New(&hclog.LoggerOptions{
Level: hclog.LevelFromString(logProviderLevel),
Output: os.Stderr,
JSONFormat: true,
})
switch logProviderLevel {
case "TRACE":
traceLogger := esLogger.StandardLogger(&hclog.StandardLoggerOptions{
ForceLevel: hclog.LevelFromString("TRACE"),
})
opts = append(opts, elastic7.SetTraceLog(traceLogger))
fallthrough
case "INFO":
infoLogger := esLogger.StandardLogger(&hclog.StandardLoggerOptions{
ForceLevel: hclog.LevelFromString("INFO"),
})
opts = append(opts, elastic7.SetInfoLog(infoLogger))
fallthrough
default:
errorLogger := esLogger.StandardLogger(&hclog.StandardLoggerOptions{
ForceLevel: hclog.LevelFromString("ERROR"),
})
opts = append(opts, elastic7.SetErrorLog(errorLogger))
}
var relevantClient interface{}
client, err := elastic7.NewClient(opts...)
if err != nil {
if errors.Is(err, elastic7.ErrNoClient) {
log.Printf("[INFO] couldn't create client: %T, %s, %T", err, err.Error(), errors.Unwrap(err))
return nil, errors.New("HEAD healthcheck failed: This is usually due to network or permission issues. The underlying error isn't accessible, please debug by disabling healthchecks.")
}
return nil, err
}
relevantClient = client
// Use the v7 client to ping the cluster to determine the version if one was not provided
if conf.esVersion == "" {
log.Printf("[INFO] Pinging url to determine version %+v with timeout %ds", conf.rawUrl, conf.pingTimeoutSeconds)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.pingTimeoutSeconds)*time.Second)
defer cancel()
info, httpStatus, err := client.Ping(conf.rawUrl).Do(ctx)
if httpStatus == http.StatusForbidden {
return nil, errors.New("HTTP 403 Forbidden: Permission denied. Please ensure that the correct credentials are being used to access the cluster.")
}
if err != nil {
// Replace the timeout error because it gives no context
if os.IsTimeout(err) {
err = fmt.Errorf("timeout after %d seconds while pinging '%+v' to determine server version, please consider setting 'elasticsearch_version' to avoid this lookup", conf.pingTimeoutSeconds, conf.rawUrl)
}
return nil, err
}
conf.esVersion = info.Version.Number
// if upstream library exposes support for OpenSearch's distribution
// param, we can use that as well
log.Printf("[INFO] ES version %+v", info.Version)
switch info.Version.BuildFlavor {
case "default":
conf.flavor = Elasticsearch
case "oss":
conf.flavor = ElasticsearchOpenSource
}
}
if conf.esVersion < "7.0.0" && conf.esVersion >= "6.0.0" {
log.Printf("[INFO] Using ES 6")
opts := []elastic6.ClientOptionFunc{
elastic6.SetURL(conf.rawUrl),
elastic6.SetScheme(conf.parsedUrl.Scheme),
elastic6.SetSniff(conf.sniffing),
elastic6.SetHealthcheck(conf.healthchecking),
}
if conf.parsedUrl.User.Username() != "" {
p, _ := conf.parsedUrl.User.Password()
opts = append(opts, elastic6.SetBasicAuth(conf.parsedUrl.User.Username(), p))
}
if conf.username != "" && conf.password != "" {
opts = append(opts, elastic6.SetBasicAuth(conf.username, conf.password))
}
if m := awsUrlRegexp.FindStringSubmatch(conf.parsedUrl.Hostname()); m != nil && conf.signAWSRequests {
log.Printf("[INFO] Using AWS: %+v", m[1])
client, err := awsHttpClient(m[1], conf, map[string]string{})
if err != nil {
return nil, err
}
opts = append(opts, elastic6.SetHttpClient(client), elastic6.SetSniff(false))
} else if awsRegion := conf.awsRegion; conf.awsRegion != "" && conf.signAWSRequests {
log.Printf("[INFO] Using AWS: %+v", conf.awsRegion)
client, err := awsHttpClient(awsRegion, conf, map[string]string{})
if err != nil {
return nil, err
}
opts = append(opts, elastic6.SetHttpClient(client), elastic6.SetSniff(false))
} else if conf.insecure || conf.cacertFile != "" {
opts = append(opts, elastic6.SetHttpClient(tlsHttpClient(conf, map[string]string{})), elastic6.SetSniff(false))
} else if conf.token != "" {
opts = append(opts, elastic6.SetHttpClient(tokenHttpClient(conf, map[string]string{})), elastic6.SetSniff(false))
} else {
opts = append(opts, elastic6.SetHttpClient(defaultHttpClient(conf, map[string]string{})))
}
switch logProviderLevel {
case "TRACE":
traceLogger := esLogger.StandardLogger(&hclog.StandardLoggerOptions{
ForceLevel: hclog.LevelFromString("TRACE"),
})
opts = append(opts, elastic6.SetTraceLog(traceLogger))
fallthrough
case "INFO":
infoLogger := esLogger.StandardLogger(&hclog.StandardLoggerOptions{
ForceLevel: hclog.LevelFromString("INFO"),
})
opts = append(opts, elastic6.SetInfoLog(infoLogger))
fallthrough
default:
errorLogger := esLogger.StandardLogger(&hclog.StandardLoggerOptions{
ForceLevel: hclog.LevelFromString("ERROR"),
})
opts = append(opts, elastic6.SetErrorLog(errorLogger))
}
relevantClient, err = elastic6.NewClient(opts...)
if err != nil {
return nil, err
}
} else if conf.flavor == Unknown && conf.esVersion < "2.0.0" && conf.esVersion >= "1.0.0" {
// Version 1.x of OpenSearch very likely. Nothing to do since it's API
// compatible with 7.x of ES. If elastic client library supports detecting
// flavor, update to Opensearch.
} else if conf.esVersion < "6.0.0" {
return nil, fmt.Errorf("ElasticSearch version %s is older than 6.0.0 and is not supported, flavor: %v.", conf.esVersion, conf.flavor)
}
return relevantClient, nil
}
func getKibanaClient(conf *ProviderConf) (interface{}, error) {
// use either the provided version of elasticsearch or the version of
// elasticsearch determined by pinging the cluster. Base AWS or other auth
// off of the same ES config
esClient, err := getClient(conf)
if err != nil {
return nil, err
}
switch esClient.(type) {
case *elastic7.Client:
opts := []elastic7.ClientOptionFunc{
elastic7.SetURL(conf.kibanaUrl),
elastic7.SetScheme(conf.parsedUrl.Scheme),
// kibana api does not support sniff/health check
elastic7.SetSniff(false),
elastic7.SetHealthcheck(false),
}
if conf.parsedUrl.User.Username() != "" {
p, _ := conf.parsedUrl.User.Password()
opts = append(opts, elastic7.SetBasicAuth(conf.parsedUrl.User.Username(), p))
}
if conf.username != "" && conf.password != "" {
opts = append(opts, elastic7.SetBasicAuth(conf.username, conf.password))
}
headers := map[string]string{"kbn-xsrf": "true"}
if m := awsUrlRegexp.FindStringSubmatch(conf.parsedUrl.Hostname()); m != nil && conf.signAWSRequests {
log.Printf("[INFO] Using AWS: %+v", m[1])
client, err := awsHttpClient(m[1], conf, headers)
if err != nil {
return nil, err
}
opts = append(opts, elastic7.SetHttpClient(client), elastic7.SetSniff(false))
} else if awsRegion := conf.awsRegion; conf.awsRegion != "" && conf.signAWSRequests {
log.Printf("[INFO] Using AWS: %+v", awsRegion)
client, err := awsHttpClient(awsRegion, conf, headers)
if err != nil {
return nil, err
}
opts = append(opts, elastic7.SetHttpClient(client), elastic7.SetSniff(false))
} else if conf.insecure || conf.cacertFile != "" {
opts = append(opts, elastic7.SetHttpClient(tlsHttpClient(conf, headers)))
} else if conf.token != "" {
opts = append(opts, elastic7.SetHttpClient(tokenHttpClient(conf, headers)), elastic7.SetSniff(false))
} else {
opts = append(opts, elastic7.SetHttpClient(defaultHttpClient(conf, headers)))
}
return elastic7.NewClient(opts...)
case *elastic6.Client:
return nil, errors.New("ElasticSearch is older than 6.0.0!")
default:
return nil, errors.New("ElasticSearch is older than 5.0.0!")
}
}
func assumeRoleCredentials(region, roleARN, roleExternalID, roleSessionName, profile string) *awscredentials.Credentials {
sessOpts := awsSessionOptions(region)
sessOpts.Profile = profile
sess := awssession.Must(awssession.NewSessionWithOptions(sessOpts))
stsClient := awssts.New(sess)
assumeRoleProvider := &awsstscreds.AssumeRoleProvider{
Client: stsClient,
RoleARN: roleARN,
RoleSessionName: roleSessionName,
ExternalID: aws.String(roleExternalID),
}
return awscredentials.NewChainCredentials([]awscredentials.Provider{assumeRoleProvider})
}
func awsSessionOptions(region string) awssession.Options {
return awssession.Options{
Config: aws.Config{
Region: aws.String(region),
LogLevel: aws.LogLevel(aws.LogDebugWithHTTPBody),
Logger: aws.LoggerFunc(func(args ...interface{}) {
log.Print(append([]interface{}{"[DEBUG] "}, args...))
}),
CredentialsChainVerboseErrors: aws.Bool(true),
MaxRetries: aws.Int(1),
// HTTP client is required to fetch EC2 metadata values
// having zero timeout on the default HTTP client sometimes makes
// it fail with Credential error
// https://github.com/aws/aws-sdk-go/issues/2914
HTTPClient: &http.Client{Timeout: 10 * time.Second},
},
SharedConfigState: awssession.SharedConfigEnable,
}
}
func awsSession(region string, conf *ProviderConf) *awssession.Session {
sessOpts := awsSessionOptions(region)
// 1. access keys take priority
// 2. next is an assume role configuration
// 2.b check if the role external ID is set and use it
// 3. followed by a profile (for assume role)
// 4. let the default credentials provider figure out the rest (env, ec2, etc..)
//
// note: if #1 is chosen, then no further providers will be tested, since we've overridden the credentials with just a static provider
if conf.awsAccessKeyId != "" {
sessOpts.Config.Credentials = awscredentials.NewStaticCredentials(conf.awsAccessKeyId, conf.awsSecretAccessKey, conf.awsSessionToken)
} else if conf.awsAssumeRoleArn != "" {
if conf.awsAssumeRoleExternalID == "" {
conf.awsAssumeRoleExternalID = ""
}
sessOpts.Config.Credentials = assumeRoleCredentials(region, conf.awsAssumeRoleArn, conf.awsAssumeRoleExternalID, conf.awsAssumeRoleSessionName, conf.awsProfile)
} else if conf.awsProfile != "" {
sessOpts.Profile = conf.awsProfile
}
// If configured as insecure, turn off SSL verification
if conf.insecure {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
sessOpts.Config.HTTPClient = client
} else if conf.hostOverride != "" {
// Only use `host_override` to set `ServerName` if we're using a secure connection
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{ServerName: conf.hostOverride},
}}
sessOpts.Config.HTTPClient = client
}
return awssession.Must(awssession.NewSessionWithOptions(sessOpts))
}
func awsHttpClient(region string, conf *ProviderConf, headers map[string]string) (*http.Client, error) {
session := awsSession(region, conf)
// Call Get() to ensure concurrency safe retrieval of credentials. Since the
// client is created in many go routines, this synchronizes it.
_, err := session.Config.Credentials.Get()
if err != nil {
return nil, err
}
signer := awssigv4.NewSigner(session.Config.Credentials)
client, err := aws_signing_client.New(signer, session.Config.HTTPClient, conf.awsSig4Service, region)
if err != nil {
return nil, err
}
rt := WithHeader(client.Transport)
rt.hostOverride = conf.hostOverride
for k, v := range headers {
rt.Set(k, v)
}
client.Transport = rt
return client, nil
}
func tokenHttpClient(conf *ProviderConf, headers map[string]string) *http.Client {
// Setup TLS options
tlsConfig := &tls.Config{}
if conf.insecure {
tlsConfig.InsecureSkipVerify = true
} else if conf.hostOverride != "" {
tlsConfig.ServerName = conf.hostOverride
}
// Wrapper to inject headers as needed
transport := &http.Transport{TLSClientConfig: tlsConfig}
rt := WithHeader(transport)
rt.hostOverride = conf.hostOverride
rt.Set("Authorization", fmt.Sprintf("%s %s", conf.tokenName, conf.token))
for k, v := range headers {
rt.Set(k, v)
}
client := &http.Client{Transport: rt}
return client
}
func tlsHttpClient(conf *ProviderConf, headers map[string]string) *http.Client {
// Configure TLS/SSL
tlsConfig := &tls.Config{}
if conf.certPemPath != "" && conf.keyPemPath != "" {
certPem, _, err := readPathOrContent(conf.certPemPath)
if err != nil {
log.Fatal(err)
}
keyPem, _, err := readPathOrContent(conf.keyPemPath)
if err != nil {
log.Fatal(err)
}
cert, err := tls.X509KeyPair([]byte(certPem), []byte(keyPem))
if err != nil {
log.Fatal(err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
// If a cacertFile has been specified, use that for cert validation
if conf.cacertFile != "" {
caCert, _, _ := readPathOrContent(conf.cacertFile)
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
tlsConfig.RootCAs = caCertPool
}
// If configured as insecure, turn off SSL verification
if conf.insecure {
tlsConfig.InsecureSkipVerify = true
} else if conf.hostOverride != "" {
tlsConfig.ServerName = conf.hostOverride
}
transport := &http.Transport{TLSClientConfig: tlsConfig}
rt := WithHeader(transport)
rt.hostOverride = conf.hostOverride
for k, v := range headers {
rt.Set(k, v)
}
client := &http.Client{Transport: rt}
return client
}
func defaultHttpClient(conf *ProviderConf, headers map[string]string) *http.Client {
// Setup TLS options
tlsConfig := &tls.Config{}
if conf.insecure {
tlsConfig.InsecureSkipVerify = true
} else if conf.hostOverride != "" {
tlsConfig.ServerName = conf.hostOverride
}
// Wrapper to inject headers as needed
transport := &http.Transport{TLSClientConfig: tlsConfig}
rt := WithHeader(transport)
rt.hostOverride = conf.hostOverride
for k, v := range headers {
rt.Set(k, v)
}
client := &http.Client{Transport: rt}
return client
}