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

feat(inputs.nats_consumer): Add nkey-seed-file authentication #14375

Merged
merged 2 commits into from
Dec 4, 2023
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
14 changes: 12 additions & 2 deletions plugins/inputs/nats_consumer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ creates metrics using one of the supported [input data formats][].
A [Queue Group][queue group] is used when subscribing to subjects so multiple
instances of telegraf can read from a NATS cluster in parallel.

There are three methods of (optionally) authenticating with NATS:
[username/password][userpass], [a NATS creds file][creds] (NATS 2.0), or
an [nkey seed file][nkey] (NATS 2.0).

## Service Input <!-- @/docs/includes/service_input.md -->

This plugin is a service input. Normal plugins gather metrics determined by the
Expand Down Expand Up @@ -51,13 +55,16 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## name a queue group
queue_group = "telegraf_consumers"

## Optional credentials
## Optional authentication with username and password credentials
# username = ""
# password = ""

## Optional NATS 2.0 and NATS NGS compatible user credentials
## Optional authentication with NATS credentials file (NATS 2.0)
# credentials = "/etc/telegraf/nats.creds"

## Optional authentication with nkey seed file (NATS 2.0)
# nkey_seed = "/etc/telegraf/seed.txt"

## Use Transport Layer Security
# secure = false

Expand Down Expand Up @@ -95,6 +102,9 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[nats]: https://www.nats.io/about/
[input data formats]: /docs/DATA_FORMATS_INPUT.md
[queue group]: https://www.nats.io/documentation/concepts/nats-queueing/
[userpass]: https://docs.nats.io/using-nats/developer/connecting/userpass
[creds]: https://docs.nats.io/using-nats/developer/connecting/creds
[nkey]: https://docs.nats.io/using-nats/developer/connecting/nkey

## Metrics

Expand Down
9 changes: 9 additions & 0 deletions plugins/inputs/nats_consumer/nats_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type natsConsumer struct {
Username string `toml:"username"`
Password string `toml:"password"`
Credentials string `toml:"credentials"`
NkeySeed string `toml:"nkey_seed"`
JsSubjects []string `toml:"jetstream_subjects"`

tls.ClientConfig
Expand Down Expand Up @@ -106,6 +107,14 @@ func (n *natsConsumer) Start(acc telegraf.Accumulator) error {
options = append(options, nats.UserCredentials(n.Credentials))
}

if n.NkeySeed != "" {
powersj marked this conversation as resolved.
Show resolved Hide resolved
opt, err := nats.NkeyOptionFromSeed(n.NkeySeed)
if err != nil {
return err
}
options = append(options, opt)
}

if n.Secure {
tlsConfig, err := n.ClientConfig.TLSConfig()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions plugins/inputs/nats_consumer/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
## name a queue group
queue_group = "telegraf_consumers"

## Optional credentials
## Optional authentication with username and password credentials
# username = ""
# password = ""

## Optional NATS 2.0 and NATS NGS compatible user credentials
## Optional authentication with NATS credentials file (NATS 2.0)
# credentials = "/etc/telegraf/nats.creds"

## Optional authentication with nkey seed file (NATS 2.0)
# nkey_seed = "/etc/telegraf/seed.txt"

## Use Transport Layer Security
# secure = false

Expand Down
Loading