diff --git a/changelogs/current.yaml b/changelogs/current.yaml index 594159b83d07..5148890868a4 100644 --- a/changelogs/current.yaml +++ b/changelogs/current.yaml @@ -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() ` + method to the Stream info object API. - area: quic change: | added support for QUIC listener filters with ECDS support reusing the same config API diff --git a/docs/root/configuration/http/http_filters/lua_filter.rst b/docs/root/configuration/http/http_filters/lua_filter.rst index 925320330cf5..4fc3589c1267 100644 --- a/docs/root/configuration/http/http_filters/lua_filter.rst +++ b/docs/root/configuration/http/http_filters/lua_filter.rst @@ -791,6 +791,8 @@ downstreamLocalAddress() Returns the string representation of :repo:`downstream local address ` used by the current request. +.. _config_http_filters_lua_stream_info_downstream_direct_remote_address: + downstreamDirectRemoteAddress() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -801,6 +803,19 @@ downstreamDirectRemoteAddress() Returns the string representation of :repo:`downstream directly connected address ` 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() ` depending upon the setting of +:ref:`xff_num_trusted_hops `. + dynamicMetadata() ^^^^^^^^^^^^^^^^^ diff --git a/source/extensions/filters/http/lua/wrappers.cc b/source/extensions/filters/http/lua/wrappers.cc index 78e33443f6b7..e8f45539dc2e 100644 --- a/source/extensions/filters/http/lua/wrappers.cc +++ b/source/extensions/filters/http/lua/wrappers.cc @@ -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(); diff --git a/source/extensions/filters/http/lua/wrappers.h b/source/extensions/filters/http/lua/wrappers.h index f61ea91237a6..bf7a842156fc 100644 --- a/source/extensions/filters/http/lua/wrappers.h +++ b/source/extensions/filters/http/lua/wrappers.h @@ -209,6 +209,7 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObjectheaders() + .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() diff --git a/test/extensions/filters/http/lua/wrappers_test.cc b/test/extensions/filters/http/lua/wrappers_test.cc index 85f5c13b31a9..714611eefc9e 100644 --- a/test/extensions/filters/http/lua/wrappers_test.cc +++ b/test/extensions/filters/http/lua/wrappers_test.cc @@ -349,6 +349,7 @@ TEST_F(LuaStreamInfoWrapperTest, ReturnCurrentDownstreamAddresses) { function callMe(object) testPrint(object:downstreamLocalAddress()) testPrint(object:downstreamDirectRemoteAddress()) + testPrint(object:downstreamRemoteAddress()) end )EOF"}; @@ -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 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(); }