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
34 changes: 34 additions & 0 deletions go/gosdk/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ size_t envoy_dynamic_module_callback_http_get_response_headers_count(
bool envoy_dynamic_module_callback_http_get_response_headers(
uintptr_t filter_envoy_ptr,
uintptr_t* result_headers);

#cgo noescape envoy_dynamic_module_callback_http_filter_get_attribute_string
#cgo nocallback envoy_dynamic_module_callback_http_filter_get_attribute_string
bool envoy_dynamic_module_callback_http_filter_get_attribute_string(
uintptr_t filter_envoy_ptr,
size_t attribute_id,
uintptr_t* result, size_t* result_length);
*/
import "C"

Expand Down Expand Up @@ -375,6 +382,33 @@ type envoySlice struct {
// envoyFilter implements [EnvoyHttpFilter].
type envoyFilter struct{ raw uintptr }

// GetRequestProtocol implements [EnvoyHttpFilter].
func (e envoyFilter) GetRequestProtocol() string {
// https://github.com/envoyproxy/envoy/blob/05223ee2cd143d70b32402783c2a866a9dd18bd1/source/extensions/dynamic_modules/abi.h#L237-L372
return e.getStringAttribute(10) // request.protocol
}

// GetSourceAddress implements [EnvoyHttpFilter].
func (e envoyFilter) GetSourceAddress() string {
// https://github.com/envoyproxy/envoy/blob/05223ee2cd143d70b32402783c2a866a9dd18bd1/source/extensions/dynamic_modules/abi.h#L237-L372
return e.getStringAttribute(24) // source.address
}

func (e envoyFilter) getStringAttribute(id int) string {
var resultBufferPtr *byte
var resultBufferLengthPtr int
ret := C.envoy_dynamic_module_callback_http_filter_get_attribute_string(
C.uintptr_t(e.raw),
C.size_t(id),
(*C.uintptr_t)(unsafe.Pointer(&resultBufferPtr)),
(*C.size_t)(unsafe.Pointer(&resultBufferLengthPtr)),
)
if !ret {
return ""
}
return string(unsafe.Slice(resultBufferPtr, resultBufferLengthPtr)) // Copy the result to a Go string.
}

// GetRequestHeaders implements EnvoyHttpFilter.
func (e envoyFilter) GetRequestHeaders() map[string][]string {
count := C.envoy_dynamic_module_callback_http_get_request_headers_count(C.uintptr_t(e.raw))
Expand Down
5 changes: 5 additions & 0 deletions go/gosdk/gosdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type EnvoyHttpFilter interface {
AppendResponseBody(data []byte) bool
// SendLocalReply sends a local reply to the client. This must not be used in after returning continue from the response headers phase.
SendLocalReply(statusCode uint32, headers [][2]string, body []byte)
// GetSourceAddress gets the source address of the request in the format of "IP:PORT".
// This corresponds to `source.address` attribute https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes.
GetSourceAddress() string
// GetRequestProtocol gets the request protocol. This corresponds to `request.protocol` attribute https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes.
GetRequestProtocol() string
}

// HttpFilter is an interface that represents each Http request.
Expand Down
2 changes: 2 additions & 0 deletions go/passthrough.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (p passthroughFilter) RequestHeaders(e gosdk.EnvoyHttpFilter, endOfStream b
fmt.Printf("gosdk: RequestHeaders, header: %s: %s\n", k, v)
}
}
fmt.Printf("gosdk: RequestHeaders, source address: %s\n", e.GetSourceAddress())
fmt.Printf("gosdk: RequestHeaders, request protocol: %s\n", e.GetRequestProtocol())
return gosdk.RequestHeadersStatusContinue
}

Expand Down
6 changes: 1 addition & 5 deletions integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func TestIntegration(t *testing.T) {
require.NoError(t, cmd.Start())
t.Cleanup(func() { require.NoError(t, cmd.Process.Signal(os.Interrupt)) })

// Let's wait at least 5 seconds for Envoy to start since it might take a while
// to pull the image.
time.Sleep(5 * time.Second)

t.Run("http_access_logger", func(t *testing.T) {
t.Run("health checking", func(t *testing.T) {
require.Eventually(t, func() bool {
Expand All @@ -80,7 +76,7 @@ func TestIntegration(t *testing.T) {
}
t.Logf("response: status=%d body=%s", resp.StatusCode, string(body))
return resp.StatusCode == 200
}, 30*time.Second, 1*time.Second)
}, 120*time.Second, 1*time.Second)
})

require.Eventually(t, func() bool {
Expand Down
Loading