Skip to content

Commit f38ae46

Browse files
authored
Adds support for some attributes in experimental Go SDK (#29)
#25 --------- Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
1 parent 4cf9533 commit f38ae46

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

go/gosdk/abi.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ size_t envoy_dynamic_module_callback_http_get_response_headers_count(
119119
bool envoy_dynamic_module_callback_http_get_response_headers(
120120
uintptr_t filter_envoy_ptr,
121121
uintptr_t* result_headers);
122+
123+
#cgo noescape envoy_dynamic_module_callback_http_filter_get_attribute_string
124+
#cgo nocallback envoy_dynamic_module_callback_http_filter_get_attribute_string
125+
bool envoy_dynamic_module_callback_http_filter_get_attribute_string(
126+
uintptr_t filter_envoy_ptr,
127+
size_t attribute_id,
128+
uintptr_t* result, size_t* result_length);
122129
*/
123130
import "C"
124131

@@ -375,6 +382,33 @@ type envoySlice struct {
375382
// envoyFilter implements [EnvoyHttpFilter].
376383
type envoyFilter struct{ raw uintptr }
377384

385+
// GetRequestProtocol implements [EnvoyHttpFilter].
386+
func (e envoyFilter) GetRequestProtocol() string {
387+
// https://github.com/envoyproxy/envoy/blob/05223ee2cd143d70b32402783c2a866a9dd18bd1/source/extensions/dynamic_modules/abi.h#L237-L372
388+
return e.getStringAttribute(10) // request.protocol
389+
}
390+
391+
// GetSourceAddress implements [EnvoyHttpFilter].
392+
func (e envoyFilter) GetSourceAddress() string {
393+
// https://github.com/envoyproxy/envoy/blob/05223ee2cd143d70b32402783c2a866a9dd18bd1/source/extensions/dynamic_modules/abi.h#L237-L372
394+
return e.getStringAttribute(24) // source.address
395+
}
396+
397+
func (e envoyFilter) getStringAttribute(id int) string {
398+
var resultBufferPtr *byte
399+
var resultBufferLengthPtr int
400+
ret := C.envoy_dynamic_module_callback_http_filter_get_attribute_string(
401+
C.uintptr_t(e.raw),
402+
C.size_t(id),
403+
(*C.uintptr_t)(unsafe.Pointer(&resultBufferPtr)),
404+
(*C.size_t)(unsafe.Pointer(&resultBufferLengthPtr)),
405+
)
406+
if !ret {
407+
return ""
408+
}
409+
return string(unsafe.Slice(resultBufferPtr, resultBufferLengthPtr)) // Copy the result to a Go string.
410+
}
411+
378412
// GetRequestHeaders implements EnvoyHttpFilter.
379413
func (e envoyFilter) GetRequestHeaders() map[string][]string {
380414
count := C.envoy_dynamic_module_callback_http_get_request_headers_count(C.uintptr_t(e.raw))

go/gosdk/gosdk.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ type EnvoyHttpFilter interface {
5454
AppendResponseBody(data []byte) bool
5555
// SendLocalReply sends a local reply to the client. This must not be used in after returning continue from the response headers phase.
5656
SendLocalReply(statusCode uint32, headers [][2]string, body []byte)
57+
// GetSourceAddress gets the source address of the request in the format of "IP:PORT".
58+
// This corresponds to `source.address` attribute https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes.
59+
GetSourceAddress() string
60+
// GetRequestProtocol gets the request protocol. This corresponds to `request.protocol` attribute https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes.
61+
GetRequestProtocol() string
5762
}
5863

5964
// HttpFilter is an interface that represents each Http request.

go/passthrough.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func (p passthroughFilter) RequestHeaders(e gosdk.EnvoyHttpFilter, endOfStream b
3333
fmt.Printf("gosdk: RequestHeaders, header: %s: %s\n", k, v)
3434
}
3535
}
36+
fmt.Printf("gosdk: RequestHeaders, source address: %s\n", e.GetSourceAddress())
37+
fmt.Printf("gosdk: RequestHeaders, request protocol: %s\n", e.GetRequestProtocol())
3638
return gosdk.RequestHeadersStatusContinue
3739
}
3840

integration/main_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ func TestIntegration(t *testing.T) {
5555
require.NoError(t, cmd.Start())
5656
t.Cleanup(func() { require.NoError(t, cmd.Process.Signal(os.Interrupt)) })
5757

58-
// Let's wait at least 5 seconds for Envoy to start since it might take a while
59-
// to pull the image.
60-
time.Sleep(5 * time.Second)
61-
6258
t.Run("http_access_logger", func(t *testing.T) {
6359
t.Run("health checking", func(t *testing.T) {
6460
require.Eventually(t, func() bool {
@@ -80,7 +76,7 @@ func TestIntegration(t *testing.T) {
8076
}
8177
t.Logf("response: status=%d body=%s", resp.StatusCode, string(body))
8278
return resp.StatusCode == 200
83-
}, 30*time.Second, 1*time.Second)
79+
}, 120*time.Second, 1*time.Second)
8480
})
8581

8682
require.Eventually(t, func() bool {

0 commit comments

Comments
 (0)