Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions include/envoy/ssl/handshaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "envoy/network/connection.h"
#include "envoy/network/post_io_action.h"
#include "envoy/protobuf/message_validator.h"
#include "envoy/ssl/connection.h"
#include "envoy/ssl/ssl_socket_state.h"

#include "openssl/ssl.h"

Expand Down Expand Up @@ -46,9 +48,38 @@ class Handshaker {
virtual Network::PostIoAction doHandshake() PURE;
};

using HandshakerSharedPtr = std::shared_ptr<Handshaker>;
using HandshakerFactoryCb =
std::function<HandshakerSharedPtr(bssl::UniquePtr<SSL>, int, HandshakeCallbacks*)>;
/**
* Base interface for a combined `handshaker-and-connectioninfo` class
* which can both perform handshakes and provide connection-specific
* information.
*/
class HandshakerAndConnectionInfo : public Handshaker, public ConnectionInfo {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not thrilled with this interface, with extra virtual method. What is the goal of this PR? If we just want to provide an impl, then we might just make some mixin pattern class for ConnectionInfoImpl. If we want make the cast to ConnectionInfo safe, then perhaps make Handshaker return a ConnectionInfo pointer (in the default impl it just return this).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal of this PR is to make the cast safe.

If we have Handshaker return a connectioninfo pointer and save that as info_, it's unclear to me who holds on to the SslHandshaker class for lifetime reasons. Plus info_ also needs socketstate setter/getters, which aren't a part of either the ConnectionInfo or Handshaker interfaces.

But, I agree, I don't love this interface. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this either. So we're requiring one class to implement both of these interfaces. Why not just combine them into the same interface? Or if that isn't the right solution, some other refactoring appears needed.

@mattklein123 any thoughts (since you left the original comment)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original thinking was to have a connectionInfo() method on the Handshaker interface and then you can handle the lifetime independently, but I don't have a strong opinion either way.

public:
/**
* Return the current SocketState.
*/
virtual SocketState state() PURE;

/**
* Update the SocketState.
*/
virtual void setState(SocketState state) PURE;

/**
* Returns a pointer to the SSL object.
*/
virtual SSL* ssl() const PURE;

/**
* Returns a pointer to the HandshakeCallbacks object.
*/
virtual HandshakeCallbacks* handshakeCallbacks() PURE;
};

using HandshakerAndConnectionInfoSharedPtr = std::shared_ptr<HandshakerAndConnectionInfo>;

using HandshakerFactoryCb = std::function<HandshakerAndConnectionInfoSharedPtr(
bssl::UniquePtr<SSL>, int, HandshakeCallbacks*)>;

class HandshakerFactoryContext {
public:
Expand Down
13 changes: 6 additions & 7 deletions source/extensions/transport_sockets/tls/ssl_handshaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SslExtendedSocketInfoImpl : public Envoy::Ssl::SslExtendedSocketInfo {
Envoy::Ssl::ClientValidationStatus::NotValidated};
};

class SslHandshakerImpl : public Ssl::ConnectionInfo, public Ssl::Handshaker {
class SslHandshakerImpl : public Ssl::HandshakerAndConnectionInfo {
public:
SslHandshakerImpl(bssl::UniquePtr<SSL> ssl, int ssl_extended_socket_info_index,
Ssl::HandshakeCallbacks* handshake_callbacks);
Expand Down Expand Up @@ -67,10 +67,11 @@ class SslHandshakerImpl : public Ssl::ConnectionInfo, public Ssl::Handshaker {
// Ssl::Handshaker
Network::PostIoAction doHandshake() override;

Ssl::SocketState state() { return state_; }
void setState(Ssl::SocketState state) { state_ = state; }
SSL* ssl() const { return ssl_.get(); }
Ssl::HandshakeCallbacks* handshakeCallbacks() { return handshake_callbacks_; }
// Ssl::HandshakerAndConnectionInfo
Ssl::SocketState state() override { return state_; }
void setState(Ssl::SocketState state) override { state_ = state; }
SSL* ssl() const override { return ssl_.get(); }
Ssl::HandshakeCallbacks* handshakeCallbacks() override { return handshake_callbacks_; }

bssl::UniquePtr<SSL> ssl_;

Expand All @@ -95,8 +96,6 @@ class SslHandshakerImpl : public Ssl::ConnectionInfo, public Ssl::Handshaker {
mutable SslExtendedSocketInfoImpl extended_socket_info_;
};

using SslHandshakerImplSharedPtr = std::shared_ptr<SslHandshakerImpl>;

class HandshakerFactoryContextImpl : public Ssl::HandshakerFactoryContext {
public:
HandshakerFactoryContextImpl(Api::Api& api, absl::string_view alpn_protocols)
Expand Down
5 changes: 2 additions & 3 deletions source/extensions/transport_sockets/tls/ssl_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ SslSocket::SslSocket(Envoy::Ssl::ContextSharedPtr ctx, InitialState state,
Ssl::HandshakerFactoryCb handshaker_factory_cb)
: transport_socket_options_(transport_socket_options),
ctx_(std::dynamic_pointer_cast<ContextImpl>(ctx)),
info_(std::dynamic_pointer_cast<SslHandshakerImpl>(
handshaker_factory_cb(ctx_->newSsl(transport_socket_options_.get()),
ctx_->sslExtendedSocketInfoIndex(), this))) {
info_(handshaker_factory_cb(ctx_->newSsl(transport_socket_options_.get()),
ctx_->sslExtendedSocketInfoIndex(), this)) {
if (state == InitialState::Client) {
SSL_set_connect_state(rawSsl());
} else {
Expand Down
4 changes: 2 additions & 2 deletions source/extensions/transport_sockets/tls/ssl_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SslSocket : public Network::TransportSocket,
SSL* rawSslForTest() const { return rawSsl(); }

protected:
SSL* rawSsl() const { return info_->ssl_.get(); }
SSL* rawSsl() const { return info_->ssl(); }

private:
struct ReadResult {
Expand All @@ -94,7 +94,7 @@ class SslSocket : public Network::TransportSocket,
uint64_t bytes_to_retry_{};
std::string failure_reason_;

SslHandshakerImplSharedPtr info_;
Ssl::HandshakerAndConnectionInfoSharedPtr info_;
};

class ClientSslSocketFactory : public Network::TransportSocketFactory,
Expand Down