Skip to content

Commit 4c22830

Browse files
authored
Merge pull request #23 from git-flexi/master
Adding first simple key value implementation
2 parents faf4022 + 6855608 commit 4c22830

File tree

4 files changed

+203
-12
lines changed

4 files changed

+203
-12
lines changed

client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ func NewClient(addr string, tlsConf *TLSConfig, opts ...ClientOpts) (*Client, er
7070
return nil, err
7171
}
7272

73-
client := &Client{Client: vaultClient, conf: conf, tlsConf: tlsConf}
73+
client := &Client{
74+
Client: vaultClient,
75+
conf: conf,
76+
tlsConf: tlsConf,
77+
}
7478

7579
for _, opt := range opts {
7680
err := opt(client)
@@ -184,3 +188,7 @@ func (c *Client) Delete(path []string, body, response interface{}, opts *Request
184188
func (c *Client) List(path []string, body, response interface{}, opts *RequestOptions) error {
185189
return c.Request("LIST", path, body, response, opts)
186190
}
191+
192+
func (c *Client) Put(path []string, body, response interface{}, opts *RequestOptions) error {
193+
return c.Request("PUT", path, body, response, opts)
194+
}

kv_v1.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package vault
2+
3+
import (
4+
"net/url"
5+
)
6+
7+
const (
8+
pathPrefix string = "v1"
9+
)
10+
11+
type KVv1 struct {
12+
Service
13+
}
14+
15+
func (c *Client) KVv1() *KVv1 {
16+
return c.KVv1WithMountPoint("kv")
17+
}
18+
19+
func (c *Client) KVv1WithMountPoint(mountPoint string) *KVv1 {
20+
return &KVv1{
21+
Service: Service{
22+
client: c,
23+
MountPoint: mountPoint,
24+
},
25+
}
26+
}
27+
28+
func (k *KVv1) Create(id string, data map[string]string) error {
29+
err := k.client.Write(
30+
[]string{
31+
pathPrefix,
32+
k.MountPoint,
33+
url.PathEscape(id),
34+
}, data, nil, nil,
35+
)
36+
if err != nil {
37+
return err
38+
}
39+
40+
return nil
41+
}
42+
43+
type KVv1ReadResponse struct {
44+
Data map[string]string `json:"data"`
45+
}
46+
47+
func (k *KVv1) Read(key string) (*KVv1ReadResponse, error) {
48+
readRes := &KVv1ReadResponse{}
49+
50+
err := k.client.Read(
51+
[]string{
52+
pathPrefix,
53+
k.MountPoint,
54+
url.PathEscape(key),
55+
}, readRes, nil,
56+
)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return readRes, nil
62+
}
63+
64+
func (k *KVv1) Delete(key string) error {
65+
err := k.client.Delete(
66+
[]string{
67+
pathPrefix,
68+
k.MountPoint,
69+
url.PathEscape(key),
70+
}, nil, nil, nil,
71+
)
72+
if err != nil {
73+
return err
74+
}
75+
76+
return nil
77+
}

kv_v1_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package vault_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
vault "github.com/mittwald/vaultgo"
10+
"github.com/mittwald/vaultgo/test/testdata"
11+
12+
"github.com/stretchr/testify/suite"
13+
)
14+
15+
type KVv1TestSuite struct {
16+
suite.Suite
17+
client *vault.KVv1
18+
}
19+
20+
func TestKVv1TestSuite(t *testing.T) {
21+
for _, v := range testdata.VaultVersions {
22+
require.NoError(t, testdata.Init(context.Background(), v))
23+
24+
t.Logf("using vault uri %v", testdata.Vault.URI())
25+
client, _ := vault.NewClient(testdata.Vault.URI(), vault.WithCaPath(""))
26+
client.SetToken(testdata.Vault.Token())
27+
keyValue := client.KVv1()
28+
29+
keyValueTestSuite := new(KVv1TestSuite)
30+
keyValueTestSuite.client = keyValue
31+
32+
suite.Run(t, keyValueTestSuite)
33+
}
34+
}
35+
36+
func (s *KVv1TestSuite) TestCreateAndRead() {
37+
testKeyValues := make(map[string]string)
38+
testKeyValues["PrivateKey"] = "abcde"
39+
40+
err := s.client.Create("9697fdce-39df-45ac-9115-5e3913c34613", testKeyValues)
41+
require.NoError(s.T(), err)
42+
43+
readResponse, readErr := s.client.Read("9697fdce-39df-45ac-9115-5e3913c34613")
44+
require.NoError(s.T(), readErr)
45+
46+
require.Equal(s.T(), readResponse.Data, testKeyValues)
47+
}
48+
49+
func (s *KVv1TestSuite) TestOverwriteAndRead() {
50+
testKeyValues := make(map[string]string)
51+
testKeyValues["PrivateKey"] = "abcde"
52+
testKeyValues["PrivateKey2"] = "fghji"
53+
54+
err := s.client.Create("9697fdce-39df-45ac-9115-5e3913c34613", testKeyValues)
55+
require.NoError(s.T(), err)
56+
57+
testKeyValuesNew := make(map[string]string)
58+
testKeyValuesNew["PrivateKey"] = "klmnop"
59+
60+
err = s.client.Create("9697fdce-39df-45ac-9115-5e3913c34613", testKeyValuesNew)
61+
require.NoError(s.T(), err)
62+
63+
readResponse, readErr := s.client.Read("9697fdce-39df-45ac-9115-5e3913c34613")
64+
require.NoError(s.T(), readErr)
65+
66+
require.Equal(s.T(), readResponse.Data, testKeyValuesNew)
67+
}
68+
69+
func (s *KVv1TestSuite) TestCreateAndDelete() {
70+
testKeyValues := make(map[string]string)
71+
testKeyValues["PrivateKey"] = "abcde"
72+
73+
err := s.client.Create("2b7ff26d-30b7-43ba-96d5-79b4baba9b39", testKeyValues)
74+
require.NoError(s.T(), err)
75+
76+
deleteErr := s.client.Delete("2b7ff26d-30b7-43ba-96d5-79b4baba9b39")
77+
require.NoError(s.T(), deleteErr)
78+
79+
_, readErr := s.client.Read("2b7ff26d-30b7-43ba-96d5-79b4baba9b39")
80+
require.Error(s.T(), readErr)
81+
}

test/testdata/container_vault.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import (
99
"github.com/testcontainers/testcontainers-go/wait"
1010
)
1111

12-
var VaultVersions = []string{"1.6.7", "1.7.5", "1.8.4", "1.9.3", "1.12.2"}
12+
var VaultVersions = []string{
13+
"1.6.7",
14+
"1.7.5",
15+
"1.8.4",
16+
"1.9.3",
17+
"1.12.2",
18+
}
1319

1420
type VaultContainer struct {
1521
container testcontainers.Container
@@ -59,10 +65,12 @@ func InitVaultContainer(ctx context.Context, version string) (*VaultContainer, e
5965
Privileged: true,
6066
}
6167

62-
v, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
63-
ContainerRequest: req,
64-
Started: true,
65-
})
68+
v, err := testcontainers.GenericContainer(
69+
ctx, testcontainers.GenericContainerRequest{
70+
ContainerRequest: req,
71+
Started: true,
72+
},
73+
)
6674
if err != nil {
6775
return nil, err
6876
}
@@ -84,12 +92,29 @@ func InitVaultContainer(ctx context.Context, version string) (*VaultContainer, e
8492
return nil, err
8593
}
8694

87-
_, _, err = vc.container.Exec(ctx, []string{
88-
"vault",
89-
"secrets",
90-
"enable",
91-
"transit",
92-
})
95+
// transit mount
96+
_, _, err = vc.container.Exec(
97+
ctx, []string{
98+
"vault",
99+
"secrets",
100+
"enable",
101+
"transit",
102+
},
103+
)
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
// KVv1 mount
109+
_, _, err = vc.container.Exec(
110+
ctx, []string{
111+
"vault",
112+
"secrets",
113+
"enable",
114+
"-version=1",
115+
"kv",
116+
},
117+
)
93118
if err != nil {
94119
return nil, err
95120
}

0 commit comments

Comments
 (0)