Skip to content

Commit

Permalink
Allow escaping $ in environment variable interpolation (#560)
Browse files Browse the repository at this point in the history
* Add test for escaped dollar signs

* Check for 2663 and return it rather than an error in env parsing

* Consolidate the parEnv and envMapper functions
  • Loading branch information
Andrew Suderman committed Apr 1, 2022
1 parent ae5c047 commit d570393
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
6 changes: 6 additions & 0 deletions end_to_end_testing/course_files/03_test_env_var.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ charts:
comment-problem-detector:
# This is a comment "${this_var_should_not_be_parsed}"
- test-value
escape-values:
repository: fairwinds-incubator
chart: basic-demo
namespace: 03-infra
values:
non-used: "A string that needs a $$ in it"
19 changes: 10 additions & 9 deletions pkg/course/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,21 +625,22 @@ func parseSecrets(courseData []byte) error {
}

func parseEnv(data string) (string, error) {
dataWithEnv := os.Expand(data, envMapper)
dataWithEnv := os.Expand(data, func(key string) string {
if key == "$" {
return "$$"
}
if value, ok := os.LookupEnv(key); ok {
return value
}
color.Red("ERROR: environment variable %s is not set", key)
return "_ENV_NOT_SET_"
})
if strings.Contains(dataWithEnv, "_ENV_NOT_SET_") {
return data, fmt.Errorf("course has env variables that are not properly set")
}
return dataWithEnv, nil
}

func envMapper(key string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
color.Red("ERROR: environment variable %s is not set", key)
return "_ENV_NOT_SET_"
}

func boolPtr(b bool) *bool {
return &b
}
56 changes: 56 additions & 0 deletions pkg/course/course_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package course

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -221,3 +222,58 @@ func TestFileV2_populateDefaultRepository(t *testing.T) {
})
}
}

func Test_parseEnv(t *testing.T) {

tests := []struct {
name string
data string
want string
envMap map[string]string
wantErr bool
}{
{
name: "basic error check",
data: "$this-is-certainly-not-a-valid-env-var",
wantErr: true,
},
{
name: "basic test",
data: "$TEST_ENV_KEY",
want: "test-env-value",
envMap: map[string]string{
"TEST_ENV_KEY": "test-env-value",
},
wantErr: false,
},
{
name: "escaping test",
data: "$$TEST_ENV_KEY",
want: "$$TEST_ENV_KEY",
envMap: map[string]string{
"TEST_ENV_KEY": "test-env-value",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for k, v := range tt.envMap {
os.Setenv(k, v)
}

got, err := parseEnv(tt.data)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.EqualValues(t, tt.want, got)
}
})
}
}

func Test_boolPtr(t *testing.T) {
testBool := true
assert.EqualValues(t, &testBool, boolPtr(testBool))
}

0 comments on commit d570393

Please sign in to comment.