Skip to content

Commit

Permalink
Merge branch 'influxdata:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
mannukalra authored Dec 11, 2022
2 parents 869c675 + 4f7717b commit 606126b
Show file tree
Hide file tree
Showing 136 changed files with 6,183 additions and 699 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ executors:
working_directory: '/go/src/github.com/influxdata/telegraf'
resource_class: large
docker:
- image: 'quay.io/influxdb/telegraf-ci:1.19.3'
- image: 'quay.io/influxdb/telegraf-ci:1.19.4'
environment:
GOFLAGS: -p=4
mac:
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ embed_readme_%:
go generate -run="readme_config_includer/generator$$" ./plugins/$*/...

.PHONY: docs
docs: build_tools embed_readme_inputs embed_readme_outputs embed_readme_processors embed_readme_aggregators
docs: build_tools embed_readme_inputs embed_readme_outputs embed_readme_processors embed_readme_aggregators embed_readme_secretstores

.PHONY: build
build:
Expand Down Expand Up @@ -238,8 +238,8 @@ plugins/parsers/influx/machine.go: plugins/parsers/influx/machine.go.rl

.PHONY: ci
ci:
docker build -t quay.io/influxdb/telegraf-ci:1.19.3 - < scripts/ci.docker
docker push quay.io/influxdb/telegraf-ci:1.19.3
docker build -t quay.io/influxdb/telegraf-ci:1.19.4 - < scripts/ci.docker
docker push quay.io/influxdb/telegraf-ci:1.19.4

.PHONY: install
install: $(buildbin)
Expand Down
2 changes: 1 addition & 1 deletion cmd/telegraf/agent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
## If set to true, do no set the "host" tag in the telegraf agent.
omit_hostname = false

## Method of translating SNMP objects. Can be "netsnmp" which
## Method of translating SNMP objects. Can be "netsnmp" (deprecated) which
## translates by calling external programs snmptranslate and snmptable,
## or "gosmi" which translates using the built-in gosmi library.
# snmp_translator = "netsnmp"
258 changes: 258 additions & 0 deletions cmd/telegraf/cmd_secretstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
// Command handling for secret-stores' "secret" command
package main

import (
"errors"
"fmt"
"os"
"sort"
"strings"

"github.com/influxdata/telegraf/config"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)

func processFilterOnlySecretStoreFlags(ctx *cli.Context) Filters {
sectionFilters := []string{"inputs", "outputs", "processors", "aggregators"}
inputFilters := []string{"-"}
outputFilters := []string{"-"}
processorFilters := []string{"-"}
aggregatorFilters := []string{"-"}

// Only load the secret-stores
var secretstore string
if len(ctx.Lineage()) >= 2 {
parent := ctx.Lineage()[1] // ancestor contexts in order from child to parent
secretstore = parent.String("secretstore-filter")
}

// If both the parent and command filters are defined, append them together
secretstore = appendFilter(secretstore, ctx.String("secretstore-filter"))
secretstoreFilters := deleteEmpty(strings.Split(secretstore, ":"))
return Filters{sectionFilters, inputFilters, outputFilters, aggregatorFilters, processorFilters, secretstoreFilters}
}

