-
Notifications
You must be signed in to change notification settings - Fork 55
feat(agent-proxy): resolve connect config from flag, env, and .infisical.json #319
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6ee3e89
feat(agent-proxy): resolve connect config from flag, env, and .infisiβ¦
saifsmailbox98 d2df451
test(agent-proxy): mirror connect's empty --env default; fix resolverβ¦
saifsmailbox98 c29d9c4
fix(agent-proxy): do not source the proxy address from .infisical.json
saifsmailbox98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,180 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // newResolveTestCmd builds a command with the flags the resolvers read. Passing a non-empty | ||
| // value marks that flag as explicitly set (Changed == true), mirroring a user-passed flag. | ||
| func newResolveTestCmd(t *testing.T) *cobra.Command { | ||
| t.Helper() | ||
| cmd := &cobra.Command{Use: "test"} | ||
| cmd.Flags().String("proxy", "", "") | ||
| cmd.Flags().StringP("env", "e", "", "") | ||
| cmd.Flags().String("path", "/", "") | ||
| cmd.Flags().Bool("allow", false, "") | ||
| return cmd | ||
| } | ||
|
|
||
| // writeWorkspace drops a .infisical.json into an isolated cwd so file-fallback is deterministic. | ||
| func writeWorkspace(t *testing.T, contents string) { | ||
| t.Helper() | ||
| dir := t.TempDir() | ||
| t.Chdir(dir) | ||
| if err := os.WriteFile(filepath.Join(dir, ".infisical.json"), []byte(contents), 0o600); err != nil { | ||
| t.Fatalf("write workspace: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveEnvironmentName(t *testing.T) { | ||
| t.Run("flag wins over env and file", func(t *testing.T) { | ||
| writeWorkspace(t, `{"defaultEnvironment":"fromfile"}`) | ||
| t.Setenv(INFISICAL_ENVIRONMENT_NAME, "fromenv") | ||
| cmd := newResolveTestCmd(t) | ||
| _ = cmd.Flags().Set("env", "fromflag") | ||
| if got := ResolveEnvironmentName(cmd); got != "fromflag" { | ||
| t.Fatalf("got %q, want fromflag", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("env wins over file when flag unset", func(t *testing.T) { | ||
| writeWorkspace(t, `{"defaultEnvironment":"fromfile"}`) | ||
| t.Setenv(INFISICAL_ENVIRONMENT_NAME, "fromenv") | ||
| if got := ResolveEnvironmentName(newResolveTestCmd(t)); got != "fromenv" { | ||
| t.Fatalf("got %q, want fromenv", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("file used when flag and env unset", func(t *testing.T) { | ||
| writeWorkspace(t, `{"defaultEnvironment":"fromfile"}`) | ||
| t.Setenv(INFISICAL_ENVIRONMENT_NAME, "") | ||
| if got := ResolveEnvironmentName(newResolveTestCmd(t)); got != "fromfile" { | ||
| t.Fatalf("got %q, want fromfile", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("flag default is the final fallback", func(t *testing.T) { | ||
| t.Chdir(t.TempDir()) // no workspace file | ||
| t.Setenv(INFISICAL_ENVIRONMENT_NAME, "") | ||
| if got := ResolveEnvironmentName(newResolveTestCmd(t)); got != "" { | ||
| t.Fatalf("got %q, want empty", got) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestResolveSecretPath(t *testing.T) { | ||
| t.Run("flag wins", func(t *testing.T) { | ||
| writeWorkspace(t, `{"defaultSecretPath":"/fromfile"}`) | ||
| t.Setenv(INFISICAL_SECRET_PATH_NAME, "/fromenv") | ||
| cmd := newResolveTestCmd(t) | ||
| _ = cmd.Flags().Set("path", "/fromflag") | ||
| if got := ResolveSecretPath(cmd); got != "/fromflag" { | ||
| t.Fatalf("got %q, want /fromflag", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("env over file", func(t *testing.T) { | ||
| writeWorkspace(t, `{"defaultSecretPath":"/fromfile"}`) | ||
| t.Setenv(INFISICAL_SECRET_PATH_NAME, "/fromenv") | ||
| if got := ResolveSecretPath(newResolveTestCmd(t)); got != "/fromenv" { | ||
| t.Fatalf("got %q, want /fromenv", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("file when flag and env unset", func(t *testing.T) { | ||
| writeWorkspace(t, `{"defaultSecretPath":"/fromfile"}`) | ||
| t.Setenv(INFISICAL_SECRET_PATH_NAME, "") | ||
| if got := ResolveSecretPath(newResolveTestCmd(t)); got != "/fromfile" { | ||
| t.Fatalf("got %q, want /fromfile", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("defaults to /", func(t *testing.T) { | ||
| t.Chdir(t.TempDir()) | ||
| t.Setenv(INFISICAL_SECRET_PATH_NAME, "") | ||
| if got := ResolveSecretPath(newResolveTestCmd(t)); got != "/" { | ||
| t.Fatalf("got %q, want /", got) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestResolveAgentProxyAddress(t *testing.T) { | ||
| t.Run("flag wins over env", func(t *testing.T) { | ||
| t.Setenv(INFISICAL_AGENT_PROXY_ADDRESS_NAME, "env:1") | ||
| cmd := newResolveTestCmd(t) | ||
| _ = cmd.Flags().Set("proxy", "flag:1") | ||
| if got := ResolveAgentProxyAddress(cmd); got != "flag:1" { | ||
| t.Fatalf("got %q, want flag:1", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("env when flag unset", func(t *testing.T) { | ||
| t.Setenv(INFISICAL_AGENT_PROXY_ADDRESS_NAME, "env:1") | ||
| if got := ResolveAgentProxyAddress(newResolveTestCmd(t)); got != "env:1" { | ||
| t.Fatalf("got %q, want env:1", got) | ||
| } | ||
| }) | ||
|
|
||
| // The proxy address must never come from .infisical.json (a committed file), or a poisoned | ||
| // value could silently redirect all agent traffic. Prove the file is ignored. | ||
| t.Run("workspace file is not a source", func(t *testing.T) { | ||
| writeWorkspace(t, `{"agentProxyAddress":"file:1"}`) | ||
| t.Setenv(INFISICAL_AGENT_PROXY_ADDRESS_NAME, "") | ||
| if got := ResolveAgentProxyAddress(newResolveTestCmd(t)); got != "" { | ||
| t.Fatalf("got %q, want empty (.infisical.json must not set the proxy address)", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("empty when nothing set", func(t *testing.T) { | ||
| t.Chdir(t.TempDir()) | ||
| t.Setenv(INFISICAL_AGENT_PROXY_ADDRESS_NAME, "") | ||
| if got := ResolveAgentProxyAddress(newResolveTestCmd(t)); got != "" { | ||
| t.Fatalf("got %q, want empty", got) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestGetBoolFlagOrEnv(t *testing.T) { | ||
| const env = "INFISICAL_TEST_BOOL" | ||
|
|
||
| t.Run("explicit flag wins", func(t *testing.T) { | ||
| t.Setenv(env, "false") | ||
| cmd := newResolveTestCmd(t) | ||
| _ = cmd.Flags().Set("allow", "true") | ||
| if !GetBoolFlagOrEnv(cmd, "allow", env) { | ||
| t.Fatal("expected true from flag") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("env true when flag unset", func(t *testing.T) { | ||
| t.Setenv(env, "true") | ||
| if !GetBoolFlagOrEnv(newResolveTestCmd(t), "allow", env) { | ||
| t.Fatal("expected true from env") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("env false when flag unset", func(t *testing.T) { | ||
| t.Setenv(env, "false") | ||
| if GetBoolFlagOrEnv(newResolveTestCmd(t), "allow", env) { | ||
| t.Fatal("expected false from env") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("unparseable env fails closed to false", func(t *testing.T) { | ||
| t.Setenv(env, "yeah") | ||
| if GetBoolFlagOrEnv(newResolveTestCmd(t), "allow", env) { | ||
| t.Fatal("expected false on unparseable env") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("flag default when nothing set", func(t *testing.T) { | ||
| t.Setenv(env, "") | ||
| if GetBoolFlagOrEnv(newResolveTestCmd(t), "allow", env) { | ||
| t.Fatal("expected false (flag default)") | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.