From a1ec8c69e02dd00487637e8d2d51481397ca1c45 Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Wed, 2 Dec 2015 15:02:13 -0800 Subject: [PATCH] unify the timestamp handler with the handler for getting other tuf metadata Signed-off-by: David Lawrence (github: endophage) --- server/handlers/default.go | 17 ++++++----------- server/server.go | 6 +----- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/server/handlers/default.go b/server/handlers/default.go index 43de258f82..471214dfcb 100644 --- a/server/handlers/default.go +++ b/server/handlers/default.go @@ -96,6 +96,10 @@ func GetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) err logger := ctxu.GetLoggerWithFields(ctx, map[string]interface{}{"gun": gun, "tufRole": tufRole}) + if data.CanonicalRole(tufRole) == data.CanonicalTimestampRole { + return getTimestamp(ctx, w, logger, store, gun) + } + out, err := store.GetCurrent(gun, tufRole) if err != nil { if _, ok := err.(*storage.ErrNotFound); ok { @@ -132,23 +136,14 @@ func DeleteHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) return nil } -// GetTimestampHandler returns a timestamp.json given a GUN -func GetTimestampHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) error { - s := ctx.Value("metaStore") - store, ok := s.(storage.MetaStore) - if !ok { - return errors.ErrNoStorage.WithDetail(nil) - } +// getTimestampHandler returns a timestamp.json given a GUN +func getTimestamp(ctx context.Context, w http.ResponseWriter, logger ctxu.Logger, store storage.MetaStore, gun string) error { cryptoServiceVal := ctx.Value("cryptoService") cryptoService, ok := cryptoServiceVal.(signed.CryptoService) if !ok { return errors.ErrNoCryptoService.WithDetail(nil) } - vars := mux.Vars(r) - gun := vars["imageName"] - logger := ctxu.GetLoggerWithField(ctx, gun, "gun") - out, err := timestamp.GetOrCreateTimestamp(gun, store, cryptoService) if err != nil { switch err.(type) { diff --git a/server/server.go b/server/server.go index ca56f359c3..de16c0392c 100644 --- a/server/server.go +++ b/server/server.go @@ -89,14 +89,10 @@ func RootHandler(ac auth.AccessController, ctx context.Context, trust signed.Cry prometheus.InstrumentHandlerWithOpts( prometheusOpts("UpdateTuf"), hand(handlers.AtomicUpdateHandler, "push", "pull"))) - r.Methods("GET").Path("/v2/{imageName:.*}/_trust/tuf/{tufRole:(root|targets|snapshot)}.json").Handler( + r.Methods("GET").Path("/v2/{imageName:.*}/_trust/tuf/{tufRole:(root|targets|snapshot|timestamp)}.json").Handler( prometheus.InstrumentHandlerWithOpts( prometheusOpts("GetRole"), hand(handlers.GetHandler, "pull"))) - r.Methods("GET").Path("/v2/{imageName:.*}/_trust/tuf/timestamp.json").Handler( - prometheus.InstrumentHandlerWithOpts( - prometheusOpts("GetTimestamp"), - hand(handlers.GetTimestampHandler, "pull"))) r.Methods("GET").Path("/v2/{imageName:.*}/_trust/tuf/timestamp.key").Handler( prometheus.InstrumentHandlerWithOpts( prometheusOpts("GetTimestampKey"),