-
Notifications
You must be signed in to change notification settings - Fork 7
/
state_test.go
262 lines (227 loc) · 7.32 KB
/
state_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
/*
Copyright 2018 Tuenti Technologies S.L. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pouch
import (
"encoding/json"
"io/ioutil"
"reflect"
"sort"
"testing"
"time"
)
var filesUsingCases = []struct {
Secret *SecretState
RegisteredFiles *PriorityFileSortedList
SortedFiles *PriorityFileSortedList
}{
{
secretWithFilesUsing,
filesUsingPriorities,
sortedFilesUsingPriorities,
},
{
secretWithFilesUsing,
filesUsingNoPriorities,
sortedFilesUsingNoPriorities,
},
}
var filesUsingPriorities = &PriorityFileSortedList{
PriorityFile{Priority: 10, Path: "/tmp2"},
PriorityFile{Priority: 10, Path: "/tmp1"},
PriorityFile{Priority: 90, Path: "/tmp3a"},
PriorityFile{Path: "/bar"},
PriorityFile{Priority: 20, Path: "/tmp3b"},
}
var sortedFilesUsingPriorities = &PriorityFileSortedList{
PriorityFile{Path: "/bar"},
PriorityFile{Priority: 10, Path: "/tmp1"},
PriorityFile{Priority: 10, Path: "/tmp2"},
PriorityFile{Priority: 20, Path: "/tmp3b"},
PriorityFile{Priority: 90, Path: "/tmp3a"},
}
var secretWithFilesUsing = &SecretState{
Data: map[string]interface{}{"test_key": "value_secret"},
FilesUsing: PriorityFileSortedList{},
}
var filesUsingNoPriorities = &PriorityFileSortedList{
PriorityFile{Path: "/temp2"},
PriorityFile{Path: "/temp1"},
PriorityFile{Path: "/tempcc"},
PriorityFile{Path: "/tempbb"},
PriorityFile{Path: "/other"},
}
var sortedFilesUsingNoPriorities = &PriorityFileSortedList{
PriorityFile{Path: "/other"},
PriorityFile{Path: "/temp1"},
PriorityFile{Path: "/temp2"},
PriorityFile{Path: "/tempbb"},
PriorityFile{Path: "/tempcc"},
}
func TestSortingFilesUsing(t *testing.T) {
var filesUsing PriorityFileSortedList
for _, c := range filesUsingCases {
filesUsing = *c.RegisteredFiles
sort.Sort(filesUsing)
if !reflect.DeepEqual(filesUsing, *c.SortedFiles) {
t.Fatalf("Registered files (%+v) are not sorted as expected (%+v)", filesUsing, *c.SortedFiles)
}
}
}
func TestFilesUsingBySecrets(t *testing.T) {
state, cleanup := newTestState()
defer cleanup()
state.Secrets = make(map[string]*SecretState)
for _, c := range filesUsingCases {
state.Secrets["foo"] = c.Secret
state.Secrets["foo"].FilesUsing = nil
for _, pf := range *c.RegisteredFiles {
state.Secrets["foo"].RegisterUsage(pf.Path, pf.Priority)
}
state.Save()
jsonSaved, err := json.Marshal(state)
if err != nil {
t.Fatalf("State saved to file could not be converted to JSON")
}
d, err := ioutil.ReadFile(state.Path)
if err != nil {
t.Fatalf("State has not been written - %s\n", err)
}
var loadedState PouchState
err = json.Unmarshal(d, &loadedState)
if err != nil {
t.Fatalf("JSON has failures - %s\n", err)
}
jsonLoaded, err := json.Marshal(loadedState)
if err != nil {
t.Fatalf("State retrieved from file could not be converted to JSON")
}
if string(jsonSaved) != string(jsonLoaded) {
t.Fatalf("JSON saved and read are differents\n%s\n%s\n", jsonSaved, jsonLoaded)
}
}
}
var testCert = `
-----BEGIN CERTIFICATE-----
MIIBrzCCAVmgAwIBAgIJALFGkQ7RBNsEMA0GCSqGSIb3DQEBCwUAMDMxCzAJBgNV
BAYTAkVTMRMwEQYDVQQIDApTb21lLVN0YXRlMQ8wDQYDVQQKDAZUdWVudGkwHhcN
MTgwMjA1MTcwMDM5WhcNMTgwMjA2MTcwMDM5WjAzMQswCQYDVQQGEwJFUzETMBEG
A1UECAwKU29tZS1TdGF0ZTEPMA0GA1UECgwGVHVlbnRpMFwwDQYJKoZIhvcNAQEB
BQADSwAwSAJBALqLUd6kagFERSjV/eN1wexU/quN4poWy1Lf1iFun+3uXrzbolqr
/Gx7XmuHKYkuW8+6zSQdedXEfYMJkXC/NgkCAwEAAaNQME4wHQYDVR0OBBYEFAsa
aDUVlmlGLt8GMBQ+sIs6WRL7MB8GA1UdIwQYMBaAFAsaaDUVlmlGLt8GMBQ+sIs6
WRL7MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADQQBcyxIwCFr9B5y2ZYVA
Yf/tGEoZCjAWsMlS2OoQjBKnOFfz1X+p0/NSQBoRI9MFs7FnyrBgqrsl1mQ8WfIa
aNh1
-----END CERTIFICATE-----`
var testCertNotBefore = time.Date(2018, 2, 5, 17, 00, 39, 0, time.UTC)
var testCertNotAfter = time.Date(2019, 2, 6, 17, 00, 39, 0, time.UTC)
var testKey = `
-----BEGIN PRIVATE KEY-----
MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAuotR3qRqAURFKNX9
43XB7FT+q43imhbLUt/WIW6f7e5evNuiWqv8bHtea4cpiS5bz7rNJB151cR9gwmR
cL82CQIDAQABAkBjTKJKB+89uV+vOyopGJgf+6aNH7wOFjApb2mG5mJPvnigA0Ng
LCAZJRscEkYPf53d9y7CGVqOitscVdAk77B5AiEA6RmjcLfAz8jb5skkug2DhSBs
ZrkJ6u7/VTOp5hAQ6u8CIQDM3tIylG/NgyKg0n+JqtqwMRTsDUslwrqyHMrJ7anO
hwIhAKsZN5/gMTYToF4ZnMy4aKaKMyd/gSkiPudiYb5OYqyfAiB6EXX7DzjCohUa
7/FwDK469zO5Jn6VJD7ra35k7MgVtwIhANKLhLCtt5I+WXy+SbG8EDRW5eKqBy8v
5n1aU0/ed9d2
-----END PRIVATE KEY-----`
var unknownTTL = &SecretState{Timestamp: time.Time{}}
var secretCaseTTL = &SecretState{
Data: SecretData{"ttl": json.Number("360")},
Timestamp: time.Time{},
DurationRatio: 0.5,
}
var secretWithCertificate = &SecretState{
Data: SecretData{"certificate": testCert, "private_key": testKey},
Timestamp: time.Time{},
DurationRatio: 0.5,
}
var secretBeforeCertificate = &SecretState{
Data: SecretData{"ttl": json.Number("60")},
Timestamp: testCertNotBefore,
DurationRatio: 0.5,
}
var secretAfterCertificate = &SecretState{
Data: SecretData{"ttl": json.Number("60")},
Timestamp: testCertNotAfter,
DurationRatio: 0.5,
}
var allSecretCases = []*SecretState{
unknownTTL,
secretCaseTTL,
secretWithCertificate,
secretBeforeCertificate,
secretAfterCertificate,
}
var nextUpdateCases = []struct {
State PouchState
Secret *SecretState
TTU time.Time
}{
// No secrets
{PouchState{}, nil, time.Time{}},
// Secret without TTL
{PouchState{
Secrets: map[string]*SecretState{
"unknown": unknownTTL,
},
}, nil, time.Time{}},
// A secret with TTL, other unknown
{PouchState{
Secrets: map[string]*SecretState{
"foo": secretCaseTTL,
"unknown": unknownTTL,
},
}, secretCaseTTL, time.Time{}.Add(180 * time.Second)},
// A secret with a certificate
{PouchState{
Secrets: map[string]*SecretState{
"cert": secretWithCertificate,
},
}, secretWithCertificate, testCertNotBefore.Add(12 * time.Hour)},
// A secret to be updated before a certificate
{PouchState{
Secrets: map[string]*SecretState{
"cert": secretWithCertificate,
"before": secretBeforeCertificate,
},
}, secretBeforeCertificate, testCertNotBefore.Add(30 * time.Second)},
// A secret to be updated after a certificate
{PouchState{
Secrets: map[string]*SecretState{
"cert": secretWithCertificate,
"after": secretAfterCertificate,
},
}, secretWithCertificate, testCertNotBefore.Add(12 * time.Hour)},
}
func TestPouchStateNextUpdate(t *testing.T) {
for i, c := range nextUpdateCases {
foundSecret, foundTTU := c.State.NextUpdate()
if foundSecret != c.Secret {
t.Fatalf("Case #%d: found secret %v, expected %v", i, foundSecret, c.Secret)
}
if foundSecret != nil && foundTTU != c.TTU {
t.Fatalf("Case #%d: found TTU %s, expected %s", i, foundTTU, c.TTU)
}
}
}
func TestConsistentTTU(t *testing.T) {
for _, c := range allSecretCases {
firstTTU, firstKnown := c.TimeToUpdate()
secondTTU, secondKnown := c.TimeToUpdate()
if firstTTU != secondTTU || firstKnown != secondKnown {
t.Fatalf("TTU changed after some time for %+v", c)
}
}
}