Skip to content

Commit

Permalink
lua http filter: Expose requestedServerName on streamInfo
Browse files Browse the repository at this point in the history
Fixes envoyproxy#15142

Signed-off-by: Sunjay Bhatia <sunjayb@vmware.com>
  • Loading branch information
sunjayBhatia committed Feb 26, 2021
1 parent 98ac592 commit 5b17655
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/root/configuration/http/http_filters/lua_filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,17 @@ Returns a downstream :ref:`SSL connection info object <config_http_filters_lua_s

.. _config_http_filters_lua_stream_info_dynamic_metadata_wrapper:

requestedServerName()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: lua
streamInfo:requestedServerName()
// TODO: fix this below
Returns the string representation of :repo:`requested server name <include/envoy/stream_info/stream_info.h>`
(e.g. SNI in TLS) for the current request if present.

Dynamic metadata object API
---------------------------

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

int StreamInfoWrapper::luaRequestedServerName(lua_State* state) {
lua_pushstring(state, stream_info_.requestedServerName().c_str());
return 1;
}

DynamicMetadataMapIterator::DynamicMetadataMapIterator(DynamicMetadataMapWrapper& parent)
: parent_{parent}, current_{parent_.streamInfo().dynamicMetadata().filter_metadata().begin()} {}

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 @@ -186,7 +186,8 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObject<StreamInfoW
{"dynamicMetadata", static_luaDynamicMetadata},
{"downstreamLocalAddress", static_luaDownstreamLocalAddress},
{"downstreamDirectRemoteAddress", static_luaDownstreamDirectRemoteAddress},
{"downstreamSslConnection", static_luaDownstreamSslConnection}};
{"downstreamSslConnection", static_luaDownstreamSslConnection},
{"requestedServerName", static_luaRequestedServerName}};
}

private:
Expand Down Expand Up @@ -221,6 +222,12 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObject<StreamInfoW
*/
DECLARE_LUA_FUNCTION(StreamInfoWrapper, luaDownstreamDirectRemoteAddress);

/**
* Get requested server name
* @return requested server name (e.g. SNI in TLS), if any.
*/
DECLARE_LUA_FUNCTION(StreamInfoWrapper, luaRequestedServerName);

// Envoy::Lua::BaseLuaObject
void onMarkDead() override {
dynamic_metadata_wrapper_.reset();
Expand Down
20 changes: 20 additions & 0 deletions test/extensions/filters/http/lua/lua_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,26 @@ TEST_F(LuaHttpFilterTest, GetCurrentProtocol) {
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, true));
}

// Get the requested server name.
TEST_F(LuaHttpFilterTest, GetRequestedServerName) {
const std::string SCRIPT{R"EOF(
function envoy_on_request(request_handle)
request_handle:logTrace(request_handle:streamInfo():requestedServerName())
end
)EOF"};

InSequence s;
setup(SCRIPT);

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillOnce(ReturnRef(stream_info_));
std::string server_name = "foo.example.com";
EXPECT_CALL(stream_info_, requestedServerName()).WillOnce(ReturnRef(server_name));

Http::TestRequestHeaderMapImpl request_headers{{":path", "/"}};
EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("foo.example.com")));
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, true));
}

// Set and get stream info dynamic metadata.
TEST_F(LuaHttpFilterTest, SetGetDynamicMetadata) {
const std::string SCRIPT{R"EOF(
Expand Down
7 changes: 7 additions & 0 deletions test/extensions/filters/http/lua/lua_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,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_requested_server_name",
request_handle:streamInfo():requestedServerName())
end
function envoy_on_response(response_handle)
Expand Down Expand Up @@ -364,6 +366,11 @@ name: lua
.getStringView(),
GetParam() == Network::Address::IpVersion::v4 ? "127.0.0.1:" : "[::1]:"));

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

Http::TestResponseHeaderMapImpl response_headers{{":status", "200"}, {"foo", "bar"}};
upstream_request_->encodeHeaders(response_headers, false);
Buffer::OwnedImpl response_data1("good");
Expand Down
19 changes: 19 additions & 0 deletions test/extensions/filters/http/lua/wrappers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,25 @@ TEST_F(LuaStreamInfoWrapperTest, ReturnCurrentDownstreamAddresses) {
wrapper.reset();
}

TEST_F(LuaStreamInfoWrapperTest, ReturnRequestedServerName) {
const std::string SCRIPT{R"EOF(
function callMe(object)
testPrint(object:requestedServerName())
end
)EOF"};

InSequence s;
setup(SCRIPT);

NiceMock<Envoy::StreamInfo::MockStreamInfo> stream_info;
stream_info.requested_server_name_ = "some.sni.io";
Filters::Common::Lua::LuaDeathRef<StreamInfoWrapper> wrapper(
StreamInfoWrapper::create(coroutine_->luaState(), stream_info), true);
EXPECT_CALL(printer_, testPrint("some.sni.io"));
start("callMe");
wrapper.reset();
}

// Set, get and iterate stream info dynamic metadata.
TEST_F(LuaStreamInfoWrapperTest, SetGetAndIterateDynamicMetadata) {
const std::string SCRIPT{R"EOF(
Expand Down

0 comments on commit 5b17655

Please sign in to comment.