#include "extensions/transport_sockets/tls/context_manager_impl.h" #include #include #include #include "envoy/stats/scope.h" #include "common/common/assert.h" #include "extensions/transport_sockets/tls/context_impl.h" namespace Envoy { namespace Extensions { namespace TransportSockets { namespace Tls { ContextManagerImpl::~ContextManagerImpl() { removeEmptyContexts(); KNOWN_ISSUE_ASSERT(contexts_.empty(), "https://github.com/envoyproxy/envoy/issues/10030"); } void ContextManagerImpl::removeEmptyContexts() { contexts_.remove_if([](const std::weak_ptr& n) { return n.expired(); }); } void ContextManagerImpl::removeOldContext(std::shared_ptr old_context) { if (old_context) { contexts_.remove_if([old_context](const std::weak_ptr& n) { std::shared_ptr sp = n.lock(); if (sp) { return old_context == sp; } return false; }); } } Envoy::Ssl::ClientContextSharedPtr ContextManagerImpl::createSslClientContext(Stats::Scope& scope, const Envoy::Ssl::ClientContextConfig& config, Envoy::Ssl::ClientContextSharedPtr old_context) { if (!config.isReady()) { return nullptr; } Envoy::Ssl::ClientContextSharedPtr context = std::make_shared(scope, config, time_source_); removeOldContext(old_context); removeEmptyContexts(); contexts_.emplace_back(context); return context; } Envoy::Ssl::ServerContextSharedPtr ContextManagerImpl::createSslServerContext( Stats::Scope& scope, const Envoy::Ssl::ServerContextConfig& config, const std::vector& server_names, Envoy::Ssl::ServerContextSharedPtr old_context) { if (!config.isReady()) { return nullptr; } Envoy::Ssl::ServerContextSharedPtr context = std::make_shared(scope, config, server_names, time_source_); removeOldContext(old_context); removeEmptyContexts(); contexts_.emplace_back(context); return context; } size_t ContextManagerImpl::daysUntilFirstCertExpires() const { size_t ret = std::numeric_limits::max(); for (const auto& ctx_weak_ptr : contexts_) { Envoy::Ssl::ContextSharedPtr context = ctx_weak_ptr.lock(); if (context) { ret = std::min(context->daysUntilFirstCertExpires(), ret); } } return ret; } absl::optional ContextManagerImpl::secondsUntilFirstOcspResponseExpires() const { absl::optional ret; for (const auto& ctx_weak_ptr : contexts_) { Envoy::Ssl::ContextSharedPtr context = ctx_weak_ptr.lock(); if (context) { auto next_expiration = context->secondsUntilFirstOcspResponseExpires(); if (next_expiration) { ret = std::min(next_expiration.value(), ret.value_or(std::numeric_limits::max())); } } } return ret; } void ContextManagerImpl::iterateContexts(std::function callback) { for (const auto& ctx_weak_ptr : contexts_) { Envoy::Ssl::ContextSharedPtr context = ctx_weak_ptr.lock(); if (context) { callback(*context); } } } } // namespace Tls } // namespace TransportSockets } // namespace Extensions } // namespace Envoy