-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextractSecrets_test.go
190 lines (144 loc) · 4.32 KB
/
extractSecrets_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
package vault
import (
"fmt"
"os"
"testing"
"github.com/BESTSELLER/harpocrates/config"
"github.com/BESTSELLER/harpocrates/files"
"github.com/BESTSELLER/harpocrates/util"
"github.com/hashicorp/vault/api"
vaulthttp "github.com/hashicorp/vault/http"
hashivault "github.com/hashicorp/vault/vault"
"gotest.tools/v3/assert"
)
var testVault vaultTest
// rootVaultToken is the Vault token used for tests
var rootVaultToken = "unittesttoken"
type vaultTest struct {
Cluster *hashivault.TestCluster
Client *api.Client
}
func TestMain(t *testing.T) {
testVault = GetTestVaultServer(t)
}
// GetTestVaultServer creates the test server
func GetTestVaultServer(t *testing.T) vaultTest {
t.Helper()
cluster := hashivault.NewTestCluster(t, &hashivault.CoreConfig{
DevToken: rootVaultToken,
}, &hashivault.TestClusterOptions{
HandlerFunc: vaulthttp.Handler,
})
cluster.Start()
core := cluster.Cores[0].Core
hashivault.TestWaitActive(t, core)
client := cluster.Cores[0].Client
// put secrets
secretPath := "secret/data/secret"
secret := map[string]interface{}{"key1": "value1", "key2": "value2", "key3": "value3", "key4": 123, "key5": true}
_, err := client.Logical().Write(secretPath, secret)
if err != nil {
panic(err)
}
// return server
return vaultTest{
Cluster: cluster,
Client: client,
}
}
// TestExtractSecretsWithFormatAsExpected tests if a two secrets one with a format is extracted correct
func TestExtractSecretsWithFormatAsExpected(t *testing.T) {
// arrange
// define input
data := files.Read("../test_data/two_secrets_with_format.yaml")
input := util.ReadInput(data)
// mock prefix
config.Config.Prefix = input.Prefix
vaultClient := &API{
Client: testVault.Client,
}
// act
result, err := vaultClient.ExtractSecrets(input, false)
if err != nil {
t.Error(err)
}
for _, v := range result {
// assert
expected := fmt.Sprintf("%v", map[string]interface{}{input.Prefix + "key1": "value1", input.Prefix + "key2": "value2", input.Prefix + "key3": "value3", input.Prefix + "key4": 123, input.Prefix + "key5": true})
actual := fmt.Sprintf("%v", v.Result)
assert.Equal(t, expected, actual)
}
}
// TestExtractSecretsAsExpected tests if a simple secret is extracted correct
func TestExtractSecretsAsExpected(t *testing.T) {
// arrange
// define input
data := files.Read("../test_data/single_secret.yaml")
input := util.ReadInput(data)
// mock prefix
config.Config.Prefix = input.Prefix
vaultClient := &API{
Client: testVault.Client,
}
// act
result, err := vaultClient.ExtractSecrets(input, false)
if err != nil {
t.Error(err)
}
for _, v := range result {
// assert
expected := fmt.Sprintf("%v", map[string]interface{}{input.Prefix + "key1": "value1", input.Prefix + "key2": "value2", input.Prefix + "key3": "value3", input.Prefix + "key4": 123, input.Prefix + "key5": true})
actual := fmt.Sprintf("%v", v.Result)
assert.Equal(t, expected, actual)
}
}
// TestExtractSecretsWithPrefixAsExpected tests if a simple secret is extracted correct
func TestExtractSecretsWithPrefixAsExpected(t *testing.T) {
// arrange
// define input
data := files.Read("../test_data/keys_with_prefix.yaml")
input := util.ReadInput(data)
// mock prefix
config.Config.Prefix = input.Prefix
vaultClient := &API{
Client: testVault.Client,
}
// act
result, err := vaultClient.ExtractSecrets(input, false)
if err != nil {
t.Error(err)
}
for _, v := range result {
// assert
expected := fmt.Sprintf("%v", map[string]interface{}{"PRE_key1": "value1", "FIX_key2": "value2", input.Prefix + "key3": "value3"})
actual := fmt.Sprintf("%v", v.Result)
assert.Equal(t, expected, actual)
}
}
// TestExtractSecretsSaveAsFileAsExpected tests if a simple secret is extracted correct
func TestExtractSecretsSaveAsFileAsExpected(t *testing.T) {
// arrange
// define input
data := files.Read("../test_data/save_as_file.yaml")
input := util.ReadInput(data)
// mock prefix
config.Config.Prefix = input.Prefix
vaultClient := &API{
Client: testVault.Client,
}
// act
_, err := vaultClient.ExtractSecrets(input, false)
if err != nil {
t.Error(err)
}
// clean up
defer os.Remove("../.tmp/TEST_key1")
// assert
content, err := os.ReadFile("../.tmp/TEST_key1")
if err != nil {
t.Errorf("could not read file: %v", err)
}
expected := "value1"
actual := string(content)
assert.Equal(t, expected, actual)
}