From 48e2fabeb828b42c043820e6bc010db08b765b96 Mon Sep 17 00:00:00 2001 From: Mariano Cano Date: Wed, 27 Apr 2022 11:38:06 -0700 Subject: [PATCH] Add authority.MustFromContext --- api/api.go | 8 +------- authority/authority.go | 15 ++++++++------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/api/api.go b/api/api.go index e5f4f944..0ca4a5ef 100644 --- a/api/api.go +++ b/api/api.go @@ -52,15 +52,9 @@ type Authority interface { Version() authority.Version } -var errAuthority = errors.New("authority is not in context") - // mustAuthority will be replaced on unit tests. var mustAuthority = func(ctx context.Context) Authority { - a, ok := authority.FromContext(ctx) - if !ok { - panic(errAuthority) - } - return a + return authority.MustFromContext(ctx) } // TimeDuration is an alias of provisioner.TimeDuration diff --git a/authority/authority.go b/authority/authority.go index 091a01ae..92ed6b31 100644 --- a/authority/authority.go +++ b/authority/authority.go @@ -7,7 +7,6 @@ import ( "crypto/x509" "encoding/hex" "log" - "net/http" "strings" "sync" "time" @@ -167,12 +166,14 @@ func FromContext(ctx context.Context) (a *Authority, ok bool) { return } -// Middleware adds the current authority to the request context. -func (a *Authority) Middleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := NewContext(r.Context(), a) - next.ServeHTTP(w, r.WithContext(ctx)) - }) +// MustFromContext returns the current authority from the given context. It will +// panic if the authority is not in the context. +func MustFromContext(ctx context.Context) *Authority { + if a, ok := FromContext(ctx); !ok { + panic("authority is not in the context") + } else { + return a + } } // reloadAdminResources reloads admins and provisioners from the DB.