Skip to content

Commit 3d9c020

Browse files
authored
OPA filters: Reduce default buffer size for reading the requests' body (#3257)
* OPA filters: Reduce default buffer size for reading the requests' body and expose it via command line and config Signed-off-by: Magnus Jungsbluth <[email protected]>
1 parent 0e80fe7 commit 3d9c020

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

config/config.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,14 @@ type Config struct {
284284
LuaModules *listFlag `yaml:"lua-modules"`
285285
LuaSources *listFlag `yaml:"lua-sources"`
286286

287-
EnableOpenPolicyAgent bool `yaml:"enable-open-policy-agent"`
288-
OpenPolicyAgentConfigTemplate string `yaml:"open-policy-agent-config-template"`
289-
OpenPolicyAgentEnvoyMetadata string `yaml:"open-policy-agent-envoy-metadata"`
290-
OpenPolicyAgentCleanerInterval time.Duration `yaml:"open-policy-agent-cleaner-interval"`
291-
OpenPolicyAgentStartupTimeout time.Duration `yaml:"open-policy-agent-startup-timeout"`
292-
OpenPolicyAgentMaxRequestBodySize int64 `yaml:"open-policy-agent-max-request-body-size"`
293-
OpenPolicyAgentMaxMemoryBodyParsing int64 `yaml:"open-policy-agent-max-memory-body-parsing"`
287+
EnableOpenPolicyAgent bool `yaml:"enable-open-policy-agent"`
288+
OpenPolicyAgentConfigTemplate string `yaml:"open-policy-agent-config-template"`
289+
OpenPolicyAgentEnvoyMetadata string `yaml:"open-policy-agent-envoy-metadata"`
290+
OpenPolicyAgentCleanerInterval time.Duration `yaml:"open-policy-agent-cleaner-interval"`
291+
OpenPolicyAgentStartupTimeout time.Duration `yaml:"open-policy-agent-startup-timeout"`
292+
OpenPolicyAgentRequestBodyBufferSize int64 `yaml:"open-policy-agent-request-body-buffer-size"`
293+
OpenPolicyAgentMaxRequestBodySize int64 `yaml:"open-policy-agent-max-request-body-size"`
294+
OpenPolicyAgentMaxMemoryBodyParsing int64 `yaml:"open-policy-agent-max-memory-body-parsing"`
294295

295296
PassiveHealthCheck mapFlags `yaml:"passive-health-check"`
296297
}
@@ -513,6 +514,7 @@ func NewConfig() *Config {
513514
flag.DurationVar(&cfg.OpenPolicyAgentCleanerInterval, "open-policy-agent-cleaner-interval", openpolicyagent.DefaultCleanIdlePeriod, "Duration in seconds to wait before cleaning up unused opa instances")
514515
flag.DurationVar(&cfg.OpenPolicyAgentStartupTimeout, "open-policy-agent-startup-timeout", openpolicyagent.DefaultOpaStartupTimeout, "Maximum duration in seconds to wait for the open policy agent to start up")
515516
flag.Int64Var(&cfg.OpenPolicyAgentMaxRequestBodySize, "open-policy-agent-max-request-body-size", openpolicyagent.DefaultMaxRequestBodySize, "Maximum number of bytes from a http request body that are passed as input to the policy")
517+
flag.Int64Var(&cfg.OpenPolicyAgentRequestBodyBufferSize, "open-policy-agent-request-body-buffer-size", openpolicyagent.DefaultRequestBodyBufferSize, "Read buffer size for the request body")
516518
flag.Int64Var(&cfg.OpenPolicyAgentMaxMemoryBodyParsing, "open-policy-agent-max-memory-body-parsing", openpolicyagent.DefaultMaxMemoryBodyParsing, "Total number of bytes used to parse http request bodies across all requests. Once the limit is met, requests will be rejected.")
517519

518520
// TLS client certs
@@ -926,13 +928,14 @@ func (c *Config) ToOptions() skipper.Options {
926928
LuaModules: c.LuaModules.values,
927929
LuaSources: c.LuaSources.values,
928930

929-
EnableOpenPolicyAgent: c.EnableOpenPolicyAgent,
930-
OpenPolicyAgentConfigTemplate: c.OpenPolicyAgentConfigTemplate,
931-
OpenPolicyAgentEnvoyMetadata: c.OpenPolicyAgentEnvoyMetadata,
932-
OpenPolicyAgentCleanerInterval: c.OpenPolicyAgentCleanerInterval,
933-
OpenPolicyAgentStartupTimeout: c.OpenPolicyAgentStartupTimeout,
934-
OpenPolicyAgentMaxRequestBodySize: c.OpenPolicyAgentMaxRequestBodySize,
935-
OpenPolicyAgentMaxMemoryBodyParsing: c.OpenPolicyAgentMaxMemoryBodyParsing,
931+
EnableOpenPolicyAgent: c.EnableOpenPolicyAgent,
932+
OpenPolicyAgentConfigTemplate: c.OpenPolicyAgentConfigTemplate,
933+
OpenPolicyAgentEnvoyMetadata: c.OpenPolicyAgentEnvoyMetadata,
934+
OpenPolicyAgentCleanerInterval: c.OpenPolicyAgentCleanerInterval,
935+
OpenPolicyAgentStartupTimeout: c.OpenPolicyAgentStartupTimeout,
936+
OpenPolicyAgentMaxRequestBodySize: c.OpenPolicyAgentMaxRequestBodySize,
937+
OpenPolicyAgentRequestBodyBufferSize: c.OpenPolicyAgentRequestBodyBufferSize,
938+
OpenPolicyAgentMaxMemoryBodyParsing: c.OpenPolicyAgentMaxMemoryBodyParsing,
936939

937940
PassiveHealthCheck: c.PassiveHealthCheck.values,
938941
}

config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func defaultConfig(with func(*Config)) *Config {
163163
OpenPolicyAgentStartupTimeout: 30 * time.Second,
164164
OpenPolicyAgentMaxRequestBodySize: openpolicyagent.DefaultMaxRequestBodySize,
165165
OpenPolicyAgentMaxMemoryBodyParsing: openpolicyagent.DefaultMaxMemoryBodyParsing,
166+
OpenPolicyAgentRequestBodyBufferSize: openpolicyagent.DefaultRequestBodyBufferSize,
166167
}
167168
with(cfg)
168169
return cfg

filters/openpolicyagent/openpolicyagent.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"errors"
77
"fmt"
8-
"google.golang.org/protobuf/proto"
98
"io"
109
"net/http"
1110
"os"
@@ -14,6 +13,8 @@ import (
1413
"text/template"
1514
"time"
1615

16+
"google.golang.org/protobuf/proto"
17+
1718
ext_authz_v3_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
1819
"github.com/google/uuid"
1920
"github.com/open-policy-agent/opa/ast"
@@ -44,9 +45,9 @@ const (
4445
defaultShutdownGracePeriod = 30 * time.Second
4546
DefaultOpaStartupTimeout = 30 * time.Second
4647

47-
DefaultMaxRequestBodySize = 1 << 20 // 1 MB
48-
DefaultMaxMemoryBodyParsing = 100 * DefaultMaxRequestBodySize
49-
defaultBodyBufferSize = 8192 * 1024
48+
DefaultMaxRequestBodySize = 1 << 20 // 1 MB
49+
DefaultMaxMemoryBodyParsing = 100 * DefaultMaxRequestBodySize
50+
DefaultRequestBodyBufferSize = 8 * 1024 // 8 KB
5051

5152
spanNameEval = "open-policy-agent"
5253
)
@@ -129,7 +130,7 @@ func NewOpenPolicyAgentRegistry(opts ...func(*OpenPolicyAgentRegistry) error) *O
129130
lastused: make(map[*OpenPolicyAgentInstance]time.Time),
130131
quit: make(chan struct{}),
131132
maxRequestBodyBytes: DefaultMaxMemoryBodyParsing,
132-
bodyReadBufferSize: defaultBodyBufferSize,
133+
bodyReadBufferSize: DefaultRequestBodyBufferSize,
133134
}
134135

135136
for _, opt := range opts {

filters/openpolicyagent/openpolicyagent_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
pbstruct "google.golang.org/protobuf/types/known/structpb"
98
"io"
109
"net/http"
1110
"os"
1211
"strconv"
1312
"testing"
1413
"time"
1514

15+
pbstruct "google.golang.org/protobuf/types/known/structpb"
16+
1617
"github.com/open-policy-agent/opa/ast"
1718

1819
ext_authz_v3_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
@@ -250,7 +251,7 @@ func TestOpaEngineStartFailureWithTimeout(t *testing.T) {
250251
cfg, err := NewOpenPolicyAgentConfig(WithConfigTemplate(config), WithStartupTimeout(1*time.Second))
251252
assert.NoError(t, err)
252253

253-
engine, err := registry.new(inmem.New(), config, *cfg, "testfilter", "test", DefaultMaxRequestBodySize, defaultBodyBufferSize)
254+
engine, err := registry.new(inmem.New(), config, *cfg, "testfilter", "test", DefaultMaxRequestBodySize, DefaultRequestBodyBufferSize)
254255
assert.NoError(t, err)
255256

256257
ctx, cancel := context.WithTimeout(context.Background(), cfg.startupTimeout)
@@ -533,7 +534,7 @@ func TestBodyExtraction(t *testing.T) {
533534
msg: "Read body ",
534535
body: `{ "welcome": "world" }`,
535536
maxBodySize: 1024,
536-
readBodyBuffer: defaultBodyBufferSize,
537+
readBodyBuffer: DefaultRequestBodyBufferSize,
537538
bodyInPolicy: `{ "welcome": "world" }`,
538539
},
539540
{

skipper.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -933,13 +933,14 @@ type Options struct {
933933
// filters.
934934
LuaSources []string
935935

936-
EnableOpenPolicyAgent bool
937-
OpenPolicyAgentConfigTemplate string
938-
OpenPolicyAgentEnvoyMetadata string
939-
OpenPolicyAgentCleanerInterval time.Duration
940-
OpenPolicyAgentStartupTimeout time.Duration
941-
OpenPolicyAgentMaxRequestBodySize int64
942-
OpenPolicyAgentMaxMemoryBodyParsing int64
936+
EnableOpenPolicyAgent bool
937+
OpenPolicyAgentConfigTemplate string
938+
OpenPolicyAgentEnvoyMetadata string
939+
OpenPolicyAgentCleanerInterval time.Duration
940+
OpenPolicyAgentStartupTimeout time.Duration
941+
OpenPolicyAgentMaxRequestBodySize int64
942+
OpenPolicyAgentRequestBodyBufferSize int64
943+
OpenPolicyAgentMaxMemoryBodyParsing int64
943944

944945
PassiveHealthCheck map[string]string
945946
}
@@ -1877,6 +1878,7 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error {
18771878
opaRegistry = openpolicyagent.NewOpenPolicyAgentRegistry(
18781879
openpolicyagent.WithMaxRequestBodyBytes(o.OpenPolicyAgentMaxRequestBodySize),
18791880
openpolicyagent.WithMaxMemoryBodyParsing(o.OpenPolicyAgentMaxMemoryBodyParsing),
1881+
openpolicyagent.WithReadBodyBufferSize(o.OpenPolicyAgentRequestBodyBufferSize),
18801882
openpolicyagent.WithCleanInterval(o.OpenPolicyAgentCleanerInterval),
18811883
openpolicyagent.WithTracer(tracer))
18821884
defer opaRegistry.Close()

0 commit comments

Comments
 (0)