Skip to content

Commit cd55e82

Browse files
srebhanpowersj
authored andcommitted
fix(config): Replace environment variables if existing but empty (#13570)
(cherry picked from commit 1dd45b1)
1 parent d5a19fb commit cd55e82

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,8 @@ func removeComments(contents []byte) ([]byte, error) {
858858
func substituteEnvironment(contents []byte, oldReplacementBehavior bool) ([]byte, error) {
859859
options := []template.Option{
860860
template.WithReplacementFunction(func(s string, m template.Mapping, cfg *template.Config) (string, error) {
861-
result, err := template.DefaultReplacementFunc(s, m, cfg)
862-
if err == nil && result == "" {
861+
result, applied, err := template.DefaultReplacementAppliedFunc(s, m, cfg)
862+
if err == nil && !applied {
863863
// Keep undeclared environment-variable patterns to reproduce
864864
// pre-v1.27 behavior
865865
return s, nil

config/internal_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func TestEnvironmentSubstitution(t *testing.T) {
5555
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}",
5656
expected: "Env var VALUE is set, with VALUE syntax and default on this Substituted, no default on this VALUE2",
5757
},
58+
{
59+
name: "empty but set",
60+
setEnv: func(t *testing.T) {
61+
t.Setenv("EMPTY", "")
62+
},
63+
contents: "Contains ${EMPTY} nothing",
64+
expected: "Contains nothing",
65+
},
5866
{
5967
name: "Default has special chars",
6068
contents: `Not recommended but supported ${MY_VAR:-Default with special chars Supported#$\"}`,
@@ -181,12 +189,18 @@ func TestEnvironmentSubstitutionOldBehavior(t *testing.T) {
181189
contents: `${1}`,
182190
expected: `${1}`,
183191
},
192+
{
193+
name: "empty but set",
194+
contents: "Contains ${EMPTY} nothing",
195+
expected: "Contains nothing",
196+
},
184197
}
185198

186199
for _, tt := range tests {
187200
t.Run(tt.name, func(t *testing.T) {
188201
t.Setenv("VAR", "foobar")
189202
t.Setenv("FALLBACK", "default")
203+
t.Setenv("EMPTY", "")
190204
actual, err := substituteEnvironment([]byte(tt.contents), true)
191205
require.NoError(t, err)
192206
require.EqualValues(t, tt.expected, string(actual))

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ require (
5555
github.com/caio/go-tdigest v3.1.0+incompatible
5656
github.com/cisco-ie/nx-telemetry-proto v0.0.0-20230117155933-f64c045c77df
5757
github.com/clarify/clarify-go v0.2.4
58-
github.com/compose-spec/compose-go v1.15.0
58+
github.com/compose-spec/compose-go v1.16.0
5959
github.com/coocood/freecache v1.2.3
6060
github.com/coreos/go-semver v0.3.1
6161
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWH
388388
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
389389
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
390390
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
391-
github.com/compose-spec/compose-go v1.15.0 h1:rv3TTgbS3U4Y8sRTngrcxDmpbz+fq26wTqHculSCi6s=
392-
github.com/compose-spec/compose-go v1.15.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc=
391+
github.com/compose-spec/compose-go v1.16.0 h1:HYk4uYWXgArHh6NG+WE4yGYayOXw+hjqJ+eJxpjWWjk=
392+
github.com/compose-spec/compose-go v1.16.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc=
393393
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
394394
github.com/containerd/containerd v1.6.18 h1:qZbsLvmyu+Vlty0/Ex5xc0z2YtKpIsb5n45mAMI+2Ns=
395395
github.com/containerd/containerd v1.6.18/go.mod h1:1RdCUu95+gc2v9t3IL+zIlpClSmew7/0YS8O5eQZrOw=

0 commit comments

Comments
 (0)