proxygen
fizz::server::test::FizzTestServer Class Reference

#include <Utils.h>

Inheritance diagram for fizz::server::test::FizzTestServer:
folly::AsyncServerSocket::AcceptCallback

Classes

class  CallbackFactory
 

Public Member Functions

 FizzTestServer (folly::EventBase &evb, CallbackFactory *factory, int port=0)
 
void setFizzContext (std::shared_ptr< FizzServerContext > ctx)
 
void acceptError (const std::exception &ex) noexceptoverride
 
void connectionAccepted (int fd, const folly::SocketAddress &) noexceptoverride
 
void setResumption (bool enable)
 
void setCertificate (std::unique_ptr< SelfCert > cert)
 
void enableClientAuthWithChain (std::string path, ClientAuthMode mode=ClientAuthMode::Optional)
 
void disableClientAuth ()
 
void setAcceptEarlyData (bool enable)
 
void stopAccepting ()
 
folly::SocketAddress getAddress ()
 
std::shared_ptr< FizzServerContextgetFizzContext ()
 
- Public Member Functions inherited from folly::AsyncServerSocket::AcceptCallback
virtual ~AcceptCallback ()=default
 
virtual void acceptStarted () noexcept
 
virtual void acceptStopped () noexcept
 

Private Attributes

folly::AsyncServerSocket::UniquePtr socket_
 
std::shared_ptr< FizzServerContextctx_
 
CallbackFactoryfactory_
 
folly::EventBaseevb_
 

Detailed Description

Definition at line 21 of file Utils.h.

Constructor & Destructor Documentation

fizz::server::test::FizzTestServer::FizzTestServer ( folly::EventBase evb,
CallbackFactory factory,
int  port = 0 
)
inline

Definition at line 30 of file Utils.h.

References fizz::test::createCert(), ctx_, evb_, folly::gen::move, and socket_.

31  : factory_(factory), evb_(evb) {
32  auto certData =
33  fizz::test::createCert("fizz-test-selfsign", false, nullptr);
34  std::vector<folly::ssl::X509UniquePtr> certChain;
35  certChain.push_back(std::move(certData.cert));
36  auto fizzCert = std::make_unique<SelfCertImpl<KeyType::P256>>(
37  std::move(certData.key), std::move(certChain));
38  auto certManager = std::make_unique<CertManager>();
39  certManager->addCert(std::move(fizzCert), true);
40  ctx_ = std::make_shared<FizzServerContext>();
41  ctx_->setCertManager(std::move(certManager));
44  socket_->bind(port);
45  socket_->listen(100);
46  socket_->addAcceptCallback(this, &evb_);
47  socket_->startAccepting();
48  }
folly::AsyncServerSocket::UniquePtr socket_
Definition: Utils.h:147
folly::EventBase & evb_
Definition: Utils.h:150
CallbackFactory * factory_
Definition: Utils.h:149
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
std::unique_ptr< AsyncServerSocket, Destructor > UniquePtr
CertAndKey createCert(std::string cn, bool ca, CertAndKey *issuer)
Definition: Utilities.h:35

Member Function Documentation

void fizz::server::test::FizzTestServer::acceptError ( const std::exception &  ex)
inlineoverridevirtualnoexcept

acceptError() is called if an error occurs while accepting.

The AcceptCallback will remain installed even after an accept error, as the errors are typically somewhat transient, such as being out of file descriptors. The server socket must be explicitly stopped if you wish to stop accepting after an error.

Parameters
exAn exception representing the error.

Implements folly::AsyncServerSocket::AcceptCallback.

Definition at line 54 of file Utils.h.

54  {
55  LOG(ERROR) << "Accept error: " << ex.what();
56  }
void fizz::server::test::FizzTestServer::connectionAccepted ( int  fd,
const folly::SocketAddress clientAddr 
)
inlineoverridevirtualnoexcept

connectionAccepted() is called whenever a new client connection is received.

The AcceptCallback will remain installed after connectionAccepted() returns.

Parameters
fdThe newly accepted client socket. The AcceptCallback assumes ownership of this socket, and is responsible for closing it when done. The newly accepted file descriptor will have already been put into non-blocking mode.
clientAddrA reference to a SocketAddress struct containing the client's address. This struct is only guaranteed to remain valid until connectionAccepted() returns.

Implements folly::AsyncServerSocket::AcceptCallback.

Definition at line 58 of file Utils.h.

References ctx_, evb_, factory_, and fizz::server::test::FizzTestServer::CallbackFactory::getCallback().

