forked from gavincabbage/envsecret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret_test.go
205 lines (182 loc) · 7.93 KB
/
secret_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
package envsecret_test
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/gavincabbage/envsecret"
)
func TestMustProcess(t *testing.T) {
assert.Panics(t, func() {
envsecret.MustProcess(nil, nil)
})
}
func TestProcess_Errors(t *testing.T) {
type testSpec struct {
DefaultString envsecret.String
OverriddenString envsecret.String `secret_keys:"otherkey"`
RequiredString envsecret.String `required:"true"`
}
tests := []struct {
name string
store *spySecretStore
spec testSpec
}{
{
name: "store error",
store: &spySecretStore{
Out: map[string]map[string]interface{}{
"secret-id": {
"value": "default secret value",
"otherkey": "other value",
},
},
Err: errors.New("retrieval error"),
},
spec: testSpec{
DefaultString: envsecret.NewString("bad-id"),
OverriddenString: envsecret.NewString("bad-id"),
RequiredString: envsecret.NewString("bad-id"),
},
},
{
name: "missing specified override key",
store: &spySecretStore{
Out: map[string]map[string]interface{}{
"secret-id": {
"value": "default secret value",
},
},
},
spec: testSpec{
DefaultString: envsecret.NewString("secret-id"),
OverriddenString: envsecret.NewString("secret-id"),
RequiredString: envsecret.NewString("secret-id"),
},
},
{
name: "missing required key",
store: &spySecretStore{
Out: map[string]map[string]interface{}{
"secret-id": {
"value": "default secret value",
"otherkey": "other value",
},
},
},
spec: testSpec{
DefaultString: envsecret.NewString("secret-id"),
OverriddenString: envsecret.NewString("secret-id"),
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := envsecret.Process(&test.spec, test.store)
assert.Error(t, err)
})
}
}
func TestProcess(t *testing.T) {
store := &spySecretStore{
Out: map[string]map[string]interface{}{
"default-string-id": {
"value": "default secret value",
},
"override-string-id": {
"overridden": "overridden secret value",
"value": "default secret value",
},
"public-key-id": {
"value": testPublicKey,
},
"private-key-id": {
"value": testPrivateKey,
},
"login-id": {
"username": "testUser",
"password": "testPassword",
},
"default-map-id": {
"key1": "val1",
"key2": "val2",
"key3": "val3",
},
"filtered-map-id": {
"key1": "val1",
"key2": "val2",
"key3": "val3",
},
"rds-login-id": {
"engine": "postgres",
"username": "testUser",
"password": "testPassword",
"host": "testHost",
"port": "testPort",
"dbname": "testDb",
},
},
}
testSpec := struct {
NotSecret string
AnotherNotSecret int
ThirdNotSecret struct{ string }
DefaultString envsecret.String
DummyDefaultString envsecret.String
OverrideString envsecret.String `secret_keys:"overridden"`
PublicKey envsecret.PublicKey `required:"true"`
PrivateKey envsecret.PrivateKey
Login envsecret.Login
DefaultMap envsecret.Map
FilteredMap envsecret.Map `secret_keys:"key1,key3"`
IgnoredSecret envsecret.String `ignored:"true"`
ExplicitlyNotRequiredSecret envsecret.String `required:"false"`
}{
NotSecret: "some string",
AnotherNotSecret: 42,
ThirdNotSecret: struct{ string }{"string inside"},
DefaultString: envsecret.NewString("default-string-id"),
DummyDefaultString: envsecret.NewString("default-string-id"),
OverrideString: envsecret.NewString("override-string-id"),
PublicKey: envsecret.NewPublicKey("public-key-id"),
PrivateKey: envsecret.NewPrivateKey("private-key-id"),
Login: envsecret.NewLogin("login-id"),
DefaultMap: envsecret.NewMap("default-map-id"),
FilteredMap: envsecret.NewMap("filtered-map-id"),
}
err := envsecret.Process(&testSpec, store)
assert.NoError(t, err)
assert.Equal(t, 7, store.GetCount) // duplicate secret id retrievals should be cached
// Didn't affect non-secrets
assert.Equal(t, "some string", testSpec.NotSecret)
assert.Equal(t, 42, testSpec.AnotherNotSecret)
assert.Equal(t, "string inside", testSpec.ThirdNotSecret.string)
// String
assert.Equal(t, "default secret value", testSpec.DefaultString.Value)
assert.Equal(t, "overridden secret value", testSpec.OverrideString.Value)
// PublicKey
assert.NotNil(t, testSpec.PublicKey.Key)
// PrivateKey
assert.NotNil(t, testSpec.PrivateKey.Key)
// Login
assert.Equal(t, "testUser", testSpec.Login.Username)
assert.Equal(t, "testPassword", testSpec.Login.Password)
// Map
assert.Equal(t, 3, len(testSpec.DefaultMap.Values))
assert.Equal(t, 2, len(testSpec.FilteredMap.Values))
}
func (spy *spySecretStore) Get(id string) (map[string]interface{}, error) {
spy.GetCount++
if spy.Err != nil {
return nil, spy.Err
}
return spy.Out[id], nil
}
type spySecretStore struct {
Out map[string]map[string]interface{}
Err error
GetCount int
}
const (
testPublicKey = "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF5ZkpSMTAwZWFldFJmWHl2blZzRgpPaWZrVkdBeUFiNTk0RGRDMHdKSDNWdVFDRys4Y1RGUUY0aXd5MmRpNGJKbktsVHNhUlp4dERnaWJKTFRCeEw4CnlHRy9wT1dSVHdkRDErS2xKdmt4Vmp2N05uUmhFM1VTeG1zdllYN2FURWNKSWFVdUVJUmFJQlZxMW1rUFptUTMKRHJOM2dPNlZEK0hHYjkrTytRTHMwbEtuSWlJc3JmK3Q0SGdINnNIUVpWdVdIWlVIcVZ4QTVjNTRFWUJoUDVtdQptYlJ4QUVCbmEyVm02MkxaSXdiVGs5SHVuL1JxVmZsYkZCdkdaaitVSDN6U0VGemtjQThUekg3RjJBcGpkc2NiCnowT0NkZ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