Skip to content

Commit

Permalink
test_utils: add config modifier for UDP proxy (envoyproxy#35725)
Browse files Browse the repository at this point in the history
Additional Description: Adding ``addConfigModifier`` testing utility for
UdpProxy configurations. This will be used in a separate PR, but wanted
to separate this to another so it's easier to review
Risk Level: low
Testing: none
Docs Changes: none
Release Notes: none
Platform Specific Features: none
Signed-off-by: ohadvano <[email protected]>
  • Loading branch information
ohadvano authored Aug 20, 2024
1 parent e6b1c8b commit 6dcd221
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ envoy_cc_test_library(
"@envoy_api//envoy/extensions/access_loggers/file/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/http/upstream_codec/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/filters/udp/udp_proxy/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/transport_sockets/quic/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/transport_sockets/tls/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/upstreams/http/v3:pkg_cc_proto",
Expand Down
34 changes: 34 additions & 0 deletions test/config/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,21 @@ envoy::config::listener::v3::Filter* ConfigHelper::getFilterFromListener(const s
return nullptr;
}

envoy::config::listener::v3::ListenerFilter*
ConfigHelper::getListenerFilterFromListener(const std::string& name) {
RELEASE_ASSERT(!finalized_, "");
if (bootstrap_.mutable_static_resources()->listeners_size() == 0) {
return nullptr;
}
auto* listener = bootstrap_.mutable_static_resources()->mutable_listeners(0);
for (ssize_t i = 0; i < listener->listener_filters_size(); i++) {
if (listener->mutable_listener_filters(i)->name() == name) {
return listener->mutable_listener_filters(i);
}
}
return nullptr;
}

void ConfigHelper::addNetworkFilter(const std::string& filter_yaml) {
RELEASE_ASSERT(!finalized_, "");
auto* filter_chain =
Expand Down Expand Up @@ -1639,6 +1654,14 @@ void ConfigHelper::storeHttpConnectionManager(
"http", hcm);
}

bool ConfigHelper::loadUdpProxyFilter(UdpProxyConfig& udp_proxy) {
return loadListenerFilter<UdpProxyConfig>("udp_proxy", udp_proxy);
}

void ConfigHelper::storeUdpProxyFilter(const UdpProxyConfig& udp_proxy) {
return storeListenerFilter<UdpProxyConfig>("udp_proxy", udp_proxy);
}

void ConfigHelper::addConfigModifier(ConfigModifierFunction function) {
RELEASE_ASSERT(!finalized_, "");
config_modifiers_.push_back(std::move(function));
Expand All @@ -1656,6 +1679,17 @@ void ConfigHelper::addConfigModifier(HttpModifierFunction function) {
});
}

void ConfigHelper::addConfigModifier(UdpProxyModifierFunction function) {
addConfigModifier([function, this](envoy::config::bootstrap::v3::Bootstrap&) -> void {
UdpProxyConfig udp_proxy_config;
if (!loadUdpProxyFilter(udp_proxy_config)) {
return;
}
function(udp_proxy_config);
storeUdpProxyFilter(udp_proxy_config);
});
}

void ConfigHelper::setLds(absl::string_view version_info) {
applyConfigModifiers();

Expand Down
39 changes: 39 additions & 0 deletions test/config/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "envoy/config/listener/v3/listener_components.pb.h"
#include "envoy/config/route/v3/route_components.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "envoy/extensions/filters/udp/udp_proxy/v3/udp_proxy.pb.h"
#include "envoy/extensions/transport_sockets/tls/v3/cert.pb.h"
#include "envoy/extensions/transport_sockets/tls/v3/common.pb.h"
#include "envoy/extensions/upstreams/http/v3/http_protocol_options.pb.h"
Expand All @@ -34,6 +35,8 @@ class ConfigHelper {
public:
using HttpConnectionManager =
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager;
using UdpProxyConfig = envoy::extensions::filters::udp::udp_proxy::v3::UdpProxyConfig;

struct ServerSslOptions {
ServerSslOptions& setAllowExpiredCertificate(bool allow) {
allow_expired_certificate_ = allow;
Expand Down Expand Up @@ -185,6 +188,7 @@ class ConfigHelper {
const ServerSslOptions& options);
using ConfigModifierFunction = std::function<void(envoy::config::bootstrap::v3::Bootstrap&)>;
using HttpModifierFunction = std::function<void(HttpConnectionManager&)>;
using UdpProxyModifierFunction = std::function<void(UdpProxyConfig&)>;

// A basic configuration (admin port, cluster_0, no listeners) with no network filters.
static std::string baseConfigNoListeners();
Expand Down Expand Up @@ -377,6 +381,10 @@ class ConfigHelper {
// Modifiers will be applied just before ports are modified in finalize
void addConfigModifier(HttpModifierFunction function);

// Allows callers to easily modify the UDP Proxy configuration.
// Modifiers will be applied just before ports are modified in finalize
void addConfigModifier(UdpProxyModifierFunction function);

// Allows callers to easily modify the filter named 'name' from the first filter chain from the
// first listener. Modifiers will be applied just before ports are modified in finalize
template <class FilterType>
Expand Down Expand Up @@ -462,6 +470,11 @@ class ConfigHelper {
// Take the contents of the provided HCM proto and stuff them into the first HCM
// struct of the first listener.
void storeHttpConnectionManager(const HttpConnectionManager& hcm);
// Load the first UDP Proxy from the first listener into a parsed proto.
bool loadUdpProxyFilter(UdpProxyConfig& udp_proxy);
// Take the contents of the provided UDP Proxy and stuff them into the first HCM
// struct of the first listener.
void storeUdpProxyFilter(const UdpProxyConfig& udp_proxy);

private:
// Load the first FilterType struct from the first listener into a parsed proto.
Expand All @@ -484,9 +497,35 @@ class ConfigHelper {
filter_config_any->PackFrom(filter);
}

// Load the first FilterType struct from the first listener filters into a parsed proto.
template <class FilterType> bool loadListenerFilter(const std::string& name, FilterType& filter) {
RELEASE_ASSERT(!finalized_, "");
auto* filter_config = getListenerFilterFromListener(name);
if (filter_config) {
auto* config = filter_config->mutable_typed_config();
filter = MessageUtil::anyConvert<FilterType>(*config);
return true;
}
return false;
}

// Take the contents of the provided FilterType proto and stuff them into the first FilterType
// struct of the first listener.
template <class FilterType>
void storeListenerFilter(const std::string& name, const FilterType& filter) {
RELEASE_ASSERT(!finalized_, "");
auto* filter_config_any = getListenerFilterFromListener(name)->mutable_typed_config();

filter_config_any->PackFrom(filter);
}

// Finds the filter named 'name' from the first filter chain from the first listener.
envoy::config::listener::v3::Filter* getFilterFromListener(const std::string& name);

// Finds the filter named 'name' from the first listener filter from the first listener.
envoy::config::listener::v3::ListenerFilter*
getListenerFilterFromListener(const std::string& name);

// The bootstrap proto Envoy will start up with.
envoy::config::bootstrap::v3::Bootstrap bootstrap_;

Expand Down

0 comments on commit 6dcd221

Please sign in to comment.