Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grpc): allow to configure grpc msg rcv/send sizes #323

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions internal/internal.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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