Line data Source code
1 : #include "source/common/network/transport_socket_options_impl.h" 2 : 3 : #include <cstdint> 4 : #include <memory> 5 : #include <string> 6 : #include <utility> 7 : #include <vector> 8 : 9 : #include "envoy/common/hashable.h" 10 : 11 : #include "source/common/common/scalar_to_byte_vector.h" 12 : #include "source/common/common/utility.h" 13 : #include "source/common/network/application_protocol.h" 14 : #include "source/common/network/filter_state_proxy_info.h" 15 : #include "source/common/network/proxy_protocol_filter_state.h" 16 : #include "source/common/network/upstream_server_name.h" 17 : #include "source/common/network/upstream_subject_alt_names.h" 18 : 19 : namespace Envoy { 20 : namespace Network { 21 : 22 : void CommonUpstreamTransportSocketFactory::hashKey( 23 183 : std::vector<uint8_t>& key, TransportSocketOptionsConstSharedPtr options) const { 24 183 : if (!options) { 25 0 : return; 26 0 : } 27 183 : const auto& server_name_overide = options->serverNameOverride(); 28 183 : if (server_name_overide.has_value()) { 29 0 : pushScalarToByteVector(StringUtil::CaseInsensitiveHash()(server_name_overide.value()), key); 30 0 : } 31 : 32 183 : const auto& verify_san_list = options->verifySubjectAltNameListOverride(); 33 183 : for (const auto& san : verify_san_list) { 34 0 : pushScalarToByteVector(StringUtil::CaseInsensitiveHash()(san), key); 35 0 : } 36 : 37 183 : const auto& alpn_list = options->applicationProtocolListOverride(); 38 183 : for (const auto& protocol : alpn_list) { 39 0 : pushScalarToByteVector(StringUtil::CaseInsensitiveHash()(protocol), key); 40 0 : } 41 : 42 183 : const auto& alpn_fallback = options->applicationProtocolFallback(); 43 183 : for (const auto& protocol : alpn_fallback) { 44 0 : pushScalarToByteVector(StringUtil::CaseInsensitiveHash()(protocol), key); 45 0 : } 46 : 47 183 : for (const auto& object : options->downstreamSharedFilterStateObjects()) { 48 0 : if (auto hashable = dynamic_cast<const Hashable*>(object.data_.get()); hashable != nullptr) { 49 0 : if (auto hash = hashable->hash(); hash) { 50 0 : pushScalarToByteVector(hash.value(), key); 51 0 : } 52 0 : } 53 0 : } 54 183 : } 55 : 56 : TransportSocketOptionsConstSharedPtr 57 251 : TransportSocketOptionsUtility::fromFilterState(const StreamInfo::FilterState& filter_state) { 58 251 : absl::string_view server_name; 59 251 : std::vector<std::string> application_protocols; 60 251 : std::vector<std::string> subject_alt_names; 61 251 : std::vector<std::string> alpn_fallback; 62 251 : absl::optional<Network::ProxyProtocolData> proxy_protocol_options; 63 251 : std::unique_ptr<const TransportSocketOptions::Http11ProxyInfo> proxy_info; 64 : 65 251 : bool needs_transport_socket_options = false; 66 251 : if (auto typed_data = filter_state.getDataReadOnly<UpstreamServerName>(UpstreamServerName::key()); 67 251 : typed_data != nullptr) { 68 0 : server_name = typed_data->value(); 69 0 : needs_transport_socket_options = true; 70 0 : } 71 : 72 251 : if (auto typed_data = filter_state.getDataReadOnly<Network::ApplicationProtocols>( 73 251 : Network::ApplicationProtocols::key()); 74 251 : typed_data != nullptr) { 75 0 : application_protocols = typed_data->value(); 76 0 : needs_transport_socket_options = true; 77 0 : } 78 : 79 251 : if (auto typed_data = 80 251 : filter_state.getDataReadOnly<UpstreamSubjectAltNames>(UpstreamSubjectAltNames::key()); 81 251 : typed_data != nullptr) { 82 0 : subject_alt_names = typed_data->value(); 83 0 : needs_transport_socket_options = true; 84 0 : } 85 : 86 251 : if (auto typed_data = 87 251 : filter_state.getDataReadOnly<ProxyProtocolFilterState>(ProxyProtocolFilterState::key()); 88 251 : typed_data != nullptr) { 89 183 : proxy_protocol_options.emplace(typed_data->value()); 90 183 : needs_transport_socket_options = true; 91 183 : } 92 : 93 251 : if (auto typed_data = filter_state.getDataReadOnly<Http11ProxyInfoFilterState>( 94 251 : Http11ProxyInfoFilterState::key()); 95 251 : typed_data != nullptr) { 96 0 : proxy_info = std::make_unique<TransportSocketOptions::Http11ProxyInfo>(typed_data->hostname(), 97 0 : typed_data->address()); 98 0 : needs_transport_socket_options = true; 99 0 : } 100 : 101 251 : StreamInfo::FilterState::ObjectsPtr objects = filter_state.objectsSharedWithUpstreamConnection(); 102 251 : if (!objects->empty()) { 103 0 : needs_transport_socket_options = true; 104 0 : } 105 : 106 251 : if (needs_transport_socket_options) { 107 183 : return std::make_shared<Network::TransportSocketOptionsImpl>( 108 183 : server_name, std::move(subject_alt_names), std::move(application_protocols), 109 183 : std::move(alpn_fallback), proxy_protocol_options, std::move(objects), 110 183 : std::move(proxy_info)); 111 213 : } else { 112 68 : return nullptr; 113 68 : } 114 251 : } 115 : 116 : } // namespace Network 117 : } // namespace Envoy