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

Add response flags into cel expression context #8827

Merged
merged 6 commits into from
Nov 5, 2019
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
1 change: 1 addition & 0 deletions docs/root/intro/arch_overview/security/rbac_filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ The following attributes are exposed to the language runtime:
response.headers, string map, All response headers
response.trailers, string map, All response trailers
response.size, int, Size of the response body
response.flags, int, Additional details about the response beyond the standard response code
source.address, string, Downstream connection remote address
source.port, int, Downstream connection remote port
destination.address, string, Downstream connection local address
Expand Down
5 changes: 5 additions & 0 deletions include/envoy/stream_info/stream_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ class StreamInfo {
*/
virtual bool hasAnyResponseFlag() const PURE;

/**
* @return response flags encoded as an integer.
*/
virtual uint64_t responseFlags() const PURE;

/**
* @return upstream host description.
*/
Expand Down
2 changes: 2 additions & 0 deletions source/common/stream_info/stream_info_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ struct StreamInfoImpl : public StreamInfo {

bool hasAnyResponseFlag() const override { return response_flags_ != 0; }

uint64_t responseFlags() const override { return response_flags_; }

void onUpstreamHostSelected(Upstream::HostDescriptionConstSharedPtr host) override {
upstream_host_ = host;
}
Expand Down
1 change: 1 addition & 0 deletions source/extensions/filters/common/expr/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ envoy_cc_library(
hdrs = ["context.h"],
deps = [
"//source/common/http:utility_lib",
"//source/common/stream_info:utility_lib",
"@com_google_cel_cpp//eval/public:cel_value",
],
)
2 changes: 2 additions & 0 deletions source/extensions/filters/common/expr/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ absl::optional<CelValue> ResponseWrapper::operator[](CelValue key) const {
return CelValue::CreateMap(&headers_);
} else if (value == Trailers) {
return CelValue::CreateMap(&trailers_);
} else if (value == Flags) {
return CelValue::CreateInt64(info_.responseFlags());
}
return {};
}
Expand Down
1 change: 1 addition & 0 deletions source/extensions/filters/common/expr/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ constexpr absl::string_view Duration = "duration";
constexpr absl::string_view Response = "response";
constexpr absl::string_view Code = "code";
constexpr absl::string_view Trailers = "trailers";
constexpr absl::string_view Flags = "flags";

// Per-request or per-connection metadata
constexpr absl::string_view Metadata = "metadata";
Expand Down
1 change: 1 addition & 0 deletions test/common/stream_info/stream_info_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ TEST_F(StreamInfoImplTest, ResponseFlagTest) {
<< fmt::format("Flag: {} was expected to be set", flag);
}
EXPECT_TRUE(stream_info.hasAnyResponseFlag());
EXPECT_EQ(0xFFF, stream_info.responseFlags());

StreamInfoImpl stream_info2(Http::Protocol::Http2, test_time_.timeSystem());
stream_info2.setResponseFlag(FailedLocalHealthCheck);
Expand Down
1 change: 1 addition & 0 deletions test/common/stream_info/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TestStreamInfo : public StreamInfo::StreamInfo {
void setResponseFlag(Envoy::StreamInfo::ResponseFlag response_flag) override {
response_flags_ |= response_flag;
}
uint64_t responseFlags() const override { return response_flags_; }
void onUpstreamHostSelected(Upstream::HostDescriptionConstSharedPtr host) override {
upstream_host_ = host;
}
Expand Down
7 changes: 7 additions & 0 deletions test/extensions/filters/common/expr/context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ TEST(Context, ResponseAttributes) {

EXPECT_CALL(info, responseCode()).WillRepeatedly(Return(404));
EXPECT_CALL(info, bytesSent()).WillRepeatedly(Return(123));
EXPECT_CALL(info, responseFlags()).WillRepeatedly(Return(0x1));

{
auto value = response[CelValue::CreateString(Undefined)];
Expand Down Expand Up @@ -250,6 +251,12 @@ TEST(Context, ResponseAttributes) {
ASSERT_TRUE(header.value().IsString());
EXPECT_EQ("b", header.value().StringOrDie().value());
}
{
auto value = response[CelValue::CreateString(Flags)];
EXPECT_TRUE(value.has_value());
ASSERT_TRUE(value.value().IsInt64());
EXPECT_EQ(0x1, value.value().Int64OrDie());
}
}

TEST(Context, ConnectionAttributes) {
Expand Down
1 change: 1 addition & 0 deletions test/mocks/stream_info/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MockStreamInfo : public StreamInfo {
MOCK_CONST_METHOD0(bytesSent, uint64_t());
MOCK_CONST_METHOD1(hasResponseFlag, bool(ResponseFlag));
MOCK_CONST_METHOD0(hasAnyResponseFlag, bool());
MOCK_CONST_METHOD0(responseFlags, uint64_t());
MOCK_CONST_METHOD0(upstreamHost, Upstream::HostDescriptionConstSharedPtr());
MOCK_METHOD1(setUpstreamLocalAddress, void(const Network::Address::InstanceConstSharedPtr&));
MOCK_CONST_METHOD0(upstreamLocalAddress, const Network::Address::InstanceConstSharedPtr&());
Expand Down