From 536eb689dca9e5456bece211ca9da604714865c3 Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 29 Oct 2020 04:37:46 -0700 Subject: [PATCH] Add Deprecation headers for legacy rest endpoints (#7686) * add deprecation headers for legacy rest endpoints * add deprecation headers for missing tx routes * rm handler-level deprecation headers * switch to middleware Route.Use method for setting deprecation Headers * set deprecation headers using subrouter * cleanup gofmt * goimports * Update client/rest/rest.go * update deprecation headers to be set on each module individually Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- client/docs/swagger-ui/swagger-ui-bundle.js | 2 +- client/docs/swagger_legacy.yaml | 2 +- client/rest/rest.go | 27 +++++++++++++++++++++ types/module/module.go | 3 +-- x/auth/client/rest/rest.go | 7 ++++-- x/bank/client/rest/rest.go | 4 ++- x/distribution/client/rest/rest.go | 5 +++- x/evidence/client/rest/rest.go | 5 +++- x/gov/client/rest/rest.go | 4 ++- x/mint/client/rest/rest.go | 4 ++- x/slashing/client/rest/rest.go | 5 +++- x/staking/client/rest/rest.go | 4 ++- x/upgrade/client/rest/rest.go | 4 ++- 13 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 client/rest/rest.go diff --git a/client/docs/swagger-ui/swagger-ui-bundle.js b/client/docs/swagger-ui/swagger-ui-bundle.js index 4491b4b284..07eceace93 100644 --- a/client/docs/swagger-ui/swagger-ui-bundle.js +++ b/client/docs/swagger-ui/swagger-ui-bundle.js @@ -41773,4 +41773,4 @@ }, o.resolve = i, e.exports = o, o.id = 1058 }]) }); -//# sourceMappingURL=swagger-ui-bundle.js.map \ No newline at end of file +//# sourceMappingURL=swagger-ui-bundle.js.map diff --git a/client/docs/swagger_legacy.yaml b/client/docs/swagger_legacy.yaml index 6e996d42cd..fe00efa484 100644 --- a/client/docs/swagger_legacy.yaml +++ b/client/docs/swagger_legacy.yaml @@ -2595,4 +2595,4 @@ definitions: total: type: array items: - $ref: "#/definitions/Coin" \ No newline at end of file + $ref: "#/definitions/Coin" diff --git a/client/rest/rest.go b/client/rest/rest.go new file mode 100644 index 0000000000..9a0f901840 --- /dev/null +++ b/client/rest/rest.go @@ -0,0 +1,27 @@ +package rest + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +// addHTTPDeprecationHeaders is a mux middleware function for adding HTTP +// Deprecation headers to a http handler +func addHTTPDeprecationHeaders(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Deprecation", "true") + w.Header().Set("Link", "; rel=\"deprecation\"") + w.Header().Set("Warning", "199 - \"this endpoint is deprecated and may not work as before, see deprecation link for more info\"") + h.ServeHTTP(w, r) + }) +} + +// WithHTTPDeprecationHeaders returns a new *mux.Router, identical to its input +// but with the addition of HTTP Deprecation headers. This is used to mark legacy +// amino REST endpoints as deprecated in the REST API. +func WithHTTPDeprecationHeaders(r *mux.Router) *mux.Router { + subRouter := r.NewRoute().Subrouter() + subRouter.Use(addHTTPDeprecationHeaders) + return subRouter +} diff --git a/types/module/module.go b/types/module/module.go index 97319e5d00..6b678745e2 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -31,9 +31,8 @@ package module import ( "encoding/json" - "github.com/grpc-ecosystem/grpc-gateway/runtime" - "github.com/gorilla/mux" + "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" diff --git a/x/auth/client/rest/rest.go b/x/auth/client/rest/rest.go index d3106edca2..77f8f48964 100644 --- a/x/auth/client/rest/rest.go +++ b/x/auth/client/rest/rest.go @@ -4,6 +4,7 @@ import ( "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/rest" ) // REST query and parameter values @@ -12,7 +13,8 @@ const ( ) // RegisterRoutes registers the auth module REST routes. -func RegisterRoutes(clientCtx client.Context, r *mux.Router, storeName string) { +func RegisterRoutes(clientCtx client.Context, rtr *mux.Router, storeName string) { + r := rest.WithHTTPDeprecationHeaders(rtr) r.HandleFunc( "/auth/accounts/{address}", QueryAccountRequestHandlerFn(storeName, clientCtx), ).Methods(MethodGet) @@ -24,7 +26,8 @@ func RegisterRoutes(clientCtx client.Context, r *mux.Router, storeName string) { } // RegisterTxRoutes registers all transaction routes on the provided router. -func RegisterTxRoutes(clientCtx client.Context, r *mux.Router) { +func RegisterTxRoutes(clientCtx client.Context, rtr *mux.Router) { + r := rest.WithHTTPDeprecationHeaders(rtr) r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(clientCtx)).Methods("GET") r.HandleFunc("/txs", QueryTxsRequestHandlerFn(clientCtx)).Methods("GET") r.HandleFunc("/txs", BroadcastTxRequest(clientCtx)).Methods("POST") diff --git a/x/bank/client/rest/rest.go b/x/bank/client/rest/rest.go index e70a946d26..4325386d44 100644 --- a/x/bank/client/rest/rest.go +++ b/x/bank/client/rest/rest.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/cosmos/cosmos-sdk/client/rest" "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" @@ -8,7 +9,8 @@ import ( // RegisterHandlers registers all x/bank transaction and query HTTP REST handlers // on the provided mux router. -func RegisterHandlers(clientCtx client.Context, r *mux.Router) { +func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) { + r := rest.WithHTTPDeprecationHeaders(rtr) r.HandleFunc("/bank/accounts/{address}/transfers", NewSendRequestHandlerFn(clientCtx)).Methods("POST") r.HandleFunc("/bank/balances/{address}", QueryBalancesRequestHandlerFn(clientCtx)).Methods("GET") r.HandleFunc("/bank/total", totalSupplyHandlerFn(clientCtx)).Methods("GET") diff --git a/x/distribution/client/rest/rest.go b/x/distribution/client/rest/rest.go index 0269e92611..3fdcef89e1 100644 --- a/x/distribution/client/rest/rest.go +++ b/x/distribution/client/rest/rest.go @@ -6,6 +6,7 @@ import ( "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" + clientrest "github.com/cosmos/cosmos-sdk/client/rest" "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/distribution/types" @@ -13,7 +14,9 @@ import ( govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" ) -func RegisterHandlers(clientCtx client.Context, r *mux.Router) { +func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) { + r := clientrest.WithHTTPDeprecationHeaders(rtr) + registerQueryRoutes(clientCtx, r) registerTxHandlers(clientCtx, r) } diff --git a/x/evidence/client/rest/rest.go b/x/evidence/client/rest/rest.go index 89756eb58a..f2714cc7ba 100644 --- a/x/evidence/client/rest/rest.go +++ b/x/evidence/client/rest/rest.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/rest" "github.com/gorilla/mux" ) @@ -24,7 +25,9 @@ type EvidenceRESTHandler struct { // RegisterRoutes registers all Evidence submission handlers for the evidence module's // REST service handler. -func RegisterRoutes(clientCtx client.Context, r *mux.Router, handlers []EvidenceRESTHandler) { +func RegisterRoutes(clientCtx client.Context, rtr *mux.Router, handlers []EvidenceRESTHandler) { + r := rest.WithHTTPDeprecationHeaders(rtr) + registerQueryRoutes(clientCtx, r) registerTxRoutes(clientCtx, r, handlers) } diff --git a/x/gov/client/rest/rest.go b/x/gov/client/rest/rest.go index 5136e19ea1..f11798e967 100644 --- a/x/gov/client/rest/rest.go +++ b/x/gov/client/rest/rest.go @@ -6,6 +6,7 @@ import ( "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" + clientrest "github.com/cosmos/cosmos-sdk/client/rest" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" ) @@ -28,7 +29,8 @@ type ProposalRESTHandler struct { Handler func(http.ResponseWriter, *http.Request) } -func RegisterHandlers(clientCtx client.Context, r *mux.Router, phs []ProposalRESTHandler) { +func RegisterHandlers(clientCtx client.Context, rtr *mux.Router, phs []ProposalRESTHandler) { + r := clientrest.WithHTTPDeprecationHeaders(rtr) registerQueryRoutes(clientCtx, r) registerTxHandlers(clientCtx, r, phs) } diff --git a/x/mint/client/rest/rest.go b/x/mint/client/rest/rest.go index a159739a0b..2ed28d41d7 100644 --- a/x/mint/client/rest/rest.go +++ b/x/mint/client/rest/rest.go @@ -4,9 +4,11 @@ import ( "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/rest" ) // RegisterRoutes registers minting module REST handlers on the provided router. -func RegisterRoutes(clientCtx client.Context, r *mux.Router) { +func RegisterRoutes(clientCtx client.Context, rtr *mux.Router) { + r := rest.WithHTTPDeprecationHeaders(rtr) registerQueryRoutes(clientCtx, r) } diff --git a/x/slashing/client/rest/rest.go b/x/slashing/client/rest/rest.go index cdce1a34a0..21d73a5e88 100644 --- a/x/slashing/client/rest/rest.go +++ b/x/slashing/client/rest/rest.go @@ -1,12 +1,15 @@ package rest import ( + "github.com/cosmos/cosmos-sdk/client/rest" "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" ) -func RegisterHandlers(clientCtx client.Context, r *mux.Router) { +func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) { + r := rest.WithHTTPDeprecationHeaders(rtr) + registerQueryRoutes(clientCtx, r) registerTxHandlers(clientCtx, r) } diff --git a/x/staking/client/rest/rest.go b/x/staking/client/rest/rest.go index cdce1a34a0..bb4e82917c 100644 --- a/x/staking/client/rest/rest.go +++ b/x/staking/client/rest/rest.go @@ -1,12 +1,14 @@ package rest import ( + "github.com/cosmos/cosmos-sdk/client/rest" "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" ) -func RegisterHandlers(clientCtx client.Context, r *mux.Router) { +func RegisterHandlers(clientCtx client.Context, rtr *mux.Router) { + r := rest.WithHTTPDeprecationHeaders(rtr) registerQueryRoutes(clientCtx, r) registerTxHandlers(clientCtx, r) } diff --git a/x/upgrade/client/rest/rest.go b/x/upgrade/client/rest/rest.go index cdb078e7da..83577c0cbc 100644 --- a/x/upgrade/client/rest/rest.go +++ b/x/upgrade/client/rest/rest.go @@ -1,13 +1,15 @@ package rest import ( + "github.com/cosmos/cosmos-sdk/client/rest" "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" ) // RegisterRoutes registers REST routes for the upgrade module under the path specified by routeName. -func RegisterRoutes(clientCtx client.Context, r *mux.Router) { +func RegisterRoutes(clientCtx client.Context, rtr *mux.Router) { + r := rest.WithHTTPDeprecationHeaders(rtr) registerQueryRoutes(clientCtx, r) registerTxHandlers(clientCtx, r) }