Skip to content
Closed
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
5 changes: 2 additions & 3 deletions source/extensions/formatter/cel/cel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ CELFormatterCommandParser::parse(absl::string_view command, absl::string_view su
if (!parse_status.ok()) {
throw EnvoyException("Not able to parse expression: " + parse_status.status().ToString());
}
Server::Configuration::ServerFactoryContext& context =
Server::Configuration::ServerFactoryContextInstance::get();
return std::make_unique<CELFormatter>(
context.localInfo(), Extensions::Filters::Common::Expr::getBuilder(context),
server_factory_context_.localInfo(),
Extensions::Filters::Common::Expr::getBuilder(server_factory_context_),
parse_status.value().expr(), max_length, command == "TYPED_CEL");
}

Expand Down
7 changes: 6 additions & 1 deletion source/extensions/formatter/cel/cel.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ class CELFormatter : public ::Envoy::Formatter::FormatterProvider {

class CELFormatterCommandParser : public ::Envoy::Formatter::CommandParser {
public:
CELFormatterCommandParser() = default;
explicit CELFormatterCommandParser(
Server::Configuration::ServerFactoryContext& server_factory_context)
: server_factory_context_(server_factory_context) {}
::Envoy::Formatter::FormatterProviderPtr parse(absl::string_view command,
absl::string_view subcommand,
absl::optional<size_t> max_length) const override;

private:
Server::Configuration::ServerFactoryContext& server_factory_context_;
};

} // namespace Formatter
Expand Down
11 changes: 6 additions & 5 deletions source/extensions/formatter/cel/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ namespace Envoy {
namespace Extensions {
namespace Formatter {

::Envoy::Formatter::CommandParserPtr
CELFormatterFactory::createCommandParserFromProto(const Protobuf::Message&,
Server::Configuration::GenericFactoryContext&) {
::Envoy::Formatter::CommandParserPtr CELFormatterFactory::createCommandParserFromProto(
const Protobuf::Message&, Server::Configuration::GenericFactoryContext& context) {
#if defined(USE_CEL_PARSER)
ENVOY_LOG_TO_LOGGER(Logger::Registry::getLog(Logger::Id::config), warn,
"'CEL' formatter is treated as a built-in formatter and does not "
"require configuration.");
return std::make_unique<CELFormatterCommandParser>();
return std::make_unique<CELFormatterCommandParser>(context.serverFactoryContext());
#else
UNREFERENCED_PARAMETER(context);
throw EnvoyException("CEL is not available for use in this environment.");
Expand All @@ -35,7 +34,9 @@ class BuiltInCELFormatterFactory : public Envoy::Formatter::BuiltInCommandParser
std::string name() const override { return "envoy.built_in_formatters.cel"; }

::Envoy::Formatter::CommandParserPtr createCommandParser() const override {
return std::make_unique<CELFormatterCommandParser>();
Server::Configuration::ServerFactoryContext& context =
Server::Configuration::ServerFactoryContextInstance::get();
return std::make_unique<CELFormatterCommandParser>(context);
}
};

Expand Down
32 changes: 16 additions & 16 deletions test/extensions/formatter/cel/cel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CELFormatterTest : public ::testing::Test {

#ifdef USE_CEL_PARSER
TEST_F(CELFormatterTest, TestNodeId) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "xds.node.id", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -99,7 +99,7 @@ TEST_F(CELFormatterTest, TestNodeId) {
}

TEST_F(CELFormatterTest, Testformat) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "xds.node.id", max_length);
EXPECT_THAT(formatter->format(formatter_context_, stream_info_), "node_name");
Expand All @@ -109,7 +109,7 @@ TEST_F(CELFormatterTest, Testformat) {
}

TEST_F(CELFormatterTest, TestFormatStringValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "request.headers[':method']", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -121,7 +121,7 @@ TEST_F(CELFormatterTest, TestFormatStringValue) {
}

TEST_F(CELFormatterTest, TestFormatNumberValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "request.headers[':method'].size()", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -134,7 +134,7 @@ TEST_F(CELFormatterTest, TestFormatNumberValue) {
}

TEST_F(CELFormatterTest, TestFormatNullValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "request.headers.nope", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -148,7 +148,7 @@ TEST_F(CELFormatterTest, TestFormatNullValue) {
TEST_F(CELFormatterTest, TestFormatNoHeaders) {
Envoy::Formatter::Context formatter_context;

auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;

{
Expand Down Expand Up @@ -183,7 +183,7 @@ TEST_F(CELFormatterTest, TestFormatNoHeaders) {
}

TEST_F(CELFormatterTest, TestFormatBoolValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "request.headers[':method'] == 'GET'", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -196,7 +196,7 @@ TEST_F(CELFormatterTest, TestFormatBoolValue) {
}

TEST_F(CELFormatterTest, TestFormatDurationValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "duration(\"1h30m\")", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -208,7 +208,7 @@ TEST_F(CELFormatterTest, TestFormatDurationValue) {
}

TEST_F(CELFormatterTest, TestFormatTimestampValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "timestamp(\"2023-08-26T12:39:00-07:00\")", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -221,7 +221,7 @@ TEST_F(CELFormatterTest, TestFormatTimestampValue) {
}

TEST_F(CELFormatterTest, TestFormatBytesValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "bytes(\"hello\")", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -233,7 +233,7 @@ TEST_F(CELFormatterTest, TestFormatBytesValue) {
}

TEST_F(CELFormatterTest, TestFormatListValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "[\"foo\", 42, true]", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -247,7 +247,7 @@ TEST_F(CELFormatterTest, TestFormatListValue) {
}

TEST_F(CELFormatterTest, TestFormatMapValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "{\"foo\": \"42\"}", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -265,7 +265,7 @@ TEST_F(CELFormatterTest, TestFormatMapValue) {
}

TEST_F(CELFormatterTest, TestTruncation) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = 2;
auto formatter = cel_parser->parse("CEL", "request.headers[':method']", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
Expand All @@ -277,22 +277,22 @@ TEST_F(CELFormatterTest, TestTruncation) {
}

TEST_F(CELFormatterTest, TestParseFail) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
EXPECT_EQ(nullptr,
cel_parser->parse("INVALID_CMD", "requests.headers['missing_headers']", max_length));
}

TEST_F(CELFormatterTest, TestNullFormatValue) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;
auto formatter = cel_parser->parse("CEL", "requests.headers['missing_headers']", max_length);
EXPECT_THAT(formatter->formatValue(formatter_context_, stream_info_),
ProtoEq(ValueUtil::nullValue()));
}

TEST_F(CELFormatterTest, TestFormatConversionV1AlphaToDevCel) {
auto cel_parser = std::make_unique<CELFormatterCommandParser>();
auto cel_parser = std::make_unique<CELFormatterCommandParser>(context_.server_factory_context_);
absl::optional<size_t> max_length = absl::nullopt;

// Test with a basic path expression
Expand Down
Loading