-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement CreateSecret and DeleteSecret operations (#35)
Closes #33. Add create secret and delete secret operations to the SDK.
- Loading branch information
1 parent
eee8425
commit 6f30855
Showing
4 changed files
with
113 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -50,3 +50,4 @@ _artifacts | |
**/*/bin | ||
mage_output_file.go | ||
dist/ | ||
/test_config.json |
This file contains 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 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 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 |
---|---|---|
@@ -1,20 +1,90 @@ | ||
//go:build integration | ||
package vault | ||
|
||
import "testing" | ||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const secretName = "/test/secret" | ||
const letterBytes = "abcdefghijklmnopqrstuvwxyz" | ||
|
||
func init() { | ||
rand.Seed(time.Now().UnixNano()) | ||
} | ||
|
||
// TestSecret tests Secret | ||
func TestSecret(t *testing.T) { | ||
secret, err := dsv.Secret(secretName) | ||
path := makeRandomSecretPath() | ||
_, cleanup := createSecret(t, path) | ||
defer cleanup() | ||
|
||
secret, err := dsv.Secret(path) | ||
|
||
if err != nil { | ||
t.Fatalf("Secret for path=%s: %s", path, err) | ||
return | ||
} | ||
|
||
if secret.Data == nil { | ||
t.Error("secret.Data is nil") | ||
} | ||
} | ||
|
||
func TestCreateSecret(t *testing.T) { | ||
path := makeRandomSecretPath() | ||
secret, err := dsv.CreateSecret( | ||
path, &SecretCreateRequest{Data: map[string]interface{}{"foo": "bar"}}, | ||
) | ||
|
||
if err != nil { | ||
t.Error("calling secrets.Secret:", err) | ||
t.Fatalf("CreateSecret for path=%s: %s", path, err) | ||
return | ||
} else { | ||
defer func() { deleteSecret(t, path) }() | ||
} | ||
|
||
if secret.Data == nil { | ||
t.Error("secret.Data is nil") | ||
} | ||
} | ||
|
||
func TestDeleteSecret(t *testing.T) { | ||
path := makeRandomSecretPath() | ||
_, _ = createSecret(t, path) // no cleanup required as test tries to delete anyway | ||
|
||
err := dsv.DeleteSecret(path) | ||
if err != nil { | ||
t.Errorf("DeleteSecret for path=%s: %s", path, err) | ||
} | ||
} | ||
|
||
// createSecret creates a secret with given path and returns the created secret as well | ||
// as a cleanup function which should be deferred to remove the secret from the vault. | ||
func createSecret(t *testing.T, path string) (s *Secret, cleanup func()) { | ||
t.Helper() | ||
s, err := dsv.CreateSecret(path, &SecretCreateRequest{Data: map[string]interface{}{ | ||
"foo": "bar", | ||
}}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
cleanup = func() { deleteSecret(t, path) } | ||
return | ||
} | ||
|
||
// deleteSecret deletes the secret given by path from the vault. | ||
func deleteSecret(t *testing.T, path string) { | ||
t.Helper() | ||
if err := dsv.DeleteSecret(path); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
// makeRandomSecretPath creates a pseudo-random secret path. | ||
func makeRandomSecretPath() string { | ||
b := make([]byte, 10) | ||
for i := range b { | ||
b[i] = letterBytes[rand.Intn(len(letterBytes))] | ||
} | ||
return "test:" + string(b) | ||
} |