Line data Source code
1 : #include "source/extensions/transport_sockets/tls/context_manager_impl.h" 2 : 3 : #include <algorithm> 4 : #include <cstddef> 5 : #include <functional> 6 : #include <limits> 7 : 8 : #include "envoy/stats/scope.h" 9 : 10 : #include "source/common/common/assert.h" 11 : #include "source/extensions/transport_sockets/tls/context_impl.h" 12 : 13 : namespace Envoy { 14 : namespace Extensions { 15 : namespace TransportSockets { 16 : namespace Tls { 17 : 18 229 : ContextManagerImpl::ContextManagerImpl(TimeSource& time_source) : time_source_(time_source) {} 19 : 20 : Envoy::Ssl::ClientContextSharedPtr 21 : ContextManagerImpl::createSslClientContext(Stats::Scope& scope, 22 0 : const Envoy::Ssl::ClientContextConfig& config) { 23 0 : if (!config.isReady()) { 24 0 : return nullptr; 25 0 : } 26 : 27 0 : Envoy::Ssl::ClientContextSharedPtr context = 28 0 : std::make_shared<ClientContextImpl>(scope, config, time_source_); 29 0 : contexts_.insert(context); 30 0 : return context; 31 0 : } 32 : 33 : Envoy::Ssl::ServerContextSharedPtr 34 : ContextManagerImpl::createSslServerContext(Stats::Scope& scope, 35 : const Envoy::Ssl::ServerContextConfig& config, 36 0 : const std::vector<std::string>& server_names) { 37 0 : if (!config.isReady()) { 38 0 : return nullptr; 39 0 : } 40 : 41 0 : Envoy::Ssl::ServerContextSharedPtr context = 42 0 : std::make_shared<ServerContextImpl>(scope, config, server_names, time_source_); 43 0 : contexts_.insert(context); 44 0 : return context; 45 0 : } 46 : 47 205 : absl::optional<uint32_t> ContextManagerImpl::daysUntilFirstCertExpires() const { 48 205 : absl::optional<uint32_t> ret = absl::make_optional(std::numeric_limits<uint32_t>::max()); 49 205 : for (const auto& context : contexts_) { 50 0 : if (context) { 51 0 : const absl::optional<uint32_t> tmp = context->daysUntilFirstCertExpires(); 52 0 : if (!tmp.has_value()) { 53 0 : return absl::nullopt; 54 0 : } 55 0 : ret = std::min<uint32_t>(tmp.value(), ret.value()); 56 0 : } 57 0 : } 58 205 : return ret; 59 205 : } 60 : 61 205 : absl::optional<uint64_t> ContextManagerImpl::secondsUntilFirstOcspResponseExpires() const { 62 205 : absl::optional<uint64_t> ret; 63 205 : for (const auto& context : contexts_) { 64 0 : if (context) { 65 0 : auto next_expiration = context->secondsUntilFirstOcspResponseExpires(); 66 0 : if (next_expiration) { 67 0 : ret = std::min<uint64_t>(next_expiration.value(), 68 0 : ret.value_or(std::numeric_limits<uint64_t>::max())); 69 0 : } 70 0 : } 71 0 : } 72 205 : return ret; 73 205 : } 74 : 75 0 : void ContextManagerImpl::iterateContexts(std::function<void(const Envoy::Ssl::Context&)> callback) { 76 0 : for (const auto& context : contexts_) { 77 0 : if (context) { 78 0 : callback(*context); 79 0 : } 80 0 : } 81 0 : } 82 : 83 0 : void ContextManagerImpl::removeContext(const Envoy::Ssl::ContextSharedPtr& old_context) { 84 0 : if (old_context != nullptr) { 85 0 : auto erased = contexts_.erase(old_context); 86 : // The contexts is expected to be added before is removed. 87 : // And the prod ssl factory implementation guarantees any context is removed exactly once. 88 0 : ASSERT(erased == 1); 89 0 : } 90 0 : } 91 : 92 : } // namespace Tls 93 : } // namespace TransportSockets 94 : } // namespace Extensions 95 : } // namespace Envoy