Skip to content
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

Adding first simple key value implementation #23

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ func NewClient(addr string, tlsConf *TLSConfig, opts ...ClientOpts) (*Client, er
return nil, err
}

client := &Client{Client: vaultClient, conf: conf, tlsConf: tlsConf}
client := &Client{
Client: vaultClient,
conf: conf,
tlsConf: tlsConf,
}

for _, opt := range opts {
err := opt(client)
Expand Down Expand Up @@ -184,3 +188,7 @@ func (c *Client) Delete(path []string, body, response interface{}, opts *Request
func (c *Client) List(path []string, body, response interface{}, opts *RequestOptions) error {
return c.Request("LIST", path, body, response, opts)
}

func (c *Client) Put(path []string, body, response interface{}, opts *RequestOptions) error {
return c.Request("PUT", path, body, response, opts)
}
77 changes: 77 additions & 0 deletions kv_v1.go
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
}
81 changes: 81 additions & 0 deletions kv_v1_test.go
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)
}
47 changes: 36 additions & 11 deletions test/testdata/container_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import (
"github.com/testcontainers/testcontainers-go/wait"
)

var VaultVersions = []string{"1.6.7", "1.7.5", "1.8.4", "1.9.3", "1.12.2"}
var VaultVersions = []string{
"1.6.7",
"1.7.5",
"1.8.4",
"1.9.3",
"1.12.2",
}

type VaultContainer struct {
container testcontainers.Container
Expand Down Expand Up @@ -59,10 +65,12 @@ func InitVaultContainer(ctx context.Context, version string) (*VaultContainer, e
Privileged: true,
}

v, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
v, err := testcontainers.GenericContainer(
ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
},
)
if err != nil {
return nil, err
}
Expand All @@ -84,12 +92,29 @@ func InitVaultContainer(ctx context.Context, version string) (*VaultContainer, e
return nil, err
}

_, _, err = vc.container.Exec(ctx, []string{
"vault",
"secrets",
"enable",
"transit",
})
// transit mount
_, _, err = vc.container.Exec(
ctx, []string{
"vault",
"secrets",
"enable",
"transit",
},
)
if err != nil {
return nil, err
}

// KVv1 mount
_, _, err = vc.container.Exec(
ctx, []string{
"vault",
"secrets",
"enable",
"-version=1",
"kv",
},
)
if err != nil {
return nil, err
}
Expand Down
Loading