Skip to content

Commit

Permalink
Add support for env variables to shim config (#7603)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored Jun 2, 2020
1 parent f27b709 commit 1e7f714
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
25 changes: 21 additions & 4 deletions plugins/inputs/execd/shim/goshim.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
Expand All @@ -23,9 +24,13 @@ import (
type empty struct{}

var (
stdout io.Writer = os.Stdout
stdin io.Reader = os.Stdin
forever = 100 * 365 * 24 * time.Hour
stdout io.Writer = os.Stdout
stdin io.Reader = os.Stdin
forever = 100 * 365 * 24 * time.Hour
envVarEscaper = strings.NewReplacer(
`"`, `\"`,
`\`, `\\`,
)
)

const (
Expand Down Expand Up @@ -257,11 +262,13 @@ func LoadConfig(filePath *string) ([]telegraf.Input, error) {
return nil, err
}

s := expandEnvVars(b)

conf := struct {
Inputs map[string][]toml.Primitive
}{}

md, err := toml.Decode(string(b), &conf)
md, err := toml.Decode(s, &conf)
if err != nil {
return nil, err
}
Expand All @@ -274,6 +281,16 @@ func LoadConfig(filePath *string) ([]telegraf.Input, error) {
return loadedInputs, err
}

func expandEnvVars(contents []byte) string {
return os.Expand(string(contents), getEnv)
}

func getEnv(key string) string {
v := os.Getenv(key)

return envVarEscaper.Replace(v)
}

func loadConfigIntoInputs(md toml.MetaData, inputConfigs map[string][]toml.Primitive) ([]telegraf.Input, error) {
renderedInputs := []telegraf.Input{}

Expand Down
54 changes: 54 additions & 0 deletions plugins/inputs/execd/shim/shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bufio"
"bytes"
"io"
"os"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)

func TestShimWorks(t *testing.T) {
Expand Down Expand Up @@ -118,3 +120,55 @@ func (i *testInput) Start(acc telegraf.Accumulator) error {

func (i *testInput) Stop() {
}

func TestLoadConfig(t *testing.T) {
os.Setenv("SECRET_TOKEN", "xxxxxxxxxx")
os.Setenv("SECRET_VALUE", `test"\test`)

inputs.Add("test", func() telegraf.Input {
return &serviceInput{}
})

c := "./testdata/plugin.conf"
inputs, err := LoadConfig(&c)
require.NoError(t, err)

inp := inputs[0].(*serviceInput)

require.Equal(t, "awesome name", inp.ServiceName)
require.Equal(t, "xxxxxxxxxx", inp.SecretToken)
require.Equal(t, `test"\test`, inp.SecretValue)
}

type serviceInput struct {
ServiceName string `toml:"service_name"`
SecretToken string `toml:"secret_token"`
SecretValue string `toml:"secret_value"`
}

func (i *serviceInput) SampleConfig() string {
return ""
}

func (i *serviceInput) Description() string {
return ""
}

func (i *serviceInput) Gather(acc telegraf.Accumulator) error {
acc.AddFields("measurement",
map[string]interface{}{
"field": 1,
},
map[string]string{
"tag": "tag",
}, time.Unix(1234, 5678))

return nil
}

func (i *serviceInput) Start(acc telegraf.Accumulator) error {
return nil
}

func (i *serviceInput) Stop() {
}
4 changes: 4 additions & 0 deletions plugins/inputs/execd/shim/testdata/plugin.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[inputs.test]]
service_name = "awesome name"
secret_token = "${SECRET_TOKEN}"
secret_value = "$SECRET_VALUE"

0 comments on commit 1e7f714

Please sign in to comment.