diff --git a/cmd/server/handler.go b/cmd/server/handler.go index c0db0e5286..7effb3b825 100644 --- a/cmd/server/handler.go +++ b/cmd/server/handler.go @@ -49,6 +49,8 @@ import ( var _ = &consent.Handler{} +var errNilDependency = errors.New("A dependency was expected to be defined but is nil. Please open an issue with the stack trace.") + func RunHost(c *config.Config) func(cmd *cobra.Command, args []string) { return func(cmd *cobra.Command, args []string) { fmt.Println(banner) @@ -163,9 +165,10 @@ func (h *Handler) registerRoutes(router *httprouter.Router) { // Set up dependencies injectJWKManager(c) clientsManager := newClientManager(c) - injectConsentManager(c, clientsManager) injectFositeStore(c, clientsManager) + injectConsentManager(c, clientsManager) + oauth2Provider := newOAuth2Provider(c) // Set up handlers diff --git a/cmd/server/handler_client_factory.go b/cmd/server/handler_client_factory.go index fddfe2f5b3..b84c7df08a 100644 --- a/cmd/server/handler_client_factory.go +++ b/cmd/server/handler_client_factory.go @@ -35,8 +35,10 @@ func newClientManager(c *config.Config) client.Manager { switch con := ctx.Connection.(type) { case *config.MemoryConnection: + expectDependency(c.GetLogger(), ctx.Hasher) return client.NewMemoryManager(ctx.Hasher) case *sqlcon.SQLConnection: + expectDependency(c.GetLogger(), ctx.Hasher, con.GetDatabase()) return &client.SQLManager{ DB: con.GetDatabase(), Hasher: ctx.Hasher, @@ -58,6 +60,7 @@ func newClientHandler(c *config.Config, router *httprouter.Router, manager clien w := herodot.NewJSONWriter(c.GetLogger()) w.ErrorEnhancer = writerErrorEnhancer + expectDependency(c.GetLogger(), manager) h := client.NewHandler(manager, w, strings.Split(c.DefaultClientScope, ",")) h.SetRoutes(router) return h diff --git a/cmd/server/handler_consent_factory.go b/cmd/server/handler_consent_factory.go index 51bae59473..c4047f4bf6 100644 --- a/cmd/server/handler_consent_factory.go +++ b/cmd/server/handler_consent_factory.go @@ -35,9 +35,11 @@ func injectConsentManager(c *config.Config, cm client.Manager) { switch con := ctx.Connection.(type) { case *config.MemoryConnection: + expectDependency(c.GetLogger(), ctx.FositeStore) manager = consent.NewMemoryManager(ctx.FositeStore) break case *sqlcon.SQLConnection: + expectDependency(c.GetLogger(), ctx.FositeStore, con.GetDatabase()) manager = consent.NewSQLManager( con.GetDatabase(), cm, @@ -63,11 +65,8 @@ func newConsentHandler(c *config.Config, router *httprouter.Router) *consent.Han w := herodot.NewJSONWriter(c.GetLogger()) w.ErrorEnhancer = writerErrorEnhancer - h := &consent.Handler{ - H: w, - M: ctx.ConsentManager, - } - + expectDependency(c.GetLogger(), ctx.ConsentManager) + h := consent.NewHandler(w, ctx.ConsentManager) h.SetRoutes(router) return h } diff --git a/cmd/server/handler_health_factory.go b/cmd/server/handler_health_factory.go index 7ef17ebfbb..6b502c3e84 100644 --- a/cmd/server/handler_health_factory.go +++ b/cmd/server/handler_health_factory.go @@ -39,6 +39,7 @@ func newHealthHandler(c *config.Config, router *httprouter.Router) *health.Handl } break case *sqlcon.SQLConnection: + expectDependency(c.GetLogger(), con.GetDatabase()) rc = func() error { return con.GetDatabase().Ping() } diff --git a/cmd/server/handler_jwk_factory.go b/cmd/server/handler_jwk_factory.go index 784cdd9870..3672ddc8f9 100644 --- a/cmd/server/handler_jwk_factory.go +++ b/cmd/server/handler_jwk_factory.go @@ -36,6 +36,7 @@ func injectJWKManager(c *config.Config) { ctx.KeyManager = &jwk.MemoryManager{} break case *sqlcon.SQLConnection: + expectDependency(c.GetLogger(), con.GetDatabase()) ctx.KeyManager = &jwk.SQLManager{ DB: con.GetDatabase(), Cipher: &jwk.AEAD{ @@ -60,6 +61,7 @@ func newJWKHandler(c *config.Config, router *httprouter.Router) *jwk.Handler { w := herodot.NewJSONWriter(c.GetLogger()) w.ErrorEnhancer = writerErrorEnhancer + expectDependency(c.GetLogger(), ctx.KeyManager) h := &jwk.Handler{ H: w, Manager: ctx.KeyManager, diff --git a/cmd/server/handler_oauth2_factory.go b/cmd/server/handler_oauth2_factory.go index af08325e56..fc46fedf31 100644 --- a/cmd/server/handler_oauth2_factory.go +++ b/cmd/server/handler_oauth2_factory.go @@ -51,6 +51,7 @@ func injectFositeStore(c *config.Config, clients client.Manager) { store = oauth2.NewFositeMemoryStore(clients, c.GetAccessTokenLifespan()) break case *sqlcon.SQLConnection: + expectDependency(c.GetLogger(), con.GetDatabase()) store = oauth2.NewFositeSQLStore(clients, con.GetDatabase(), c.GetLogger(), c.GetAccessTokenLifespan()) break case *config.PluginConnection: @@ -69,6 +70,7 @@ func injectFositeStore(c *config.Config, clients client.Manager) { func newOAuth2Provider(c *config.Config) fosite.OAuth2Provider { var ctx = c.Context() var store = ctx.FositeStore + expectDependency(c.GetLogger(), ctx.FositeStore) kid := uuid.New() if _, err := createOrGetJWK(c, oauth2.OpenIDConnectKeyName, kid, "private"); err != nil { @@ -137,6 +139,8 @@ func setDefaultConsentURL(s string, c *config.Config, path string) string { //func newOAuth2Handler(c *config.Config, router *httprouter.Router, cm oauth2.ConsentRequestManager, o fosite.OAuth2Provider, idTokenKeyID string) *oauth2.Handler { func newOAuth2Handler(c *config.Config, router *httprouter.Router, cm consent.Manager, o fosite.OAuth2Provider) *oauth2.Handler { + expectDependency(c.GetLogger(), c.Context().FositeStore) + c.ConsentURL = setDefaultConsentURL(c.ConsentURL, c, "oauth2/fallbacks/consent") c.LoginURL = setDefaultConsentURL(c.LoginURL, c, "oauth2/fallbacks/consent") c.ErrorURL = setDefaultConsentURL(c.ErrorURL, c, "oauth2/fallbacks/error") diff --git a/cmd/server/helper_cert.go b/cmd/server/helper_cert.go index 23dc0317fa..a9566f3f32 100644 --- a/cmd/server/helper_cert.go +++ b/cmd/server/helper_cert.go @@ -91,6 +91,8 @@ func getOrCreateTLSCertificate(cmd *cobra.Command, c *config.Config) tls.Certifi } ctx := c.Context() + expectDependency(c.GetLogger(), ctx.KeyManager) + privateKey, err := createOrGetJWK(c, tlsKeyName, "", "private") if err != nil { c.GetLogger().WithError(err).Fatalf(`Could not fetch TLS keys - did you forget to run "hydra migrate sql" or forget to set the SYSTEM_SECRET?`) diff --git a/cmd/server/helper_deps.go b/cmd/server/helper_deps.go new file mode 100644 index 0000000000..4601670bbf --- /dev/null +++ b/cmd/server/helper_deps.go @@ -0,0 +1,34 @@ +/* + * Copyright © 2015-2018 Aeneas Rekkas + * + * 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. + * + * @author Aeneas Rekkas + * @Copyright 2017-2018 Aeneas Rekkas + * @license Apache-2.0 + */ + +package server + +import ( + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +func expectDependency(logger logrus.FieldLogger, dependencies ...interface{}) { + for _, d := range dependencies { + if d == nil { + logger.WithError(errors.WithStack(errNilDependency)).Fatalf("A fatal issue occurred.") + } + } +} diff --git a/cmd/server/helper_keys.go b/cmd/server/helper_keys.go index f2d6eaa7e2..81809e8ea4 100644 --- a/cmd/server/helper_keys.go +++ b/cmd/server/helper_keys.go @@ -34,6 +34,8 @@ import ( func createOrGetJWK(c *config.Config, set string, kid string, prefix string) (key *jose.JSONWebKey, err error) { ctx := c.Context() + expectDependency(c.GetLogger(), ctx.KeyManager) + keys, err := ctx.KeyManager.GetKeySet(set) if errors.Cause(err) == pkg.ErrNotFound || keys != nil && len(keys.Keys) == 0 { c.GetLogger().Infof("JSON Web Key Set %s does not exist yet, generating new key pair...", set)