60  {
61  auto sock = new folly::AsyncSocket(&evb_, fd);
62  std::shared_ptr<AsyncFizzServer> transport = AsyncFizzServer::UniquePtr(
64  auto callback = factory_->getCallback(transport);
65  transport->accept(callback);
66  }
folly::EventBase & evb_
Definition: Utils.h:150
CallbackFactory * factory_
Definition: Utils.h:149
virtual AsyncFizzServer::HandshakeCallback * getCallback(std::shared_ptr< AsyncFizzServer > server)=0
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
std::unique_ptr< AsyncFizzServerT, folly::DelayedDestruction::Destructor > UniquePtr
AsyncFizzServerT< ServerStateMachine > AsyncFizzServer
std::unique_ptr< AsyncSocket, Destructor > UniquePtr
Definition: AsyncSocket.h:83
void fizz::server::test::FizzTestServer::disableClientAuth ( )
inline

Definition at line 116 of file Utils.h.

References ctx_, and fizz::server::None.

116  {
117  ctx_->setClientAuthMode(ClientAuthMode::None);
118  ctx_->setClientCertVerifier(nullptr);
119  }
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
void fizz::server::test::FizzTestServer::enableClientAuthWithChain ( std::string  path,
ClientAuthMode  mode = ClientAuthMode::Optional 
)
inline

Definition at line 88 of file Utils.h.

References ctx_, mode, folly::gen::move, folly::ssl::OpenSSLCertUtils::readCertsFromBuffer(), folly::readFile(), fizz::Server, and string.

90  {
91  ctx_->setClientAuthMode(mode);
92  std::string certData;
93  CHECK(folly::readFile(path.c_str(), certData));
94  auto certRange = folly::ByteRange(folly::StringPiece(certData));
95 
96  auto clientAuthCerts =
98  ERR_clear_error();
99  folly::ssl::X509StoreUniquePtr store(X509_STORE_new());
100  for (auto& caCert : clientAuthCerts) {
101  if (X509_STORE_add_cert(store.get(), caCert.get()) != 1) {
102  auto err = ERR_get_error();
103  CHECK(
104  ERR_GET_LIB(err) == ERR_LIB_X509 &&
105  ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)
106  << "Could not insert CA certificate into store: "
107  << std::string(ERR_error_string(err, nullptr));
108  }
109  }
110 
111  auto verifier = std::make_shared<DefaultCertificateVerifier>(
113  ctx_->setClientCertVerifier(std::move(verifier));
114  }
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::Optional< PskKeyExchangeMode > mode
std::unique_ptr< X509_STORE, X509StoreDeleter > X509StoreUniquePtr
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
const char * string
Definition: Conv.cpp:212
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
static std::vector< X509UniquePtr > readCertsFromBuffer(ByteRange range)
folly::SocketAddress fizz::server::test::FizzTestServer::getAddress ( )
inline

Definition at line 136 of file Utils.h.

References addr, and socket_.

136  {
138  socket_->getAddress(&addr);
139  return addr;
140  }
folly::AsyncServerSocket::UniquePtr socket_
Definition: Utils.h:147
ThreadPoolListHook * addr
std::shared_ptr<FizzServerContext> fizz::server::test::FizzTestServer::getFizzContext ( )
inline

Definition at line 142 of file Utils.h.

References ctx_.

142  {
143  return ctx_;
144  }
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
void fizz::server::test::FizzTestServer::setAcceptEarlyData ( bool  enable)
inline

Definition at line 121 of file Utils.h.

References ctx_.

121  {
122  if (enable) {
123  ctx_->setEarlyDataSettings(
124  true,
125  {std::chrono::seconds(-10), std::chrono::seconds(10)},
126  std::make_shared<AllowAllReplayReplayCache>());
127  } else {
128  ctx_->setEarlyDataSettings(false, ClockSkewTolerance(), nullptr);
129  }
130  }
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
void fizz::server::test::FizzTestServer::setCertificate ( std::unique_ptr< SelfCert cert)
inline

Definition at line 82 of file Utils.h.

References ctx_, and folly::gen::move.

82  {
83  auto certManager = std::make_unique<CertManager>();
84  certManager->addCert(std::move(cert), true);
85  ctx_->setCertManager(std::move(certManager));
86  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
void fizz::server::test::FizzTestServer::setFizzContext ( std::shared_ptr< FizzServerContext ctx)
inline

Definition at line 50 of file Utils.h.

References ctx_.

50  {
51  ctx_ = ctx;
52  }
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
void fizz::server::test::FizzTestServer::setResumption ( bool  enable)
inline

Definition at line 68 of file Utils.h.

References ctx_, and folly::range().

68  {
69  if (enable) {
70  auto ticketCipher = std::make_shared<AeadTicketCipher<
71  OpenSSLEVPCipher<AESGCM128>,
72  TicketCodec<CertificateStorage::X509>,
73  HkdfImpl<Sha256>>>();
74  auto ticketSeed = RandomGenerator<32>().generateRandom();
75  ticketCipher->setTicketSecrets({{folly::range(ticketSeed)}});
76  ctx_->setTicketCipher(ticketCipher);
77  } else {
78  ctx_->setTicketCipher(nullptr);
79  }
80  }
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
void fizz::server::test::FizzTestServer::stopAccepting ( )
inline

Definition at line 132 of file Utils.h.

References socket_.

132  {
133  socket_.reset();
134  }
folly::AsyncServerSocket::UniquePtr socket_
Definition: Utils.h:147

Member Data Documentation

std::shared_ptr<FizzServerContext> fizz::server::test::FizzTestServer::ctx_
private
folly::EventBase& fizz::server::test::FizzTestServer::evb_
private

Definition at line 150 of file Utils.h.

Referenced by connectionAccepted(), and FizzTestServer().

CallbackFactory* fizz::server::test::FizzTestServer::factory_
private

Definition at line 149 of file Utils.h.

Referenced by connectionAccepted().

folly::AsyncServerSocket::UniquePtr fizz::server::test::FizzTestServer::socket_
private

Definition at line 147 of file Utils.h.

Referenced by FizzTestServer(), getAddress(), and stopAccepting().


The documentation for this class was generated from the following file: