Skip to content

Commit bbf04fa

Browse files
authored
Merge pull request #3 from gordonbondon/cacert_support
Add support for custom CA cert
2 parents d427670 + 01e0538 commit bbf04fa

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ provider "elasticsearch" {
2424
aws_access_key = ""
2525
aws_secret_key = ""
2626
aws_token = "" # if necessary
27+
insecure = true # to bypass certificate check
28+
cacert_file = "/path/to/ca.crt" # when connecting to elastic with self-signed certificate
2729
}
2830
2931
resource "elasticsearch_index_template" "test" {

glide.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provider.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"crypto/tls"
5+
"crypto/x509"
46
"log"
57
"net/http"
68
"net/url"
@@ -9,6 +11,7 @@ import (
911
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
1012
awssigv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
1113
"github.com/deoxxa/aws_signing_client"
14+
"github.com/hashicorp/terraform/helper/pathorcontents"
1215
"github.com/hashicorp/terraform/helper/schema"
1316
"github.com/hashicorp/terraform/terraform"
1417
elastic "gopkg.in/olivere/elastic.v5"
@@ -46,12 +49,26 @@ func Provider() terraform.ResourceProvider {
4649
Default: "",
4750
Description: "The session token for use with AWS Elasticsearch Service domains",
4851
},
52+
53+
"cacert_file": &schema.Schema{
54+
Type: schema.TypeString,
55+
Optional: true,
56+
Default: "",
57+
Description: "A Custom CA certificate",
58+
},
59+
60+
"insecure": &schema.Schema{
61+
Type: schema.TypeBool,
62+
Optional: true,
63+
Default: false,
64+
Description: "Disable SSL verification of API calls",
65+
},
4966
},
5067

5168
ResourcesMap: map[string]*schema.Resource{
5269
"elasticsearch_index_template": resourceElasticsearchIndexTemplate(),
5370
"elasticsearch_snapshot_repository": resourceElasticsearchSnapshotRepository(),
54-
"elasticsearch_kibana_object": resourceElasticsearchKibanaObject(),
71+
"elasticsearch_kibana_object": resourceElasticsearchKibanaObject(),
5572
},
5673

5774
ConfigureFunc: providerConfigure,
@@ -60,6 +77,8 @@ func Provider() terraform.ResourceProvider {
6077

6178
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
6279
rawUrl := d.Get("url").(string)
80+
insecure := d.Get("insecure").(bool)
81+
cacertFile := d.Get("cacert_file").(string)
6382
parsedUrl, err := url.Parse(rawUrl)
6483
if err != nil {
6584
return nil, err
@@ -72,6 +91,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
7291
if m := awsUrlRegexp.FindStringSubmatch(parsedUrl.Hostname()); m != nil {
7392
log.Printf("[INFO] Using AWS: %+v", m[1])
7493
opts = append(opts, elastic.SetHttpClient(awsHttpClient(m[1], d)), elastic.SetSniff(false))
94+
} else if insecure || cacertFile != "" {
95+
opts = append(opts, elastic.SetHttpClient(tlsHttpClient(d)), elastic.SetSniff(false))
7596
}
7697

7798
return elastic.NewClient(opts...)
@@ -93,3 +114,31 @@ func awsHttpClient(region string, d *schema.ResourceData) *http.Client {
93114

94115
return client
95116
}
117+
118+
func tlsHttpClient(d *schema.ResourceData) *http.Client {
119+
insecure := d.Get("insecure").(bool)
120+
cacertFile := d.Get("cacert_file").(string)
121+
122+
// Configure TLS/SSL
123+
tlsConfig := &tls.Config{}
124+
125+
// If a cacertFile has been specified, use that for cert validation
126+
if cacertFile != "" {
127+
caCert, _, _ := pathorcontents.Read(cacertFile)
128+
129+
caCertPool := x509.NewCertPool()
130+
caCertPool.AppendCertsFromPEM([]byte(caCert))
131+
tlsConfig.RootCAs = caCertPool
132+
}
133+
134+
// If configured as insecure, turn off SSL verification
135+
if insecure {
136+
tlsConfig.InsecureSkipVerify = true
137+
}
138+
139+
transport := &http.Transport{TLSClientConfig: tlsConfig}
140+
141+
client := &http.Client{Transport: transport}
142+
143+
return client
144+
}

website/source/docs/providers/elasticsearch/index.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ The following arguments are supported:
6262
* `aws_access_key` - (Optional) The access key for use with AWS Elasticsearch Service domains. It can also be sourced from the `AWS_ACCESS_KEY_ID` environment variable.
6363
* `aws_secret_key` - (Optional) The secret key for use with AWS Elasticsearch Service domains. It can also be sourced from the `AWS_SECRET_ACCESS_KEY` environment variable.
6464
* `aws_token` - (Optional) The session token for use with AWS Elasticsearch Service domains. It can also be sourced from the `AWS_SESSION_TOKEN` environment variable.
65+
* `cacert_file` - (Optional) Specify a custom CA certificate when communicating over SSL. You can specify either a path to the file or the contents of the certificate.
66+
* `insecure` - (Optional) Trust self-signed certificates.

0 commit comments

Comments
 (0)