func getSecretStoreCommands(m App) []*cli.Command {
return []*cli.Command{
{
Name: "secrets",
Usage: "commands for listing, adding and removing secrets on all known secret-stores",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "list known secrets and secret-stores",
Description: `
The 'list' command requires passing in your configuration file
containing the secret-store definitions you want to access. To get a
list of available secret-store plugins, please have a look at
https://github.com/influxdata/telegraf/tree/master/plugins/secretstores.
For help on how to define secret-stores, check the documentation of the
different plugins.
Assuming you use the default configuration file location, you can run
the following command to list the keys of all known secrets in ALL
available stores
> telegraf secrets list
To get the keys of all known secrets in a particular store, you can run
> telegraf secrets list mystore
To also reveal the actual secret, i.e. the value, you can pass the
'--reveal-secret' flag.
`,
ArgsUsage: "[secret-store ID]...[secret-store ID]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "reveal-secret",
Usage: "also print the secret value",
},
},
Action: func(cCtx *cli.Context) error {
// Only load the secret-stores
filters := processFilterOnlySecretStoreFlags(cCtx)
g := GlobalFlags{
config: cCtx.StringSlice("config"),
configDir: cCtx.StringSlice("config-directory"),
plugindDir: cCtx.String("plugin-directory"),
debug: cCtx.Bool("debug"),
}
w := WindowFlags{}
m.Init(nil, filters, g, w)

args := cCtx.Args()
var storeIDs []string
if args.Present() {
storeIDs = args.Slice()
} else {
ids, err := m.ListSecretStores()
if err != nil {
return fmt.Errorf("unable to determine secret-store IDs: %w", err)
}
storeIDs = ids
}
sort.Strings(storeIDs)

reveal := cCtx.Bool("reveal-secret")
for _, storeID := range storeIDs {
store, err := m.GetSecretStore(storeID)
if err != nil {
return fmt.Errorf("unable to get secret-store %q: %w", storeID, err)
}
keys, err := store.List()
if err != nil {
return fmt.Errorf("unable to get secrets from store %q: %w", storeID, err)
}
sort.Strings(keys)

_, _ = fmt.Printf("Known secrets for store %q:\n", storeID)
for _, k := range keys {
var v []byte
if reveal {
if v, err = store.Get(k); err != nil {
return fmt.Errorf("unable to get value of secret %q from store %q: %w", k, storeID, err)
}
}
_, _ = fmt.Printf(" %-30s %s\n", k, string(v))
config.ReleaseSecret(v)
}
}

return nil
},
},
{
Name: "get",
Usage: "retrieves value of given secret from given store",
Description: `
The 'get' command requires passing in your configuration file
containing the secret-store definitions you want to access. To get a
list of available secret-store plugins, please have a look at
https://github.com/influxdata/telegraf/tree/master/plugins/secretstores.
and use the 'secrets list' command to get the IDs of available stores and
key(s) of available secrets.
For help on how to define secret-stores, check the documentation of the
different plugins.
Assuming you use the default configuration file location, you can run
the following command to retrieve a secret from a secret store
available stores
> telegraf secrets get mystore mysecretkey
This will fetch the secret with the key 'mysecretkey' from the secret-store
with the ID 'mystore'.
`,
ArgsUsage: "<secret-store ID> <secret key>",
Action: func(cCtx *cli.Context) error {
// Only load the secret-stores
filters := processFilterOnlySecretStoreFlags(cCtx)
g := GlobalFlags{
config: cCtx.StringSlice("config"),
configDir: cCtx.StringSlice("config-directory"),
plugindDir: cCtx.String("plugin-directory"),
debug: cCtx.Bool("debug"),
}
w := WindowFlags{}
m.Init(nil, filters, g, w)

args := cCtx.Args()
if !args.Present() || args.Len() != 2 {
return errors.New("invalid number of arguments")
}

storeID := args.First()
key := args.Get(1)

store, err := m.GetSecretStore(storeID)
if err != nil {
return fmt.Errorf("unable to get secret-store: %w", err)
}
value, err := store.Get(key)
if err != nil {
return fmt.Errorf("unable to get secret: %w", err)
}
_, _ = fmt.Printf("%s:%s = %s\n", storeID, key, value)

return nil
},
},
{
Name: "set",
Usage: "create or modify a secret in the given store",
Description: `
The 'set' command requires passing in your configuration file
containing the secret-store definitions you want to access. To get a
list of available secret-store plugins, please have a look at
https://github.com/influxdata/telegraf/tree/master/plugins/secretstores.
and use the 'secrets list' command to get the IDs of available stores and keys.
For help on how to define secret-stores, check the documentation of the
different plugins.
Assuming you use the default configuration file location, you can run
the following command to create a secret in anm available secret-store
> telegraf secrets set mystore mysecretkey mysecretvalue
This will create a secret with the key 'mysecretkey' in the secret-store
with the ID 'mystore' with the value being set to 'mysecretvalue'. If a
secret with that key ('mysecretkey') already existed in that store, its
value will be modified.
When you leave out the value of the secret like
> telegraf secrets set mystore mysecretkey
you will be prompted to enter the value of the secret.
`,
ArgsUsage: "<secret-store ID> <secret key>",
Action: func(cCtx *cli.Context) error {
// Only load the secret-stores
filters := processFilterOnlySecretStoreFlags(cCtx)
g := GlobalFlags{
config: cCtx.StringSlice("config"),
configDir: cCtx.StringSlice("config-directory"),
plugindDir: cCtx.String("plugin-directory"),
debug: cCtx.Bool("debug"),
}
w := WindowFlags{}
m.Init(nil, filters, g, w)

args := cCtx.Args()
if !args.Present() || args.Len() < 2 {
return errors.New("invalid number of arguments")
}

storeID := args.First()
key := args.Get(1)
value := args.Get(2)
if value == "" {
fmt.Printf("enter secret: ")
b, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return err
}
fmt.Println()
value = string(b)
}

store, err := m.GetSecretStore(storeID)
if err != nil {
return fmt.Errorf("unable to get secret-store: %w", err)
}
if err := store.Set(key, value); err != nil {
return fmt.Errorf("unable to set secret: %w", err)
}

return nil
},
},
},
},
}
}
Loading

0 comments on commit 606126b

Please sign in to comment.