From 1585ea37346ee4c861e11925058570fa9a6d3e49 Mon Sep 17 00:00:00 2001 From: woz5999 Date: Tue, 25 Aug 2020 15:02:25 -0700 Subject: [PATCH] support env var expansion in envoy statsd urls Fixes #8561 --- command/connect/envoy/bootstrap_config.go | 14 ++++++++ .../connect/envoy/bootstrap_config_test.go | 34 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/command/connect/envoy/bootstrap_config.go b/command/connect/envoy/bootstrap_config.go index af383c01d..28cd7ac9b 100644 --- a/command/connect/envoy/bootstrap_config.go +++ b/command/connect/envoy/bootstrap_config.go @@ -280,6 +280,8 @@ func (c *BootstrapConfig) generateStatsSinkJSON(name string, typeName string, ad // Resolve address ENV var if len(addr) > 2 && addr[0] == '$' { addr = os.Getenv(addr[1:]) + } else { + addr = os.Expand(addr, statsSinkEnvMapping) } u, err := url.Parse(addr) @@ -318,6 +320,18 @@ func (c *BootstrapConfig) generateStatsSinkJSON(name string, typeName string, ad }`, nil } +func statsSinkEnvMapping(s string) string { + allowedStatsSinkEnvVars := map[string]bool{ + "HOST_IP": true, + } + + if !allowedStatsSinkEnvVars[s] { + // if the specified env var isn't explicitly allowed, unexpand it + return fmt.Sprintf("${%s}", s) + } + return os.Getenv(s) +} + // resourceTagSpecifiers returns patterns used to generate tags from cluster and filter metric names. func resourceTagSpecifiers(omitDeprecatedTags bool) ([]string, error) { const ( diff --git a/command/connect/envoy/bootstrap_config_test.go b/command/connect/envoy/bootstrap_config_test.go index 35a0505b6..de9105efc 100644 --- a/command/connect/envoy/bootstrap_config_test.go +++ b/command/connect/envoy/bootstrap_config_test.go @@ -485,6 +485,40 @@ func TestBootstrapConfig_ConfigureArgs(t *testing.T) { }, wantErr: false, }, + { + name: "simple-statsd-sink-inline-env-allowed", + input: BootstrapConfig{ + StatsdURL: "udp://$HOST_IP:9125", + }, + env: []string{"HOST_IP=127.0.0.1"}, + wantArgs: BootstrapTplArgs{ + StatsConfigJSON: defaultStatsConfigJSON, + StatsSinksJSON: `[{ + "name": "envoy.stat_sinks.statsd", + "typedConfig": { + "@type": "type.googleapis.com/envoy.config.metrics.v3.StatsdSink", + "address": { + "socket_address": { + "address": "127.0.0.1", + "port_value": 9125 + } + } + } + }]`, + }, + wantErr: false, + }, + { + name: "simple-statsd-sink-inline-env-disallowed", + input: BootstrapConfig{ + StatsdURL: "udp://$HOST_ADDRESS:9125", + }, + env: []string{"HOST_ADDRESS=127.0.0.1"}, + wantArgs: BootstrapTplArgs{ + StatsConfigJSON: defaultStatsConfigJSON, + }, + wantErr: true, + }, { name: "simple-dogstatsd-sink", input: BootstrapConfig{