Skip to content
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,22 @@ The release has `proxyfile-linux-amd64`, `proxyfile-linux-arm64`, `proxyfile-dar
`proxyfile-darwin-arm64`, with `checksums.txt` and `proxyfile.schema.json` beside them.

```sh
curl -fsSLO https://github.com/kitecorp/proxyfile/releases/download/v2.2.0/proxyfile-linux-amd64
curl -fsSLO https://github.com/kitecorp/proxyfile/releases/download/v2.2.0/checksums.txt
curl -fsSLO https://github.com/kitecorp/proxyfile/releases/download/v2.4.0/proxyfile-linux-amd64
curl -fsSLO https://github.com/kitecorp/proxyfile/releases/download/v2.4.0/checksums.txt
shasum -a 256 -c --ignore-missing checksums.txt
install -m 0755 proxyfile-linux-amd64 /usr/local/bin/proxyfile
```

In a Dockerfile, copy the program out of the image. The image holds `/proxyfile` and nothing else.

```dockerfile
COPY --from=ghcr.io/kitecorp/proxyfile:2.2.0 /proxyfile /usr/local/bin/proxyfile
COPY --from=ghcr.io/kitecorp/proxyfile:2.4.0 /proxyfile /usr/local/bin/proxyfile
```

With Go installed (the Go version `go.mod` names or newer):

```sh
go install github.com/kitecorp/proxyfile/v2/cmd/proxyfile@v2.2.0
go install github.com/kitecorp/proxyfile/v2/cmd/proxyfile@v2.4.0
```

## Commands
Expand Down
17 changes: 13 additions & 4 deletions conformance/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,15 @@ func (w *world) offering(offer tlsOffer) *http.Client {
}

// readEcho returns what the stub that wrote the answer received, or nil when no stub wrote it: a stub's echo is a
// JSON object naming its service, sent as field 1 of the message when the answer is gRPC.
// JSON object naming its service, sent as field 1 of the message when the answer is gRPC, and so as field 1 of the
// whole body when the answer is a Connect call's in protobuf, which the Connect bridge makes of the stub's gRPC answer
// by taking the message out of its frame.
func readEcho(contentType string, body []byte) *echo {
if strings.HasPrefix(contentType, "application/grpc") {
switch {
case strings.HasPrefix(contentType, "application/grpc"):
body = field1(body)
case contentType == "application/proto":
body = messageField1(body)
}
var e echo
if json.Unmarshal(body, &e) != nil || e.Service == "" {
Expand All @@ -269,7 +274,7 @@ func readEcho(contentType string, body []byte) *echo {
}

// field1 returns the bytes of field 1 of the one message in a gRPC body, or nil. The body is a flag byte, the
// message's length as a big-endian uint32 and the message; field 1 is the tag 0a, a varint length and the bytes.
// message's length as a big-endian uint32 and the message.
func field1(body []byte) []byte {
if len(body) < 5 {
return nil
Expand All @@ -279,7 +284,11 @@ func field1(body []byte) []byte {
if uint64(len(message)) < uint64(length) {
return nil
}
message = message[:length]
return messageField1(message[:length])
}

// messageField1 returns the bytes of field 1 of message, or nil: the tag 0a, a varint length and the bytes.
func messageField1(message []byte) []byte {
if len(message) < 2 || message[0] != 0x0a {
return nil
}
Expand Down
156 changes: 155 additions & 1 deletion conformance/scenarios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conformance

import (
"crypto/tls"
"encoding/base64"
"fmt"
"maps"
"math/rand/v2"
Expand All @@ -16,7 +17,7 @@ import (
// wait beside the rest of their own scenario alone: a later scenario's quiet and burst count on nothing else reaching
// the proxy meanwhile. The rate limit runs last, so the requests the scenarios before it send to its route come first,
// and its quiet lets that route's bucket fill again before its burst.
var scenarios = []scenario{routing, mutualTLS, cors, unlisted, auth, toldScheme, timeout, retry, gRPC, paths, asWritten, offRoute, clientKeys, hash, tracing, tracingRetried, rateLimit, cost, backendLimits, traffic}
var scenarios = []scenario{routing, mutualTLS, cors, unlisted, auth, toldScheme, timeout, retry, gRPC, requestLimit, paths, asWritten, offRoute, clientKeys, hash, tracing, tracingRetried, rateLimit, cost, backendLimits, traffic}

// get is a GET over HTTP/1.1 to localhost, a host of the example's local environment.
func get(path string) request {
Expand Down Expand Up @@ -2427,6 +2428,159 @@ var gRPC = scenario{name: "gRPC",
"failure_mode_allowed": logText("true"), "route": logText("shop"), "status": logNumber(200)}}}},
}}

// requestLimit: a method's request limit, rpc.methods[].max_request_bytes, on conformance/testdata/request-limit.yaml
// and on examples/full, each of which limits example.shop.v1.CartService/AddItem to 2,048 bytes. Every value as
// measured on Envoy 1.40.2 with the buffer filter and the routes the Envoy output writes for a limit, at a limit of
// 2,048 bytes. A Connect call's body counts with the 5 bytes of the gRPC prefix the bridge writes before it, as a gRPC
// call's frame does, so a body or a message of exactly the limit is served and one byte more is refused: a Connect call
// with 413 in plain text, and a gRPC call over HTTP/1.1, the form a proxy in front speaking HTTP/1.1 sends, with HTTP
// 200 and grpc-status 2. A compressed request is refused whatever its size, before any filter counts its bytes: a
// Connect call with 415, and a gRPC call with grpc-status 2. A limited method takes its message in a request body only:
// a Connect GET, whose message is in the query, where the buffer filter counts nothing, is answered 405 with allow:
// POST, a POST or a DELETE with no body and its message in the query is answered 400, and a call in the Connect
// streaming form, which names its compression in connect-content-encoding, is refused as the other compressed calls
// are, with 415. Those rows were written before the routes that answer them: through the routes written before them,
// Envoy 1.40.2 answered the GET with the stub's 200 and the streaming call with a 200 no stub wrote, and with the GET's
// route in place it answered the POST and the DELETE with the stub's 200. A compression header written on two lines,
// identity on one and gzip on the other, is refused in either order as the one line gzip is: Envoy 1.40.2 matches each
// value of such a header on its own, and a service may read either line. A call that names identity alone is served.
// A Connect unary body the bridge does not frame, one sent without connect-protocol-version, is counted without the
// 5-byte prefix, so up to the limit and 5 bytes of it reach the service and one byte more is refused. Each refused row
// wants an answer no stub wrote; the battery reads no list of the calls a stub received, so no row shows that the stub
// never received one. Only Envoy's entry runs the scenario, as the nginx and HAProxy generators refuse the key.
var requestLimit = scenario{name: "a method's request limit",
exchanges: []exchange{
{name: "a Connect call whose body is the limit is served", keys: requestLimitKeys, send: connectCall(2048, nil),
want: answer{status: 200, service: "shop", port: "8081", proto: "HTTP/2.0", path: limitedPath}},
{name: "a Connect call whose body is one byte over the limit is answered 413 in plain text", keys: requestLimitKeys,
send: connectCall(2049, nil),
want: answer{status: 413, headers: map[string]string{"content-type": "text/plain"}, ownBody: "Payload Too Large"}},
{name: "a gRPC call over HTTP/1.1 whose message is one byte over the limit is answered grpc-status 2", keys: requestLimitKeys,
send: grpcOverHTTP1(2049),
want: answer{status: 200, grpcStatus: "2", headers: map[string]string{"grpc-message": "Payload Too Large"}}},
{name: "a Connect call with content-encoding gzip is answered 415", keys: requestLimitKeys,
send: connectCall(16, map[string]string{"content-encoding": "gzip"}), want: answer{status: 415}},
{name: "a gRPC call with grpc-encoding gzip is answered grpc-status 2", keys: requestLimitKeys,
send: request{proto: "grpc", method: "POST", host: "localhost", path: limitedPath, headers: map[string]string{"grpc-encoding": "gzip"}},
want: answer{grpcStatus: "2"}},
{name: "a Connect GET whose message is under the limit is answered 405 with allow POST", keys: requestLimitKeys,
send: connectQuery("GET", 16), want: answer{status: 405, headers: map[string]string{"allow": "POST"}}},
// The bridge reads a message from the query of any request that ends with its headers, whatever its method, so a
// query on the limited path is refused before the bridge for every method but GET, which the 405 takes first.
{name: "a POST with no body and its message in the Connect query form is answered 400", keys: requestLimitKeys,
send: connectQuery("POST", 16), want: answer{status: 400}},
{name: "a DELETE with no body and its message in the Connect query form is answered 400", keys: requestLimitKeys,
send: connectQuery("DELETE", 16), want: answer{status: 400}},
// The envelope's flag says its message is compressed; its 16 bytes are not gzip, as the 415 is decided by the
// header alone, before any filter reads the body.
{name: "a call in the Connect streaming form with connect-content-encoding gzip is answered 415", keys: requestLimitKeys,
send: request{proto: "http/1.1", method: "POST", host: "localhost", path: limitedPath,
headers: map[string]string{"content-type": "application/connect+proto", "connect-content-encoding": "gzip"},
body: framed(1, strings.Repeat("x", 16))},
want: answer{status: 415}},
{name: "a gRPC call with grpc-encoding identity then gzip on two lines is answered grpc-status 2", keys: requestLimitKeys,
send: grpcRaw(twice("grpc-encoding", "identity", "gzip")), want: answer{grpcStatus: "2"}},
{name: "a gRPC call with grpc-encoding gzip then identity on two lines is answered grpc-status 2", keys: requestLimitKeys,
send: grpcRaw(twice("grpc-encoding", "gzip", "identity")), want: answer{grpcStatus: "2"}},
{name: "a call in the Connect streaming form with connect-content-encoding identity then gzip on two lines is answered 415",
keys: requestLimitKeys, send: streamingRaw(twice("connect-content-encoding", "identity", "gzip")), want: answer{status: 415}},
{name: "a call in the Connect streaming form with connect-content-encoding gzip then identity on two lines is answered 415",
keys: requestLimitKeys, send: streamingRaw(twice("connect-content-encoding", "gzip", "identity")), want: answer{status: 415}},
{name: "a Connect call with content-encoding identity then gzip on two lines is answered 415", keys: requestLimitKeys,
send: connectRaw(twice("content-encoding", "identity", "gzip")), want: answer{status: 415}},
{name: "a Connect call with content-encoding gzip then identity on two lines is answered 415", keys: requestLimitKeys,
send: connectRaw(twice("content-encoding", "gzip", "identity")), want: answer{status: 415}},
{name: "a gRPC call with grpc-encoding identity is served", keys: requestLimitKeys,
send: request{proto: "grpc", method: "POST", host: "localhost", path: limitedPath, headers: map[string]string{"grpc-encoding": "identity"}},
want: answer{status: 200, grpcStatus: "0", service: "shop", path: limitedPath}},
{name: "a Connect body without connect-protocol-version of the limit and 5 bytes is served whole", keys: requestLimitKeys,
send: unframedCall(2053), want: answer{status: 200, service: "shop", path: limitedPath, body: strings.Repeat("x", 2053)}},
{name: "a Connect body without connect-protocol-version of the limit and 6 bytes is answered 413 in plain text", keys: requestLimitKeys,
send: unframedCall(2054),
want: answer{status: 413, headers: map[string]string{"content-type": "text/plain"}, ownBody: "Payload Too Large"}},
}}

// twice is a header written on two lines of a raw HTTP/1.1 request, first then second: the value of the first line,
// then the second line whole, as sendRaw writes a value after its name.
func twice(name, first, second string) map[string]string {
return map[string]string{name: first + "\r\n" + name + ": " + second}
}

// grpcRaw is a unary gRPC call to limitedPath over HTTP/1.1 sent by hand, so a header may be written on two lines: one
// message of 16 bytes behind the prefix, its flag saying it is compressed, chunked, with the headers in more.
func grpcRaw(more map[string]string) request {
headers := map[string]string{"content-type": "application/grpc", "te": "trailers"}
maps.Copy(headers, more)
return request{proto: "http/1.1", method: "POST", host: "localhost", path: limitedPath, headers: headers, raw: true,
body: framed(1, strings.Repeat("x", 16))}
}

// streamingRaw is a call to limitedPath in the Connect streaming form over HTTP/1.1 sent by hand: one envelope of 16
// bytes, its flag saying the message is compressed, chunked, with the headers in more.
func streamingRaw(more map[string]string) request {
headers := map[string]string{"content-type": "application/connect+proto"}
maps.Copy(headers, more)
return request{proto: "http/1.1", method: "POST", host: "localhost", path: limitedPath, headers: headers, raw: true,
body: framed(1, strings.Repeat("x", 16))}
}

// connectRaw is a Connect unary call to limitedPath over HTTP/1.1 sent by hand: a body of 16 bytes, chunked, with
// protobuf's content type, Connect-Protocol-Version and the headers in more.
func connectRaw(more map[string]string) request {
headers := map[string]string{"content-type": "application/proto", "connect-protocol-version": "1"}
maps.Copy(headers, more)
return request{proto: "http/1.1", method: "POST", host: "localhost", path: limitedPath, headers: headers, raw: true,
body: strings.Repeat("x", 16)}
}

// unframedCall is a Connect unary call to limitedPath over HTTP/1.1 without Connect-Protocol-Version: the bridge then
// leaves it as it is, so its body of size bytes, framed by its length, goes to the service with no gRPC prefix.
func unframedCall(size int) request {
return request{proto: "http/1.1", method: "POST", host: "localhost", path: limitedPath,
headers: map[string]string{"content-type": "application/proto"}, body: strings.Repeat("x", size)}
}

// requestLimitKeys are what the rows of a method's request limit depend on: the limit the method writes.
var requestLimitKeys = []string{"rpc.methods[].max_request_bytes"}

// limitedPath is the path of the method request-limit.yaml and examples/full limit to 2,048 bytes.
const limitedPath = "/example.shop.v1.CartService/AddItem"

// connectCall is a Connect unary call to limitedPath over HTTP/1.1, as a browser sends one: a body of size bytes framed
// by its length, protobuf's content type, Connect-Protocol-Version and the headers in more.
func connectCall(size int, more map[string]string) request {
headers := map[string]string{"content-type": "application/proto", "connect-protocol-version": "1"}
for name, value := range more {
headers[name] = value
}
return request{proto: "http/1.1", method: "POST", host: "localhost", path: limitedPath, headers: headers,
body: strings.Repeat("x", size)}
}

// grpcOverHTTP1 is a unary gRPC call to limitedPath over HTTP/1.1: one uncompressed message of size bytes behind the
// 5-byte prefix, framed by its length.
func grpcOverHTTP1(size int) request {
return request{proto: "http/1.1", method: "POST", host: "localhost", path: limitedPath,
headers: map[string]string{"content-type": "application/grpc", "te": "trailers"}, body: framed(0, strings.Repeat("x", size))}
}

// framed is one message behind the 5-byte prefix a gRPC frame and an envelope of the Connect streaming form share: a
// flag byte, 1 when the message is compressed, and the message's length as a big-endian uint32.
func framed(flag byte, message string) string {
n := len(message)
return string([]byte{flag, byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n)}) + message
}

// connectQuery is a request to limitedPath over HTTP/1.1 with method and no body, its message in the query as a Connect
// GET carries it, the form a Connect client may send for a method without side effects: protobuf encoded (field 1
// holding size bytes, under 128 so its length is one byte) and then base64url encoded without padding. net/http sends
// Content-Length: 0 with a POST or a DELETE and none with a GET (transfer.go, shouldSendContentLength).
func connectQuery(method string, size int) request {
message := append([]byte{0x0a, byte(size)}, strings.Repeat("x", size)...)
return request{proto: "http/1.1", method: method, host: "localhost",
path: limitedPath + "?connect=v1&encoding=proto&base64=1&message=" + base64.RawURLEncoding.EncodeToString(message)}
}

// paths: every value measured on Envoy 1.40.2; no key, because path normalisation is written for every file
// (docs/portability.md). A segment that is . or .. followed by ;, or by ; escaped once in either case of hex, is
// refused wherever the proxy's normalisation leaves it; ; escaped twice, ; on any other segment and anything in the
Expand Down
4 changes: 4 additions & 0 deletions conformance/targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ var (
// own, which only Envoy's entry runs, as the nginx and HAProxy generators refuse the key; it runs the rows of that
// scenario alone.
unlistedFile = file{label: "unlisted-origins", proxyfile: "testdata/unlisted-origins.yaml", scenarios: []string{unlisted.name}}
// requestLimitFile is the battery's file with a method's request limit, which only Envoy's entry runs, as the nginx
// and HAProxy generators refuse the key; it runs the rows of that scenario alone.
requestLimitFile = file{label: "request-limit", proxyfile: "testdata/request-limit.yaml", scenarios: []string{requestLimit.name}}
)

// haproxyLimitsFile is the battery's file for HAProxy's connection limit, which Envoy's entry and each HAProxy entry
Expand Down Expand Up @@ -115,6 +118,7 @@ func targets(t testing.TB) []target {
limitsFile,
connectionFile,
unlistedFile,
requestLimitFile,
},
image: image,
// Envoy writes its log lines to stdout every 10 s by default; every 100 ms, a row that reads them (answer.logged)
Expand Down
20 changes: 20 additions & 0 deletions conformance/testdata/request-limit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# yaml-language-server: $schema=../../schema/proxyfile.schema.json
# The conformance battery's file for a method's request limit (rpc.methods[].max_request_bytes): shop's gRPC port at
# its two addresses in the world, the package example.shop.v1 routed to it, and AddItem limited to 2,048 bytes, as
# examples/full limits it, with the Connect bridge on. It runs the scenario of a method's request limit alone, and only
# Envoy's entry runs it: the nginx and HAProxy generators refuse the key. It writes no tls, so the battery's clients
# reach it in plain HTTP.
listen: 10000
admin: 9901
services:
shop:
grpc: 8081
addresses: ["10.0.1.11", "10.0.1.12"]
origins:
local: {hosts: [127.0.0.1, localhost]}
routes:
- rpc: {to: {"example.shop.*": shop}}
rpc:
methods:
- {service: example.shop.v1.CartService, method: AddItem, max_request_bytes: 2048}
envoy: {connect_bridge: true}
Loading