From db1d0cbf35668eb28fa7544c18e04e6fd199f5e1 Mon Sep 17 00:00:00 2001 From: David Wu Date: Tue, 1 Nov 2016 16:44:18 -0700 Subject: [PATCH] Add registry middleware access to storage drivers Signed-off-by: David Wu --- registry/handlers/app.go | 6 +++--- registry/middleware/registry/middleware.go | 7 ++++--- registry/registry.go | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/registry/handlers/app.go b/registry/handlers/app.go index 6ace01ac1..442c70051 100644 --- a/registry/handlers/app.go +++ b/registry/handlers/app.go @@ -301,7 +301,7 @@ func NewApp(ctx context.Context, config *configuration.Configuration) *App { } } - app.registry, err = applyRegistryMiddleware(app, app.registry, config.Middleware["registry"]) + app.registry, err = applyRegistryMiddleware(app, app.registry, app.driver, config.Middleware["registry"]) if err != nil { panic(err) } @@ -958,9 +958,9 @@ func appendCatalogAccessRecord(accessRecords []auth.Access, r *http.Request) []a } // applyRegistryMiddleware wraps a registry instance with the configured middlewares -func applyRegistryMiddleware(ctx context.Context, registry distribution.Namespace, middlewares []configuration.Middleware) (distribution.Namespace, error) { +func applyRegistryMiddleware(ctx context.Context, registry distribution.Namespace, driver storagedriver.StorageDriver, middlewares []configuration.Middleware) (distribution.Namespace, error) { for _, mw := range middlewares { - rmw, err := registrymiddleware.Get(ctx, mw.Name, mw.Options, registry) + rmw, err := registrymiddleware.Get(ctx, mw.Name, mw.Options, registry, driver) if err != nil { return nil, fmt.Errorf("unable to configure registry middleware (%s): %s", mw.Name, err) } diff --git a/registry/middleware/registry/middleware.go b/registry/middleware/registry/middleware.go index c9d80ef3c..603239b9f 100644 --- a/registry/middleware/registry/middleware.go +++ b/registry/middleware/registry/middleware.go @@ -6,11 +6,12 @@ import ( "github.com/distribution/distribution/v3" "github.com/distribution/distribution/v3/registry/storage" + storagedriver "github.com/distribution/distribution/v3/registry/storage/driver" ) // InitFunc is the type of a RegistryMiddleware factory function and is // used to register the constructor for different RegistryMiddleware backends. -type InitFunc func(ctx context.Context, registry distribution.Namespace, options map[string]interface{}) (distribution.Namespace, error) +type InitFunc func(ctx context.Context, registry distribution.Namespace, driver storagedriver.StorageDriver, options map[string]interface{}) (distribution.Namespace, error) var middlewares map[string]InitFunc var registryoptions []storage.RegistryOption @@ -31,10 +32,10 @@ func Register(name string, initFunc InitFunc) error { } // Get constructs a RegistryMiddleware with the given options using the named backend. -func Get(ctx context.Context, name string, options map[string]interface{}, registry distribution.Namespace) (distribution.Namespace, error) { +func Get(ctx context.Context, name string, options map[string]interface{}, registry distribution.Namespace, driver storagedriver.StorageDriver) (distribution.Namespace, error) { if middlewares != nil { if initFunc, exists := middlewares[name]; exists { - return initFunc(ctx, registry, options) + return initFunc(ctx, registry, driver, options) } } diff --git a/registry/registry.go b/registry/registry.go index 05e920669..cce628d32 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -92,6 +92,16 @@ var tlsVersions = map[string]uint16{ // this channel gets notified when process receives signal. It is global to ease unit testing var quit = make(chan os.Signal, 1) +// HandlerFunc defines an http middleware +type HandlerFunc func(config *configuration.Configuration, handler http.Handler) http.Handler + +var handlerMiddlewares []HandlerFunc + +// RegisterHandler is used to register http middlewares to the registry service +func RegisterHandler(handlerFunc HandlerFunc) { + handlerMiddlewares = append(handlerMiddlewares, handlerFunc) +} + // ServeCmd is a cobra command for running the registry. var ServeCmd = &cobra.Command{ Use: "serve ", @@ -172,6 +182,10 @@ func NewRegistry(ctx context.Context, config *configuration.Configuration) (*Reg handler = gorhandlers.CombinedLoggingHandler(os.Stdout, handler) } + for _, applyHandlerMiddleware := range handlerMiddlewares { + handler = applyHandlerMiddleware(config, handler) + } + server := &http.Server{ Handler: handler, }