77 "sync"
88
99 "github.com/influxdata/telegraf"
10+ "github.com/influxdata/telegraf/internal/tls"
1011 "github.com/influxdata/telegraf/plugins/inputs"
1112 "github.com/influxdata/telegraf/plugins/parsers"
1213 nats "github.com/nats-io/go-nats"
@@ -34,7 +35,11 @@ type natsConsumer struct {
3435 QueueGroup string `toml:"queue_group"`
3536 Subjects []string `toml:"subjects"`
3637 Servers []string `toml:"servers"`
37- Secure bool `toml:"secure"`
38+ Username string `toml:"username"`
39+ Password string `toml:"password"`
40+ tls.ClientConfig
41+ // Legacy; Should be deprecated
42+ Secure bool `toml:"secure"`
3843
3944 // Client pending limits:
4045 PendingMessageLimit int `toml:"pending_message_limit"`
@@ -61,13 +66,24 @@ type natsConsumer struct {
6166var sampleConfig = `
6267 ## urls of NATS servers
6368 servers = ["nats://localhost:4222"]
64- ## Use Transport Layer Security
69+ ## Deprecated: Use Transport Layer Security
6570 secure = false
6671 ## subject(s) to consume
6772 subjects = ["telegraf"]
6873 ## name a queue group
6974 queue_group = "telegraf_consumers"
7075
76+ ## Optional credentials
77+ # username = ""
78+ # password = ""
79+
80+ ## Optional TLS Config
81+ # tls_ca = "/etc/telegraf/ca.pem"
82+ # tls_cert = "/etc/telegraf/cert.pem"
83+ # tls_key = "/etc/telegraf/key.pem"
84+ ## Use TLS but skip chain & host verification
85+ # insecure_skip_verify = false
86+
7187 ## Sets the limits for pending msgs and bytes for each subscription
7288 ## These shouldn't need to be adjusted except in very high throughput scenarios
7389 # pending_message_limit = 65536
@@ -125,7 +141,25 @@ func (n *natsConsumer) Start(acc telegraf.Accumulator) error {
125141 // override servers if any were specified
126142 opts .Servers = n .Servers
127143
128- opts .Secure = n .Secure
144+ // override authentication, if any was specified
145+ if n .Username != "" {
146+ opts .User = n .Username
147+ opts .Password = n .Password
148+ }
149+
150+ // override TLS, if it was specified
151+ tlsConfig , err := n .ClientConfig .TLSConfig ()
152+ if err != nil {
153+ return err
154+ }
155+ if tlsConfig != nil {
156+ // set NATS connection TLS options
157+ opts .Secure = true
158+ opts .TLSConfig = tlsConfig
159+ } else {
160+ // should be deprecated; use TLS
161+ opts .Secure = n .Secure
162+ }
129163
130164 if n .conn == nil || n .conn .IsClosed () {
131165 n .conn , connectErr = opts .Connect ()
0 commit comments