Add support for env variables to shim config (#7603)

This commit is contained in:
Yamagishi Kazutoshi 2020-06-02 22:29:57 +09:00 committed by GitHub
parent f27b709efa
commit 1e7f714b2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import (
"io/ioutil"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
@ -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 (
@ -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
}
@ -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{}

View File

@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"io"
"os"
"strings"
"testing"
"time"
@ -11,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
func TestShimWorks(t *testing.T) {
@ -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() {
}

View File

@ -0,0 +1,4 @@
[[inputs.test]]
service_name = "awesome name"
secret_token = "${SECRET_TOKEN}"
secret_value = "$SECRET_VALUE"