-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaultx_test.go
333 lines (254 loc) · 10.7 KB
/
vaultx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package vaultx_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/jaredpetersen/vaultx/kv"
"github.com/jaredpetersen/vaultx"
vaultxauth "github.com/jaredpetersen/vaultx/auth"
"github.com/jaredpetersen/vaultx/internal/testcontainerpostgres"
"github.com/jaredpetersen/vaultx/internal/testcontainervault"
)
func TestIntegrationAPIReadSendsGetRequest(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
vaultContainer, err := testcontainervault.NewContainer(ctx)
require.NoError(t, err, "Failed to set up Vault container")
defer vaultContainer.Terminate(ctx)
cfg := vaultx.NewConfig(vaultContainer.URI)
vltx := vaultx.New(cfg)
path := "/v1/sys/health"
res, err := vltx.API().Read(ctx, path, vaultContainer.Token)
require.NoError(t, err, "Error exists")
require.NotEmpty(t, res, "Response is empty")
type responseBody struct {
Initialized bool `json:"initialized"`
Sealed bool `json:"sealed"`
Standby bool `json:"standby"`
Version string `json:"version"`
}
expectedResBody := responseBody{
Initialized: true,
Sealed: false,
Standby: false,
Version: "1.9.4",
}
actualResBody := new(responseBody)
err = res.JSON(&actualResBody)
require.NoError(t, err, "Error converting response body to struct")
require.Equal(t, 200, res.StatusCode, "Status code mismatch")
require.Equal(t, expectedResBody, *actualResBody, "Response body is incorrect")
}
func TestIntegrationAPIWriteSendsPostRequest(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
vaultContainer, err := testcontainervault.NewContainer(ctx)
require.NoError(t, err, "Failed to set up Vault container")
defer vaultContainer.Terminate(ctx)
cfg := vaultx.NewConfig(vaultContainer.URI)
vltx := vaultx.New(cfg)
path := "/v1/secret/data/newsecret"
type requestWrapper struct {
Data map[string]interface{} `json:"data"`
}
req := requestWrapper{
Data: map[string]interface{}{
"password": "BVthg8C4VGpkqVQk2gArykTB",
},
}
res, err := vltx.API().Write(ctx, path, vaultContainer.Token, req)
require.NoError(t, err, "Error exists")
require.NotEmpty(t, res, "Response is empty")
type response struct {
Version int `json:"version"`
}
type responseWrapper struct {
Data response `json:"data"`
}
expectedResBody := responseWrapper{
Data: response{
Version: 1,
},
}
actualResBody := new(responseWrapper)
err = res.JSON(&actualResBody)
require.NoError(t, err, "Error converting response body to struct")
require.Equal(t, 200, res.StatusCode, "Status code mismatch")
require.Equal(t, expectedResBody, *actualResBody, "Response body is incorrect")
}
func TestIntegrationDBGenerateCredentialsReturnsCredentials(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
dbUser := "testytesty"
dbPassword := "password"
dbName := "testydb"
dbContainer, err := testcontainerpostgres.NewContainer(ctx, dbUser, dbPassword, dbName)
require.NoError(t, err, "Failed to set up database container")
defer dbContainer.Terminate(ctx)
vaultContainer, err := testcontainervault.NewContainer(ctx)
require.NoError(t, err, "Failed to set up Vault container")
defer vaultContainer.Terminate(ctx)
err = vaultContainer.EnableDBEngine(ctx)
require.NoError(t, err, "Failed to initialize Vault container")
dbRole := "dbrole"
err = vaultContainer.CreateDBEngineRole(ctx, dbRole, dbName)
require.NoError(t, err, "Failed to set up Vault database engine role")
dbContainerIP, err := dbContainer.ContainerIP(ctx)
require.NoError(t, err, "Failed to get database container IP")
dbURITemplate := fmt.Sprintf(
"postgresql://{{username}}:{{password}}@%s:5432/postgres?sslmode=disable",
dbContainerIP)
err = vaultContainer.CreateDBEngineConfig(ctx, dbName, dbURITemplate, dbUser, dbPassword, dbRole)
require.NoError(t, err, "Failed to set up Vault database engine config")
cfg := vaultx.NewConfig(vaultContainer.URI)
vltx := vaultx.New(cfg)
vltx.Auth().SetToken(vaultxauth.Token{Value: vaultContainer.Token})
dbCredentials, err := vltx.DB().GenerateCredentials(ctx, dbRole)
require.NoError(t, err, "Credential generation failure")
require.NotEmpty(t, dbCredentials, "Credentials are empty")
require.NotEmpty(t, dbCredentials.Username, "Username is empty")
require.NotEqual(t, dbUser, dbCredentials.Username, "Username matches original credentials")
require.True(t, strings.HasPrefix(dbCredentials.Username, "v-token-"+dbRole))
require.NotEmpty(t, dbCredentials.Password, "Password is empty")
require.NotEqual(t, dbPassword, dbCredentials.Password, "Password matches original credentials")
}
func TestIntegrationKVUpsertGetSecretReturnsSecret(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
vaultContainer, err := testcontainervault.NewContainer(ctx)
require.NoError(t, err, "Failed to set up Vault container")
defer vaultContainer.Terminate(ctx)
cfg := vaultx.NewConfig(vaultContainer.URI)
vltx := vaultx.New(cfg)
vltx.Auth().SetToken(vaultxauth.Token{Value: vaultContainer.Token})
secretPath := "mypath"
secretData := map[string]interface{}{
"username": "dbuser",
"password": "3hvu2ZLxwauHrNaZjJbJARHE",
}
err = vltx.KV().UpsertSecret(ctx, secretPath, secretData)
require.NoError(t, err, "Upsert failure")
expectedSecret := kv.Secret{
Data: secretData,
Version: 1,
}
secret, err := vltx.KV().GetSecret(ctx, secretPath)
require.NoError(t, err, "Get failure")
require.NotEmpty(t, secret, "Secret is empty")
require.Equal(t, expectedSecret, *secret, "Secret is incorrect")
}
func TestIntegrationTransitEncryptEncryptsData(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
vaultContainer, err := testcontainervault.NewContainer(ctx)
require.NoError(t, err, "Failed to set up Vault container")
defer vaultContainer.Terminate(ctx)
err = vaultContainer.EnableTransit(ctx)
require.NoError(t, err, "Failed to initialize Vault container")
transitKey := "my-key"
err = vaultContainer.CreateTransitKey(ctx, transitKey)
require.NoError(t, err, "Failed to create Vault transit key")
cfg := vaultx.NewConfig(vaultContainer.URI)
vltx := vaultx.New(cfg)
vltx.Auth().SetToken(vaultxauth.Token{Value: vaultContainer.Token})
plaintext := "this is my secret"
encrypted, err := vltx.Transit().Encrypt(ctx, transitKey, []byte(plaintext))
require.NoError(t, err, "Encryption failure")
require.NotEmpty(t, encrypted, "Encrypted value is empty")
require.True(t, strings.HasPrefix(encrypted, "vault:v1:"))
}
func TestIntegrationTransitEncryptBatchEncryptsData(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
vaultContainer, err := testcontainervault.NewContainer(ctx)
require.NoError(t, err, "Failed to set up Vault container")
defer vaultContainer.Terminate(ctx)
err = vaultContainer.EnableTransit(ctx)
require.NoError(t, err, "Failed to initialize Vault container")
transitKey := "my-key"
err = vaultContainer.CreateTransitKey(ctx, transitKey)
require.NoError(t, err, "Failed to create Vault transit key")
cfg := vaultx.NewConfig(vaultContainer.URI)
vltx := vaultx.New(cfg)
vltx.Auth().SetToken(vaultxauth.Token{Value: vaultContainer.Token})
secretA := "this is my secret"
secretB := "this is another secret"
secretC := "this is yet another secret"
encryptedBatch, err := vltx.Transit().EncryptBatch(ctx, transitKey, []byte(secretA), []byte(secretB), []byte(secretC))
require.NoError(t, err, "Encryption failure")
require.NotEmpty(t, encryptedBatch, "Encrypted batch is empty")
require.Equal(t, 3, len(encryptedBatch), "Incorrect number of encrypted items")
for _, encrypted := range encryptedBatch {
require.True(t, strings.HasPrefix(encrypted, "vault:v1:"))
}
}
func TestIntegrationTransitDecryptDecryptsData(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
vaultContainer, err := testcontainervault.NewContainer(ctx)
require.NoError(t, err, "Failed to set up Vault container")
defer vaultContainer.Terminate(ctx)
err = vaultContainer.EnableTransit(ctx)
require.NoError(t, err, "Failed to initialize Vault container")
transitKey := "my-key"
err = vaultContainer.CreateTransitKey(ctx, transitKey)
require.NoError(t, err, "Failed to create Vault transit key")
cfg := vaultx.NewConfig(vaultContainer.URI)
vltx := vaultx.New(cfg)
vltx.Auth().SetToken(vaultxauth.Token{Value: vaultContainer.Token})
plaintext := "this is my secret"
encrypted, err := vltx.Transit().Encrypt(ctx, transitKey, []byte(plaintext))
require.NoError(t, err, "Encryption failure")
require.NotEmpty(t, encrypted, "Encrypted value is empty")
decrypted, err := vltx.Transit().Decrypt(ctx, transitKey, encrypted)
require.NoError(t, err, "Decryption failure")
require.NotEmpty(t, encrypted, "Decrypted value is empty")
require.Equal(t, plaintext, string(decrypted), "Decrypted value is not equal to the original")
}
func TestIntegrationTransitDecryptBatchDecryptsData(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
vaultContainer, err := testcontainervault.NewContainer(ctx)
require.NoError(t, err, "Failed to set up Vault container")
defer vaultContainer.Terminate(ctx)
err = vaultContainer.EnableTransit(ctx)
require.NoError(t, err, "Failed to initialize Vault container")
transitKey := "my-key"
err = vaultContainer.CreateTransitKey(ctx, transitKey)
require.NoError(t, err, "Failed to create Vault transit key")
cfg := vaultx.NewConfig(vaultContainer.URI)
vltx := vaultx.New(cfg)
vltx.Auth().SetToken(vaultxauth.Token{Value: vaultContainer.Token})
secretA := "this is my secret"
secretB := "this is another secret"
secretC := "this is yet another secret"
encryptedBatch, err := vltx.Transit().EncryptBatch(ctx, transitKey, []byte(secretA), []byte(secretB), []byte(secretC))
require.NoError(t, err, "Encryption failure")
require.NotEmpty(t, encryptedBatch, "Encrypted batch is empty")
require.Equal(t, 3, len(encryptedBatch), "Incorrect number of encrypted items")
decryptedBatch, err := vltx.Transit().DecryptBatch(ctx, transitKey, encryptedBatch...)
require.NoError(t, err, "Decryption failure")
require.NotEmpty(t, encryptedBatch, "Decrypted batch is empty")
require.Equal(t, 3, len(encryptedBatch), "Incorrect number of decrypted items")
require.Equal(t, secretA, string(decryptedBatch[0]), "Decrypted value is not equal to the original")
require.Equal(t, secretB, string(decryptedBatch[1]), "Decrypted value is not equal to the original")
require.Equal(t, secretC, string(decryptedBatch[2]), "Decrypted value is not equal to the original")
}