Skip to content

Commit 196ff87

Browse files
committed
Add environment WhiteList support for Command
Prior, only BlackList-ing of environment variables was supported, and solely for exact variable names match, or "*" for all. This changeset introduces: - WhiteList-ing - prefix matching for env var names (eg: "THING_*" can now be used to white/black list any env variable which name starts with THING_) Signed-off-by: apostasie <[email protected]>
1 parent 7008b01 commit 196ff87

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

mod/tigron/internal/com/command.go

+39-9
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ type Command struct {
9393
WrapArgs []string
9494
Timeout time.Duration
9595

96-
WorkingDir string
97-
Env map[string]string
98-
// FIXME: EnvBlackList might change for a better mechanism (regexp and/or whitelist + blacklist)
96+
WorkingDir string
97+
Env map[string]string
9998
EnvBlackList []string
99+
EnvWhiteList []string
100100

101101
writers []func() io.Reader
102102

@@ -122,6 +122,7 @@ func (gc *Command) Clone() *Command {
122122
WorkingDir: gc.WorkingDir,
123123
Env: map[string]string{},
124124
EnvBlackList: append([]string(nil), gc.EnvBlackList...),
125+
EnvWhiteList: append([]string(nil), gc.EnvWhiteList...),
125126

126127
writers: append([]func() io.Reader(nil), gc.writers...),
127128

@@ -399,26 +400,55 @@ func (gc *Command) buildCommand(ctx context.Context) *exec.Cmd {
399400
//nolint:gosec
400401
cmd := exec.CommandContext(ctx, binary, args...)
401402

402-
// Add dir
403+
// Add dir.
403404
cmd.Dir = gc.WorkingDir
404405

405-
// Set wait delay after waits returns
406+
// Set wait delay after waits returns.
406407
cmd.WaitDelay = delayAfterWait
407408

408-
// Build env
409+
// Build env.
409410
cmd.Env = []string{}
410-
// TODO: replace with regexps? and/or whitelist?
411+
412+
const (
413+
star = "*"
414+
equal = "="
415+
)
416+
411417
for _, envValue := range os.Environ() {
412418
add := true
413419

414-
for _, b := range gc.EnvBlackList {
415-
if b == "*" || strings.HasPrefix(envValue, b+"=") {
420+
for _, needle := range gc.EnvBlackList {
421+
if strings.HasSuffix(needle, star) {
422+
needle = strings.TrimSuffix(needle, star)
423+
} else if needle != star && !strings.Contains(needle, equal) {
424+
needle += equal
425+
}
426+
427+
if needle == star || strings.HasPrefix(envValue, needle) {
416428
add = false
417429

418430
break
419431
}
420432
}
421433

434+
if len(gc.EnvWhiteList) > 0 {
435+
add = false
436+
437+
for _, needle := range gc.EnvWhiteList {
438+
if strings.HasSuffix(needle, star) {
439+
needle = strings.TrimSuffix(needle, star)
440+
} else if needle != star && !strings.Contains(needle, equal) {
441+
needle += equal
442+
}
443+
444+
if needle == star || strings.HasPrefix(envValue, needle) {
445+
add = true
446+
447+
break
448+
}
449+
}
450+
}
451+
422452
if add {
423453
cmd.Env = append(cmd.Env, envValue)
424454
}

0 commit comments

Comments
 (0)