Skip to content

Commit

Permalink
feat(grpc): make grpc max rcv/send message size configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Elliot Maincourt <e.maincourt@gmail.com>
  • Loading branch information
emaincourt committed Mar 24, 2022
1 parent 2977de0 commit d0acc43
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
41 changes: 27 additions & 14 deletions internal/internal.go
Expand Up @@ -7,6 +7,7 @@ package internal
import (
"context"
"fmt"
"math"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -47,6 +48,11 @@ const defaultPath = "envoy/authz/allow"
const defaultDryRun = false
const defaultEnableReflection = false

// Those are the defaults from grpc-go.
// See https://github.com/grpc/grpc-go/blob/master/server.go#L58 for more details.
const defaultGRPCServerMaxReceiveMessageSize = 1024 * 1024 * 4
const defaultGRPCServerMaxSendMessageSize = math.MaxInt32

// PluginName is the name to register with the OPA plugin manager
const PluginName = "envoy_ext_authz_grpc"

Expand All @@ -56,9 +62,11 @@ const PluginName = "envoy_ext_authz_grpc"
func Validate(m *plugins.Manager, bs []byte) (*Config, error) {

cfg := Config{
Addr: defaultAddr,
DryRun: defaultDryRun,
EnableReflection: defaultEnableReflection,
Addr: defaultAddr,
DryRun: defaultDryRun,
EnableReflection: defaultEnableReflection,
GRPCMaxRecvMsgSize: defaultGRPCServerMaxReceiveMessageSize,
GRPCMaxSendMsgSize: defaultGRPCServerMaxSendMessageSize,
}

if err := util.Unmarshal(bs, &cfg); err != nil {
Expand Down Expand Up @@ -104,9 +112,12 @@ func Validate(m *plugins.Manager, bs []byte) (*Config, error) {
func New(m *plugins.Manager, cfg *Config) plugins.Plugin {

plugin := &envoyExtAuthzGrpcServer{
manager: m,
cfg: *cfg,
server: grpc.NewServer(),
manager: m,
cfg: *cfg,
server: grpc.NewServer(
grpc.MaxRecvMsgSize(cfg.GRPCMaxRecvMsgSize),
grpc.MaxSendMsgSize(cfg.GRPCMaxSendMsgSize),
),
preparedQueryDoOnce: new(sync.Once),
interQueryBuiltinCache: iCache.NewInterQueryCache(m.InterQueryBuiltinCacheConfig()),
}
Expand All @@ -129,14 +140,16 @@ func New(m *plugins.Manager, cfg *Config) plugins.Plugin {

// Config represents the plugin configuration.
type Config struct {
Addr string `json:"addr"`
Query string `json:"query"` // Deprecated: Use Path instead
Path string `json:"path"`
DryRun bool `json:"dry-run"`
EnableReflection bool `json:"enable-reflection"`
parsedQuery ast.Body
ProtoDescriptor string `json:"proto-descriptor"`
protoSet *protoregistry.Files
Addr string `json:"addr"`
Query string `json:"query"` // Deprecated: Use Path instead
Path string `json:"path"`
DryRun bool `json:"dry-run"`
EnableReflection bool `json:"enable-reflection"`
parsedQuery ast.Body
ProtoDescriptor string `json:"proto-descriptor"`
protoSet *protoregistry.Files
GRPCMaxRecvMsgSize int `json:"grpc-max-recv-msg-size"`
GRPCMaxSendMsgSize int `json:"grpc-max-send-msg-size"`
}

type envoyExtAuthzGrpcServer struct {
Expand Down
30 changes: 30 additions & 0 deletions internal/internal_test.go
Expand Up @@ -940,6 +940,28 @@ func TestConfigValidWithPath(t *testing.T) {
}
}

func TestConfigValidWithGRPCMaxMessageSizes(t *testing.T) {

m, err := plugins.New([]byte{}, "test", inmem.New())
if err != nil {
t.Fatal(err)
}

in := `{"grpc-max-recv-msg-size": 1000, "grpc-max-send-msg-size": 1000}`
config, err := Validate(m, []byte(in))
if err != nil {
t.Fatal(err)
}

if config.GRPCMaxRecvMsgSize != 1000 {
t.Fatalf("Expected GRPC max receive message size to be 1000 but got %v", config.GRPCMaxRecvMsgSize)
}

if config.GRPCMaxSendMsgSize != 1000 {
t.Fatalf("Expected GRPC max send message size to be 1000 but got %v", config.GRPCMaxSendMsgSize)
}
}

func TestConfigValidDefault(t *testing.T) {

m, err := plugins.New([]byte{}, "test", inmem.New())
Expand Down Expand Up @@ -976,6 +998,14 @@ func TestConfigValidDefault(t *testing.T) {
if config.EnableReflection {
t.Fatal("Expected enable-reflection config to be disabled by default")
}

if config.GRPCMaxRecvMsgSize != defaultGRPCServerMaxReceiveMessageSize {
t.Fatalf("Expected GRPC max receive message size %d but got %d", defaultGRPCServerMaxReceiveMessageSize, config.GRPCMaxRecvMsgSize)
}

if config.GRPCMaxSendMsgSize != defaultGRPCServerMaxSendMessageSize {
t.Fatalf("Expected GRPC max send message size %d but got %d", defaultGRPCServerMaxSendMessageSize, config.GRPCMaxSendMsgSize)
}
}

func TestConfigInvalid(t *testing.T) {
Expand Down

0 comments on commit d0acc43

Please sign in to comment.