-
Notifications
You must be signed in to change notification settings - Fork 64
feat: Add vaultRead() and vaultKV() template functions to read secrets from HashiCorp Vault (GH-473) #848
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
Draft
DeekshithaTimmareddy
wants to merge
7
commits into
main
Choose a base branch
from
feature/vault-kv-473
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: Add vaultRead() and vaultKV() template functions to read secrets from HashiCorp Vault (GH-473) #848
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0717aae
feat: Add Vault KV integration for template variables (GH-473)
DeekshithaTimmareddy 1811673
Merge remote-tracking branch 'origin/main' into feature/vault-kv-473
DeekshithaTimmareddy 94106d4
docs: Add CHANGELOG entry for Vault integration
DeekshithaTimmareddy 6c2f8e5
Merge main into feature/vault-kv-473
DeekshithaTimmareddy 105aa30
add Vault KV variable source support
DeekshithaTimmareddy b5e5da6
Merge remote-tracking branch 'origin/main' into feature/vault-kv-473
DeekshithaTimmareddy 424bf48
stabilize Vault variable source CLI test
DeekshithaTimmareddy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ import ( | |
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path" | ||
| "path/filepath" | ||
|
|
@@ -18,6 +20,7 @@ import ( | |
|
|
||
| "github.com/hashicorp/nomad/api" | ||
| "github.com/hashicorp/nomad/command/agent" | ||
| vault "github.com/hashicorp/vault/api" | ||
| "github.com/mitchellh/cli" | ||
| "github.com/shoenig/test/must" | ||
| "github.com/shoenig/test/wait" | ||
|
|
@@ -52,6 +55,36 @@ const ( | |
| testPlanCmdString = "plan --exit-code-no-changes=90 --exit-code-makes-changes=91 --exit-code-error=92" | ||
| ) | ||
|
|
||
| func testVaultClientForCLI(t *testing.T, handler http.HandlerFunc) *vault.Client { | ||
| t.Helper() | ||
|
|
||
| server := httptest.NewServer(handler) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| cfg := vault.DefaultConfig() | ||
| cfg.Address = server.URL | ||
|
|
||
| client, err := vault.NewClient(cfg) | ||
| must.NoError(t, err) | ||
|
|
||
| return client | ||
| } | ||
|
|
||
| func withEnv(t *testing.T, key, value string) { | ||
| t.Helper() | ||
|
|
||
| prev, ok := os.LookupEnv(key) | ||
| must.NoError(t, os.Setenv(key, value)) | ||
|
|
||
| t.Cleanup(func() { | ||
| if ok { | ||
| _ = os.Setenv(key, prev) | ||
| return | ||
| } | ||
| _ = os.Unsetenv(key) | ||
| }) | ||
| } | ||
|
|
||
| func TestCLI_CreateTestRegistry(t *testing.T) { | ||
| // This test is here to help setup the pack registry cache. It needs to be | ||
| // the first one in the file and can not be `Parallel()` | ||
|
|
@@ -88,6 +121,71 @@ func TestCLI_Version(t *testing.T) { | |
| must.Zero(t, exitCode) | ||
| } | ||
|
|
||
| func TestCLI_Render_VaultVariableSource(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| vaultClient := testVaultClientForCLI(t, func(w http.ResponseWriter, r *http.Request) { | ||
| must.Eq(t, "/v1/secret/data/myapp", r.URL.Path) | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| must.NoError(t, json.NewEncoder(w).Encode(map[string]any{ | ||
| "data": map[string]any{ | ||
| "data": map[string]any{ | ||
| "region": "us-east-1", | ||
| "count": float64(3), | ||
| }, | ||
| }, | ||
| })) | ||
| }) | ||
|
|
||
| withEnv(t, "VAULT_ADDR", vaultClient.Address()) | ||
|
|
||
| result := runPackCmd(t, []string{ | ||
| "render", | ||
| "--no-format=true", | ||
| "--var-source=vault", | ||
| "--vault-var-path=secret/data/myapp", | ||
| testfixture.AbsPath(t, "v2/simple_raw_exec_v2"), | ||
| }) | ||
|
|
||
| must.Zero(t, result.exitCode, must.Sprintf("stdout:\n%s\n\nstderr:\n%s\n", result.cmdOut.String(), result.cmdErr.String())) | ||
| must.Eq(t, "", result.cmdErr.String(), must.Sprintf("cmdErr should be empty, but was %q", result.cmdErr.String())) | ||
| must.StrContains(t, result.cmdOut.String(), `region = "us-east-1"`) | ||
| must.StrContains(t, result.cmdOut.String(), `count = 3`) | ||
| } | ||
|
|
||
| func TestCLI_Render_VaultVariableSource_MissingPath(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| result := runPackCmd(t, []string{ | ||
| "render", | ||
| "--no-format=true", | ||
| "--var-source=vault", | ||
| testfixture.AbsPath(t, "v2/simple_raw_exec_v2"), | ||
| }) | ||
|
|
||
| must.Eq(t, 1, result.exitCode) | ||
| out := result.cmdOut.String() | ||
| if !strings.Contains(out, "vault variable source requires a non-empty path") && !strings.Contains(out, "a Vault-backed variable source was requested, but no Vault client is configured") { | ||
| t.Fatalf("expected missing-path or missing-client error, got:\n%s", out) | ||
| } | ||
|
Comment on lines
+169
to
+171
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could use |
||
| } | ||
|
|
||
| func TestCLI_Render_VariableSource_Unsupported(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| result := runPackCmd(t, []string{ | ||
| "render", | ||
| "--no-format=true", | ||
| "--var-source=consul", | ||
| testfixture.AbsPath(t, "v2/simple_raw_exec_v2"), | ||
| }) | ||
|
|
||
| must.Eq(t, 1, result.exitCode) | ||
| must.StrContains(t, result.cmdOut.String(), `unsupported variable source type "consul"`) | ||
|
|
||
| } | ||
|
|
||
| func TestCLI_JobRun(t *testing.T) { | ||
| ct.HTTPTestParallel(t, ct.WithDefaultConfig(), func(s *agent.TestAgent) { | ||
| expectGoodPackDeploy(t, runTestPackCmd(t, s, []string{"run", getTestPackPath(t, testPack)})) | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this list order needs to be flipped as the numbered list implies external sources have the highest priority (listed as #1), whereas the closing sentence says "CLI
--varvalues override Vault-sourced values", which means Vault is actually lowest priority.