Skip to content

Commit

Permalink
cmd: Check dependencies are defined before instantiation
Browse files Browse the repository at this point in the history
Closes #928

Signed-off-by: arekkas <aeneas@ory.am>
  • Loading branch information
arekkas committed Jul 16, 2018
1 parent 6ca0733 commit 65d43d1
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 6 deletions.
5 changes: 4 additions & 1 deletion cmd/server/handler.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cmd/server/handler_client_factory.go
Expand Up @@ -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,
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions cmd/server/handler_consent_factory.go
Expand Up @@ -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,
Expand All @@ -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
}
1 change: 1 addition & 0 deletions cmd/server/handler_health_factory.go
Expand Up @@ -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()
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/server/handler_jwk_factory.go
Expand Up @@ -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{
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions cmd/server/handler_oauth2_factory.go
Expand Up @@ -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:
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions cmd/server/helper_cert.go
Expand Up @@ -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?`)
Expand Down
34 changes: 34 additions & 0 deletions cmd/server/helper_deps.go
@@ -0,0 +1,34 @@
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* 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 <aeneas+oss@aeneas.io>
* @Copyright 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @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.")
}
}
}
2 changes: 2 additions & 0 deletions cmd/server/helper_keys.go
Expand Up @@ -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)
Expand Down

0 comments on commit 65d43d1

Please sign in to comment.