Skip to content

add "ca_file" option to the elasticstack provider #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data-sources/elasticsearch_security_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ output "user" {

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/elasticsearch_snapshot_repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ output "repo_url" {

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ provider "elasticstack" {

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A comma-separated list of endpoints where the terraform provider will point to, this must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) Password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_cluster_settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ resource "elasticstack_elasticsearch_cluster_settings" "my_cluster_settings" {

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_index_lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Required:

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_index_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Optional:

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_security_role.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Required:

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_security_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ resource "elasticstack_elasticsearch_security_user" "dev" {

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_snapshot_lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ resource "elasticstack_elasticsearch_snapshot_lifecycle" "slm_policy" {

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_snapshot_repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Optional:

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
Expand Down
21 changes: 21 additions & 0 deletions internal/clients/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -89,6 +90,19 @@ func NewApiClientFunc(version string, p *schema.Provider) func(context.Context,
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
config.Transport = tr
}

if caFile, ok := esConfig["ca_file"]; ok && caFile.(string) != "" {
caCert, err := ioutil.ReadFile(caFile.(string))
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to read CA File",
Detail: err.Error(),
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})
})
return nil, diags

It would be great if we immediately return the diags here to report the error sooner.

return nil, diags
}
config.CACert = caCert
}
}
}

Expand Down Expand Up @@ -133,6 +147,13 @@ func NewApiClient(d *schema.ResourceData, meta interface{}) (*ApiClient, error)
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
config.Transport = tr
}
if caFile, ok := conn["ca_file"]; ok && caFile.(string) != "" {
caCert, err := ioutil.ReadFile(caFile.(string))
if err != nil {
return nil, fmt.Errorf("Unable to read ca_file: %w", err)
}
config.CACert = caCert
}

es, err := elasticsearch.NewClient(config)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func New(version string) func() *schema.Provider {
Optional: true,
Default: false,
},
"ca_file": {
Description: "Path to a custom Certificate Authority certificate",
Type: schema.TypeString,
Optional: true,
},
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ func AddConnectionSchema(providedSchema map[string]*schema.Schema) {
Optional: true,
Default: false,
},
"ca_file": {
Description: "Path to a custom Certificate Authority certificate",
Type: schema.TypeString,
Optional: true,
},
},
},
}
Expand Down