Skip to content

Commit 2d4b47f

Browse files
authored
go sdk: add accessor for destination address (#50)
This can be useful for WAF purposes (though it may be desirable as well to get the ip from a header like XFF or the Envoy trusted IP). --------- Signed-off-by: William Zhang <wtzhang23@gmail.com>
1 parent 8ad029f commit 2d4b47f

4 files changed

Lines changed: 36 additions & 19 deletions

File tree

go/gosdk/abi.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ func envoy_dynamic_module_on_http_filter_http_callout_done(
298298
func envoy_dynamic_module_on_http_filter_scheduled(
299299
filterEnvoyPtr uintptr,
300300
filterModulePtr uintptr,
301-
eventID C.uint64_t) {
301+
eventID C.uint64_t,
302+
) {
302303
pinned := unwrapPinnedHttpFilter(uintptr(filterModulePtr))
303304
// Call the Scheduled method of the filter.
304305
pinned.obj.Scheduled(envoyFilter{raw: uintptr(filterEnvoyPtr)}, uint64(eventID))
@@ -469,6 +470,12 @@ func (e envoyFilter) GetSourceAddress() string {
469470
return e.getStringAttribute(24) // source.address
470471
}
471472

473+
// GetDestinationAddress implements [EnvoyHttpFilter].
474+
func (e envoyFilter) GetDestinationAddress() string {
475+
// https://github.com/envoyproxy/envoy/blob/05223ee2cd143d70b32402783c2a866a9dd18bd1/source/extensions/dynamic_modules/abi.h#L237-L372
476+
return e.getStringAttribute(26) // destination.address
477+
}
478+
472479
func (e envoyFilter) getStringAttribute(id int) string {
473480
var resultBufferPtr *byte
474481
var resultBufferLengthPtr int

go/gosdk/gosdk.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ type EnvoyHttpFilter interface {
5959
// GetSourceAddress gets the source address of the request in the format of "IP:PORT".
6060
// This corresponds to `source.address` attribute https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes.
6161
GetSourceAddress() string
62+
// GetDestinationAddress gets the destination address of the request in the format of "IP:PORT".
63+
// This corresponds to `destination.address` attribute https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes.
64+
GetDestinationAddress() string
6265
// GetRequestProtocol gets the request protocol. This corresponds to `request.protocol` attribute https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes.
6366
GetRequestProtocol() string
6467
// NewScheduler creates a new Scheduler that can be used to schedule events to the correct Envoy worker thread.

go/mock_test.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@ import (
88

99
// mockEnvoyHttpFilter is a mock implementation of [gosdk.EnvoyHttpFilter] for testing.
1010
type mockEnvoyHttpFilter struct {
11-
getRequestHeader func(key string) (string, bool)
12-
getRequestHeaders func() map[string][]string
13-
setRequestHeader func(key string, value []byte) bool
14-
getResponseHeader func(key string) (string, bool)
15-
getResponseHeaders func() map[string][]string
16-
setResponseHeader func(key string, value []byte) bool
17-
getRequestBody func() (io.Reader, bool)
18-
drainRequestBody func(n int) bool
19-
appendRequestBody func(data []byte) bool
20-
getResponseBody func() (io.Reader, bool)
21-
drainResponseBody func(n int) bool
22-
appendResponseBody func(data []byte) bool
23-
sendLocalReply func(statusCode uint32, headers [][2]string, body []byte)
24-
getSourceAddress func() string
25-
getRequestProtocol func() string
26-
newScheduler func() gosdk.Scheduler
27-
continueRequest func()
28-
continueResponse func()
11+
getRequestHeader func(key string) (string, bool)
12+
getRequestHeaders func() map[string][]string
13+
setRequestHeader func(key string, value []byte) bool
14+
getResponseHeader func(key string) (string, bool)
15+
getResponseHeaders func() map[string][]string
16+
setResponseHeader func(key string, value []byte) bool
17+
getRequestBody func() (io.Reader, bool)
18+
drainRequestBody func(n int) bool
19+
appendRequestBody func(data []byte) bool
20+
getResponseBody func() (io.Reader, bool)
21+
drainResponseBody func(n int) bool
22+
appendResponseBody func(data []byte) bool
23+
sendLocalReply func(statusCode uint32, headers [][2]string, body []byte)
24+
getSourceAddress func() string
25+
getDestinationAddress func() string
26+
getRequestProtocol func() string
27+
newScheduler func() gosdk.Scheduler
28+
continueRequest func()
29+
continueResponse func()
2930
}
3031

3132
// GetRequestHeader implements [gosdk.EnvoyHttpFilter.GetRequestHeader].
@@ -98,6 +99,11 @@ func (m mockEnvoyHttpFilter) GetSourceAddress() string {
9899
return m.getSourceAddress()
99100
}
100101

102+
// GetDestinationAddress implements [gosdk.EnvoyHttpFilter.GetDestinationAddress].
103+
func (m mockEnvoyHttpFilter) GetDestinationAddress() string {
104+
return m.getDestinationAddress()
105+
}
106+
101107
// GetRequestProtocol implements [gosdk.EnvoyHttpFilter.GetRequestProtocol].
102108
func (m mockEnvoyHttpFilter) GetRequestProtocol() string {
103109
return m.getRequestProtocol()

go/passthrough.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (p passthroughFilter) RequestHeaders(e gosdk.EnvoyHttpFilter, endOfStream b
3737
}
3838
}
3939
fmt.Printf("gosdk: RequestHeaders, source address: %s\n", e.GetSourceAddress())
40+
fmt.Printf("gosdk: RequestHeaders, destination address: %s\n", e.GetDestinationAddress())
4041
fmt.Printf("gosdk: RequestHeaders, request protocol: %s\n", e.GetRequestProtocol())
4142
return gosdk.RequestHeadersStatusContinue
4243
}

0 commit comments

Comments
 (0)