This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
/
action_test.go
591 lines (553 loc) · 16.8 KB
/
action_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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Aaron Meihm [email protected] [:alm]
package mig
import (
"encoding/json"
"testing"
"github.com/mozilla/mig/pgp"
)
var keyValidSigner1 = `
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mQENBFnwr+YBCADI7ZlSuG/KxWt1E/qZcjo/cnF/N0oNPn/z9bfzDb/smejcJ1tt
Rb1mXWINrofnbxmYKcxSdX6Q9x4rQ8aLnd3PuX84Dg5oNx4NkiKdHZGFi2hyJK6u
GO3FV1EF+3fD+cVBUGoTT6eL5p2nppMvRTky8E6RklIaOKNU6cIMuGDSnwNU7pip
tTwLILw5Brd/cbGxoov+jxu5ccIJ+07tjBu7U5UVt+97gMqBazTCKvO+Q1/DFJsq
j67sYqvpGU8OlUOPfmbhhK8rU/CIDHRKeD3OiLAn85xEUnuGGJ7H8NsFwnf+cVA2
wHOhFY3QD+hHeF0t9w98tRHUQBhbRSncge//ABEBAAG0G01JRyB1bml0IHRlc3Rz
IDx1bml0QHRlc3RzPokBOAQTAQIAIgUCWfCv5gIbAwYLCQgHAwIGFQgCCQoLBBYC
AwECHgECF4AACgkQDzgtIchMFDpcRQf+INfdZdcDhjg/6c7Gq+YAoDBnI95XQEwz
AQMlMdFbzwNq0KqzerFAQjrJ2s+tOhVEJ4loJNZsuHW9a24f8xXhQ8/yRUHw45ca
iiztR7oUjHJfdT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SjnpG2yXVqSQzbu1AVejQH8caUTpnjl+VoipE7byF10S
gGMowCF8DQ9+LnflhPyA43QLg7Riea7iG6wSgDs142pqyHrg7ADzrXI177oWgBK2
q9EW1R42bXQ3SD5fWqQbSmfTTdXP1m2E4h72MtyAxFa4JQARAQABiQEfBBgBAgAJ
BQJZ8LHLAhsMAAoJEM/e92foIB0b7lcH/iso+ZKiZtuI0J6BM0lAZZXHgHxeFoAd
TP5nMpiP96FbhjjsiV67PJZlcjUxxWReFScGGpR6qoTsXzvoRliAdcZgCSXxxC1P
3WRoG0nJr65WMi3ieAd5W9XfukrKMND3XuukQaW4NQO+7F12x6F2QXu1kwrRlmyo
lFLtiuEEAlL2qarS6ItVxAEx4PGLUmNkSJ2RXK/uyd0Do6qTmjqV0Yl1eiZcRGXn
1uUgQcqcoBEeqrzK5KvDWqzLHQiwhDW02LaeyUCgFyHJzmX63rQCcnIf2jbr9MpD
2MZRp85fHFOy5QoQENMFMcS4+nz04ePh6Zj1TXPx0wX4hn164xqUJ1k=
=jIoT
-----END PGP PUBLIC KEY BLOCK-----
`
// A valid signed action, signed using keyValidSigner1
var validSignedAction1 = `
{
"id": 0,
"name": "pkg -s -name test ",
"target": "status='online'",
"description": {},
"threat": {},
"validfrom": "2017-10-25T15:50:05.220759533Z",
"expireafter": "2017-10-25T15:56:05.220759533Z",
"operations": [
{
"module": "pkg",
"parameters": {
"pkgmatch": {
"matches": [
"test"
]
},
"vermatch": ""
}
}
],
"pgpsignatures": [
"wsBcBAABCAAQBQJZ8LLpCRAPOC0hyEwUOgAA9lgIAIMJwIlhb9FTtT6gcMaoj2JSEnSS921DA3JR7VFd057wCBjmu9IfYD/j47TS2fmE2kwmwBbNQ/uAjqM61FycvH2p/zdfmrmTaiwWUArpGtmRCnEloUfmeDh0d7PcwzkcKa9rmtH1dvEW8SgQq/yDNw9hvfyYJHQWeqk9WzpCLfJlqUu/NLZZbjP/hJuHla8B161q4r0jlzqQwT5gn7nk+O0/q2zw3QzbU8WGT7Q9STB18JcCQ0NyeMLjHCf5yCKqeh1PtGOQ8NTuYo8p4M/xvMe5bLbHsONpay9JYM6NdUdGeyYHdU3odiTYDYaT3JqCdceZVM2Vjsj+IKeOyaVJQgw==Jaj/"
],
"starttime": "0001-01-01T00:00:00Z",
"finishtime": "0001-01-01T00:00:00Z",
"lastupdatetime": "0001-01-01T00:00:00Z",
"counters": {}
}
`
// An action signed with the invalid signer key
var invalidSignedAction1 = `
{
"id": 0,
"name": "pkg -s -name test ",
"target": "status='online'",
"description": {},
"threat": {},
"validfrom": "2017-10-25T16:09:35.583635919Z",
"expireafter": "2017-10-25T16:15:35.583635919Z",
"operations": [
{
"module": "pkg",
"parameters": {
"pkgmatch": {
"matches": [
"test"
]
},
"vermatch": ""
}
}
],
"pgpsignatures": [
"wsBcBAABCAAQBQJZ8Ld7CRDP3vdn6CAdGwAAXCoIAPGzPOY21S5jpr8wZzlx1+L7PP5jY8KGDAVuI75WurKC+HOG6fecx/Tf97JtJ8QqrLb7NJg/Jx6JQ+/qsoxRPMgI9Jr6lF1nw6XL4vvCLjxoBVa6H526Hj1FgDbNjy7F6HgWGEjiXFrrYnODKA37x8eozJ8Fx3+gzojTKz+EA9uBr/0a0o+VMx3Yw/HV+Rix6I/B3Tfxpeit+3W+Z40/eXOPQfbhfaT0uzIe6giWAriy4ll3DJ2LZDfvAP+49ze7mImxMxnMNhsD2BM5VvdmhdPD4CrvC3QCRkGe6P9h3ecJ8Kx++19sVplHIX3SaKRfaTgIvQ6mrwocMIs/yMFWPxE==1Uf/"
],
"starttime": "0001-01-01T00:00:00Z",
"finishtime": "0001-01-01T00:00:00Z",
"lastupdatetime": "0001-01-01T00:00:00Z",
"counters": {}
}
`
// A multi operation action signed with the valid signer key
var signedMultiOpAction1 = `
{
"id": 0,
"name": "multiop action",
"target": "status='online'",
"description": {},
"threat": {},
"validfrom": "2017-10-25T16:50:42.601631961Z",
"expireafter": "2017-10-25T16:56:42.601631961Z",
"operations": [
{
"module": "pkg",
"parameters": {
"pkgmatch": {
"matches": [
"test"
]
},
"vermatch": ""
}
},
{
"module": "file",
"parameters": {
"searches": {
"s1": {
"names": [
"test"
],
"options": {
"macroal": false,
"matchall": true,
"matchlimit": 1000,
"maxdepth": 1000,
"maxerrors": 30,
"mismatch": null
},
"paths": [
"/etc"
]
}
}
}
}
],
"pgpsignatures": [
"wsBcBAABCAAQBQJZ8MEeCRAPOC0hyEwUOgAAcwMIAGwU1Cv5FCQB9crkbiNrIJc99UodAtjmfIHa4u/OsHNUB+OcnEPNb1T2zI+rRYZzKQgV9WxSJfVDjdHtmVkAUzVF8ybOresBYSsgNN+FYAhpF2yvy5d8HF5IXXjBKlrJvqKSMzf1pOU1b4Sbklfu02LqR/MrjYYjvIA918NT1Nf/UCxHZdURCA0xyUYzJY2bYXGK7qNJs1ImYzsuv+YCYybIynS8XwSbwhLOgcSygLDPJjTBFdsupDMGBe8L+eoQuJF9zXalPtjO/qBF4ZYECzFrbTUu1R6SIGgeaCqyaRY8dIkNGZPmgyMOzGYcUFd3/k9GgjwezrmEHCu/J1MVo4o==pa/c"
],
"starttime": "0001-01-01T00:00:00Z",
"finishtime": "0001-01-01T00:00:00Z",
"lastupdatetime": "0001-01-01T00:00:00Z",
"counters": {}
}
`
func TestVerifyACLValid(t *testing.T) {
keys := make([][]byte, 0)
keys = append(keys, []byte(keyValidSigner1))
keyring, keycount, err := pgp.ArmoredKeysToKeyring(keys)
if err != nil {
t.Fatalf("pgp.ArmoredKeysToKeyring: %v", err)
}
// We should have one valid key
if keycount != 1 {
t.Fatalf("unexpected number of keys in keyring: %v", keycount)
}
var a Action
err = json.Unmarshal([]byte(validSignedAction1), &a)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
// This should verify
err = a.VerifySignatures(keyring)
if err != nil {
t.Fatalf("VerifySignatures: %v", err)
}
aclstr := `{
"pkg": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
}
}`
// Try verification again, this time include a valid ACL as well
var acl ACL
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
// Rewind the keyring reader for a subsequent read on it
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err != nil {
t.Fatalf("VerifyACL: %v", err)
}
// Try again, use a default ACL this time though which should also work
aclstr = `{
"default": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
}
}`
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
// Rewind the keyring reader for a subsequent read on it
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err != nil {
t.Fatalf("VerifyACL: %v", err)
}
}
func TestVerifyGoodSigBadACL(t *testing.T) {
keys := make([][]byte, 0)
keys = append(keys, []byte(keyValidSigner1))
keyring, keycount, err := pgp.ArmoredKeysToKeyring(keys)
if err != nil {
t.Fatalf("pgp.ArmoredKeysToKeyring: %v", err)
}
// We should have one valid key
if keycount != 1 {
t.Fatalf("unexpected number of keys in keyring: %v", keycount)
}
var a Action
err = json.Unmarshal([]byte(validSignedAction1), &a)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
// This should verify
err = a.VerifySignatures(keyring)
if err != nil {
t.Fatalf("VerifySignatures: %v", err)
}
// This ACL should result in a verification failure as we don't have an ACL entry
// for the pkg module
aclstr := `{
"file": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
}
}`
var acl ACL
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err == nil {
t.Fatalf("VerifyACL should have failed")
}
// Rerun the test with onlyVerifyPubkey set to true, this should be successful despite
// the ACL
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, true)
if err != nil {
t.Fatalf("VerifyACL: %v", err)
}
}
func TestVerifyBadSig(t *testing.T) {
keys := make([][]byte, 0)
keys = append(keys, []byte(keyValidSigner1))
keyring, keycount, err := pgp.ArmoredKeysToKeyring(keys)
if err != nil {
t.Fatalf("pgp.ArmoredKeysToKeyring: %v", err)
}
// We should have one valid key
if keycount != 1 {
t.Fatalf("unexpected number of keys in keyring: %v", keycount)
}
var a Action
err = json.Unmarshal([]byte(invalidSignedAction1), &a)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
// This should fail
err = a.VerifySignatures(keyring)
if err == nil {
t.Fatalf("VerifySignatures should have failed")
}
// Test verification given an ACL that is valid but an invalid signature
aclstr := `{
"pkg": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "474F6DCF3515C0E802519333CFDEF767E8201D1B",
"weight": 1
}
}
}
}`
var acl ACL
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err == nil {
t.Fatalf("VerifyACL should have failed. Error: %v", err)
}
// Try this again, but this time add the invalid signer to the keyring and invalidate the entry
// in the ACL
keys = append(keys, []byte(keyInvalidSigner1))
keyring, keycount, err = pgp.ArmoredKeysToKeyring(keys)
if err != nil {
t.Fatalf("pgp.ArmoredKeysToKeyring: %v", err)
}
// We should have 2 keys
if keycount != 2 {
t.Fatalf("unexpected number of keys in keyring: %v", keycount)
}
// This should pass now that the invalid signature is in the keyring
err = a.VerifySignatures(keyring)
if err != nil {
t.Fatalf("VerifySignatures: %v", err)
}
// An ACL that does not include the signature of the invalid signer
aclstr = `{
"pkg": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
}
}`
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err == nil {
t.Fatalf("VerifyACL should have failed")
}
}
func TestVerifyMultiOperation(t *testing.T) {
keys := make([][]byte, 0)
keys = append(keys, []byte(keyValidSigner1))
keyring, keycount, err := pgp.ArmoredKeysToKeyring(keys)
if err != nil {
t.Fatalf("pgp.ArmoredKeysToKeyring: %v", err)
}
// We should have one valid key
if keycount != 1 {
t.Fatalf("unexpected number of keys in keyring: %v", keycount)
}
var a Action
err = json.Unmarshal([]byte(signedMultiOpAction1), &a)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
// This should succeed
err = a.VerifySignatures(keyring)
if err != nil {
t.Fatalf("VerifySignatures: %v", err)
}
// Test verification given an ACL with just one module from the action listed
aclstr := `{
"file": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
}
}`
var acl ACL
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err == nil {
t.Fatalf("VerifyACL should have failed")
}
// Retry verification with an ACL with both modules now, which should succeed
aclstr = `{
"pkg": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
},
"file": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
}
}`
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err != nil {
t.Fatalf("VerifyACL: %v", err)
}
}
func TestVerifyNoSignatures(t *testing.T) {
keys := make([][]byte, 0)
keys = append(keys, []byte(keyValidSigner1))
keyring, keycount, err := pgp.ArmoredKeysToKeyring(keys)
if err != nil {
t.Fatalf("pgp.ArmoredKeysToKeyring: %v", err)
}
// We should have one valid key
if keycount != 1 {
t.Fatalf("unexpected number of keys in keyring: %v", keycount)
}
var a Action
err = json.Unmarshal([]byte(validSignedAction1), &a)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
// Remove the signature from the action
a.PGPSignatures = []string{}
// This should fail
err = a.VerifySignatures(keyring)
if err == nil {
t.Fatalf("VerifySignatures should have failed")
}
aclstr := `{
"pkg": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
}
}`
var acl ACL
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err == nil {
t.Fatalf("VerifyACL should have failed")
}
}
func TestVerifyNoKeyring(t *testing.T) {
keys := make([][]byte, 0)
keyring, keycount, err := pgp.ArmoredKeysToKeyring(keys)
if err != nil {
t.Fatalf("pgp.ArmoredKeysToKeyring: %v", err)
}
// We should have no keys
if keycount != 0 {
t.Fatalf("unexpected number of keys in keyring: %v", keycount)
}
var a Action
err = json.Unmarshal([]byte(validSignedAction1), &a)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
// This should fail
err = a.VerifySignatures(keyring)
if err == nil {
t.Fatalf("VerifySignatures should have failed")
}
aclstr := `{
"pkg": {
"minimumweight": 1,
"investigators": {
"valid user": {
"fingerprint": "397FD1F5E3DD4020BEF0E37E0F382D21C84C143A",
"weight": 1
}
}
}
}`
var acl ACL
err = json.Unmarshal([]byte(aclstr), &acl)
if err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
keyring.Seek(0, 0)
err = a.VerifyACL(acl, keyring, false)
if err == nil {
t.Fatalf("VerifyACL should have failed")
}
}