-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from git-flexi/master
Adding first simple key value implementation
- Loading branch information
Showing
4 changed files
with
203 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package vault | ||
|
||
import ( | ||
"net/url" | ||
) | ||
|
||
const ( | ||
pathPrefix string = "v1" | ||
) | ||
|
||
type KVv1 struct { | ||
Service | ||
} | ||
|
||
func (c *Client) KVv1() *KVv1 { | ||
return c.KVv1WithMountPoint("kv") | ||
} | ||
|
||
func (c *Client) KVv1WithMountPoint(mountPoint string) *KVv1 { | ||
return &KVv1{ | ||
Service: Service{ | ||
client: c, | ||
MountPoint: mountPoint, | ||
}, | ||
} | ||
} | ||
|
||
func (k *KVv1) Create(id string, data map[string]string) error { | ||
err := k.client.Write( | ||
[]string{ | ||
pathPrefix, | ||
k.MountPoint, | ||
url.PathEscape(id), | ||
}, data, nil, nil, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type KVv1ReadResponse struct { | ||
Data map[string]string `json:"data"` | ||
} | ||
|
||
func (k *KVv1) Read(key string) (*KVv1ReadResponse, error) { | ||
readRes := &KVv1ReadResponse{} | ||
|
||
err := k.client.Read( | ||
[]string{ | ||
pathPrefix, | ||
k.MountPoint, | ||
url.PathEscape(key), | ||
}, readRes, nil, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return readRes, nil | ||
} | ||
|
||
func (k *KVv1) Delete(key string) error { | ||
err := k.client.Delete( | ||
[]string{ | ||
pathPrefix, | ||
k.MountPoint, | ||
url.PathEscape(key), | ||
}, nil, nil, nil, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package vault_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
vault "github.com/mittwald/vaultgo" | ||
"github.com/mittwald/vaultgo/test/testdata" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type KVv1TestSuite struct { | ||
suite.Suite | ||
client *vault.KVv1 | ||
} | ||
|
||
func TestKVv1TestSuite(t *testing.T) { | ||
for _, v := range testdata.VaultVersions { | ||
require.NoError(t, testdata.Init(context.Background(), v)) | ||
|
||
t.Logf("using vault uri %v", testdata.Vault.URI()) | ||
client, _ := vault.NewClient(testdata.Vault.URI(), vault.WithCaPath("")) | ||
client.SetToken(testdata.Vault.Token()) | ||
keyValue := client.KVv1() | ||
|
||
keyValueTestSuite := new(KVv1TestSuite) | ||
keyValueTestSuite.client = keyValue | ||
|
||
suite.Run(t, keyValueTestSuite) | ||
} | ||
} | ||
|
||
func (s *KVv1TestSuite) TestCreateAndRead() { | ||
testKeyValues := make(map[string]string) | ||
testKeyValues["PrivateKey"] = "abcde" | ||
|
||
err := s.client.Create("9697fdce-39df-45ac-9115-5e3913c34613", testKeyValues) | ||
require.NoError(s.T(), err) | ||
|
||
readResponse, readErr := s.client.Read("9697fdce-39df-45ac-9115-5e3913c34613") | ||
require.NoError(s.T(), readErr) | ||
|
||
require.Equal(s.T(), readResponse.Data, testKeyValues) | ||
} | ||
|
||
func (s *KVv1TestSuite) TestOverwriteAndRead() { | ||
testKeyValues := make(map[string]string) | ||
testKeyValues["PrivateKey"] = "abcde" | ||
testKeyValues["PrivateKey2"] = "fghji" | ||
|
||
err := s.client.Create("9697fdce-39df-45ac-9115-5e3913c34613", testKeyValues) | ||
require.NoError(s.T(), err) | ||
|
||
testKeyValuesNew := make(map[string]string) | ||
testKeyValuesNew["PrivateKey"] = "klmnop" | ||
|
||
err = s.client.Create("9697fdce-39df-45ac-9115-5e3913c34613", testKeyValuesNew) | ||
require.NoError(s.T(), err) | ||
|
||
readResponse, readErr := s.client.Read("9697fdce-39df-45ac-9115-5e3913c34613") | ||
require.NoError(s.T(), readErr) | ||
|
||
require.Equal(s.T(), readResponse.Data, testKeyValuesNew) | ||
} | ||
|
||
func (s *KVv1TestSuite) TestCreateAndDelete() { | ||
testKeyValues := make(map[string]string) | ||
testKeyValues["PrivateKey"] = "abcde" | ||
|
||
err := s.client.Create("2b7ff26d-30b7-43ba-96d5-79b4baba9b39", testKeyValues) | ||
require.NoError(s.T(), err) | ||
|
||
deleteErr := s.client.Delete("2b7ff26d-30b7-43ba-96d5-79b4baba9b39") | ||
require.NoError(s.T(), deleteErr) | ||
|
||
_, readErr := s.client.Read("2b7ff26d-30b7-43ba-96d5-79b4baba9b39") | ||
require.Error(s.T(), readErr) | ||
} |
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