Skip to content

Commit

Permalink
Test getEntries with a locally running vault
Browse files Browse the repository at this point in the history
  • Loading branch information
slarwise committed Aug 3, 2024
1 parent e7ece5f commit b791d4d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pole3
Library
70 changes: 70 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"
"os"
"os/exec"
"slices"
"testing"
"time"
)

func TestGetKeys(t *testing.T) {
token := "dev-only-token"
vaultAddr := "http://127.0.0.1:8200"
cmd := exec.Command("vault", "server", "-dev", "-dev-root-token-id", token, "-address", vaultAddr)
if err := cmd.Start(); err != nil {
t.Fatalf("Failed to start the vault server: %s", err.Error())
}
defer func() {
if err := cmd.Process.Signal(os.Interrupt); err != nil {
t.Logf("Failed to stop the vault server: %s", err.Error())
}
cmd.Wait()
}()
time.Sleep(1 * time.Second)
secrets := map[string]string{
"/foo": "a=b",
"/bar/baz": "c=d",
"/enterprise/organization/department/unit/team/user/actual-user": "free=palestine",
}
if err := populate(t, vaultAddr, token, secrets); err != nil {
t.Fatalf("Failed to populate vault with secrets: %s", err.Error())
}
vault := VaultClient{
Addr: vaultAddr,
Token: token,
Mount: "secret",
}
entrypoint := DirEnt{
IsDir: true,
Name: "/",
}
keys := getKeys(vault, entrypoint)
if len(keys) != len(secrets) {
t.Fatalf("Expected %d keys, got %d", len(secrets), len(keys))
}
for key := range secrets {
if !slices.Contains(keys, key) {
t.Fatalf("Expected %s to be in keys %v", key, keys)
}
}
}

func populate(t *testing.T, vaultAddr, token string, secrets map[string]string) error {
for key, data := range secrets {
cmd := exec.Command("vault", "kv", "put",
"-mount", "secret",
key, data)
cmd.Env = []string{
fmt.Sprintf("VAULT_ADDR=%s", vaultAddr),
fmt.Sprintf("VAULT_TOKEN=%s", token),
}
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to create secret: %s", output)
}
t.Logf("Created secret: %s", output)
}
return nil
}

0 comments on commit b791d4d

Please sign in to comment.