Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua: add downstreamRemoteAddress() method #29755

Merged
merged 1 commit into from
Sep 21, 2023
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
4 changes: 4 additions & 0 deletions changelogs/current.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ new_features:
- area: extension_discovery_service
change: |
added metric listener.listener_stat.network_extension_config_missing to track closed connections due to missing config.
- area: lua
change: |
added :ref:`downstreamRemoteAddress() <config_http_filters_lua_stream_info_downstream_remote_address>`
method to the Stream info object API.
- area: quic
change: |
added support for QUIC listener filters with ECDS support reusing the same config API
Expand Down
15 changes: 15 additions & 0 deletions docs/root/configuration/http/http_filters/lua_filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ downstreamLocalAddress()
Returns the string representation of :repo:`downstream local address <envoy/stream_info/stream_info.h>`
used by the current request.

.. _config_http_filters_lua_stream_info_downstream_direct_remote_address:

downstreamDirectRemoteAddress()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -801,6 +803,19 @@ downstreamDirectRemoteAddress()
Returns the string representation of :repo:`downstream directly connected address <envoy/stream_info/stream_info.h>`
used by the current request. This is equivalent to the address of the physical connection.

.. _config_http_filters_lua_stream_info_downstream_remote_address:

downstreamRemoteAddress()
^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: lua

streamInfo:downstreamRemoteAddress()

Returns the string representation of the downstream remote address for the current request. This may differ from
:ref:`downstreamDirectRemoteAddress() <config_http_filters_lua_stream_info_downstream_direct_remote_address>` depending upon the setting of
:ref:`xff_num_trusted_hops <envoy_v3_api_field_extensions.filters.network.http_connection_manager.v3.HttpConnectionManager.xff_num_trusted_hops>`.

dynamicMetadata()
^^^^^^^^^^^^^^^^^

Expand Down
7 changes: 7 additions & 0 deletions source/extensions/filters/http/lua/wrappers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ int StreamInfoWrapper::luaDownstreamDirectRemoteAddress(lua_State* state) {
return 1;
}

int StreamInfoWrapper::luaDownstreamRemoteAddress(lua_State* state) {
const std::string& remote_address =
stream_info_.downstreamAddressProvider().remoteAddress()->asString();
lua_pushlstring(state, remote_address.data(), remote_address.size());
return 1;
}

int StreamInfoWrapper::luaRequestedServerName(lua_State* state) {
absl::string_view requested_serve_name =
stream_info_.downstreamAddressProvider().requestedServerName();
Expand Down
9 changes: 8 additions & 1 deletion source/extensions/filters/http/lua/wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObject<StreamInfoW
{"dynamicMetadata", static_luaDynamicMetadata},
{"downstreamLocalAddress", static_luaDownstreamLocalAddress},
{"downstreamDirectRemoteAddress", static_luaDownstreamDirectRemoteAddress},
{"downstreamRemoteAddress", static_luaDownstreamRemoteAddress},
{"downstreamSslConnection", static_luaDownstreamSslConnection},
{"requestedServerName", static_luaRequestedServerName}};
}
Expand Down Expand Up @@ -239,12 +240,18 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObject<StreamInfoW
DECLARE_LUA_FUNCTION(StreamInfoWrapper, luaDownstreamLocalAddress);

/**
* Get current downstream local address
* Get current direct downstream remote address
* @return string representation of downstream directly connected address.
* This is equivalent to the address of the physical connection.
*/
DECLARE_LUA_FUNCTION(StreamInfoWrapper, luaDownstreamDirectRemoteAddress);

/**
* Get current downstream remote address
* @return string representation of downstream remote address.
*/
DECLARE_LUA_FUNCTION(StreamInfoWrapper, luaDownstreamRemoteAddress);

/**
* Get requested server name
* @return requested server name (e.g. SNI in TLS), if any.
Expand Down
8 changes: 8 additions & 0 deletions test/extensions/filters/http/lua/lua_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ name: lua
request_handle:streamInfo():downstreamLocalAddress())
request_handle:headers():add("request_downstream_directremote_address_value",
request_handle:streamInfo():downstreamDirectRemoteAddress())
request_handle:headers():add("request_downstream_remote_address_value",
request_handle:streamInfo():downstreamRemoteAddress())
request_handle:headers():add("request_requested_server_name",
request_handle:streamInfo():requestedServerName())
end
Expand Down Expand Up @@ -420,6 +422,12 @@ name: lua
.getStringView(),
GetParam() == Network::Address::IpVersion::v4 ? "127.0.0.1:" : "[::1]:"));

EXPECT_EQ("10.0.0.1:0",
upstream_request_->headers()
.get(Http::LowerCaseString("request_downstream_remote_address_value"))[0]
->value()
.getStringView());

EXPECT_EQ("", upstream_request_->headers()
.get(Http::LowerCaseString("request_requested_server_name"))[0]
->value()
Expand Down
5 changes: 5 additions & 0 deletions test/extensions/filters/http/lua/wrappers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ TEST_F(LuaStreamInfoWrapperTest, ReturnCurrentDownstreamAddresses) {
function callMe(object)
testPrint(object:downstreamLocalAddress())
testPrint(object:downstreamDirectRemoteAddress())
testPrint(object:downstreamRemoteAddress())
end
)EOF"};

Expand All @@ -360,13 +361,17 @@ TEST_F(LuaStreamInfoWrapperTest, ReturnCurrentDownstreamAddresses) {
new Network::Address::Ipv4Instance("127.0.0.1", 8000)};
auto downstream_direct_remote =
Network::Address::InstanceConstSharedPtr{new Network::Address::Ipv4Instance("8.8.8.8", 3000)};
auto downstream_remote = Network::Address::InstanceConstSharedPtr{
new Network::Address::Ipv4Instance("10.1.2.3", 5000)};
stream_info.downstream_connection_info_provider_->setLocalAddress(address);
stream_info.downstream_connection_info_provider_->setDirectRemoteAddressForTest(
downstream_direct_remote);
stream_info.downstream_connection_info_provider_->setRemoteAddress(downstream_remote);
Filters::Common::Lua::LuaDeathRef<StreamInfoWrapper> wrapper(
StreamInfoWrapper::create(coroutine_->luaState(), stream_info), true);
EXPECT_CALL(printer_, testPrint(address->asString()));
EXPECT_CALL(printer_, testPrint(downstream_direct_remote->asString()));
EXPECT_CALL(printer_, testPrint(downstream_remote->asString()));
start("callMe");
wrapper.reset();
}
Expand Down