Skip to content

Commit 048598c

Browse files
authored
Process EnvFileDir in file-based config mode (#2470)
Fixes vault secrets not being passed to workload pods when using file-based configuration in the proxy runner. The runWithFileBasedConfig function was loading the RunConfig from the ConfigMap but never processing the EnvFileDir field. This caused Vault Agent injected secrets to remain in the proxy pod at /vault/secrets without being propagated to the workload StatefulSet. The fix adds environment file processing when EnvFileDir is set, matching the behavior of the flags-based configuration path. When the operator detects Vault annotations, it sets EnvFileDir to /vault/secrets in the RunConfig. The proxy runner now reads files from that directory and merges them into config.EnvVars before deploying the workload. Fixes #2460
1 parent b68f85d commit 048598c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

cmd/thv-proxyrunner/app/execution.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ func runWithFileBasedConfig(
127127
config.EnvVars = validatedEnvVars
128128
}
129129

130+
// Process environment files from EnvFileDir if specified (e.g., for Vault secrets)
131+
if config.EnvFileDir != "" {
132+
updatedConfig, err := config.WithEnvFilesFromDirectory(config.EnvFileDir)
133+
if err != nil {
134+
return fmt.Errorf("failed to process environment files from directory %s: %v", config.EnvFileDir, err)
135+
}
136+
config = updatedConfig
137+
}
138+
130139
// Apply image metadata overrides if needed (similar to what the builder does)
131140
if imageMetadata != nil && config.Name == "" {
132141
config.Name = imageMetadata.Name

cmd/thv-proxyrunner/app/run_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package app
22

33
import (
4+
"os"
5+
"path/filepath"
46
"testing"
57

68
"github.com/stretchr/testify/assert"
79
"github.com/stretchr/testify/require"
10+
11+
"github.com/stacklok/toolhive/pkg/runner"
812
)
913

1014
func TestRunCmdFlagsAndParsing(t *testing.T) {
@@ -207,6 +211,35 @@ func TestRunWithFileBasedConfigBehavior(t *testing.T) {
207211
k8sPodPatchFlag := cmd.Flag("k8s-pod-patch")
208212
assert.NotNil(t, k8sPodPatchFlag, "k8s-pod-patch flag should exist for config file mode")
209213
})
214+
215+
t.Run("EnvFileDir processing works with WithEnvFilesFromDirectory", func(t *testing.T) {
216+
t.Parallel()
217+
218+
// This test verifies the pattern used in the fix for issue #2460
219+
// It simulates what happens when Vault secrets are injected
220+
221+
// Create temp directory with mock Vault secret files
222+
tmpDir := t.TempDir()
223+
err := os.WriteFile(filepath.Join(tmpDir, "api-key"), []byte("API_KEY=secret123"), 0644)
224+
require.NoError(t, err)
225+
err = os.WriteFile(filepath.Join(tmpDir, "db-password"), []byte("DB_PASSWORD=dbpass456"), 0644)
226+
require.NoError(t, err)
227+
228+
// Create a RunConfig with EnvFileDir set (this is what the operator does)
229+
config := &runner.RunConfig{
230+
EnvFileDir: tmpDir,
231+
EnvVars: map[string]string{"EXISTING": "value"},
232+
}
233+
234+
// Call WithEnvFilesFromDirectory (this is what the fix does)
235+
updatedConfig, err := config.WithEnvFilesFromDirectory(config.EnvFileDir)
236+
require.NoError(t, err)
237+
238+
// Verify env vars from files were merged
239+
assert.Equal(t, "value", updatedConfig.EnvVars["EXISTING"], "Existing env var should be preserved")
240+
assert.Equal(t, "secret123", updatedConfig.EnvVars["API_KEY"], "API_KEY from file should be loaded")
241+
assert.Equal(t, "dbpass456", updatedConfig.EnvVars["DB_PASSWORD"], "DB_PASSWORD from file should be loaded")
242+
})
210243
}
211244

212245
func TestConfigFileModeIgnoresConfigFlags(t *testing.T) {

0 commit comments

Comments
 (0)