From c0e5a47aa93f689246b5f5890d80406fead2674c Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 13 Apr 2024 01:11:54 +0200 Subject: [PATCH] + expand tests --- go_client/TODO.md | 5 ++- go_client/go.mod | 5 ++- go_client/go.sum | 7 +++ go_client/main.go | 29 +----------- go_client/tests/secret_test.go | 80 ++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 30 deletions(-) create mode 100644 go_client/tests/secret_test.go diff --git a/go_client/TODO.md b/go_client/TODO.md index 4002ff2..5b618bf 100644 --- a/go_client/TODO.md +++ b/go_client/TODO.md @@ -8,4 +8,7 @@ - Generation (- TLS module) -(- Auth module) \ No newline at end of file +(- Auth module) + + +==> Nur KvV2 testen \ No newline at end of file diff --git a/go_client/go.mod b/go_client/go.mod index a29e464..c7c5011 100644 --- a/go_client/go.mod +++ b/go_client/go.mod @@ -1,13 +1,14 @@ -module github.com/C0ffeeCode/rvault +module github.com/C0ffeeCode/rvault/go_client go 1.21.9 +require github.com/hashicorp/vault-client-go v0.4.3 + require ( github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-retryablehttp v0.7.5 // indirect github.com/hashicorp/go-rootcerts v1.0.2 // indirect github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect - github.com/hashicorp/vault-client-go v0.4.3 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/ryanuber/go-glob v1.0.0 // indirect golang.org/x/sys v0.19.0 // indirect diff --git a/go_client/go.sum b/go_client/go.sum index e39cfb2..2a347d0 100644 --- a/go_client/go.sum +++ b/go_client/go.sum @@ -1,6 +1,8 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= +github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI= github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M= github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8= @@ -12,11 +14,16 @@ github.com/hashicorp/vault-client-go v0.4.3 h1:zG7STGVgn/VK6rnZc0k8PGbfv2x/sJExR github.com/hashicorp/vault-client-go v0.4.3/go.mod h1:4tDw7Uhq5XOxS1fO+oMtotHL7j4sB9cp0T7U6m4FzDY= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/go_client/main.go b/go_client/main.go index 884ff59..060e2f8 100644 --- a/go_client/main.go +++ b/go_client/main.go @@ -1,19 +1,17 @@ package main import ( - "context" "log" + "log/slog" "time" "github.com/hashicorp/vault-client-go" - "github.com/hashicorp/vault-client-go/schema" ) // vault cmd args: >vault server -dev -dev-root-token-id="my-token" func main() { - ctx := context.Background() - + slog.Info("run tests in tests/ with >go test") // prepare a client with the given base address client, err := vault.New( vault.WithAddress("http://localhost:8200"), @@ -28,27 +26,4 @@ func main() { if err := client.SetToken("my-token"); err != nil { log.Fatal(err) } - - // FIRST TEST - // write a secret - _, err = client.Secrets.KvV2Write(ctx, "foo", schema.KvV2WriteRequest{ - Data: map[string]any{ - "password1": "abc123", - "password2": "correct horse battery staple", - }}, - vault.WithMountPath("kw_mount_path"), - ) - if err != nil { - log.Fatal("kv2: Failed to write secret:\n\t", err) - } else { - log.Println("kv2: Secret written (apparently)") - } - - // read the secret - s, err := client.Secrets.KvV2Read(ctx, "foo", vault.WithMountPath("secret")) - if err != nil { - log.Fatal(err) - } else { - log.Println("secret retrieved:", s.Data.Data) - } } diff --git a/go_client/tests/secret_test.go b/go_client/tests/secret_test.go new file mode 100644 index 0000000..43ac26e --- /dev/null +++ b/go_client/tests/secret_test.go @@ -0,0 +1,80 @@ +package tests + +import ( + "context" + "log" + "os" + "testing" + "time" + + "github.com/hashicorp/vault-client-go" + "github.com/hashicorp/vault-client-go/schema" +) + +var client *vault.Client +var ctx context.Context + +func TestMain(m *testing.M) { + ctx = context.Background() + var err error + // prepare a client with the given base address + client, err = vault.New( + vault.WithAddress("http://localhost:8200"), + vault.WithRequestTimeout(30*time.Second), + ) + if err != nil { + log.Fatal(err) + } + log.Println("client prepared") + + // authenticate with a root token (insecure) + if err := client.SetToken("my-token"); err != nil { + log.Fatal(err) + } + + exitCode := m.Run() // run all tests and get code + os.Exit(exitCode) +} + +// @Philip der Path steht in der KvV2Write Methode +func TestWriteSecret(t *testing.T) { + // Path foo + _, err := client.Secrets.KvV2Write(ctx, "foo", schema.KvV2WriteRequest{ + Data: map[string]any{ + "password1": "abc123", + "password2": "correct horse battery staple", + }}, + vault.WithMountPath("kw_mount_path"), + ) + if err != nil { + log.Fatal("kv2: Failed to write secret:\n\t", err) + } + log.Println("kv2: Tried to write Secret at foo") + + // Path bar + _, err = client.Secrets.KvV2Write(ctx, "bar", schema.KvV2WriteRequest{ + Data: map[string]any{ + "password1": "secure123", + "password2": "second password", + }}, + vault.WithMountPath("kw_mount_path"), + ) + if err != nil { + log.Fatal("kv2: Failed to write secret:\n\t", err) + } + log.Println("kv2: Tried to write Secret at foo") +} + +func TestDeleteSecret(t *testing.T) { + _, err := client.Secrets.KvV2Delete(ctx, "foo") // currently disregarding modifier options + if err != nil { + log.Fatal("kv2: Failed to delete secret:\n\t", err) + } +} + +func TestReadSecret(t *testing.T) { + _, err := client.Secrets.KvV2Read(ctx, "bar") + if err != nil { + log.Fatal("kv2: Failed to read secret:\n\t", err) + } +}