Skip to content
Open
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
23 changes: 8 additions & 15 deletions source/extensions/filters/network/generic_proxy/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ envoy_cc_library(
":route_lib",
":stats_lib",
":tracing_lib",
"//envoy/access_log:access_log_interface",
"//envoy/network:filter_interface",
"//envoy/server:factory_context_interface",
"//envoy/stats:timespan_interface",
"//source/common/common:linked_object",
"//source/common/common:minimal_logger_lib",
"//source/common/formatter:substitution_formatter_lib",
"//source/common/stats:timespan_lib",
"//source/common/stream_info:stream_info_lib",
"//source/common/tracing:tracer_config_lib",
Expand Down Expand Up @@ -146,20 +148,6 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "file_access_log_lib",
hdrs = [
"file_access_log.h",
],
deps = [
"//envoy/access_log:access_log_config_interface",
"//envoy/access_log:access_log_interface",
"//source/common/common:utility_lib",
"//source/common/formatter:substitution_format_string_lib",
"@envoy_api//envoy/extensions/access_loggers/file/v3:pkg_cc_proto",
],
)

envoy_cc_library(
name = "access_log_lib",
srcs = [
Expand All @@ -169,8 +157,12 @@ envoy_cc_library(
"access_log.h",
],
deps = [
":file_access_log_lib",
"//envoy/access_log:access_log_config_interface",
"//envoy/formatter:substitution_formatter_interface",
"//source/common/config:utility_lib",
"//source/extensions/filters/network/generic_proxy/interface:stream_interface",
"@envoy_api//envoy/extensions/access_loggers/file/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/network/generic_proxy/v3:pkg_cc_proto",
],
# Ensure this factory in the source is always linked in.
alwayslink = 1,
Expand Down Expand Up @@ -231,6 +223,7 @@ envoy_cc_library(
],
deps = [
":route_interface",
"//envoy/access_log:access_log_config_interface",
"//envoy/tracing:trace_config_interface",
"//envoy/tracing:tracer_interface",
"//source/extensions/filters/network/generic_proxy:access_log_lib",
Expand Down
71 changes: 24 additions & 47 deletions source/extensions/filters/network/generic_proxy/access_log.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "source/extensions/filters/network/generic_proxy/access_log.h"

#include "envoy/extensions/filters/network/generic_proxy/v3/generic_proxy.pb.h"
#include "envoy/registry/registry.h"

#include "source/common/config/utility.h"

namespace Envoy {
namespace Extensions {
namespace NetworkFilters {
Expand Down Expand Up @@ -29,26 +32,20 @@ ProtobufWkt::Value StringValueFormatterProvider::formatValueWithContext(
absl::optional<std::string>
GenericStatusCodeFormatterProvider::formatWithContext(const FormatterContext& context,
const StreamInfo::StreamInfo&) const {
if (context.response_ == nullptr) {
return absl::nullopt;
}

const int code = context.response_->status().code();
CHECK_DATA_OR_RETURN(context, response_, absl::nullopt);
const int code = checked_data->response_->status().code();
return std::to_string(code);
}

ProtobufWkt::Value
GenericStatusCodeFormatterProvider::formatValueWithContext(const FormatterContext& context,
const StreamInfo::StreamInfo&) const {
if (context.response_ == nullptr) {
return ValueUtil::nullValue();
}

const int code = context.response_->status().code();
CHECK_DATA_OR_RETURN(context, response_, ValueUtil::nullValue());
const int code = checked_data->response_->status().code();
return ValueUtil::numberValue(code);
}

class SimpleCommandParser : public CommandParser {
class GenericProxyCommandParser : public Formatter::CommandParser {
public:
using ProviderFunc =
std::function<FormatterProviderPtr(absl::string_view, absl::optional<size_t> max_length)>;
Expand All @@ -75,43 +72,35 @@ class SimpleCommandParser : public CommandParser {
return std::make_unique<StringValueFormatterProvider>(
[](const FormatterContext& context,
const StreamInfo::StreamInfo&) -> absl::optional<std::string> {
if (context.request_) {
return std::string(context.request_->method());
}
return absl::nullopt;
CHECK_DATA_OR_RETURN(context, request_, absl::nullopt);
return std::string(checked_data->request_->method());
});
}},
{"HOST",
[](absl::string_view, absl::optional<size_t>) -> FormatterProviderPtr {
return std::make_unique<StringValueFormatterProvider>(
[](const FormatterContext& context,
const StreamInfo::StreamInfo&) -> absl::optional<std::string> {
if (context.request_) {
return std::string(context.request_->host());
}
return absl::nullopt;
CHECK_DATA_OR_RETURN(context, request_, absl::nullopt);
return std::string(checked_data->request_->host());
});
}},
{"PATH",
[](absl::string_view, absl::optional<size_t>) -> FormatterProviderPtr {
return std::make_unique<StringValueFormatterProvider>(
[](const FormatterContext& context,
const StreamInfo::StreamInfo&) -> absl::optional<std::string> {
if (context.request_) {
return std::string(context.request_->path());
}
return absl::nullopt;
CHECK_DATA_OR_RETURN(context, request_, absl::nullopt);
return std::string(checked_data->request_->path());
});
}},
{"PROTOCOL",
[](absl::string_view, absl::optional<size_t>) -> FormatterProviderPtr {
return std::make_unique<StringValueFormatterProvider>(
[](const FormatterContext& context,
const StreamInfo::StreamInfo&) -> absl::optional<std::string> {
if (context.request_) {
return std::string(context.request_->protocol());
}
return absl::nullopt;
CHECK_DATA_OR_RETURN(context, request_, absl::nullopt);
return std::string(checked_data->request_->protocol());
});
}},
{"REQUEST_PROPERTY",
Expand All @@ -120,10 +109,9 @@ class SimpleCommandParser : public CommandParser {
[key = std::string(command_arg)](
const FormatterContext& context,
const StreamInfo::StreamInfo&) -> absl::optional<std::string> {
if (!context.request_) {
return absl::nullopt;
}
auto optional_view = context.request_->get(key);
CHECK_DATA_OR_RETURN(context, request_, absl::nullopt);

auto optional_view = checked_data->request_->get(key);
if (!optional_view.has_value()) {
return absl::nullopt;
}
Expand All @@ -136,10 +124,8 @@ class SimpleCommandParser : public CommandParser {
[key = std::string(command_arg)](
const FormatterContext& context,
const StreamInfo::StreamInfo&) -> absl::optional<std::string> {
if (!context.response_) {
return absl::nullopt;
}
auto optional_view = context.response_->get(key);
CHECK_DATA_OR_RETURN(context, response_, absl::nullopt);
auto optional_view = checked_data->response_->get(key);
if (!optional_view.has_value()) {
return absl::nullopt;
}
Expand All @@ -154,18 +140,9 @@ class SimpleCommandParser : public CommandParser {
}
};

class DefaultBuiltInCommandParserFactory : public BuiltInCommandParserFactory {
public:
std::string name() const override { return "envoy.built_in_formatters.generic_poxy.default"; }

// BuiltInCommandParserFactory
CommandParserPtr createCommandParser() const override {
return std::make_unique<SimpleCommandParser>();
}
};

REGISTER_FACTORY(DefaultBuiltInCommandParserFactory, BuiltInCommandParserFactory);
REGISTER_FACTORY(FileAccessLogFactory, AccessLogInstanceFactory);
Formatter::CommandParserPtr createGenericProxyCommandParser() {
return std::make_unique<GenericProxyCommandParser>();
}

} // namespace GenericProxy
} // namespace NetworkFilters
Expand Down
43 changes: 19 additions & 24 deletions source/extensions/filters/network/generic_proxy/access_log.h
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
#pragma once

#include "source/extensions/filters/network/generic_proxy/file_access_log.h"
#include "envoy/access_log/access_log_config.h"
#include "envoy/extensions/access_loggers/file/v3/file.pb.h"
#include "envoy/formatter/substitution_formatter.h"

#include "source/extensions/filters/network/generic_proxy/interface/stream.h"

namespace Envoy {
namespace Extensions {
namespace NetworkFilters {
namespace GenericProxy {

struct FormatterContext {
struct FormatterContextExtension : public Formatter::Context::Extension {
FormatterContextExtension(const RequestHeaderFrame* request, const ResponseHeaderFrame* response)
: request_(request), response_(response) {}
FormatterContextExtension() = default;

const RequestHeaderFrame* request_{};
const ResponseHeaderFrame* response_{};

static constexpr absl::string_view category() { return "generic_proxy"; }
};

// Formatter for generic proxy.
using Formatter = Formatter::FormatterBase<FormatterContext>;
using FormatterProvider = Envoy::Formatter::FormatterProviderBase<FormatterContext>;
using FormatterProviderPtr = std::unique_ptr<FormatterProvider>;
using CommandParser = Envoy::Formatter::CommandParserBase<FormatterContext>;
using CommandParserPtr = Envoy::Formatter::CommandParserBasePtr<FormatterContext>;
using CommandParserFactory = Envoy::Formatter::CommandParserFactoryBase<FormatterContext>;
using BuiltInCommandParserFactory =
Envoy::Formatter::BuiltInCommandParserFactoryBase<FormatterContext>;
#define CHECK_DATA_OR_RETURN(context, field, return_value) \
const auto checked_data = context.typedExtension<FormatterContextExtension>(); \
if (!checked_data.has_value() || checked_data->field == nullptr) { \
return return_value; \
}

// Access log for generic proxy.
using AccessLogFilter = AccessLog::FilterBase<FormatterContext>;
using AccessLogFilterPtr = std::unique_ptr<AccessLogFilter>;
using AccessLogFilterFactory = AccessLog::ExtensionFilterFactoryBase<FormatterContext>;
using AccessLogInstance = AccessLog::InstanceBase<FormatterContext>;
using AccessLogInstanceSharedPtr = std::shared_ptr<AccessLogInstance>;
using AccessLogInstanceFactory = AccessLog::AccessLogInstanceFactoryBase<FormatterContext>;

// File access log for generic proxy.
using FileAccessLog = FileAccessLogBase<FormatterContext>;
using FileAccessLogFactory = FileAccessLogFactoryBase<FormatterContext>;
using FormatterProvider = Formatter::FormatterProvider;
using FormatterProviderPtr = std::unique_ptr<FormatterProvider>;
using FormatterContext = Formatter::Context;

class StringValueFormatterProvider : public FormatterProvider {
public:
Expand Down Expand Up @@ -69,6 +62,8 @@ class GenericStatusCodeFormatterProvider : public FormatterProvider {
const StreamInfo::StreamInfo&) const override;
};

Formatter::CommandParserPtr createGenericProxyCommandParser();

} // namespace GenericProxy
} // namespace NetworkFilters
} // namespace Extensions
Expand Down
43 changes: 6 additions & 37 deletions source/extensions/filters/network/generic_proxy/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,15 @@

#include "source/common/access_log/access_log_impl.h"
#include "source/common/tracing/tracer_manager_impl.h"
#include "source/extensions/filters/network/generic_proxy/access_log.h"
#include "source/extensions/filters/network/generic_proxy/rds.h"
#include "source/extensions/filters/network/generic_proxy/rds_impl.h"

#include "access_log.h"

namespace Envoy {
namespace Extensions {
namespace NetworkFilters {
namespace GenericProxy {

template <class Context>
static AccessLog::FilterBasePtr<Context>
accessLogFilterFromProto(const envoy::config::accesslog::v3::AccessLogFilter& config,
Server::Configuration::FactoryContext& context) {
if (!config.has_extension_filter()) {
ExceptionUtil::throwEnvoyException(
"Access log filter: only extension filter is supported by non-HTTP access loggers.");
}

auto& factory =
Config::Utility::getAndCheckFactory<Envoy::AccessLog::ExtensionFilterFactoryBase<Context>>(
config.extension_filter());
return factory.createFilter(config.extension_filter(), context);
}

template <class Context>
static AccessLog::InstanceBaseSharedPtr<Context>
accessLoggerFromProto(const envoy::config::accesslog::v3::AccessLog& config,
Server::Configuration::FactoryContext& context) {
AccessLog::FilterBasePtr<Context> filter;
if (config.has_filter()) {
filter = accessLogFilterFromProto<Context>(config.filter(), context);
}

auto& factory =
Config::Utility::getAndCheckFactory<Envoy::AccessLog::AccessLogInstanceFactoryBase<Context>>(
config);
ProtobufTypes::MessagePtr message = Config::Utility::translateToFactoryConfig(
config, context.messageValidationVisitor(), factory);

return factory.createAccessLogInstance(*message, std::move(filter), context);
}
SINGLETON_MANAGER_REGISTRATION(generic_route_config_provider_manager);
SINGLETON_MANAGER_REGISTRATION(generic_proxy_code_or_flag_stats);

Expand Down Expand Up @@ -156,10 +123,12 @@ Factory::createFilterFactoryFromProtoTyped(const ProxyConfig& proto_config,
}

// Access log configuration.
std::vector<AccessLogInstanceSharedPtr> access_logs;
std::vector<AccessLog::InstanceSharedPtr> access_logs;
for (const auto& access_log : proto_config.access_log()) {
AccessLogInstanceSharedPtr current_access_log =
accessLoggerFromProto<FormatterContext>(access_log, context);
std::vector<Formatter::CommandParserPtr> command_parsers;
command_parsers.push_back(createGenericProxyCommandParser());
AccessLog::InstanceSharedPtr current_access_log =
AccessLog::AccessLogFactory::fromProto(access_log, context, std::move(command_parsers));
access_logs.push_back(current_access_log);
}

Expand Down
Loading