forked from volck/raven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sealedsecret_test.go
executable file
·282 lines (240 loc) · 9.28 KB
/
sealedsecret_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
package main
import (
"fmt"
"github.com/hashicorp/vault/api"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"reflect"
"testing"
)
func CompareSealedAndK8sSecrets(t *testing.T, UnsealedSecret *v1.Secret, k8sSecret v1.Secret) (valid bool) {
for UnsealedSecretKey, UnsealedSecretValue := range UnsealedSecret.Data {
for k8sKey, k8sValue := range k8sSecret.StringData {
if UnsealedSecretKey == k8sKey && k8sValue == string(UnsealedSecretValue) {
valid = true
fmt.Printf("found valid field: %s and valid key %s ", UnsealedSecretKey, UnsealedSecretValue)
}
}
}
return valid
}
func TestSealedSecretMatchk8sSecret(t *testing.T) {
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
pemFile: "cert.pem",
}
secretName := "secret"
generateTestSecrets(t, client, config, secretName)
//we need to unseal sealedsecret and compare it to a k8sSecret
SealedSecret, k8sSecret := ReadConvertKVFromVault(t, client, config.secretEngine, secretName, config.destEnv, config.pemFile)
var codecs serializer.CodecFactory
privateKeys := ReturnPrivateKey(t)
UnsealedSecret := UnsealSecretAndReturn(t, SealedSecret, codecs, privateKeys)
if !CompareSealedAndK8sSecrets(t, UnsealedSecret, k8sSecret) {
t.Fatal("verifying failed")
}
}
func TestGetKVAndCreateNormalSealedSecret(t *testing.T) {
t.Parallel()
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
}
// make testable secrets for cluster
secrets := map[string]interface{}{
"data": map[string]interface{}{"secretKey": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
client.Logical().Write("kv/data/secret", secrets)
secretEngine := "kv"
secretName := "secret"
pemFile := `cert.crt`
// SingleKVFromVault := getSingleKV(client, secretEngine, secretName)
input := fmt.Sprintf("%s/%s", secretEngine, secretName)
kvVaultList := iterateList(input, client, "")
fmt.Println("kvVaultList", kvVaultList)
k8sSecret := createK8sSecret(secretName, config, kvVaultList)
fmt.Println("we created a k8ssecret called", k8sSecret.Name, k8sSecret)
SealedSecret := createSealedSecret(pemFile, &k8sSecret)
fmt.Println("we converted that k8ssecret to a sealedSecret", SealedSecret.Name, SealedSecret)
fmt.Printf("k8sSecret.Data: %v \n k8sSecret.StringData: %v \n k8sSecret.Annotations: %v \n", k8sSecret.Data, k8sSecret.StringData, k8sSecret.Annotations)
fmt.Printf("SealedSecret: %v \n SealedSecret.Annotations: %v \n", SealedSecret, k8sSecret.Annotations)
}
func TestCreateThreeSealedSecrets(t *testing.T) {
t.Parallel()
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
pemFile: "cert.crt",
}
// make testable secrets for cluster
secretOne := map[string]interface{}{
"data": map[string]interface{}{"SecretOne": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretTwo := map[string]interface{}{
"data": map[string]interface{}{"secretTwo": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretThree := map[string]interface{}{
"data": map[string]interface{}{"secretThree": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
secretThreev2 := map[string]interface{}{
"data": map[string]interface{}{"secretThreev2": "secretValue"},
"metadata": map[string]interface{}{"version": 2},
}
firstPath := "kv/data/subpathone"
secondPath := "kv/data/subpathone/secretSecondPath"
thirdPath := "kv/data/subpathone/subpathtwo/secretThirdPath"
thirdPathv2 := "kv/data/subpathone/subpathtwo/secretThirdPathV2"
client.Logical().Write(firstPath, secretOne)
client.Logical().Write(secondPath, secretTwo)
client.Logical().Write(thirdPath, secretThree)
client.Logical().Write(thirdPathv2, secretThreev2)
list, err := getAllKVs(client, config)
if err != nil {
fmt.Println(err)
}
secretList := list.Data["keys"].([]interface{})
persistVaultChanges(secretList, client, config)
}
func TestGetKVAndCreateNormalSealedSecretWithNoDataFields(t *testing.T) {
t.Parallel()
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
}
// make testable secrets for cluster
secrets := map[string]interface{}{
"data": nil,
"metadata": map[string]interface{}{"version": 2},
}
client.Logical().Write("kv/data/secret", secrets)
secretEngine := "kv"
secretName := "secret"
pemFile := `cert.crt`
SingleKVFromVault := getSingleKV(client, secretEngine, secretName)
k8sSecret := createK8sSecret(secretName, config, SingleKVFromVault)
SealedSecret := createSealedSecret(pemFile, &k8sSecret)
fmt.Printf("k8sSecret.Data: %v \n k8sSecret.StringData: %v \n k8sSecret.Annotations: %v \n", k8sSecret.Data, k8sSecret.StringData, k8sSecret.Annotations)
fmt.Printf("SealedSecret: %v \n SealedSecret.Annotations: %v \n", SealedSecret, k8sSecret.Annotations)
}
func TestGetKVAndCreateSealedSecretWithDocumentKeysAnnotations(t *testing.T) {
t.Parallel()
// Initiate cluster and get client
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
}
secrets := map[string]interface{}{
"data": map[string]interface{}{"totallyDifferent": "secretValue", "raven/description": "some very secret secret that we need to use to make world go around"},
"metadata": map[string]interface{}{"version": 2},
}
//make secret with raven/description field to see if DocumentKeys are working
client.Logical().Write("kv/data/DocumentKeyAnnotation", secrets)
secretEngine := "kv"
secretName := "DocumentKeyAnnotation"
pemFile := `cert.crt`
SingleKVFromVault := getSingleKV(client, secretEngine, secretName)
k8sSecret := createK8sSecret(secretName, config, SingleKVFromVault)
SealedSecret := createSealedSecret(pemFile, &k8sSecret)
seen := false
//iterate keys to see if documentKeys are present
for keySealedSecret, _ := range SealedSecret.Annotations {
for i := range newConfig.DocumentationKeys {
if newConfig.DocumentationKeys[i] == keySealedSecret {
seen = true
fmt.Printf("DocumentationKey(%s) seen here, present in sealed secret as annotation \n", keySealedSecret)
}
}
}
if !seen {
t.Fatal("TestGetKVAndCreateSealedSecretWithDocumentKeysAnnotations failed. Seen:", seen)
}
}
func TestCompareThatSealedSecretAndSecretMetadataMatches(t *testing.T) {
t.Parallel()
// Initiate cluster and get client
cluster := createVaultTestCluster(t)
defer cluster.Cleanup()
client := cluster.Cores[0].Client
config := config{
vaultEndpoint: cluster.Cores[0].Client.Address(),
secretEngine: "kv",
token: client.Token(),
destEnv: "kv",
}
secrets := map[string]interface{}{
"data": map[string]interface{}{"totallyDifferent": "secretValue", "raven/description": "some very secret secret that we need to use to make world go around"},
"metadata": map[string]interface{}{"version": 2},
}
//make secret with raven/description field to see if DocumentKeys are working
client.Logical().Write("kv/data/TestCompareThatSealedSecretAndSecretMetadataMatches", secrets)
secretEngine := "kv"
secretName := "TestCompareThatSealedSecretAndSecretMetadataMatches"
pemFile := `cert.crt`
SingleKVFromVault := getSingleKV(client, secretEngine, secretName)
k8sSecret := createK8sSecret(secretName, config, SingleKVFromVault)
SealedSecret := createSealedSecret(pemFile, &k8sSecret)
if !reflect.DeepEqual(k8sSecret.TypeMeta, SealedSecret.TypeMeta) {
t.Fatal("TestCompareThatSealedSecretAndSecretMetadataMatches TypeMeta neq")
} else if !reflect.DeepEqual(k8sSecret.Annotations, k8sSecret.Annotations) {
t.Fatal("TestCompareThatSealedSecretAndSecretMetadataMatches Annotations neq")
}
fmt.Printf("k8s: %v \n sealed: %v \n", k8sSecret, SealedSecret)
fmt.Printf("sealedsecret.name: %v \n k8s.name: %v \n", SealedSecret.Name, k8sSecret.Name)
}
func Test_listsMatchNillist(t *testing.T) {
PreviousKV := map[string]*api.Secret{}
NewKV := map[string]*api.Secret{}
if !listsMatch(PreviousKV, NewKV) {
t.Fatal("listsMatchNillist does not match list. lists are the exact same object and should match")
}
}
func Test_listsMatchDiff(t *testing.T) {
PreviousKV := map[string]*api.Secret{}
data := make(map[string]interface{})
data["mysecret"] = "123"
previousSecret := &api.Secret{
RequestID: "",
LeaseID: "",
LeaseDuration: 0,
Renewable: false,
Data: data,
Warnings: nil,
Auth: nil,
WrapInfo: nil,
}
NewKV := map[string]*api.Secret{}
PreviousKV["theSecret"] = previousSecret
if (listsMatch(PreviousKV, NewKV)) {
t.Fatal("listsMatchDiff should have differences. ")
}
}