diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5834b2e..14946ec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,4 +31,4 @@ jobs: uses: docker/build-push-action@v4 with: push: true - tags: release.daocloud.io/skoala/envoy-extproc-payloadlimit-demo-go:${{ github.sha }} \ No newline at end of file + tags: release.daocloud.io/skoala/envoy-extproc-payloadlimit-demo-go:${{ github.sha }} diff --git a/body-rewrite.go b/body-rewrite.go new file mode 100644 index 0000000..62bf428 --- /dev/null +++ b/body-rewrite.go @@ -0,0 +1,79 @@ +package main + +import ( + ep "github.com/wrossmorrow/envoy-extproc-sdk-go" + "log" +) + +type bodyRewriteRequestProcessor struct { + opts *ep.ProcessingOptions + bodyRewrite string +} + +func (s *bodyRewriteRequestProcessor) GetName() string { + return "body-rewrite" +} + +func (s *bodyRewriteRequestProcessor) GetOptions() *ep.ProcessingOptions { + return s.opts +} + +func (s *bodyRewriteRequestProcessor) ProcessRequestHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error { + return ctx.ContinueRequest() +} + +func (s *bodyRewriteRequestProcessor) ProcessRequestBody(ctx *ep.RequestContext, body []byte) error { + ctx.ReplaceBodyChunk([]byte(s.bodyRewrite)) + return ctx.ContinueRequest() +} + +func (s *bodyRewriteRequestProcessor) ProcessRequestTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error { + return ctx.ContinueRequest() +} + +func (s *bodyRewriteRequestProcessor) ProcessResponseHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error { + return ctx.ContinueRequest() +} + +func (s *bodyRewriteRequestProcessor) ProcessResponseBody(ctx *ep.RequestContext, body []byte) error { + return ctx.ContinueRequest() +} + +func (s *bodyRewriteRequestProcessor) ProcessResponseTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error { + return ctx.ContinueRequest() +} + +const kbodyRewrite = "body-rewrite" + +func (s *bodyRewriteRequestProcessor) Init(opts *ep.ProcessingOptions, nonFlagArgs []string) error { + s.opts = opts + s.bodyRewrite = "this is a replacement body." + + var ( + i int + bodyRewrite string + ) + + nArgs := len(nonFlagArgs) + for ; i < nArgs-1; i++ { + if nonFlagArgs[i] == kbodyRewrite { + break + } + } + + if i == nArgs { + log.Printf("the argument: 'body-rewrite' is missing, use the default.\n") + return nil + } + + bodyRewrite = nonFlagArgs[i+1] + + if len(bodyRewrite) > 0 { + s.bodyRewrite = bodyRewrite + log.Printf("the body rewrite is: %s\n", s.bodyRewrite) + } + + return nil +} + +func (s *bodyRewriteRequestProcessor) Finish() {} diff --git a/go.mod b/go.mod index ee93628..0a7bea6 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/projectsesame/envoy-extproc-payloadlimit-demo-go +module github.com/projectsesame/envoy-extproc-bodyRewrite-demo-go go 1.21 diff --git a/main.go b/main.go index a16a3f8..48d37d7 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ type processor interface { } var processors = map[string]processor{ - "payload-limit": &payloadLimitRequestProcessor{}, + "body-rewrite": &bodyRewriteRequestProcessor{}, } func parseArgs(args []string) (port *int, opts *ep.ProcessingOptions, nonFlagArgs []string) { diff --git a/payload-limit.go b/payload-limit.go deleted file mode 100644 index 29e36c3..0000000 --- a/payload-limit.go +++ /dev/null @@ -1,102 +0,0 @@ -package main - -import ( - "log" - "strconv" - - typev3 "github.com/envoyproxy/go-control-plane/envoy/type/v3" - ep "github.com/wrossmorrow/envoy-extproc-sdk-go" -) - -type payloadLimitRequestProcessor struct { - opts *ep.ProcessingOptions - payloadLimit int64 -} - -func (s *payloadLimitRequestProcessor) GetName() string { - return "payload-limit" -} - -func (s *payloadLimitRequestProcessor) GetOptions() *ep.ProcessingOptions { - return s.opts -} - -const kContentLen = "content-length" - -func (s *payloadLimitRequestProcessor) ProcessRequestHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error { - cancel := func(code int32) error { - return ctx.CancelRequest(code, map[string]ep.HeaderValue{}, typev3.StatusCode_name[code]) - } - raw, ok := headers.RawHeaders[kContentLen] - if !ok { - return cancel(413) - } - - size, _ := strconv.ParseInt(string(raw), 10, 64) - if size > s.payloadLimit { - log.Printf("the body size: %d exceeded the maximum size: %d\n", size, s.payloadLimit) - return cancel(413) - } - - return ctx.ContinueRequest() -} - -func (s *payloadLimitRequestProcessor) ProcessRequestBody(ctx *ep.RequestContext, body []byte) error { - return ctx.ContinueRequest() -} - -func (s *payloadLimitRequestProcessor) ProcessRequestTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error { - return ctx.ContinueRequest() -} - -func (s *payloadLimitRequestProcessor) ProcessResponseHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error { - return ctx.ContinueRequest() -} - -func (s *payloadLimitRequestProcessor) ProcessResponseBody(ctx *ep.RequestContext, body []byte) error { - return ctx.ContinueRequest() -} - -func (s *payloadLimitRequestProcessor) ProcessResponseTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error { - return ctx.ContinueRequest() -} - -const kPayloadLimit = "payload-limit" - -func (s *payloadLimitRequestProcessor) Init(opts *ep.ProcessingOptions, nonFlagArgs []string) error { - s.opts = opts - s.payloadLimit = 16 - - var ( - i int - err error - payloadLimit int64 - ) - - nArgs := len(nonFlagArgs) - for ; i < nArgs-1; i++ { - if nonFlagArgs[i] == kPayloadLimit { - break - } - } - - if i == nArgs { - log.Printf("the argument: 'payload-limit' is missing, use the default.\n") - return nil - } - - payloadLimit, err = strconv.ParseInt(nonFlagArgs[i+1], 10, 64) - if err != nil { - log.Printf("parse the value for parameter: 'payload-limit' is failed: %v,use the default.\n", err.Error()) - return nil - } - - if payloadLimit > 0 { - s.payloadLimit = payloadLimit - log.Printf("the payload limit is: %d.\n", s.payloadLimit) - } - - return nil -} - -func (s *payloadLimitRequestProcessor) Finish() {}