Skip to content

Commit

Permalink
Merge 2a121c1 into 3c99cf7
Browse files Browse the repository at this point in the history
  • Loading branch information
akamensky committed Jan 22, 2023
2 parents 3c99cf7 + 2a121c1 commit 0816b53
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions envparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package envparse

import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
Expand All @@ -10,6 +11,7 @@ import (
var (
defaultPrefix = "APP"
defaultMaxDepth = 100
unsetEnv = false
)

// Parse scans through environment variables using mapping provided in interface
Expand All @@ -26,6 +28,15 @@ func Parse(ptr interface{}, envs []string) error {

errorList := newErrorList()
structValue.Set(parseStruct(structValue.Type(), env.GetPrefix(defaultPrefix), 0, errorList))

if unsetEnv {
for k := range env {
if strings.HasPrefix(k, defaultPrefix) {
_ = os.Unsetenv(k)
}
}
}

if !errorList.IsEmpty() {
return errorList
}
Expand Down
23 changes: 23 additions & 0 deletions envparse_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package envparse

import (
"os"
"reflect"
"testing"
)
Expand Down Expand Up @@ -379,3 +380,25 @@ func TestParse_8(t *testing.T) {
t.Errorf("expected '%v', but got '%v'", e2, c2)
}
}

func TestParse_9(t *testing.T) {
_ = os.Setenv("APP_TEST", "1")
_ = os.Setenv("APP_TEST", "1")
type testType struct {
I int `env:"name=TEST,required"`
}
c := &testType{}
origUnsetEnv := unsetEnv
unsetEnv = true
err := Parse(c, os.Environ())
unsetEnv = origUnsetEnv
if err != nil {
t.Errorf("expected err == nil, but got '%v'", FullError(err))
}
if c.I != 1 {
t.Errorf("expected c.I == 1, but got c.I == %d", c.I)
}
if os.Getenv("APP_TEST") != "" {
t.Errorf("expected APP_TEST == '', but got APP_TEST == '%s'", os.Getenv("APP_TEST"))
}
}
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ func SetPrefix(s string) {
func SetMaxDepth(i int) {
defaultMaxDepth = i
}

// SetUnsetEnv sets a flag that if `true` will ensure that all environment variables using defined prefix
// will not be readable by the process or by the children of the process.
//
// NOTE: the original environment variable is still available to be read from `/proc/<pid>/environ` or `ps eww <pid>`
// as there are no system mechanisms available to overwrite those.
func SetUnsetEnv(unset bool) {
unsetEnv = unset
}
11 changes: 11 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ func TestSetMaxDepth(t *testing.T) {

defaultMaxDepth = originalMaxDepth
}

func TestSetUnsetEnv(t *testing.T) {
originalUnsetEnv := unsetEnv

SetUnsetEnv(true)
if !unsetEnv {
t.Errorf("expected unsetEnv == true, but got unsetEnv == false")
}

unsetEnv = originalUnsetEnv
}

0 comments on commit 0816b53

Please sign in to comment.