From cd55e82f5d6164edbbb839074d725ee7d5b62097 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Mon, 10 Jul 2023 15:24:40 +0200 Subject: [PATCH] fix(config): Replace environment variables if existing but empty (#13570) (cherry picked from commit 1dd45b1d1c255010f121a7783bd2f15a58671366) --- config/config.go | 4 ++-- config/internal_test.go | 14 ++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 7261bc746f196..4233234d0f3a2 100644 --- a/config/config.go +++ b/config/config.go @@ -858,8 +858,8 @@ func removeComments(contents []byte) ([]byte, error) { func substituteEnvironment(contents []byte, oldReplacementBehavior bool) ([]byte, error) { options := []template.Option{ template.WithReplacementFunction(func(s string, m template.Mapping, cfg *template.Config) (string, error) { - result, err := template.DefaultReplacementFunc(s, m, cfg) - if err == nil && result == "" { + result, applied, err := template.DefaultReplacementAppliedFunc(s, m, cfg) + if err == nil && !applied { // Keep undeclared environment-variable patterns to reproduce // pre-v1.27 behavior return s, nil diff --git a/config/internal_test.go b/config/internal_test.go index 8a0bda6b90a71..9bc179a5b425b 100644 --- a/config/internal_test.go +++ b/config/internal_test.go @@ -55,6 +55,14 @@ func TestEnvironmentSubstitution(t *testing.T) { contents: "Env var ${MY_VAR} is set, with $MY_VAR syntax and default on this ${MY_VAR1:-Substituted}, no default on this ${MY_VAR2:-NoDefault}", expected: "Env var VALUE is set, with VALUE syntax and default on this Substituted, no default on this VALUE2", }, + { + name: "empty but set", + setEnv: func(t *testing.T) { + t.Setenv("EMPTY", "") + }, + contents: "Contains ${EMPTY} nothing", + expected: "Contains nothing", + }, { name: "Default has special chars", contents: `Not recommended but supported ${MY_VAR:-Default with special chars Supported#$\"}`, @@ -181,12 +189,18 @@ func TestEnvironmentSubstitutionOldBehavior(t *testing.T) { contents: `${1}`, expected: `${1}`, }, + { + name: "empty but set", + contents: "Contains ${EMPTY} nothing", + expected: "Contains nothing", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Setenv("VAR", "foobar") t.Setenv("FALLBACK", "default") + t.Setenv("EMPTY", "") actual, err := substituteEnvironment([]byte(tt.contents), true) require.NoError(t, err) require.EqualValues(t, tt.expected, string(actual)) diff --git a/go.mod b/go.mod index ee482c48cf28c..595018176e2fd 100644 --- a/go.mod +++ b/go.mod @@ -55,7 +55,7 @@ require ( github.com/caio/go-tdigest v3.1.0+incompatible github.com/cisco-ie/nx-telemetry-proto v0.0.0-20230117155933-f64c045c77df github.com/clarify/clarify-go v0.2.4 - github.com/compose-spec/compose-go v1.15.0 + github.com/compose-spec/compose-go v1.16.0 github.com/coocood/freecache v1.2.3 github.com/coreos/go-semver v0.3.1 github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f diff --git a/go.sum b/go.sum index 0ab375186aeb5..969f432285cbf 100644 --- a/go.sum +++ b/go.sum @@ -388,8 +388,8 @@ github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= -github.com/compose-spec/compose-go v1.15.0 h1:rv3TTgbS3U4Y8sRTngrcxDmpbz+fq26wTqHculSCi6s= -github.com/compose-spec/compose-go v1.15.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc= +github.com/compose-spec/compose-go v1.16.0 h1:HYk4uYWXgArHh6NG+WE4yGYayOXw+hjqJ+eJxpjWWjk= +github.com/compose-spec/compose-go v1.16.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc= github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= github.com/containerd/containerd v1.6.18 h1:qZbsLvmyu+Vlty0/Ex5xc0z2YtKpIsb5n45mAMI+2Ns= github.com/containerd/containerd v1.6.18/go.mod h1:1RdCUu95+gc2v9t3IL+zIlpClSmew7/0YS8O5eQZrOw=