Skip to content

Commit

Permalink
auto-merge envoyproxy/envoy[main] into envoyproxy/envoy-openssl[main]
Browse files Browse the repository at this point in the history
* upstream/main:
  formatter: removing exceptions from substitution format string (#36168)
  route: use reference wrapper for get all filter config (#36079)
  • Loading branch information
sync-envoy[bot] committed Sep 17, 2024
2 parents 6a1f8d7 + 634f71e commit 45ee470
Show file tree
Hide file tree
Showing 30 changed files with 193 additions and 177 deletions.
11 changes: 4 additions & 7 deletions contrib/golang/filters/http/source/golang_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1377,16 +1377,13 @@ void Filter::deferredDeleteRequest(HttpRequestInternal* req) {
uint64_t Filter::getMergedConfigId() {
Http::StreamFilterCallbacks* callbacks = decoding_state_.getFilterCallbacks();

auto id = config_->getConfigId();

// get all of the per route config
auto route_config_list = Http::Utility::getAllPerFilterConfig<FilterConfigPerRoute>(callbacks);

ENVOY_LOG(debug, "golang filter route config list length: {}.", route_config_list.size());

auto id = config_->getConfigId();
for (auto it : route_config_list) {
ASSERT(it != nullptr, "route config should not be null");
auto route_config = *it;
id = route_config.getPluginConfigId(id, config_->pluginName());
for (const FilterConfigPerRoute& typed_config : route_config_list) {
id = typed_config.getPluginConfigId(id, config_->pluginName());
}

return id;
Expand Down
24 changes: 13 additions & 11 deletions source/common/formatter/substitution_format_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ class SubstitutionFormatStringUtils {
* Parse list of formatter configurations to commands.
*/
template <class FormatterContext = HttpFormatterContext>
static std::vector<CommandParserBasePtr<FormatterContext>>
static absl::StatusOr<std::vector<CommandParserBasePtr<FormatterContext>>>
parseFormatters(const FormattersConfig& formatters,
Server::Configuration::GenericFactoryContext& context) {
std::vector<CommandParserBasePtr<FormatterContext>> commands;
for (const auto& formatter : formatters) {
auto* factory =
Envoy::Config::Utility::getFactory<CommandParserFactoryBase<FormatterContext>>(formatter);
if (!factory) {
throwEnvoyExceptionOrPanic(absl::StrCat("Formatter not found: ", formatter.name()));
return absl::InvalidArgumentError(absl::StrCat("Formatter not found: ", formatter.name()));
}
auto typed_config = Envoy::Config::Utility::translateAnyToFactoryConfig(
formatter.typed_config(), context.messageValidationVisitor(), *factory);
auto parser = factory->createCommandParserFromProto(*typed_config, context);
if (!parser) {
throwEnvoyExceptionOrPanic(
return absl::InvalidArgumentError(
absl::StrCat("Failed to create command parser: ", formatter.name()));
}
commands.push_back(std::move(parser));
Expand All @@ -56,26 +56,28 @@ class SubstitutionFormatStringUtils {
* Generate a formatter object from config SubstitutionFormatString.
*/
template <class FormatterContext = HttpFormatterContext>
static FormatterBasePtr<FormatterContext>
static absl::StatusOr<FormatterBasePtr<FormatterContext>>
fromProtoConfig(const envoy::config::core::v3::SubstitutionFormatString& config,
Server::Configuration::GenericFactoryContext& context) {
// Instantiate formatter extensions.
auto commands = parseFormatters<FormatterContext>(config.formatters(), context);
RETURN_IF_NOT_OK_REF(commands.status());
switch (config.format_case()) {
case envoy::config::core::v3::SubstitutionFormatString::FormatCase::kTextFormat:
return std::make_unique<FormatterBaseImpl<FormatterContext>>(
config.text_format(), config.omit_empty_values(), commands);
config.text_format(), config.omit_empty_values(), *commands);
case envoy::config::core::v3::SubstitutionFormatString::FormatCase::kJsonFormat:
return createJsonFormatter<FormatterContext>(
config.json_format(), true, config.omit_empty_values(),
config.has_json_format_options() ? config.json_format_options().sort_properties() : false,
commands);
case envoy::config::core::v3::SubstitutionFormatString::FormatCase::kTextFormatSource:
*commands);
case envoy::config::core::v3::SubstitutionFormatString::FormatCase::kTextFormatSource: {
auto data_source_or_error = Config::DataSource::read(config.text_format_source(), true,
context.serverFactoryContext().api());
RETURN_IF_NOT_OK(data_source_or_error.status());
return std::make_unique<FormatterBaseImpl<FormatterContext>>(
THROW_OR_RETURN_VALUE(Config::DataSource::read(config.text_format_source(), true,
context.serverFactoryContext().api()),
std::string),
config.omit_empty_values(), commands);
*data_source_or_error, config.omit_empty_values(), *commands);
}
case envoy::config::core::v3::SubstitutionFormatString::FormatCase::FORMAT_NOT_SET:
PANIC_DUE_TO_PROTO_UNSET;
}
Expand Down
7 changes: 4 additions & 3 deletions source/common/http/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -572,19 +573,19 @@ const ConfigType* resolveMostSpecificPerFilterConfig(const Http::StreamFilterCal
* and their lifetime is the same as the matched route.
*/
template <class ConfigType>
absl::InlinedVector<const ConfigType*, 4>
absl::InlinedVector<std::reference_wrapper<const ConfigType>, 4>
getAllPerFilterConfig(const Http::StreamFilterCallbacks* callbacks) {
ASSERT(callbacks != nullptr);

absl::InlinedVector<const ConfigType*, 4> all_configs;
absl::InlinedVector<std::reference_wrapper<const ConfigType>, 4> all_configs;

for (const auto* config : callbacks->perFilterConfigs()) {
const ConfigType* typed_config = dynamic_cast<const ConfigType*>(config);
if (typed_config == nullptr) {
ENVOY_LOG_MISC(debug, "Failed to retrieve the correct type of route specific filter config");
continue;
}
all_configs.push_back(typed_config);
all_configs.push_back(*typed_config);
}

return all_configs;
Expand Down
4 changes: 3 additions & 1 deletion source/common/local_reply/local_reply.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class BodyFormatter {

BodyFormatter(const envoy::config::core::v3::SubstitutionFormatString& config,
Server::Configuration::GenericFactoryContext& context)
: formatter_(Formatter::SubstitutionFormatStringUtils::fromProtoConfig(config, context)),
: formatter_(THROW_OR_RETURN_VALUE(
Formatter::SubstitutionFormatStringUtils::fromProtoConfig(config, context),
Formatter::FormatterBasePtr<Formatter::HttpFormatterContext>)),
content_type_(
!config.content_type().empty() ? config.content_type()
: config.format_case() ==
Expand Down
6 changes: 4 additions & 2 deletions source/common/tcp_proxy/tcp_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,10 @@ TunnelingConfigHelperImpl::TunnelingConfigHelperImpl(
envoy::config::core::v3::SubstitutionFormatString substitution_format_config;
substitution_format_config.mutable_text_format_source()->set_inline_string(
config_message.tunneling_config().hostname());
hostname_fmt_ = Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
substitution_format_config, context);
hostname_fmt_ =
THROW_OR_RETURN_VALUE(Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
substitution_format_config, context),
Formatter::FormatterBasePtr<Formatter::HttpFormatterContext>);
}

std::string TunnelingConfigHelperImpl::host(const StreamInfo::StreamInfo& stream_info) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ createStreamAccessLogInstance(const Protobuf::Message& config, AccessLog::Filter
MessageUtil::downcastAndValidate<const T&>(config, context.messageValidationVisitor());
Formatter::FormatterPtr formatter;
if (fal_config.access_log_format_case() == T::AccessLogFormatCase::kLogFormat) {
formatter =
Formatter::SubstitutionFormatStringUtils::fromProtoConfig(fal_config.log_format(), context);
formatter = THROW_OR_RETURN_VALUE(
Formatter::SubstitutionFormatStringUtils::fromProtoConfig(fal_config.log_format(), context),
Formatter::FormatterBasePtr<Formatter::HttpFormatterContext>);
} else if (fal_config.access_log_format_case() ==
T::AccessLogFormatCase::ACCESS_LOG_FORMAT_NOT_SET) {
formatter = Formatter::HttpSubstitutionFormatUtils::defaultSubstitutionFormatter();
Expand Down
13 changes: 9 additions & 4 deletions source/extensions/access_loggers/file/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ FileAccessLogFactory::createAccessLogInstance(const Protobuf::Message& config,
} else {
envoy::config::core::v3::SubstitutionFormatString sff_config;
sff_config.mutable_text_format_source()->set_inline_string(fal_config.format());
formatter = Formatter::SubstitutionFormatStringUtils::fromProtoConfig(sff_config, context);
formatter = THROW_OR_RETURN_VALUE(
Formatter::SubstitutionFormatStringUtils::fromProtoConfig(sff_config, context),
Formatter::FormatterBasePtr<Formatter::HttpFormatterContext>);
}
break;
case envoy::extensions::access_loggers::file::v3::FileAccessLog::AccessLogFormatCase::kJsonFormat:
Expand All @@ -46,12 +48,15 @@ FileAccessLogFactory::createAccessLogInstance(const Protobuf::Message& config,
kTypedJsonFormat: {
envoy::config::core::v3::SubstitutionFormatString sff_config;
*sff_config.mutable_json_format() = fal_config.typed_json_format();
formatter = Formatter::SubstitutionFormatStringUtils::fromProtoConfig(sff_config, context);
formatter = THROW_OR_RETURN_VALUE(
Formatter::SubstitutionFormatStringUtils::fromProtoConfig(sff_config, context),
Formatter::FormatterBasePtr<Formatter::HttpFormatterContext>);
break;
}
case envoy::extensions::access_loggers::file::v3::FileAccessLog::AccessLogFormatCase::kLogFormat:
formatter =
Formatter::SubstitutionFormatStringUtils::fromProtoConfig(fal_config.log_format(), context);
formatter = THROW_OR_RETURN_VALUE(
Formatter::SubstitutionFormatStringUtils::fromProtoConfig(fal_config.log_format(), context),
Formatter::FormatterBasePtr<Formatter::HttpFormatterContext>);
break;
case envoy::extensions::access_loggers::file::v3::FileAccessLog::AccessLogFormatCase::
ACCESS_LOG_FORMAT_NOT_SET:
Expand Down
6 changes: 4 additions & 2 deletions source/extensions/access_loggers/fluentd/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ FluentdAccessLogFactory::createAccessLogInstance(const Protobuf::Message& config
// payload.
// TODO(ohadvano): Improve the formatting operation by creating a dedicated formatter that
// will directly serialize the record to msgpack payload.
auto commands =
Formatter::SubstitutionFormatStringUtils::parseFormatters(proto_config.formatters(), context);
auto commands = THROW_OR_RETURN_VALUE(
Formatter::SubstitutionFormatStringUtils::parseFormatters(proto_config.formatters(), context),
std::vector<Formatter::CommandParserBasePtr<Formatter::HttpFormatterContext>>);

Formatter::FormatterPtr json_formatter =
Formatter::SubstitutionFormatStringUtils::createJsonFormatter(proto_config.record(), true,
false, false, commands);
Expand Down
5 changes: 3 additions & 2 deletions source/extensions/access_loggers/open_telemetry/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ AccessLogFactory::createAccessLogInstance(const Protobuf::Message& config,
const envoy::extensions::access_loggers::open_telemetry::v3::OpenTelemetryAccessLogConfig&>(
config, context.messageValidationVisitor());

auto commands =
Formatter::SubstitutionFormatStringUtils::parseFormatters(proto_config.formatters(), context);
auto commands = THROW_OR_RETURN_VALUE(
Formatter::SubstitutionFormatStringUtils::parseFormatters(proto_config.formatters(), context),
std::vector<Formatter::CommandParserBasePtr<Formatter::HttpFormatterContext>>);

return std::make_shared<AccessLog>(
std::move(filter), proto_config, context.serverFactoryContext().threadLocal(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ Config::parse(const Protobuf::RepeatedPtrField<FilterStateValueProto>& proto_val
break;
}
value.skip_if_empty_ = proto_value.skip_if_empty();
value.value_ = Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
proto_value.format_string(), context);
value.value_ =
THROW_OR_RETURN_VALUE(Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
proto_value.format_string(), context),
Formatter::FormatterBasePtr<Formatter::HttpFormatterContext>);
values.push_back(std::move(value));
}
return values;
Expand Down
93 changes: 46 additions & 47 deletions source/extensions/filters/http/cors/cors_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,17 @@ void CorsFilter::initializeCorsPolicies() {
// If no cors policy is configured in the per filter config, then the cors policy fields in the
// route configuration will be ignored.
if (policies_.empty()) {
policies_ = {
decoder_callbacks_->route()->routeEntry()->corsPolicy(),
decoder_callbacks_->route()->virtualHost().corsPolicy(),
};
const auto route = decoder_callbacks_->route();
ASSERT(route != nullptr);
ASSERT(route->routeEntry() != nullptr);

if (auto* typed_cfg = route->routeEntry()->corsPolicy(); typed_cfg != nullptr) {
policies_.push_back(*typed_cfg);
}

if (auto* typed_cfg = route->virtualHost().corsPolicy(); typed_cfg != nullptr) {
policies_.push_back(*typed_cfg);
}
}
}

Expand Down Expand Up @@ -204,104 +211,96 @@ void CorsFilter::setDecoderFilterCallbacks(Http::StreamDecoderFilterCallbacks& c
}

bool CorsFilter::isOriginAllowed(const Http::HeaderString& origin) {
const auto allow_origins = allowOrigins();
if (allow_origins == nullptr) {
return false;
}
for (const auto& allow_origin : *allow_origins) {
for (const auto& allow_origin : allowOrigins()) {
if (allow_origin->match("*") || allow_origin->match(origin.getStringView())) {
return true;
}
}
return false;
}

const std::vector<Matchers::StringMatcherPtr>* CorsFilter::allowOrigins() {
for (const auto policy : policies_) {
if (policy && !policy->allowOrigins().empty()) {
return &policy->allowOrigins();
absl::Span<const Matchers::StringMatcherPtr> CorsFilter::allowOrigins() {
for (const Router::CorsPolicy& policy : policies_) {
if (!policy.allowOrigins().empty()) {
return policy.allowOrigins();
}
}
return nullptr;
return {};
}

bool CorsFilter::forwardNotMatchingPreflights() {
for (const auto policy : policies_) {
if (policy && policy->forwardNotMatchingPreflights()) {
return policy->forwardNotMatchingPreflights().value();
for (const Router::CorsPolicy& policy : policies_) {
if (policy.forwardNotMatchingPreflights()) {
return policy.forwardNotMatchingPreflights().value();
}
}
return true;
}

const std::string& CorsFilter::allowMethods() {
for (const auto policy : policies_) {
if (policy && !policy->allowMethods().empty()) {
return policy->allowMethods();
absl::string_view CorsFilter::allowMethods() {
for (const Router::CorsPolicy& policy : policies_) {
if (!policy.allowMethods().empty()) {
return policy.allowMethods();
}
}
return EMPTY_STRING;
}

const std::string& CorsFilter::allowHeaders() {
for (const auto policy : policies_) {
if (policy && !policy->allowHeaders().empty()) {
return policy->allowHeaders();
absl::string_view CorsFilter::allowHeaders() {
for (const Router::CorsPolicy& policy : policies_) {
if (!policy.allowHeaders().empty()) {
return policy.allowHeaders();
}
}
return EMPTY_STRING;
}

const std::string& CorsFilter::exposeHeaders() {
for (const auto policy : policies_) {
if (policy && !policy->exposeHeaders().empty()) {
return policy->exposeHeaders();
absl::string_view CorsFilter::exposeHeaders() {
for (const Router::CorsPolicy& policy : policies_) {
if (!policy.exposeHeaders().empty()) {
return policy.exposeHeaders();
}
}
return EMPTY_STRING;
}

const std::string& CorsFilter::maxAge() {
for (const auto policy : policies_) {
if (policy && !policy->maxAge().empty()) {
return policy->maxAge();
absl::string_view CorsFilter::maxAge() {
for (const Router::CorsPolicy& policy : policies_) {
if (!policy.maxAge().empty()) {
return policy.maxAge();
}
}
return EMPTY_STRING;
}

bool CorsFilter::allowCredentials() {
for (const auto policy : policies_) {
if (policy && policy->allowCredentials()) {
return policy->allowCredentials().value();
for (const Router::CorsPolicy& policy : policies_) {
if (policy.allowCredentials()) {
return policy.allowCredentials().value();
}
}
return false;
}

bool CorsFilter::allowPrivateNetworkAccess() {
for (const auto policy : policies_) {
if (policy && policy->allowPrivateNetworkAccess()) {
return policy->allowPrivateNetworkAccess().value();
for (const Router::CorsPolicy& policy : policies_) {
if (policy.allowPrivateNetworkAccess()) {
return policy.allowPrivateNetworkAccess().value();
}
}
return false;
}

bool CorsFilter::shadowEnabled() {
for (const auto policy : policies_) {
if (policy) {
return policy->shadowEnabled();
}
for (const Router::CorsPolicy& policy : policies_) {
return policy.shadowEnabled();
}
return false;
}

bool CorsFilter::enabled() {
for (const auto policy : policies_) {
if (policy) {
return policy->enabled();
}
for (const Router::CorsPolicy& policy : policies_) {
return policy.enabled();
}
return false;
}
Expand Down
Loading

0 comments on commit 45ee470

Please sign in to comment.