Skip to content

Commit

Permalink
factory context refactor: refactor the transport factory context (env…
Browse files Browse the repository at this point in the history
…oyproxy#27081)


Signed-off-by: wbpcode <[email protected]>
  • Loading branch information
code authored May 8, 2023
1 parent ac76c49 commit 5b5d139
Show file tree
Hide file tree
Showing 58 changed files with 443 additions and 493 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,10 @@ CryptoMbPrivateKeyMethodProvider::CryptoMbPrivateKeyMethodProvider(
const envoy::extensions::private_key_providers::cryptomb::v3alpha::
CryptoMbPrivateKeyMethodConfig& conf,
Server::Configuration::TransportSocketFactoryContext& factory_context, IppCryptoSharedPtr ipp)
: api_(factory_context.api()),
tls_(ThreadLocal::TypedSlot<ThreadLocalData>::makeUnique(factory_context.threadLocal())),
stats_(generateCryptoMbStats("cryptomb", factory_context.scope())) {
: api_(factory_context.serverFactoryContext().api()),
tls_(ThreadLocal::TypedSlot<ThreadLocalData>::makeUnique(
factory_context.serverFactoryContext().threadLocal())),
stats_(generateCryptoMbStats("cryptomb", factory_context.statsScope())) {

if (!ipp->mbxIsCryptoMbApplicable(0)) {
throw EnvoyException("Multi-buffer CPU instructions not available.");
Expand All @@ -527,8 +528,7 @@ CryptoMbPrivateKeyMethodProvider::CryptoMbPrivateKeyMethodProvider(
std::chrono::milliseconds poll_delay =
std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(conf, poll_delay, 200));

std::string private_key =
Config::DataSource::read(conf.private_key(), false, factory_context.api());
std::string private_key = Config::DataSource::read(conf.private_key(), false, api_);

bssl::UniquePtr<BIO> bio(
BIO_new_mem_buf(const_cast<char*>(private_key.data()), private_key.size()));
Expand Down
4 changes: 2 additions & 2 deletions contrib/cryptomb/private_key_providers/test/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ parsePrivateKeyProviderFromV3Yaml(const std::string& yaml_string) {
class CryptoMbConfigTest : public Event::TestUsingSimulatedTime, public testing::Test {
public:
CryptoMbConfigTest() : api_(Api::createApiForTest(store_, time_system_)) {
ON_CALL(factory_context_, api()).WillByDefault(ReturnRef(*api_));
ON_CALL(factory_context_, threadLocal()).WillByDefault(ReturnRef(tls_));
ON_CALL(factory_context_.server_context_, api()).WillByDefault(ReturnRef(*api_));
ON_CALL(factory_context_.server_context_, threadLocal()).WillByDefault(ReturnRef(tls_));
ON_CALL(factory_context_, sslContextManager()).WillByDefault(ReturnRef(context_manager_));
ON_CALL(context_manager_, privateKeyMethodManager())
.WillByDefault(ReturnRef(private_key_method_manager_));
Expand Down
4 changes: 2 additions & 2 deletions contrib/cryptomb/private_key_providers/test/fake_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ FakeCryptoMbPrivateKeyMethodFactory::createPrivateKeyMethodProviderInstance(
std::make_shared<FakeIppCryptoImpl>(supported_instruction_set_);

// We need to get more RSA key params in order to be able to use BoringSSL signing functions.
std::string private_key =
Config::DataSource::read(conf.private_key(), false, private_key_provider_context.api());
std::string private_key = Config::DataSource::read(
conf.private_key(), false, private_key_provider_context.serverFactoryContext().api());

bssl::UniquePtr<BIO> bio(
BIO_new_mem_buf(const_cast<char*>(private_key.data()), private_key.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class UpstreamSSLBaseIntegrationTest : public PostgresBaseIntegrationTest {
TestUtility::loadFromYaml(TestEnvironment::substitute(yaml_plain), downstream_tls_context);

NiceMock<Server::Configuration::MockTransportSocketFactoryContext> mock_factory_ctx;
ON_CALL(mock_factory_ctx, api()).WillByDefault(testing::ReturnRef(*api_));
ON_CALL(mock_factory_ctx.server_context_, api()).WillByDefault(testing::ReturnRef(*api_));
auto cfg = std::make_unique<Extensions::TransportSockets::Tls::ServerContextConfigImpl>(
downstream_tls_context, mock_factory_ctx);
static auto* client_stats_store = new Stats::TestIsolatedStoreImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ QatPrivateKeyMethodProvider::QatPrivateKeyMethodProvider(
const envoy::extensions::private_key_providers::qat::v3alpha::QatPrivateKeyMethodConfig& conf,
Server::Configuration::TransportSocketFactoryContext& factory_context,
LibQatCryptoSharedPtr libqat)
: api_(factory_context.api()), libqat_(libqat) {
: api_(factory_context.serverFactoryContext().api()), libqat_(libqat) {

manager_ = factory_context.singletonManager().getTyped<QatManager>(
manager_ = factory_context.serverFactoryContext().singletonManager().getTyped<QatManager>(
SINGLETON_MANAGER_REGISTERED_NAME(qat_manager),
[libqat] { return std::make_shared<QatManager>(libqat); });

Expand All @@ -358,8 +358,7 @@ QatPrivateKeyMethodProvider::QatPrivateKeyMethodProvider(
std::chrono::milliseconds poll_delay =
std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(conf, poll_delay, 5));

std::string private_key =
Config::DataSource::read(conf.private_key(), false, factory_context.api());
std::string private_key = Config::DataSource::read(conf.private_key(), false, api_);

bssl::UniquePtr<BIO> bio(
BIO_new_mem_buf(const_cast<char*>(private_key.data()), private_key.size()));
Expand Down
4 changes: 2 additions & 2 deletions contrib/qat/private_key_providers/test/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class QatConfigTest : public Event::TestUsingSimulatedTime, public testing::Test
QatConfigTest()
: api_(Api::createApiForTest(store_, time_system_)),
libqat_(std::make_shared<FakeLibQatCryptoImpl>()), fsm_(libqat_) {
ON_CALL(factory_context_, api()).WillByDefault(ReturnRef(*api_));
ON_CALL(factory_context_.server_context_, api()).WillByDefault(ReturnRef(*api_));
ON_CALL(factory_context_, sslContextManager()).WillByDefault(ReturnRef(context_manager_));
ON_CALL(context_manager_, privateKeyMethodManager())
.WillByDefault(ReturnRef(private_key_method_manager_));
ON_CALL(factory_context_, singletonManager()).WillByDefault(ReturnRef(fsm_));
ON_CALL(factory_context_.server_context_, singletonManager()).WillByDefault(ReturnRef(fsm_));
}

Ssl::PrivateKeyMethodProviderSharedPtr createWithConfig(std::string yaml) {
Expand Down
5 changes: 3 additions & 2 deletions contrib/qat/private_key_providers/test/ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ class QatProviderTest : public testing::Test {
dispatcher_(api_->allocateDispatcher("test_thread")),
libqat_(std::make_shared<FakeLibQatCryptoImpl>()), fsm_(libqat_) {
handle_.setLibqat(libqat_);
ON_CALL(factory_context_, api()).WillByDefault(testing::ReturnRef(*api_));
ON_CALL(factory_context_, singletonManager()).WillByDefault(testing::ReturnRef(fsm_));
ON_CALL(factory_context_.server_context_, api()).WillByDefault(testing::ReturnRef(*api_));
ON_CALL(factory_context_.server_context_, singletonManager())
.WillByDefault(testing::ReturnRef(fsm_));
}

Stats::TestUtil::TestStore store_;
Expand Down
1 change: 1 addition & 0 deletions contrib/sip_proxy/filters/network/source/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class Utility {
public:
static const std::string& localAddress(Server::Configuration::FactoryContext& context) {
return context.getTransportSocketFactoryContext()
.serverFactoryContext()
.localInfo()
.address()
->ip()
Expand Down
3 changes: 2 additions & 1 deletion contrib/sip_proxy/filters/network/test/conn_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class SipConnectionManagerTest : public testing::Test {

EXPECT_CALL(context_, getTransportSocketFactoryContext())
.WillRepeatedly(testing::ReturnRef(factory_context_));
EXPECT_CALL(factory_context_, localInfo()).WillRepeatedly(testing::ReturnRef(local_info_));
EXPECT_CALL(factory_context_.server_context_, localInfo())
.WillRepeatedly(testing::ReturnRef(local_info_));
ON_CALL(random_, random()).WillByDefault(Return(42));
filter_ = std::make_unique<ConnectionManager>(
*config_, random_, filter_callbacks_.connection_.dispatcher_.timeSource(), context_,
Expand Down
3 changes: 2 additions & 1 deletion contrib/sip_proxy/filters/network/test/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ class SipRouterTest : public testing::Test {

EXPECT_CALL(context_, getTransportSocketFactoryContext())
.WillRepeatedly(testing::ReturnRef(factory_context_));
EXPECT_CALL(factory_context_, localInfo()).WillRepeatedly(testing::ReturnRef(local_info_));
EXPECT_CALL(factory_context_.server_context_, localInfo())
.WillRepeatedly(testing::ReturnRef(local_info_));

transaction_infos_ = std::make_shared<TransactionInfos>();
context_.cluster_manager_.initializeThreadLocalClusters({cluster_name_});
Expand Down
9 changes: 4 additions & 5 deletions contrib/sxg/filters/http/test/filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,13 @@ TEST_F(FilterTest, SdsDynamicGenericSecret) {
NiceMock<Server::Configuration::MockTransportSocketFactoryContext> secret_context;
NiceMock<LocalInfo::MockLocalInfo> local_info;
Api::ApiPtr api = Api::createApiForTest();
Stats::IsolatedStoreImpl stats;
NiceMock<Init::MockManager> init_manager;
Init::TargetHandlePtr init_handle;
NiceMock<Event::MockDispatcher> dispatcher;
EXPECT_CALL(secret_context, localInfo()).WillRepeatedly(ReturnRef(local_info));
EXPECT_CALL(secret_context, api()).WillRepeatedly(ReturnRef(*api));
EXPECT_CALL(secret_context, mainThreadDispatcher()).WillRepeatedly(ReturnRef(dispatcher));
EXPECT_CALL(secret_context, stats()).WillRepeatedly(ReturnRef(stats));
EXPECT_CALL(secret_context.server_context_, localInfo()).WillRepeatedly(ReturnRef(local_info));
EXPECT_CALL(secret_context.server_context_, api()).WillRepeatedly(ReturnRef(*api));
EXPECT_CALL(secret_context.server_context_, mainThreadDispatcher())
.WillRepeatedly(ReturnRef(dispatcher));
EXPECT_CALL(secret_context, initManager()).Times(0);
EXPECT_CALL(init_manager, add(_))
.WillRepeatedly(Invoke([&init_handle](const Init::Target& target) {
Expand Down
72 changes: 15 additions & 57 deletions envoy/server/transport_socket_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ class TransportSocketFactoryContext {
virtual ~TransportSocketFactoryContext() = default;

/**
* @return ServerFactoryContext which lifetime is no shorter than the server.
* @return ServerFactoryContext& the server factory context.
*/
virtual ServerFactoryContext& getServerFactoryContext() PURE;
virtual ServerFactoryContext& serverFactoryContext() PURE;

/**
* @return OptRef<Server::Admin> the global HTTP admin endpoint for the server.
* @return Upstream::ClusterManager& singleton for use by the entire server.
* TODO(wbpcode): clusterManager() of ServerFactoryContext still be invalid when loading
* static cluster. So we need to provide an cluster manager reference here.
* This could be removed after https://github.com/envoyproxy/envoy/issues/26653 is resolved.
*/
virtual OptRef<Server::Admin> admin() PURE;
virtual Upstream::ClusterManager& clusterManager() PURE;

/**
* @return ProtobufMessage::ValidationVisitor& validation visitor for cluster configuration
* messages.
*/
virtual ProtobufMessage::ValidationVisitor& messageValidationVisitor() PURE;

/**
* @return Ssl::ContextManager& the SSL context manager.
Expand All @@ -47,68 +56,17 @@ class TransportSocketFactoryContext {
/**
* @return Stats::Scope& the transport socket's stats scope.
*/
virtual Stats::Scope& scope() PURE;
virtual Stats::Scope& statsScope() PURE;

/**
* Return the instance of secret manager.
*/
virtual Secret::SecretManager& secretManager() PURE;

/**
* @return the instance of ClusterManager.
*/
virtual Upstream::ClusterManager& clusterManager() PURE;

/**
* @return information about the local environment the server is running in.
*/
virtual const LocalInfo::LocalInfo& localInfo() const PURE;

/**
* @return Event::Dispatcher& the main thread's dispatcher.
*/
virtual Event::Dispatcher& mainThreadDispatcher() PURE;

/**
* @return Server::Options& the command-line options that Envoy was started with.
*/
virtual const Options& options() PURE;

/**
* @return the server-wide stats store.
*/
virtual Stats::Store& stats() PURE;

/**
* @return a reference to the instance of an init manager.
* @return the init manager of the particular context.
*/
virtual Init::Manager& initManager() PURE;

/**
* @return the server's singleton manager.
*/
virtual Singleton::Manager& singletonManager() PURE;

/**
* @return the server's TLS slot allocator.
*/
virtual ThreadLocal::SlotAllocator& threadLocal() PURE;

/**
* @return ProtobufMessage::ValidationVisitor& validation visitor for filter configuration
* messages.
*/
virtual ProtobufMessage::ValidationVisitor& messageValidationVisitor() PURE;

/**
* @return reference to the Api object
*/
virtual Api::Api& api() PURE;

/**
* @return reference to the access log manager object
*/
virtual AccessLog::AccessLogManager& accessLogManager() PURE;
};

using TransportSocketFactoryContextPtr = std::unique_ptr<TransportSocketFactoryContext>;
Expand Down
5 changes: 3 additions & 2 deletions mobile/test/common/integration/test_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ Network::DownstreamTransportSocketFactoryPtr TestServer::createUpstreamTlsContex
TestServer::TestServer()
: api_(Api::createApiForTest(stats_store_, time_system_)),
version_(Network::Address::IpVersion::v4), upstream_config_(time_system_), port_(0) {
ON_CALL(factory_context_, api()).WillByDefault(testing::ReturnRef(*api_));
ON_CALL(factory_context_, scope()).WillByDefault(testing::ReturnRef(*stats_store_.rootScope()));
ON_CALL(factory_context_.server_context_, api()).WillByDefault(testing::ReturnRef(*api_));
ON_CALL(factory_context_, statsScope())
.WillByDefault(testing::ReturnRef(*stats_store_.rootScope()));
}

void TestServer::startTestServer(bool use_quic) {
Expand Down
8 changes: 4 additions & 4 deletions source/common/quic/quic_transport_socket_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ QuicServerTransportSocketConfigFactory::createTransportSocketFactory(
}

auto factory = std::make_unique<QuicServerTransportSocketFactory>(
PROTOBUF_GET_WRAPPED_OR_DEFAULT(quic_transport, enable_early_data, true), context.scope(),
std::move(server_config));
PROTOBUF_GET_WRAPPED_OR_DEFAULT(quic_transport, enable_early_data, true),
context.statsScope(), std::move(server_config));
factory->initialize();
return factory;
}
Expand Down Expand Up @@ -59,9 +59,9 @@ QuicClientTransportSocketConfigFactory::createTransportSocketFactory(
QuicClientTransportSocketFactory::QuicClientTransportSocketFactory(
Ssl::ClientContextConfigPtr config,
Server::Configuration::TransportSocketFactoryContext& factory_context)
: QuicTransportSocketFactoryBase(factory_context.scope(), "client"),
: QuicTransportSocketFactoryBase(factory_context.statsScope(), "client"),
fallback_factory_(std::make_unique<Extensions::TransportSockets::Tls::ClientSslSocketFactory>(
std::move(config), factory_context.sslContextManager(), factory_context.scope())) {}
std::move(config), factory_context.sslContextManager(), factory_context.statsScope())) {}

ProtobufTypes::MessagePtr QuicClientTransportSocketConfigFactory::createEmptyConfigProto() {
return std::make_unique<envoy::extensions::transport_sockets::quic::v3::QuicUpstreamTransport>();
Expand Down
41 changes: 20 additions & 21 deletions source/common/secret/sds_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ class TlsCertificateSdsApi : public SdsApi, public TlsCertificateConfigProvider
const std::string& sds_config_name, std::function<void()> destructor_cb) {
// We need to do this early as we invoke the subscription factory during initialization, which
// is too late to throw.
Config::Utility::checkLocalInfo("TlsCertificateSdsApi", secret_provider_context.localInfo());
auto& server_context = secret_provider_context.serverFactoryContext();
Config::Utility::checkLocalInfo("TlsCertificateSdsApi", server_context.localInfo());
return std::make_shared<TlsCertificateSdsApi>(
sds_config, sds_config_name, secret_provider_context.clusterManager().subscriptionFactory(),
secret_provider_context.mainThreadDispatcher().timeSource(),
secret_provider_context.messageValidationVisitor(), secret_provider_context.stats(),
destructor_cb, secret_provider_context.mainThreadDispatcher(),
secret_provider_context.api());
server_context.mainThreadDispatcher().timeSource(),
secret_provider_context.messageValidationVisitor(), server_context.serverScope().store(),
destructor_cb, server_context.mainThreadDispatcher(), server_context.api());
}

TlsCertificateSdsApi(const envoy::config::core::v3::ConfigSource& sds_config,
Expand Down Expand Up @@ -223,14 +223,14 @@ class CertificateValidationContextSdsApi : public SdsApi,
const std::string& sds_config_name, std::function<void()> destructor_cb) {
// We need to do this early as we invoke the subscription factory during initialization, which
// is too late to throw.
auto& server_context = secret_provider_context.serverFactoryContext();
Config::Utility::checkLocalInfo("CertificateValidationContextSdsApi",
secret_provider_context.localInfo());
server_context.localInfo());
return std::make_shared<CertificateValidationContextSdsApi>(
sds_config, sds_config_name, secret_provider_context.clusterManager().subscriptionFactory(),
secret_provider_context.mainThreadDispatcher().timeSource(),
secret_provider_context.messageValidationVisitor(), secret_provider_context.stats(),
destructor_cb, secret_provider_context.mainThreadDispatcher(),
secret_provider_context.api());
server_context.mainThreadDispatcher().timeSource(),
secret_provider_context.messageValidationVisitor(), server_context.serverScope().store(),
destructor_cb, server_context.mainThreadDispatcher(), server_context.api());
}
CertificateValidationContextSdsApi(const envoy::config::core::v3::ConfigSource& sds_config,
const std::string& sds_config_name,
Expand Down Expand Up @@ -318,14 +318,13 @@ class TlsSessionTicketKeysSdsApi : public SdsApi, public TlsSessionTicketKeysCon
const std::string& sds_config_name, std::function<void()> destructor_cb) {
// We need to do this early as we invoke the subscription factory during initialization, which
// is too late to throw.
Config::Utility::checkLocalInfo("TlsSessionTicketKeysSdsApi",
secret_provider_context.localInfo());
auto& server_context = secret_provider_context.serverFactoryContext();
Config::Utility::checkLocalInfo("TlsSessionTicketKeysSdsApi", server_context.localInfo());
return std::make_shared<TlsSessionTicketKeysSdsApi>(
sds_config, sds_config_name, secret_provider_context.clusterManager().subscriptionFactory(),
secret_provider_context.mainThreadDispatcher().timeSource(),
secret_provider_context.messageValidationVisitor(), secret_provider_context.stats(),
destructor_cb, secret_provider_context.mainThreadDispatcher(),
secret_provider_context.api());
server_context.mainThreadDispatcher().timeSource(),
secret_provider_context.messageValidationVisitor(), server_context.serverScope().store(),
destructor_cb, server_context.mainThreadDispatcher(), server_context.api());
}

TlsSessionTicketKeysSdsApi(const envoy::config::core::v3::ConfigSource& sds_config,
Expand Down Expand Up @@ -392,13 +391,13 @@ class GenericSecretSdsApi : public SdsApi, public GenericSecretConfigProvider {
const std::string& sds_config_name, std::function<void()> destructor_cb) {
// We need to do this early as we invoke the subscription factory during initialization, which
// is too late to throw.
Config::Utility::checkLocalInfo("GenericSecretSdsApi", secret_provider_context.localInfo());
auto& server_context = secret_provider_context.serverFactoryContext();
Config::Utility::checkLocalInfo("GenericSecretSdsApi", server_context.localInfo());
return std::make_shared<GenericSecretSdsApi>(
sds_config, sds_config_name, secret_provider_context.clusterManager().subscriptionFactory(),
secret_provider_context.mainThreadDispatcher().timeSource(),
secret_provider_context.messageValidationVisitor(), secret_provider_context.stats(),
destructor_cb, secret_provider_context.mainThreadDispatcher(),
secret_provider_context.api());
server_context.mainThreadDispatcher().timeSource(),
secret_provider_context.messageValidationVisitor(), server_context.serverScope().store(),
destructor_cb, server_context.mainThreadDispatcher(), server_context.api());
}

GenericSecretSdsApi(const envoy::config::core::v3::ConfigSource& sds_config,
Expand Down
Loading

0 comments on commit 5b5d139

Please sign in to comment.