Skip to content
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

fix(outputs.opensearch): Expose TLS setting correctly #14340

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions plugins/outputs/opensearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Target index name for metrics (OpenSearch will create if it not exists).
## This is a Golang template (see https://pkg.go.dev/text/template)
## You can also specify
## metric name (`{{.Name}}`), tag value (`{{.Tag "tag_name"}}`), field value (`{{.Field "feild_name"}}`)
## metric name (`{{.Name}}`), tag value (`{{.Tag "tag_name"}}`), field value (`{{.Field "feild_name"}}`)
## If the tag does not exist, the default tag value will be empty string "".
## the timestamp (`{{.Time.Format "xxxxxxxxx"}}`).
## For example: "telegraf-{{.Time.Format "2006-01-02"}}-{{.Tag "host"}}" would set it to telegraf-2023-07-27-HostName
Expand Down Expand Up @@ -63,9 +63,17 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# auth_bearer_token = ""

## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Set to true/false to enforce TLS being enabled/disabled. If not set,
## enable TLS only if any of the other options are specified.
# tls_enable =
## Trusted root certificates for server
# tls_ca = "/path/to/cafile"
## Used for TLS client certificate authentication
# tls_cert = "/path/to/certfile"
## Used for TLS client certificate authentication
# tls_key = "/path/to/keyfile"
## Send the specified TLS server name via SNI
# tls_server_name = "kubernetes.example.com"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false

Expand Down
32 changes: 16 additions & 16 deletions plugins/outputs/opensearch/opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"context"
"crypto/sha256"
"crypto/tls"
_ "embed"
"encoding/json"
"fmt"
Expand All @@ -23,7 +22,7 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal/choice"
httpconfig "github.com/influxdata/telegraf/plugins/common/http"
commontls "github.com/influxdata/telegraf/plugins/common/tls"
srebhan marked this conversation as resolved.
Show resolved Hide resolved
"github.com/influxdata/telegraf/plugins/outputs"
)

Expand All @@ -50,14 +49,14 @@ type Opensearch struct {
HealthCheckTimeout config.Duration `toml:"health_check_timeout"`
URLs []string `toml:"urls"`
Log telegraf.Logger `toml:"-"`

pipelineName string
indexTmpl *template.Template
pipelineTmpl *template.Template
onSucc func(context.Context, opensearchutil.BulkIndexerItem, opensearchutil.BulkIndexerResponseItem)
onFail func(context.Context, opensearchutil.BulkIndexerItem, opensearchutil.BulkIndexerResponseItem, error)
configOptions httpconfig.HTTPClientConfig
osClient *opensearch.Client
commontls.ClientConfig

pipelineName string
indexTmpl *template.Template
pipelineTmpl *template.Template
onSucc func(context.Context, opensearchutil.BulkIndexerItem, opensearchutil.BulkIndexerResponseItem)
onFail func(context.Context, opensearchutil.BulkIndexerItem, opensearchutil.BulkIndexerResponseItem, error)
osClient *opensearch.Client
}

//go:embed template.json
Expand Down Expand Up @@ -158,16 +157,17 @@ func (o *Opensearch) newClient() error {
}
defer password.Destroy()

tlsConfig, err := o.ClientConfig.TLSConfig()
if err != nil {
return fmt.Errorf("creating TLS config failed: %w", err)
}
clientConfig := opensearch.Config{
Addresses: o.URLs,
Username: username.String(),
Password: password.String(),
}

if o.configOptions.InsecureSkipVerify {
clientConfig.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}

header := http.Header{}
Expand Down
16 changes: 12 additions & 4 deletions plugins/outputs/opensearch/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
## Target index name for metrics (OpenSearch will create if it not exists).
## This is a Golang template (see https://pkg.go.dev/text/template)
## You can also specify
## metric name (`{{.Name}}`), tag value (`{{.Tag "tag_name"}}`), field value (`{{.Field "feild_name"}}`)
## metric name (`{{.Name}}`), tag value (`{{.Tag "tag_name"}}`), field value (`{{.Field "feild_name"}}`)
srebhan marked this conversation as resolved.
Show resolved Hide resolved
## If the tag does not exist, the default tag value will be empty string "".
## the timestamp (`{{.Time.Format "xxxxxxxxx"}}`).
## For example: "telegraf-{{.Time.Format "2006-01-02"}}-{{.Tag "host"}}" would set it to telegraf-2023-07-27-HostName
Expand Down Expand Up @@ -43,9 +43,17 @@
# auth_bearer_token = ""

## Optional TLS Config
Copy link
Contributor

Choose a reason for hiding this comment

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

What about including it with {{template "/plugins/common/tls/client.conf"}}

Copy link
Member Author

Choose a reason for hiding this comment

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

There was an issue with it but I don't remember what it was... Will take another look...

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems to be done like that with the http input.

Copy link
Member Author

Choose a reason for hiding this comment

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

Update: The issue is that the config contains Golang templates itself e.g. in the documentation of the index_name option. So the suggested replacement will try to fill in those templates as well and will fail to do so.

2023/11/27 15:23:01 Executing template failed: template: sample.conf.in:13:46: executing "sample.conf.in" at <.Tag>: Tag is not a method but has arguments

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right, these would need to be escaped then in that case.

# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Set to true/false to enforce TLS being enabled/disabled. If not set,
## enable TLS only if any of the other options are specified.
# tls_enable =
## Trusted root certificates for server
# tls_ca = "/path/to/cafile"
## Used for TLS client certificate authentication
# tls_cert = "/path/to/certfile"
## Used for TLS client certificate authentication
# tls_key = "/path/to/keyfile"
## Send the specified TLS server name via SNI
# tls_server_name = "kubernetes.example.com"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false

Expand Down
Loading