@@ -268,6 +268,131 @@ ZEp7knvU2psWRw==
268
268
}
269
269
}
270
270
271
+ func TestCAProvisioners (t * testing.T ) {
272
+ config , err := authority .LoadConfiguration ("testdata/ca.json" )
273
+ assert .FatalError (t , err )
274
+ ca , err := New (config )
275
+ assert .FatalError (t , err )
276
+
277
+ type ekt struct {
278
+ ca * CA
279
+ status int
280
+ errMsg string
281
+ }
282
+ tests := map [string ]func (t * testing.T ) * ekt {
283
+ "ok" : func (t * testing.T ) * ekt {
284
+ return & ekt {
285
+ ca : ca ,
286
+ status : http .StatusOK ,
287
+ }
288
+ },
289
+ }
290
+
291
+ for name , genTestCase := range tests {
292
+ t .Run (name , func (t * testing.T ) {
293
+ tc := genTestCase (t )
294
+
295
+ rq , err := http .NewRequest ("GET" , fmt .Sprintf ("/provisioners" ), strings .NewReader ("" ))
296
+ assert .FatalError (t , err )
297
+ rr := httptest .NewRecorder ()
298
+
299
+ tc .ca .srv .Handler .ServeHTTP (rr , rq )
300
+
301
+ if assert .Equals (t , rr .Code , tc .status ) {
302
+ body := & ClosingBuffer {rr .Body }
303
+ if rr .Code < http .StatusBadRequest {
304
+ var (
305
+ resp api.ProvisionersResponse
306
+ psList = config .AuthorityConfig .Provisioners
307
+ )
308
+
309
+ assert .FatalError (t , readJSON (body , & resp ))
310
+ psMap := resp .Provisioners
311
+
312
+ maxks , found := psMap ["max" ]
313
+ assert .Fatal (t , found )
314
+ assert .Equals (t , maxks .Keys , []jose.JSONWebKey {* psList [0 ].Key , * psList [1 ].Key })
315
+
316
+ marianoks , found := psMap ["mariano" ]
317
+ assert .Fatal (t , found )
318
+ assert .Equals (t , marianoks .Keys , []jose.JSONWebKey {* psList [3 ].Key , * psList [4 ].Key })
319
+
320
+ stepcliks , found := psMap ["step-cli" ]
321
+ assert .Fatal (t , found )
322
+ assert .Equals (t , stepcliks .Keys , []jose.JSONWebKey {* psList [2 ].Key })
323
+ } else {
324
+ err := readError (body )
325
+ if len (tc .errMsg ) == 0 {
326
+ assert .FatalError (t , errors .New ("must validate response error" ))
327
+ }
328
+ assert .HasPrefix (t , err .Error (), tc .errMsg )
329
+ }
330
+ }
331
+ })
332
+ }
333
+ }
334
+
335
+ func TestCAProvisionerEncryptedKey (t * testing.T ) {
336
+ config , err := authority .LoadConfiguration ("testdata/ca.json" )
337
+ assert .FatalError (t , err )
338
+ ca , err := New (config )
339
+ assert .FatalError (t , err )
340
+
341
+ type ekt struct {
342
+ ca * CA
343
+ kid string
344
+ expectedKey string
345
+ status int
346
+ errMsg string
347
+ }
348
+ tests := map [string ]func (t * testing.T ) * ekt {
349
+ "not-found" : func (t * testing.T ) * ekt {
350
+ return & ekt {
351
+ ca : ca ,
352
+ kid : "foo" ,
353
+ status : http .StatusNotFound ,
354
+ errMsg : "Not Found" ,
355
+ }
356
+ },
357
+ "ok" : func (t * testing.T ) * ekt {
358
+ p := config .AuthorityConfig .Provisioners [2 ]
359
+ return & ekt {
360
+ ca : ca ,
361
+ kid : p .Key .KeyID ,
362
+ expectedKey : p .EncryptedKey ,
363
+ status : http .StatusOK ,
364
+ }
365
+ },
366
+ }
367
+
368
+ for name , genTestCase := range tests {
369
+ t .Run (name , func (t * testing.T ) {
370
+ tc := genTestCase (t )
371
+
372
+ rq , err := http .NewRequest ("GET" , fmt .Sprintf ("/provisioners/%s/encrypted-key" , tc .kid ), strings .NewReader ("" ))
373
+ assert .FatalError (t , err )
374
+ rr := httptest .NewRecorder ()
375
+
376
+ tc .ca .srv .Handler .ServeHTTP (rr , rq )
377
+
378
+ if assert .Equals (t , rr .Code , tc .status ) {
379
+ body := & ClosingBuffer {rr .Body }
380
+ if rr .Code < http .StatusBadRequest {
381
+ var ek api.ProvisionerKeyResponse
382
+ assert .FatalError (t , readJSON (body , & ek ))
383
+ assert .Equals (t , ek .Key , tc .expectedKey )
384
+ } else {
385
+ err := readError (body )
386
+ if len (tc .errMsg ) == 0 {
387
+ assert .FatalError (t , errors .New ("must validate response error" ))
388
+ }
389
+ assert .HasPrefix (t , err .Error (), tc .errMsg )
390
+ }
391
+ }
392
+ })
393
+ }
394
+ }
395
+
271
396
func TestCARoot (t * testing.T ) {
272
397
config , err := authority .LoadConfiguration ("testdata/ca.json" )
273
398
assert .FatalError (t , err )
0 commit comments