Skip to content

Commit c60da46

Browse files
bianpengyuanlizan
authored andcommitted
Add response flags into cel expression context (#8827)
Description: Add response flags into expression context Risk Level: low Testing: unit test Docs Changes: Release Notes: Signed-off-by: Pengyuan Bian <[email protected]>
1 parent a3bc473 commit c60da46

File tree

10 files changed

+22
-0
lines changed

10 files changed

+22
-0
lines changed

docs/root/intro/arch_overview/security/rbac_filter.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ The following attributes are exposed to the language runtime:
8181
response.headers, string map, All response headers
8282
response.trailers, string map, All response trailers
8383
response.size, int, Size of the response body
84+
response.flags, int, Additional details about the response beyond the standard response code
8485
source.address, string, Downstream connection remote address
8586
source.port, int, Downstream connection remote port
8687
destination.address, string, Downstream connection local address

include/envoy/stream_info/stream_info.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class StreamInfo {
357357
*/
358358
virtual bool hasAnyResponseFlag() const PURE;
359359

360+
/**
361+
* @return response flags encoded as an integer.
362+
*/
363+
virtual uint64_t responseFlags() const PURE;
364+
360365
/**
361366
* @return upstream host description.
362367
*/

source/common/stream_info/stream_info_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ struct StreamInfoImpl : public StreamInfo {
123123

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

126+
uint64_t responseFlags() const override { return response_flags_; }
127+
126128
void onUpstreamHostSelected(Upstream::HostDescriptionConstSharedPtr host) override {
127129
upstream_host_ = host;
128130
}

source/extensions/filters/common/expr/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ envoy_cc_library(
2929
hdrs = ["context.h"],
3030
deps = [
3131
"//source/common/http:utility_lib",
32+
"//source/common/stream_info:utility_lib",
3233
"@com_google_cel_cpp//eval/public:cel_value",
3334
],
3435
)

source/extensions/filters/common/expr/context.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ absl::optional<CelValue> ResponseWrapper::operator[](CelValue key) const {
129129
return CelValue::CreateMap(&headers_);
130130
} else if (value == Trailers) {
131131
return CelValue::CreateMap(&trailers_);
132+
} else if (value == Flags) {
133+
return CelValue::CreateInt64(info_.responseFlags());
132134
}
133135
return {};
134136
}

source/extensions/filters/common/expr/context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ constexpr absl::string_view Duration = "duration";
3434
constexpr absl::string_view Response = "response";
3535
constexpr absl::string_view Code = "code";
3636
constexpr absl::string_view Trailers = "trailers";
37+
constexpr absl::string_view Flags = "flags";
3738

3839
// Per-request or per-connection metadata
3940
constexpr absl::string_view Metadata = "metadata";

test/common/stream_info/stream_info_impl_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ TEST_F(StreamInfoImplTest, ResponseFlagTest) {
121121
<< fmt::format("Flag: {} was expected to be set", flag);
122122
}
123123
EXPECT_TRUE(stream_info.hasAnyResponseFlag());
124+
EXPECT_EQ(0xFFF, stream_info.responseFlags());
124125

125126
StreamInfoImpl stream_info2(Http::Protocol::Http2, test_time_.timeSystem());
126127
stream_info2.setResponseFlag(FailedLocalHealthCheck);

test/common/stream_info/test_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TestStreamInfo : public StreamInfo::StreamInfo {
4949
void setResponseFlag(Envoy::StreamInfo::ResponseFlag response_flag) override {
5050
response_flags_ |= response_flag;
5151
}
52+
uint64_t responseFlags() const override { return response_flags_; }
5253
void onUpstreamHostSelected(Upstream::HostDescriptionConstSharedPtr host) override {
5354
upstream_host_ = host;
5455
}

test/extensions/filters/common/expr/context_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ TEST(Context, ResponseAttributes) {
195195

196196
EXPECT_CALL(info, responseCode()).WillRepeatedly(Return(404));
197197
EXPECT_CALL(info, bytesSent()).WillRepeatedly(Return(123));
198+
EXPECT_CALL(info, responseFlags()).WillRepeatedly(Return(0x1));
198199

199200
{
200201
auto value = response[CelValue::CreateString(Undefined)];
@@ -250,6 +251,12 @@ TEST(Context, ResponseAttributes) {
250251
ASSERT_TRUE(header.value().IsString());
251252
EXPECT_EQ("b", header.value().StringOrDie().value());
252253
}
254+
{
255+
auto value = response[CelValue::CreateString(Flags)];
256+
EXPECT_TRUE(value.has_value());
257+
ASSERT_TRUE(value.value().IsInt64());
258+
EXPECT_EQ(0x1, value.value().Int64OrDie());
259+
}
253260
}
254261

255262
TEST(Context, ConnectionAttributes) {

test/mocks/stream_info/mocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class MockStreamInfo : public StreamInfo {
5252
MOCK_CONST_METHOD0(bytesSent, uint64_t());
5353
MOCK_CONST_METHOD1(hasResponseFlag, bool(ResponseFlag));
5454
MOCK_CONST_METHOD0(hasAnyResponseFlag, bool());
55+
MOCK_CONST_METHOD0(responseFlags, uint64_t());
5556
MOCK_CONST_METHOD0(upstreamHost, Upstream::HostDescriptionConstSharedPtr());
5657
MOCK_METHOD1(setUpstreamLocalAddress, void(const Network::Address::InstanceConstSharedPtr&));
5758
MOCK_CONST_METHOD0(upstreamLocalAddress, const Network::Address::InstanceConstSharedPtr&());

0 commit comments

Comments
 (0)