This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(GH-15) Unset necessary env vars in pdkshell
- Loading branch information
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package pdkshell | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
var osUtil osHelpers | ||
|
||
func init() { | ||
osUtil = osHelpersImpl{} | ||
} | ||
|
||
// There are a number of ENV VARs that need to be unset on POSIX systems before execution: | ||
// https://github.com/puppetlabs/pdk-vanagon/blob/0aa54c9129b137c2deabb0a417d59215df36fd91/resources/files/posix/pdk_env_wrapper | ||
func getEnvVarsToUnset() []string { | ||
return []string{ | ||
"GEM_HOME", | ||
"GEM_PATH", | ||
"DLN_LIBRARY_PATH", | ||
"RUBYLIB", | ||
"RUBYLIB_PREFIX", | ||
"RUBYOPT", | ||
"RUBYPATH", | ||
"RUBYSHELL", | ||
"LD_LIBRARY_PATH", | ||
"LD_PRELOAD", | ||
} | ||
} | ||
|
||
func validEnvVar(envVar string) (valid bool) { | ||
for _, illegalEnvVar := range getEnvVarsToUnset() { | ||
if strings.HasPrefix(envVar, illegalEnvVar) { | ||
log.Trace().Msgf("Dropping ENV VAR: %s", envVar) | ||
return false | ||
} | ||
} | ||
log.Trace().Msgf("Permitting ENV VAR: %s", envVar) | ||
return true | ||
} | ||
|
||
func getEnvironVars() []string { | ||
env := []string{} | ||
for _, envVar := range osUtil.Environ() { | ||
if validEnvVar(envVar) { | ||
env = append(env, envVar) | ||
} | ||
} | ||
return env | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package pdkshell | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type osHelpersImplMock struct{} | ||
|
||
var mockedEnvironReturn []string | ||
|
||
func (osHelpersImplMock) Environ() []string { | ||
return mockedEnvironReturn | ||
} | ||
|
||
func Test_getEnvironVars(t *testing.T) { | ||
|
||
osUtil = osHelpersImplMock{} | ||
|
||
tests := []struct { | ||
name string | ||
mockedEnvironReturn []string | ||
want []string | ||
}{ | ||
{ | ||
name: "Drops ENV VAR to unset and passes through permitted", | ||
mockedEnvironReturn: []string{"GEM_HOME=/foo/bar", "VALID_VAR=/baz/qux"}, | ||
want: []string{"VALID_VAR=/baz/qux"}, | ||
}, | ||
{ | ||
name: "Handles multiple instances of the same ENV VAR to be dropped", | ||
mockedEnvironReturn: []string{"RUBYPATH=/foo/bar", "VALID_VAR=/baz/qux", "RUBYPATH=/foo/bar/bizz/buzz"}, | ||
want: []string{"VALID_VAR=/baz/qux"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
mockedEnvironReturn = tt.mockedEnvironReturn | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := getEnvironVars(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("getEnvironVars() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package pdkshell | ||
|
||
import "os" | ||
|
||
type osHelpers interface { | ||
Environ() []string | ||
} | ||
|
||
type osHelpersImpl struct{} | ||
|
||
func (osHelpersImpl) Environ() []string { | ||
return os.Environ() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters