From 6086124485612fd776278a5c45e7386089a81e36 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Thu, 30 Jul 2015 09:47:12 -0700 Subject: [PATCH 1/3] Lazy initialize UUID for Background context Fixes #782 Signed-off-by: Darren Shepherd --- context/context.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/context/context.go b/context/context.go index 7a3a70e0..07b844ef 100644 --- a/context/context.go +++ b/context/context.go @@ -1,10 +1,16 @@ package context import ( + "sync" + "github.com/docker/distribution/uuid" "golang.org/x/net/context" ) +var ( + once sync.Once +) + // Context is a copy of Context from the golang.org/x/net/context package. type Context interface { context.Context @@ -19,6 +25,13 @@ type instanceContext struct { func (ic *instanceContext) Value(key interface{}) interface{} { if key == "instance.id" { + once.Do(func() { + // We want to lazy initialize the UUID such that we don't + // call a random generator from the package initialization + // code. For various reasons random could not be available + // https://github.com/docker/distribution/issues/782 + ic.id = uuid.Generate().String() + }) return ic.id } @@ -27,7 +40,6 @@ func (ic *instanceContext) Value(key interface{}) interface{} { var background = &instanceContext{ Context: context.Background(), - id: uuid.Generate().String(), } // Background returns a non-nil, empty Context. The background context From c737d19235181760ce2d6df3b57ea44864e81af8 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Thu, 30 Jul 2015 13:59:44 -0700 Subject: [PATCH 2/3] Move context once to instanceContext from global scope Signed-off-by: Stephen J Day --- context/context.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/context/context.go b/context/context.go index 07b844ef..23cbf5b5 100644 --- a/context/context.go +++ b/context/context.go @@ -7,10 +7,6 @@ import ( "golang.org/x/net/context" ) -var ( - once sync.Once -) - // Context is a copy of Context from the golang.org/x/net/context package. type Context interface { context.Context @@ -20,12 +16,13 @@ type Context interface { // provided as the main background context. type instanceContext struct { Context - id string // id of context, logged as "instance.id" + id string // id of context, logged as "instance.id" + once sync.Once // once protect generation of the id } func (ic *instanceContext) Value(key interface{}) interface{} { if key == "instance.id" { - once.Do(func() { + ic.once.Do(func() { // We want to lazy initialize the UUID such that we don't // call a random generator from the package initialization // code. For various reasons random could not be available From 2c9ab4f441b3e5218e10ef99923d7f86e8494f2c Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Thu, 30 Jul 2015 14:00:28 -0700 Subject: [PATCH 3/3] Only enable uuid generation warning when using registry handlers Signed-off-by: Stephen J Day --- cmd/registry/main.go | 5 +++++ context/logger.go | 7 ------- uuid/uuid.go | 3 +-- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/cmd/registry/main.go b/cmd/registry/main.go index 66ce6367..efea4d15 100644 --- a/cmd/registry/main.go +++ b/cmd/registry/main.go @@ -29,6 +29,7 @@ import ( _ "github.com/docker/distribution/registry/storage/driver/middleware/cloudfront" _ "github.com/docker/distribution/registry/storage/driver/s3" _ "github.com/docker/distribution/registry/storage/driver/swift" + "github.com/docker/distribution/uuid" "github.com/docker/distribution/version" gorhandlers "github.com/gorilla/handlers" "github.com/yvasiyarov/gorelic" @@ -62,6 +63,10 @@ func main() { fatalf("error configuring logger: %v", err) } + // inject a logger into the uuid library. warns us if there is a problem + // with uuid generation under low entropy. + uuid.Loggerf = context.GetLogger(ctx).Warnf + app := handlers.NewApp(ctx, *config) handler := configureReporting(app) handler = panicHandler(handler) diff --git a/context/logger.go b/context/logger.go index b0f0c508..78e4212a 100644 --- a/context/logger.go +++ b/context/logger.go @@ -3,8 +3,6 @@ package context import ( "fmt" - "github.com/docker/distribution/uuid" - "github.com/Sirupsen/logrus" ) @@ -101,8 +99,3 @@ func getLogrusLogger(ctx Context, keys ...interface{}) *logrus.Entry { return logger.WithFields(fields) } - -func init() { - // inject a logger into the uuid library. - uuid.Loggerf = GetLogger(Background()).Warnf -} diff --git a/uuid/uuid.go b/uuid/uuid.go index 02de33f2..d433ccaf 100644 --- a/uuid/uuid.go +++ b/uuid/uuid.go @@ -8,7 +8,6 @@ import ( "crypto/rand" "fmt" "io" - "log" "os" "syscall" "time" @@ -30,7 +29,7 @@ var ( // Loggerf can be used to override the default logging destination. Such // log messages in this library should be logged at warning or higher. - Loggerf = log.Printf + Loggerf = func(format string, args ...interface{}) {} ) // UUID represents a UUID value. UUIDs can be compared and set to other values