diff --git a/driver/driver.go b/driver/driver.go deleted file mode 100644 index be8c204ad0d..00000000000 --- a/driver/driver.go +++ /dev/null @@ -1,11 +0,0 @@ -package driver - -import ( - "github.com/ory/hydra/driver/configuration" -) - -type Driver interface { - Configuration() configuration.Provider - Registry() Registry - CallRegistry() Driver -} diff --git a/driver/driver_default.go b/driver/driver_default.go deleted file mode 100644 index ea71e2daaf1..00000000000 --- a/driver/driver_default.go +++ /dev/null @@ -1,47 +0,0 @@ -package driver - -import ( - "github.com/ory/x/logrusx" - - "github.com/ory/hydra/driver/configuration" -) - -type DefaultDriver struct { - c configuration.Provider - r Registry -} - -func NewDefaultDriver(l *logrusx.Logger, forcedHTTP bool, insecureRedirects []string, version, build, date string, validate bool) Driver { - c := configuration.NewViperProvider(l, forcedHTTP, insecureRedirects) - if validate { - configuration.MustValidate(l, c) - } - - r, err := NewRegistry(c, l) - if err != nil { - l.WithError(err).Fatal("Unable to instantiate service registry.") - } - - r. - WithConfig(c). - WithBuildInfo(version, build, date) - - if err = r.Init(); err != nil { - l.WithError(err).Fatal("Unable to initialize service registry.") - } - - return &DefaultDriver{r: r, c: c} -} - -func (r *DefaultDriver) Configuration() configuration.Provider { - return r.c -} - -func (r *DefaultDriver) Registry() Registry { - return r.r -} - -func (r *DefaultDriver) CallRegistry() Driver { - CallRegistry(r.Registry()) - return r -} diff --git a/driver/factory.go b/driver/factory.go new file mode 100644 index 00000000000..fbabe20d52c --- /dev/null +++ b/driver/factory.go @@ -0,0 +1,91 @@ +package driver + +import ( + "context" + "github.com/spf13/pflag" + + "github.com/ory/x/logrusx" + + "github.com/ory/hydra/driver/config" +) + +type options struct { + forcedValues map[string]interface{} + preload bool + validate bool +} + +func newOptions() *options { + return &options{ + forcedValues: make(map[string]interface{}), + validate: true, + preload: true, + } +} + +type OptionsModifier func(*options) + +// ForceConfigValue overrides any config values set by one of the providers. +func ForceConfigValue(key string, value interface{}) OptionsModifier { + return func(o *options) { + o.forcedValues[key] = value + } +} + +// DisableValidation validating the config. +// +// This does not affect schema validation! +func DisableValidation() OptionsModifier { + return func(o *options) { + o.validate = false + } +} + +// DisableValidation validating the config. +// +// This does not affect schema validation! +func DisablePreloading() OptionsModifier { + return func(o *options) { + o.preload = false + } +} + +func New(flags *pflag.FlagSet, opts ...OptionsModifier) Registry { + o := newOptions() + for _, f := range opts { + f(o) + } + + l := logrusx.New("ORY Hydra", config.Version) + c, err := config.New(flags, l) + if err != nil { + l.WithError(err).Fatal("Unable to instantiate service registry.") + } + l.UseConfig(c.Source()) + + for k, v := range o.forcedValues { + c.Set(k, v) + } + + if o.validate { + config.MustValidate(l, c) + } + + r, err := NewRegistryFromDSN(c, l) + if err != nil { + l.WithError(err).Fatal("Unable to instantiate service registry.") + } + + if err = r.Init(); err != nil { + l.WithError(err).Fatal("Unable to initialize service registry.") + } + + // Avoid cold cache issues on boot: + if o.preload { + CallRegistry(r) + } + + c.Source().SetTracer(context.Background(), r.Tracer()) + + return r +} diff --git a/driver/registry.go b/driver/registry.go index b3d919aa5ea..94be52fd0d6 100644 --- a/driver/registry.go +++ b/driver/registry.go @@ -21,7 +21,7 @@ import ( "github.com/ory/hydra/client" "github.com/ory/hydra/consent" - "github.com/ory/hydra/driver/configuration" + "github.com/ory/hydra/driver/config" "github.com/ory/hydra/jwk" "github.com/ory/hydra/oauth2" "github.com/ory/hydra/x" @@ -32,16 +32,10 @@ type Registry interface { Init() error - WithConfig(c configuration.Provider) Registry + WithConfig(c *config.ViperProvider) Registry WithLogger(l *logrusx.Logger) Registry - Config() configuration.Provider - - WithBuildInfo(version, hash, date string) Registry - BuildVersion() string - BuildDate() string - BuildHash() string - + Config() *config.ViperProvider persistence.Provider x.RegistryLogger x.RegistryWriter @@ -65,7 +59,7 @@ type Registry interface { WithConsentStrategy(c consent.Strategy) } -func NewRegistry(c configuration.Provider, l *logrusx.Logger) (Registry, error) { +func NewRegistryFromDSN(c *config.ViperProvider, l *logrusx.Logger) (Registry, error) { driver, err := dbal.GetDriverFor(c.DSN()) if err != nil { return nil, errorsx.WithStack(err) diff --git a/driver/registry_base.go b/driver/registry_base.go index 50019f37368..6cd67200915 100644 --- a/driver/registry_base.go +++ b/driver/registry_base.go @@ -6,6 +6,8 @@ import ( "strings" "time" + "github.com/ory/hydra/x/oauth2cors" + "github.com/ory/hydra/persistence" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -27,7 +29,7 @@ import ( "github.com/ory/hydra/client" "github.com/ory/hydra/consent" - "github.com/ory/hydra/driver/configuration" + "github.com/ory/hydra/driver/config" "github.com/ory/hydra/jwk" "github.com/ory/hydra/oauth2" "github.com/ory/hydra/x" @@ -36,7 +38,7 @@ import ( type RegistryBase struct { l *logrusx.Logger al *logrusx.Logger - C configuration.Provider + C *config.ViperProvider ch *client.Handler fh fosite.Hasher kh *jwk.Handler @@ -83,7 +85,7 @@ func (m *RegistryBase) WithBuildInfo(version, hash, date string) Registry { func (m *RegistryBase) OAuth2AwareMiddleware() func(h http.Handler) http.Handler { if m.oa2mw == nil { - m.oa2mw = OAuth2AwareCORSMiddleware("public", m.r, m.C) + m.oa2mw = oauth2cors.Middleware(m.r) } return m.oa2mw } @@ -114,7 +116,7 @@ func (m *RegistryBase) BuildHash() string { return m.buildHash } -func (m *RegistryBase) WithConfig(c configuration.Provider) Registry { +func (m *RegistryBase) WithConfig(c *config.ViperProvider) Registry { m.C = c return m.r } @@ -394,17 +396,11 @@ func (m *RegistryBase) SubjectIdentifierAlgorithm() map[string]consent.SubjectId func (m *RegistryBase) Tracer() *tracing.Tracer { if m.trc == nil { - m.trc = &tracing.Tracer{ - ServiceName: m.C.TracingServiceName(), - JaegerConfig: m.C.TracingJaegerConfig(), - ZipkinConfig: m.C.TracingZipkinConfig(), - Provider: m.C.TracingProvider(), - Logger: m.Logger(), - } - - if err := m.trc.Setup(); err != nil { + t, err := tracing.New(m.l, m.C.Tracing()) + if err != nil { m.Logger().WithError(err).Fatalf("Unable to initialize Tracer.") } + m.trc = t } return m.trc @@ -421,7 +417,7 @@ func (m *RegistryBase) Persister() persistence.Persister { return m.persister } -func (m *RegistryBase) Config() configuration.Provider { +func (m *RegistryBase) Config() *config.ViperProvider { return m.C } diff --git a/driver/registry_sql.go b/driver/registry_sql.go index fbb80649637..0c45bb7f7d6 100644 --- a/driver/registry_sql.go +++ b/driver/registry_sql.go @@ -10,8 +10,6 @@ import ( "github.com/luna-duclos/instrumentedsql" "github.com/luna-duclos/instrumentedsql/opentracing" - "github.com/ory/hydra/driver/configuration" - "github.com/ory/x/resilience" "github.com/gobuffalo/pop/v5" @@ -83,7 +81,7 @@ func (m *RegistrySQL) Init() error { } // if dsn is memory we have to run the migrations on every start - if m.C.DSN() == configuration.DefaultSQLiteMemoryDSN { + if m.C.DSN() == dbal.InMemoryDSN { m.Logger().Print("Hydra is running migrations on every startup as DSN is memory.\n") m.Logger().Print("This means your data is lost when Hydra terminates.\n") if err := m.persister.MigrateUp(context.Background()); err != nil {