Skip to content

Commit f9c34f6

Browse files
committed
refactor: Display excluded env vars in debug logs
1 parent 84265a4 commit f9c34f6

File tree

3 files changed

+61
-41
lines changed

3 files changed

+61
-41
lines changed

internal/commands/launch.go

-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ func (l *LaunchCommand) Execute(cfg *config.Config) error {
3737

3838
runnerEnv := env.PrepareRunnerEnv(cfg)
3939

40-
logs.Debugf("Prepared env vars for runner")
41-
4240
for {
4341
// 3. check until task broker is ready
4442

@@ -89,7 +87,6 @@ func (l *LaunchCommand) Execute(cfg *config.Config) error {
8987
logs.Debug("Task ready for pickup, launching runner...")
9088
logs.Debugf("Command: %s", cfg.Runner.Command)
9189
logs.Debugf("Args: %v", cfg.Runner.Args)
92-
logs.Debugf("Env vars: %v", env.Keys(runnerEnv))
9390

9491
ctx, cancelHealthMonitor := context.WithCancel(context.Background())
9592
var wg sync.WaitGroup

internal/env/env.go

+23-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sort"
77
"strings"
88
"task-runner-launcher/internal/config"
9+
"task-runner-launcher/internal/logs"
910
)
1011

1112
const (
@@ -28,33 +29,38 @@ const (
2829
RunnerServerURI = "http://127.0.0.1:5681"
2930
)
3031

31-
// allowedOnly filters the current environment down to only those
32-
// environment variables in the allowlist.
33-
func allowedOnly(allowlist []string) []string {
34-
var filtered []string
35-
32+
// partitionByAllowlist divides the current env vars into those included in and
33+
// excluded from the allowlist.
34+
func partitionByAllowlist(allowlist []string) (included, excluded []string) {
3635
for _, env := range os.Environ() {
3736
parts := strings.SplitN(env, "=", 2)
3837
if len(parts) != 2 {
3938
continue
4039
}
4140

4241
key := parts[0]
42+
isAllowed := false
4343
for _, allowedKey := range allowlist {
4444
if key == allowedKey {
45-
filtered = append(filtered, env)
45+
included = append(included, env)
46+
isAllowed = true
4647
break
4748
}
4849
}
50+
if !isAllowed {
51+
excluded = append(excluded, env)
52+
}
4953
}
5054

51-
sort.Strings(filtered) // ensure consistent order
55+
// ensure consistent order
56+
sort.Strings(included)
57+
sort.Strings(excluded)
5258

53-
return filtered
59+
return included, excluded
5460
}
5561

56-
// Keys returns the keys of the environment variables.
57-
func Keys(env []string) []string {
62+
// keys returns the keys of the environment variables.
63+
func keys(env []string) []string {
5864
keys := make([]string, len(env))
5965
for i, env := range env {
6066
keys[i] = strings.SplitN(env, "=", 2)[0]
@@ -81,10 +87,15 @@ func PrepareRunnerEnv(cfg *config.Config) []string {
8187
defaultEnvs := []string{"LANG", "PATH", "TZ", "TERM"}
8288
allowedEnvs := append(defaultEnvs, cfg.Runner.AllowedEnv...)
8389

84-
runnerEnv := allowedOnly(allowedEnvs)
85-
runnerEnv = append(runnerEnv, "N8N_RUNNERS_HEALTH_CHECK_SERVER_ENABLED=true")
90+
includedEnvs, excludedEnvs := partitionByAllowlist(allowedEnvs)
91+
92+
logs.Debugf("Env vars to exclude from runner: %v", keys(excludedEnvs))
93+
94+
runnerEnv := append(includedEnvs, "N8N_RUNNERS_HEALTH_CHECK_SERVER_ENABLED=true")
8695
runnerEnv = append(runnerEnv, fmt.Sprintf("%s=%s", EnvVarAutoShutdownTimeout, cfg.AutoShutdownTimeout))
8796
runnerEnv = append(runnerEnv, fmt.Sprintf("%s=%s", EnvVarTaskTimeout, cfg.TaskTimeout))
8897

98+
logs.Debugf("Env vars to pass to runner: %v", keys(runnerEnv))
99+
89100
return runnerEnv
90101
}

internal/env/env_test.go

+38-26
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,52 @@ import (
88
"testing"
99
)
1010

11-
func TestAllowedOnly(t *testing.T) {
11+
func TestPartitionByAllowlist(t *testing.T) {
1212
tests := []struct {
13-
name string
14-
envVars map[string]string
15-
allowed []string
16-
expected []string
13+
name string
14+
envVars map[string]string
15+
allowList []string
16+
expectedInclude []string
17+
expectedExclude []string
1718
}{
1819
{
19-
name: "returns only allowed env vars",
20+
name: "partitions env vars correctly",
2021
envVars: map[string]string{
2122
"ALLOWED1": "value1",
2223
"ALLOWED2": "value2",
2324
"NOT_ALLOWED1": "value3",
2425
"NOT_ALLOWED2": "value4",
2526
},
26-
allowed: []string{"ALLOWED1", "ALLOWED2"},
27-
expected: []string{
27+
allowList: []string{"ALLOWED1", "ALLOWED2"},
28+
expectedInclude: []string{
2829
"ALLOWED1=value1",
2930
"ALLOWED2=value2",
3031
},
32+
expectedExclude: []string{
33+
"NOT_ALLOWED1=value3",
34+
"NOT_ALLOWED2=value4",
35+
},
3136
},
3237
{
33-
name: "returns empty slice when no env vars match allowlist",
34-
envVars: map[string]string{"FOO": "bar"},
35-
allowed: []string{"BAZ"},
36-
expected: nil,
38+
name: "returns empty slices when no env vars match allowlist",
39+
envVars: map[string]string{"FOO": "bar"},
40+
allowList: []string{"BAZ"},
41+
expectedInclude: nil,
42+
expectedExclude: []string{"FOO=bar"},
3743
},
3844
{
39-
name: "returns empty slice when allowlist is empty",
40-
envVars: map[string]string{"FOO": "bar"},
41-
allowed: []string{},
42-
expected: nil,
45+
name: "returns empty included and all in excluded when allowlist is empty",
46+
envVars: map[string]string{"FOO": "bar"},
47+
allowList: []string{},
48+
expectedInclude: nil,
49+
expectedExclude: []string{"FOO=bar"},
4350
},
4451
{
45-
name: "returns empty slice when env vars is empty",
46-
envVars: map[string]string{},
47-
allowed: []string{"FOO"},
48-
expected: nil,
52+
name: "returns empty slices when env vars is empty",
53+
envVars: map[string]string{},
54+
allowList: []string{"FOO"},
55+
expectedInclude: nil,
56+
expectedExclude: nil,
4957
},
5058
}
5159

@@ -56,14 +64,18 @@ func TestAllowedOnly(t *testing.T) {
5664
os.Setenv(k, v)
5765
}
5866

59-
got := allowedOnly(tt.allowed)
67+
included, excluded := partitionByAllowlist(tt.allowList)
6068

61-
if tt.expected == nil && len(got) == 0 {
62-
return
69+
if tt.expectedInclude == nil && len(included) == 0 {
70+
// ok
71+
} else if !reflect.DeepEqual(included, tt.expectedInclude) {
72+
t.Errorf("partitionByAllowlist() included = %v, want %v", included, tt.expectedInclude)
6373
}
6474

65-
if !reflect.DeepEqual(got, tt.expected) {
66-
t.Errorf("AllowedOnly() = %v, want %v", got, tt.expected)
75+
if tt.expectedExclude == nil && len(excluded) == 0 {
76+
// ok
77+
} else if !reflect.DeepEqual(excluded, tt.expectedExclude) {
78+
t.Errorf("partitionByAllowlist() excluded = %v, want %v", excluded, tt.expectedExclude)
6779
}
6880
})
6981
}
@@ -94,7 +106,7 @@ func TestKeys(t *testing.T) {
94106

95107
for _, tt := range tests {
96108
t.Run(tt.name, func(t *testing.T) {
97-
got := Keys(tt.input)
109+
got := keys(tt.input)
98110
if !reflect.DeepEqual(got, tt.expected) {
99111
t.Errorf("Keys() = %v, want %v", got, tt.expected)
100112
}

0 commit comments

Comments
 (0)