allow Ping testing grpc only if explicitly enabled

This commit is contained in:
Larry Ruane 2021-01-14 15:34:34 -07:00 committed by Larry Ruane
parent 86a76a96cb
commit ad739ce055
5 changed files with 19 additions and 7 deletions

View File

@ -55,6 +55,7 @@ var rootCmd = &cobra.Command{
GenCertVeryInsecure: viper.GetBool("gen-cert-very-insecure"),
DataDir: viper.GetString("data-dir"),
Redownload: viper.GetBool("redownload"),
PingEnable: viper.GetBool("ping-very-insecure"),
Darkside: viper.GetBool("darkside-very-insecure"),
DarksideTimeout: viper.GetUint64("darkside-timeout"),
}
@ -248,7 +249,7 @@ func startServer(opts *common.Options) error {
// Compact transaction service initialization
{
service, err := frontend.NewLwdStreamer(cache, chainName)
service, err := frontend.NewLwdStreamer(cache, chainName, opts.PingEnable)
if err != nil {
common.Log.WithFields(logrus.Fields{
"error": err,
@ -325,6 +326,7 @@ func init() {
rootCmd.Flags().Bool("gen-cert-very-insecure", false, "run with self-signed TLS certificate, only for debugging, DO NOT use in production")
rootCmd.Flags().Bool("redownload", false, "re-fetch all blocks from zcashd; reinitialize local cache files")
rootCmd.Flags().String("data-dir", "/var/lib/lightwalletd", "data directory (such as db)")
rootCmd.Flags().Bool("ping-very-insecure", false, "allow Ping GRPC for testing")
rootCmd.Flags().Bool("darkside-very-insecure", false, "run with GRPC-controllable mock zcashd for integration testing (shuts down after 30 minutes)")
rootCmd.Flags().Int("darkside-timeout", 30, "override 30 minute default darkside timeout")
@ -356,6 +358,8 @@ func init() {
viper.SetDefault("redownload", false)
viper.BindPFlag("data-dir", rootCmd.Flags().Lookup("data-dir"))
viper.SetDefault("data-dir", "/var/lib/lightwalletd")
viper.BindPFlag("ping-very-insecure", rootCmd.Flags().Lookup("ping-very-insecure"))
viper.SetDefault("ping-very-insecure", false)
viper.BindPFlag("darkside-very-insecure", rootCmd.Flags().Lookup("darkside-very-insecure"))
viper.SetDefault("darkside-very-insecure", false)
viper.BindPFlag("darkside-timeout", rootCmd.Flags().Lookup("darkside-timeout"))

View File

@ -43,6 +43,7 @@ type Options struct {
GenCertVeryInsecure bool `json:"gen_cert_very_insecure,omitempty"`
Redownload bool `json:"redownload"`
DataDir string `json:"data_dir"`
PingEnable bool `json:"ping_enable"`
Darkside bool `json:"darkside"`
DarksideTimeout uint64 `json:"darkside_timeout"`
}

View File

@ -37,7 +37,7 @@ const (
func testsetup() (walletrpc.CompactTxStreamerServer, *common.BlockCache) {
os.RemoveAll(unitTestPath)
cache := common.NewBlockCache(unitTestPath, unitTestChain, 380640, true)
lwd, err := NewLwdStreamer(cache, "main")
lwd, err := NewLwdStreamer(cache, "main", false /* enablePing */)
if err != nil {
os.Stderr.WriteString(fmt.Sprint("NewLwdStreamer failed:", err))
os.Exit(1)

View File

@ -24,14 +24,15 @@ import (
)
type lwdStreamer struct {
cache *common.BlockCache
chainName string
cache *common.BlockCache
chainName string
pingEnable bool
walletrpc.UnimplementedCompactTxStreamerServer
}
// NewLwdStreamer constructs a gRPC context.
func NewLwdStreamer(cache *common.BlockCache, chainName string) (walletrpc.CompactTxStreamerServer, error) {
return &lwdStreamer{cache: cache, chainName: chainName}, nil
func NewLwdStreamer(cache *common.BlockCache, chainName string, enablePing bool) (walletrpc.CompactTxStreamerServer, error) {
return &lwdStreamer{cache: cache, chainName: chainName, pingEnable: enablePing}, nil
}
// DarksideStreamer holds the gRPC state for darksidewalletd.
@ -575,6 +576,12 @@ func (s *lwdStreamer) GetAddressUtxosStream(arg *walletrpc.GetAddressUtxosArg, r
var concurrent int64
func (s *lwdStreamer) Ping(ctx context.Context, in *walletrpc.Duration) (*walletrpc.PingResponse, error) {
// This gRPC allows the client to create an arbitrary number of
// concurrent threads, which could run the server out of resources,
// so only allow if explicitly enabled.
if !s.pingEnable {
return nil, errors.New("Ping not enabled, start lightwalletd with --ping-very-insecure")
}
var response walletrpc.PingResponse
response.Entry = atomic.AddInt64(&concurrent, 1)
time.Sleep(time.Duration(in.IntervalUs) * time.Microsecond)

View File

@ -172,6 +172,6 @@ service CompactTxStreamer {
// Return information about this lightwalletd instance and the blockchain
rpc GetLightdInfo(Empty) returns (LightdInfo) {}
// Testing-only
// Testing-only, requires lightwalletd --ping-very-insecure (do not enable in production)
rpc Ping(Duration) returns (PingResponse) {}
}