Skip to content

Commit

Permalink
Merge pull request #3 from gordonbondon/cacert_support
Browse files Browse the repository at this point in the history
Add support for custom CA cert
  • Loading branch information
phillbaker authored Dec 5, 2017
2 parents d427670 + 01e0538 commit bbf04fa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ provider "elasticsearch" {
aws_access_key = ""
aws_secret_key = ""
aws_token = "" # if necessary
insecure = true # to bypass certificate check
cacert_file = "/path/to/ca.crt" # when connecting to elastic with self-signed certificate
}
resource "elasticsearch_index_template" "test" {
Expand Down
3 changes: 2 additions & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 50 additions & 1 deletion provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"crypto/tls"
"crypto/x509"
"log"
"net/http"
"net/url"
Expand All @@ -9,6 +11,7 @@ import (
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
awssigv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/deoxxa/aws_signing_client"
"github.com/hashicorp/terraform/helper/pathorcontents"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
elastic "gopkg.in/olivere/elastic.v5"
Expand Down Expand Up @@ -46,12 +49,26 @@ func Provider() terraform.ResourceProvider {
Default: "",
Description: "The session token for use with AWS Elasticsearch Service domains",
},

"cacert_file": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A Custom CA certificate",
},

"insecure": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Disable SSL verification of API calls",
},
},

ResourcesMap: map[string]*schema.Resource{
"elasticsearch_index_template": resourceElasticsearchIndexTemplate(),
"elasticsearch_snapshot_repository": resourceElasticsearchSnapshotRepository(),
"elasticsearch_kibana_object": resourceElasticsearchKibanaObject(),
"elasticsearch_kibana_object": resourceElasticsearchKibanaObject(),
},

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

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

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

return client
}

func tlsHttpClient(d *schema.ResourceData) *http.Client {
insecure := d.Get("insecure").(bool)
cacertFile := d.Get("cacert_file").(string)

// Configure TLS/SSL
tlsConfig := &tls.Config{}

// If a cacertFile has been specified, use that for cert validation
if cacertFile != "" {
caCert, _, _ := pathorcontents.Read(cacertFile)

caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
tlsConfig.RootCAs = caCertPool
}

// If configured as insecure, turn off SSL verification
if insecure {
tlsConfig.InsecureSkipVerify = true
}

transport := &http.Transport{TLSClientConfig: tlsConfig}

client := &http.Client{Transport: transport}

return client
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ The following arguments are supported:
* `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.
* `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.
* `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.
* `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.
* `insecure` - (Optional) Trust self-signed certificates.

0 comments on commit bbf04fa

Please sign in to comment.