diff --git a/dashboard/app/build/handler.go b/dashboard/app/build/handler.go index 59aa7842..1771593f 100644 --- a/dashboard/app/build/handler.go +++ b/dashboard/app/build/handler.go @@ -20,6 +20,7 @@ import ( "appengine/datastore" "cache" + "key" ) const commitsPerPage = 30 @@ -789,12 +790,12 @@ func validKey(c appengine.Context, key, builder string) bool { return isMasterKey(c, key) || key == builderKey(c, builder) } -func isMasterKey(c appengine.Context, key string) bool { - return appengine.IsDevAppServer() || key == secretKey(c) +func isMasterKey(c appengine.Context, k string) bool { + return appengine.IsDevAppServer() || k == key.Secret(c) } func builderKey(c appengine.Context, builder string) string { - h := hmac.New(md5.New, []byte(secretKey(c))) + h := hmac.New(md5.New, []byte(key.Secret(c))) h.Write([]byte(builder)) return fmt.Sprintf("%x", h.Sum(nil)) } diff --git a/dashboard/app/build/init.go b/dashboard/app/build/init.go index 81fc9793..e7d63ed5 100644 --- a/dashboard/app/build/init.go +++ b/dashboard/app/build/init.go @@ -12,7 +12,9 @@ import ( "appengine" "appengine/datastore" + "cache" + "key" ) func initHandler(w http.ResponseWriter, r *http.Request) { @@ -38,7 +40,7 @@ func initHandler(w http.ResponseWriter, r *http.Request) { } // Create secret key. - secretKey(c) + key.Secret(c) fmt.Fprint(w, "OK") } diff --git a/dashboard/app/cache/cache.go b/dashboard/app/cache/cache.go index 3c6a5c63..4b57614e 100644 --- a/dashboard/app/cache/cache.go +++ b/dashboard/app/cache/cache.go @@ -15,9 +15,11 @@ import ( "appengine/memcache" ) +// TimeKey specifies the memcache entity that keeps the logical datastore time. +var TimeKey = "cachetime" + const ( nocache = "nocache" - timeKey = "cachetime" expiry = 600 // 10 minutes ) @@ -25,7 +27,7 @@ func newTime() uint64 { return uint64(time.Now().Unix()) << 32 } // Now returns the current logical datastore time to use for cache lookups. func Now(c appengine.Context) uint64 { - t, err := memcache.Increment(c, timeKey, 0, newTime()) + t, err := memcache.Increment(c, TimeKey, 0, newTime()) if err != nil { c.Errorf("cache.Now: %v", err) return 0 @@ -36,7 +38,7 @@ func Now(c appengine.Context) uint64 { // Tick sets the current logical datastore time to a never-before-used time // and returns that time. It should be called to invalidate the cache. func Tick(c appengine.Context) uint64 { - t, err := memcache.Increment(c, timeKey, 1, newTime()) + t, err := memcache.Increment(c, TimeKey, 1, newTime()) if err != nil { c.Errorf("cache.Tick: %v", err) return 0 diff --git a/dashboard/app/build/key.go b/dashboard/app/key/key.go similarity index 80% rename from dashboard/app/build/key.go rename to dashboard/app/key/key.go index 91882734..e52554f1 100644 --- a/dashboard/app/build/key.go +++ b/dashboard/app/key/key.go @@ -4,7 +4,7 @@ // +build appengine -package build +package key import ( "sync" @@ -15,18 +15,18 @@ import ( var theKey struct { sync.RWMutex - BuilderKey + builderKey } -type BuilderKey struct { +type builderKey struct { Secret string } -func (k *BuilderKey) Key(c appengine.Context) *datastore.Key { +func (k *builderKey) Key(c appengine.Context) *datastore.Key { return datastore.NewKey(c, "BuilderKey", "root", 0, nil) } -func secretKey(c appengine.Context) string { +func Secret(c appengine.Context) string { // check with rlock theKey.RLock() k := theKey.Secret @@ -43,7 +43,7 @@ func secretKey(c appengine.Context) string { } // fill - if err := datastore.Get(c, theKey.Key(c), &theKey.BuilderKey); err != nil { + if err := datastore.Get(c, theKey.Key(c), &theKey.builderKey); err != nil { if err == datastore.ErrNoSuchEntity { // If the key is not stored in datastore, write it. // This only happens at the beginning of a new deployment. @@ -54,7 +54,7 @@ func secretKey(c appengine.Context) string { panic("lost key from datastore") } theKey.Secret = "gophers rule" - datastore.Put(c, theKey.Key(c), &theKey.BuilderKey) + datastore.Put(c, theKey.Key(c), &theKey.builderKey) return theKey.Secret } panic("cannot load builder key: " + err.Error())