@@ -52,6 +52,16 @@ type Authority interface {
52
52
Version () authority.Version
53
53
}
54
54
55
+ var errAuthority = errors .New ("authority is not in context" )
56
+
57
+ func mustAuthority (ctx context.Context ) Authority {
58
+ a , ok := authority .FromContext (ctx )
59
+ if ! ok {
60
+ panic (errAuthority )
61
+ }
62
+ return a
63
+ }
64
+
55
65
// TimeDuration is an alias of provisioner.TimeDuration
56
66
type TimeDuration = provisioner.TimeDuration
57
67
@@ -251,58 +261,58 @@ func New(auth Authority) RouterHandler {
251
261
}
252
262
253
263
func (h * caHandler ) Route (r Router ) {
254
- r .MethodFunc ("GET" , "/version" , h . Version )
255
- r .MethodFunc ("GET" , "/health" , h . Health )
256
- r .MethodFunc ("GET" , "/root/{sha}" , h . Root )
257
- r .MethodFunc ("POST" , "/sign" , h . Sign )
258
- r .MethodFunc ("POST" , "/renew" , h . Renew )
259
- r .MethodFunc ("POST" , "/rekey" , h . Rekey )
260
- r .MethodFunc ("POST" , "/revoke" , h . Revoke )
261
- r .MethodFunc ("GET" , "/provisioners" , h . Provisioners )
262
- r .MethodFunc ("GET" , "/provisioners/{kid}/encrypted-key" , h . ProvisionerKey )
263
- r .MethodFunc ("GET" , "/roots" , h . Roots )
264
- r .MethodFunc ("GET" , "/roots.pem" , h . RootsPEM )
265
- r .MethodFunc ("GET" , "/federation" , h . Federation )
264
+ r .MethodFunc ("GET" , "/version" , Version )
265
+ r .MethodFunc ("GET" , "/health" , Health )
266
+ r .MethodFunc ("GET" , "/root/{sha}" , Root )
267
+ r .MethodFunc ("POST" , "/sign" , Sign )
268
+ r .MethodFunc ("POST" , "/renew" , Renew )
269
+ r .MethodFunc ("POST" , "/rekey" , Rekey )
270
+ r .MethodFunc ("POST" , "/revoke" , Revoke )
271
+ r .MethodFunc ("GET" , "/provisioners" , Provisioners )
272
+ r .MethodFunc ("GET" , "/provisioners/{kid}/encrypted-key" , ProvisionerKey )
273
+ r .MethodFunc ("GET" , "/roots" , Roots )
274
+ r .MethodFunc ("GET" , "/roots.pem" , RootsPEM )
275
+ r .MethodFunc ("GET" , "/federation" , Federation )
266
276
// SSH CA
267
- r .MethodFunc ("POST" , "/ssh/sign" , h . SSHSign )
268
- r .MethodFunc ("POST" , "/ssh/renew" , h . SSHRenew )
269
- r .MethodFunc ("POST" , "/ssh/revoke" , h . SSHRevoke )
270
- r .MethodFunc ("POST" , "/ssh/rekey" , h . SSHRekey )
271
- r .MethodFunc ("GET" , "/ssh/roots" , h . SSHRoots )
272
- r .MethodFunc ("GET" , "/ssh/federation" , h . SSHFederation )
273
- r .MethodFunc ("POST" , "/ssh/config" , h . SSHConfig )
274
- r .MethodFunc ("POST" , "/ssh/config/{type}" , h . SSHConfig )
275
- r .MethodFunc ("POST" , "/ssh/check-host" , h . SSHCheckHost )
276
- r .MethodFunc ("GET" , "/ssh/hosts" , h . SSHGetHosts )
277
- r .MethodFunc ("POST" , "/ssh/bastion" , h . SSHBastion )
277
+ r .MethodFunc ("POST" , "/ssh/sign" , SSHSign )
278
+ r .MethodFunc ("POST" , "/ssh/renew" , SSHRenew )
279
+ r .MethodFunc ("POST" , "/ssh/revoke" , SSHRevoke )
280
+ r .MethodFunc ("POST" , "/ssh/rekey" , SSHRekey )
281
+ r .MethodFunc ("GET" , "/ssh/roots" , SSHRoots )
282
+ r .MethodFunc ("GET" , "/ssh/federation" , SSHFederation )
283
+ r .MethodFunc ("POST" , "/ssh/config" , SSHConfig )
284
+ r .MethodFunc ("POST" , "/ssh/config/{type}" , SSHConfig )
285
+ r .MethodFunc ("POST" , "/ssh/check-host" , SSHCheckHost )
286
+ r .MethodFunc ("GET" , "/ssh/hosts" , SSHGetHosts )
287
+ r .MethodFunc ("POST" , "/ssh/bastion" , SSHBastion )
278
288
279
289
// For compatibility with old code:
280
- r .MethodFunc ("POST" , "/re-sign" , h . Renew )
281
- r .MethodFunc ("POST" , "/sign-ssh" , h . SSHSign )
282
- r .MethodFunc ("GET" , "/ssh/get-hosts" , h . SSHGetHosts )
290
+ r .MethodFunc ("POST" , "/re-sign" , Renew )
291
+ r .MethodFunc ("POST" , "/sign-ssh" , SSHSign )
292
+ r .MethodFunc ("GET" , "/ssh/get-hosts" , SSHGetHosts )
283
293
}
284
294
285
295
// Version is an HTTP handler that returns the version of the server.
286
- func ( h * caHandler ) Version (w http.ResponseWriter , r * http.Request ) {
287
- v := h . Authority .Version ()
296
+ func Version (w http.ResponseWriter , r * http.Request ) {
297
+ v := mustAuthority ( r . Context ()) .Version ()
288
298
render .JSON (w , VersionResponse {
289
299
Version : v .Version ,
290
300
RequireClientAuthentication : v .RequireClientAuthentication ,
291
301
})
292
302
}
293
303
294
304
// Health is an HTTP handler that returns the status of the server.
295
- func ( h * caHandler ) Health (w http.ResponseWriter , r * http.Request ) {
305
+ func Health (w http.ResponseWriter , r * http.Request ) {
296
306
render .JSON (w , HealthResponse {Status : "ok" })
297
307
}
298
308
299
309
// Root is an HTTP handler that using the SHA256 from the URL, returns the root
300
310
// certificate for the given SHA256.
301
- func ( h * caHandler ) Root (w http.ResponseWriter , r * http.Request ) {
311
+ func Root (w http.ResponseWriter , r * http.Request ) {
302
312
sha := chi .URLParam (r , "sha" )
303
313
sum := strings .ToLower (strings .ReplaceAll (sha , "-" , "" ))
304
314
// Load root certificate with the
305
- cert , err := h . Authority .Root (sum )
315
+ cert , err := mustAuthority ( r . Context ()) .Root (sum )
306
316
if err != nil {
307
317
render .Error (w , errs .Wrapf (http .StatusNotFound , err , "%s was not found" , r .RequestURI ))
308
318
return
@@ -320,38 +330,40 @@ func certChainToPEM(certChain []*x509.Certificate) []Certificate {
320
330
}
321
331
322
332
// Provisioners returns the list of provisioners configured in the authority.
323
- func ( h * caHandler ) Provisioners (w http.ResponseWriter , r * http.Request ) {
333
+ func Provisioners (w http.ResponseWriter , r * http.Request ) {
324
334
cursor , limit , err := ParseCursor (r )
325
335
if err != nil {
326
336
render .Error (w , err )
327
337
return
328
338
}
329
339
330
- p , next , err := h . Authority .GetProvisioners (cursor , limit )
340
+ p , next , err := mustAuthority ( r . Context ()) .GetProvisioners (cursor , limit )
331
341
if err != nil {
332
342
render .Error (w , errs .InternalServerErr (err ))
333
343
return
334
344
}
345
+
335
346
render .JSON (w , & ProvisionersResponse {
336
347
Provisioners : p ,
337
348
NextCursor : next ,
338
349
})
339
350
}
340
351
341
352
// ProvisionerKey returns the encrypted key of a provisioner by it's key id.
342
- func ( h * caHandler ) ProvisionerKey (w http.ResponseWriter , r * http.Request ) {
353
+ func ProvisionerKey (w http.ResponseWriter , r * http.Request ) {
343
354
kid := chi .URLParam (r , "kid" )
344
- key , err := h . Authority .GetEncryptedKey (kid )
355
+ key , err := mustAuthority ( r . Context ()) .GetEncryptedKey (kid )
345
356
if err != nil {
346
357
render .Error (w , errs .NotFoundErr (err ))
347
358
return
348
359
}
360
+
349
361
render .JSON (w , & ProvisionerKeyResponse {key })
350
362
}
351
363
352
364
// Roots returns all the root certificates for the CA.
353
- func ( h * caHandler ) Roots (w http.ResponseWriter , r * http.Request ) {
354
- roots , err := h . Authority .GetRoots ()
365
+ func Roots (w http.ResponseWriter , r * http.Request ) {
366
+ roots , err := mustAuthority ( r . Context ()) .GetRoots ()
355
367
if err != nil {
356
368
render .Error (w , errs .ForbiddenErr (err , "error getting roots" ))
357
369
return
@@ -368,8 +380,8 @@ func (h *caHandler) Roots(w http.ResponseWriter, r *http.Request) {
368
380
}
369
381
370
382
// RootsPEM returns all the root certificates for the CA in PEM format.
371
- func ( h * caHandler ) RootsPEM (w http.ResponseWriter , r * http.Request ) {
372
- roots , err := h . Authority .GetRoots ()
383
+ func RootsPEM (w http.ResponseWriter , r * http.Request ) {
384
+ roots , err := mustAuthority ( r . Context ()) .GetRoots ()
373
385
if err != nil {
374
386
render .Error (w , errs .InternalServerErr (err ))
375
387
return
@@ -391,8 +403,8 @@ func (h *caHandler) RootsPEM(w http.ResponseWriter, r *http.Request) {
391
403
}
392
404
393
405
// Federation returns all the public certificates in the federation.
394
- func ( h * caHandler ) Federation (w http.ResponseWriter , r * http.Request ) {
395
- federated , err := h . Authority .GetFederation ()
406
+ func Federation (w http.ResponseWriter , r * http.Request ) {
407
+ federated , err := mustAuthority ( r . Context ()) .GetFederation ()
396
408
if err != nil {
397
409
render .Error (w , errs .ForbiddenErr (err , "error getting federated roots" ))
398
410
return
0 commit comments