Merge PR #5671: CORS - Add a Flag for CORS
This commit is contained in:
parent
238928ed50
commit
e3dc0fd4a6
|
@ -68,6 +68,7 @@ const (
|
||||||
FlagKeyringBackend = "keyring-backend"
|
FlagKeyringBackend = "keyring-backend"
|
||||||
FlagPage = "page"
|
FlagPage = "page"
|
||||||
FlagLimit = "limit"
|
FlagLimit = "limit"
|
||||||
|
FlagUnsafeCORS = "unsafe-cors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LineBreak can be included in a command list to provide a blank line
|
// LineBreak can be included in a command list to provide a blank line
|
||||||
|
@ -141,6 +142,7 @@ func RegisterRestServerFlags(cmd *cobra.Command) *cobra.Command {
|
||||||
cmd.Flags().Uint(FlagMaxOpenConnections, 1000, "The number of maximum open connections")
|
cmd.Flags().Uint(FlagMaxOpenConnections, 1000, "The number of maximum open connections")
|
||||||
cmd.Flags().Uint(FlagRPCReadTimeout, 10, "The RPC read timeout (in seconds)")
|
cmd.Flags().Uint(FlagRPCReadTimeout, 10, "The RPC read timeout (in seconds)")
|
||||||
cmd.Flags().Uint(FlagRPCWriteTimeout, 10, "The RPC write timeout (in seconds)")
|
cmd.Flags().Uint(FlagRPCWriteTimeout, 10, "The RPC write timeout (in seconds)")
|
||||||
|
cmd.Flags().Bool(FlagUnsafeCORS, false, "Allows CORS requests from all domains. For development purposes only, use it at your own risk.")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/handlers"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/rakyll/statik/fs"
|
"github.com/rakyll/statik/fs"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -46,7 +47,7 @@ func NewRestServer(cdc *codec.Codec) *RestServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start starts the rest server
|
// Start starts the rest server
|
||||||
func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTimeout uint) (err error) {
|
func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTimeout uint, cors bool) (err error) {
|
||||||
server.TrapSignal(func() {
|
server.TrapSignal(func() {
|
||||||
err := rs.listener.Close()
|
err := rs.listener.Close()
|
||||||
rs.log.Error("error closing listener", "err", err)
|
rs.log.Error("error closing listener", "err", err)
|
||||||
|
@ -68,6 +69,11 @@ func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTi
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var h http.Handler = rs.Mux
|
||||||
|
if cors {
|
||||||
|
return rpcserver.StartHTTPServer(rs.listener, handlers.CORS()(h), rs.log, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
return rpcserver.StartHTTPServer(rs.listener, rs.Mux, rs.log, cfg)
|
return rpcserver.StartHTTPServer(rs.listener, rs.Mux, rs.log, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +96,7 @@ func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.C
|
||||||
viper.GetInt(flags.FlagMaxOpenConnections),
|
viper.GetInt(flags.FlagMaxOpenConnections),
|
||||||
uint(viper.GetInt(flags.FlagRPCReadTimeout)),
|
uint(viper.GetInt(flags.FlagRPCReadTimeout)),
|
||||||
uint(viper.GetInt(flags.FlagRPCWriteTimeout)),
|
uint(viper.GetInt(flags.FlagRPCWriteTimeout)),
|
||||||
|
viper.GetBool(flags.FlagUnsafeCORS),
|
||||||
)
|
)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -7,8 +7,8 @@ synopsis: "This document describes how to create a REST interface for an SDK **a
|
||||||
|
|
||||||
## Prerequisites {hide}
|
## Prerequisites {hide}
|
||||||
|
|
||||||
* [Query Lifecycle](./query-lifecycle.md) {prereq}
|
- [Query Lifecycle](./query-lifecycle.md) {prereq}
|
||||||
* [Application CLI](./cli.md) {prereq}
|
- [Application CLI](./cli.md) {prereq}
|
||||||
|
|
||||||
## Application REST Interface
|
## Application REST Interface
|
||||||
|
|
||||||
|
@ -30,11 +30,11 @@ Note that if `trust-node` is set to `false`, the REST server will verify the que
|
||||||
|
|
||||||
A REST Server is used to receive and route HTTP Requests, obtain the results from the application, and return a response to the user. The REST Server defined by the SDK `rest` package contains the following:
|
A REST Server is used to receive and route HTTP Requests, obtain the results from the application, and return a response to the user. The REST Server defined by the SDK `rest` package contains the following:
|
||||||
|
|
||||||
* **Router:** A router for HTTP requests. A new router can be instantiated for an application and used to match routes based on path, request method, headers, etc. The SDK uses the [Gorilla Mux Router](https://github.com/gorilla/mux).
|
- **Router:** A router for HTTP requests. A new router can be instantiated for an application and used to match routes based on path, request method, headers, etc. The SDK uses the [Gorilla Mux Router](https://github.com/gorilla/mux).
|
||||||
* **CLIContext:** A [`CLIContext`](./query-lifecycle.md#clicontext) created for a user interaction.
|
- **CLIContext:** A [`CLIContext`](./query-lifecycle.md#clicontext) created for a user interaction.
|
||||||
* **Keybase:** A [Keybase](../basics/accounts.md#keybase) is a key manager.
|
- **Keybase:** A [Keybase](../basics/accounts.md#keybase) is a key manager.
|
||||||
* **Logger:** A logger from Tendermint `Log`, a log package structured around key-value pairs that allows logging level to be set differently for different keys. The logger takes `Debug()`, `Info()`, and `Error()`s.
|
- **Logger:** A logger from Tendermint `Log`, a log package structured around key-value pairs that allows logging level to be set differently for different keys. The logger takes `Debug()`, `Info()`, and `Error()`s.
|
||||||
* **Listener:** A [listener](https://golang.org/pkg/net/#Listener) from the net package.
|
- **Listener:** A [listener](https://golang.org/pkg/net/#Listener) from the net package.
|
||||||
|
|
||||||
Of the five, the only attribute that application developers need interact with is the `router`: they need to add routes to it so that the REST server can properly handle queries. See the next section for more information on registering routes.
|
Of the five, the only attribute that application developers need interact with is the `router`: they need to add routes to it so that the REST server can properly handle queries. See the next section for more information on registering routes.
|
||||||
|
|
||||||
|
@ -58,3 +58,14 @@ This function is specific to the application and passed in to the `ServeCommand`
|
||||||
```go
|
```go
|
||||||
rootCmd.AddCommand(rest.ServeCommand(cdc, registerRoutes))
|
rootCmd.AddCommand(rest.ServeCommand(cdc, registerRoutes))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Cross-Origin Resource Sharing (CORS)
|
||||||
|
|
||||||
|
[CORS policies](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) are not enabled by default to help with security. If you would like to use the rest-server in a public environment we recommend you provide a reverse proxy, this can be done with [nginx](https://www.nginx.com/). For testing and development purposes there is an `unsafe_cors` flag that can be passed to the cmd to enable accepting cors from everyone.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
gaiacli rest-server --chain-id=test \
|
||||||
|
--laddr=tcp://localhost:1317 \
|
||||||
|
--node tcp://localhost:26657 \
|
||||||
|
--trust-node=true --unsafe-cors
|
||||||
|
```
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -10,6 +10,7 @@ require (
|
||||||
github.com/gogo/protobuf v1.3.1
|
github.com/gogo/protobuf v1.3.1
|
||||||
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129
|
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129
|
||||||
github.com/golang/protobuf v1.3.3
|
github.com/golang/protobuf v1.3.3
|
||||||
|
github.com/gorilla/handlers v1.4.2
|
||||||
github.com/gorilla/mux v1.7.4
|
github.com/gorilla/mux v1.7.4
|
||||||
github.com/hashicorp/golang-lru v0.5.4
|
github.com/hashicorp/golang-lru v0.5.4
|
||||||
github.com/mattn/go-isatty v0.0.12
|
github.com/mattn/go-isatty v0.0.12
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -105,6 +105,8 @@ github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||||
|
github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
|
||||||
|
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
|
||||||
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
|
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
|
||||||
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
|
|
Loading…
Reference in New Issue