Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to unset env after parsing #2

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}