From 831de2cd086f81a6c5ced5cdffdf583057150633 Mon Sep 17 00:00:00 2001 From: hperl <34397+hperl@users.noreply.github.com> Date: Tue, 7 Feb 2023 15:00:30 +0100 Subject: [PATCH] feat: forward config options in middleware --- driver/registry_memory.go | 2 +- middleware/definitions.go | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/driver/registry_memory.go b/driver/registry_memory.go index 3f514d6fe2..d159a9f641 100644 --- a/driver/registry_memory.go +++ b/driver/registry_memory.go @@ -428,7 +428,7 @@ func (r *RegistryMemory) Tracer() trace.Tracer { var err error r.trc, err = otelx.New(r.c.TracingServiceName(), r.Logger(), r.c.TracingConfig()) if err != nil { - r.Logger().WithError(err).Fatalf("Unable to initialize Tracer.") + r.Logger().WithError(err).Fatalf("Unable to initialize Tracer for Oathkeeper.") } } return r.trc.Tracer() diff --git a/middleware/definitions.go b/middleware/definitions.go index 257f6c9b2e..2537ece08a 100644 --- a/middleware/definitions.go +++ b/middleware/definitions.go @@ -44,6 +44,7 @@ type ( configFile string registryAddr *driver.Registry configProviderAddr *configuration.Provider + configProviderOpts []configx.OptionModifier } Option func(*options) @@ -59,6 +60,18 @@ func WithLogger(logger *logrusx.Logger) Option { return func(o *options) { o.logger = logger } } +// WithConfigOption sets a config option for the middleware. The following +// options will be set regardless: +// - configx.WithContext +// - configx.WithLogger +// - configx.WithConfigFiles +// - configx.DisableEnvLoading +func WithConfigOption(option configx.OptionModifier) Option { + return func(o *options) { + o.configProviderOpts = append(o.configProviderOpts, option) + } +} + // New creates an Oathkeeper middleware from the options. By default, it tries // to read the configuration from the file "oathkeeper.yaml". func New(ctx context.Context, opts ...Option) (Middleware, error) { @@ -72,10 +85,12 @@ func New(ctx context.Context, opts ...Option) (Middleware, error) { c, err := configuration.NewKoanfProvider( ctx, nil, o.logger, - configx.WithContext(ctx), - configx.WithLogger(o.logger), - configx.WithConfigFiles(o.configFile), - configx.DisableEnvLoading(), + append(o.configProviderOpts, + configx.WithContext(ctx), + configx.WithLogger(o.logger), + configx.WithConfigFiles(o.configFile), + configx.DisableEnvLoading(), + )..., ) if err != nil { return nil, errors.WithStack(err)