fix(config): Replace environment variables if existing but empty (#13570)

This commit is contained in:
Sven Rebhan 2023-07-10 15:24:40 +02:00 committed by GitHub
parent 00b0ae68e0
commit 1dd45b1d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 5 deletions

View File

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

View File

@ -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}", 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", 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", name: "Default has special chars",
contents: `Not recommended but supported ${MY_VAR:-Default with special chars Supported#$\"}`, contents: `Not recommended but supported ${MY_VAR:-Default with special chars Supported#$\"}`,
@ -181,12 +189,18 @@ func TestEnvironmentSubstitutionOldBehavior(t *testing.T) {
contents: `${1}`, contents: `${1}`,
expected: `${1}`, expected: `${1}`,
}, },
{
name: "empty but set",
contents: "Contains ${EMPTY} nothing",
expected: "Contains nothing",
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Setenv("VAR", "foobar") t.Setenv("VAR", "foobar")
t.Setenv("FALLBACK", "default") t.Setenv("FALLBACK", "default")
t.Setenv("EMPTY", "")
actual, err := substituteEnvironment([]byte(tt.contents), true) actual, err := substituteEnvironment([]byte(tt.contents), true)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, tt.expected, string(actual)) require.EqualValues(t, tt.expected, string(actual))

2
go.mod
View File

@ -55,7 +55,7 @@ require (
github.com/caio/go-tdigest v3.1.0+incompatible github.com/caio/go-tdigest v3.1.0+incompatible
github.com/cisco-ie/nx-telemetry-proto v0.0.0-20230117155933-f64c045c77df github.com/cisco-ie/nx-telemetry-proto v0.0.0-20230117155933-f64c045c77df
github.com/clarify/clarify-go v0.2.4 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/coocood/freecache v1.2.3
github.com/coreos/go-semver v0.3.1 github.com/coreos/go-semver v0.3.1
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f

4
go.sum
View File

@ -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/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 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= 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.16.0 h1:HYk4uYWXgArHh6NG+WE4yGYayOXw+hjqJ+eJxpjWWjk=
github.com/compose-spec/compose-go v1.15.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc= 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/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 h1:qZbsLvmyu+Vlty0/Ex5xc0z2YtKpIsb5n45mAMI+2Ns=
github.com/containerd/containerd v1.6.18/go.mod h1:1RdCUu95+gc2v9t3IL+zIlpClSmew7/0YS8O5eQZrOw= github.com/containerd/containerd v1.6.18/go.mod h1:1RdCUu95+gc2v9t3IL+zIlpClSmew7/0YS8O5eQZrOw=