proxygen
wangle::TLSCredProcessor Class Reference

#include <TLSCredProcessor.h>

Public Member Functions

 TLSCredProcessor ()
 
 TLSCredProcessor (std::chrono::milliseconds pollInterval)
 
 ~TLSCredProcessor ()
 
void setTicketPathToWatch (const std::string &ticketFile)
 
void setCertPathsToWatch (std::set< std::string > certFiles)
 
void addTicketCallback (std::function< void(wangle::TLSTicketKeySeeds)> callback)
 
void addCertCallback (std::function< void()> callback)
 
void stop ()
 
void setPollInterval (std::chrono::milliseconds pollInterval)
 

Static Public Member Functions

static folly::Optional< wangle::TLSTicketKeySeedsprocessTLSTickets (const std::string &fileName)
 

Private Member Functions

void ticketFileUpdated (const std::string &ticketFile) noexcept
 
void certFileUpdated () noexcept
 

Private Attributes

std::unique_ptr< FilePollerpoller_
 
std::string ticketFile_
 
std::set< std::stringcertFiles_
 
std::vector< std::function< void(wangle::TLSTicketKeySeeds)> > ticketCallbacks_
 
std::vector< std::function< void()> > certCallbacks_
 

Detailed Description

A class that monitors files related to TLS credentials that fire callbacks when they change. Callbacks are fired in a background thread.

Definition at line 31 of file TLSCredProcessor.h.

Constructor & Destructor Documentation

wangle::TLSCredProcessor::TLSCredProcessor ( )

Definition at line 44 of file TLSCredProcessor.cpp.

45  : poller_(std::make_unique<FilePoller>(kCredentialPollInterval)) {}
std::unique_ptr< FilePoller > poller_
wangle::TLSCredProcessor::TLSCredProcessor ( std::chrono::milliseconds  pollInterval)
explicit

Definition at line 47 of file TLSCredProcessor.cpp.

48  : poller_(std::make_unique<FilePoller>(pollInterval)) {}
std::unique_ptr< FilePoller > poller_
wangle::TLSCredProcessor::~TLSCredProcessor ( )

Definition at line 54 of file TLSCredProcessor.cpp.

References stop().

Member Function Documentation

void wangle::TLSCredProcessor::addCertCallback ( std::function< void()>  callback)

Definition at line 68 of file TLSCredProcessor.cpp.

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

Referenced by TEST_F().

69  {
70  certCallbacks_.push_back(std::move(callback));
71 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::vector< std::function< void()> > certCallbacks_
void wangle::TLSCredProcessor::addTicketCallback ( std::function< void(wangle::TLSTicketKeySeeds)>  callback)

Definition at line 63 of file TLSCredProcessor.cpp.

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

Referenced by TEST_F().

64  {
65  ticketCallbacks_.push_back(std::move(callback));
66 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::vector< std::function< void(wangle::TLSTicketKeySeeds)> > ticketCallbacks_
void wangle::TLSCredProcessor::certFileUpdated ( )
privatenoexcept

Definition at line 107 of file TLSCredProcessor.cpp.

References certCallbacks_.

Referenced by setCertPathsToWatch().

107  {
108  for (const auto& callback: certCallbacks_) {
109  callback();
110  }
111 }
std::vector< std::function< void()> > certCallbacks_
Optional< TLSTicketKeySeeds > wangle::TLSCredProcessor::processTLSTickets ( const std::string fileName)
static

This parses a TLS ticket file with the tickets and returns a TLSTicketKeySeeds structure if the file is valid. The TLS ticket file is formatted as a json blob { "old": [ "seed1", ... ], "new": [ ... ], "current": [ ... ] } Seeds are aribitrary length secret strings which are used to derive ticket encryption keys.

Definition at line 113 of file TLSCredProcessor.cpp.

References folly::dynamic::count(), wangle::TLSTicketKeySeeds::currentSeeds, wangle::TLSTicketKeySeeds::newSeeds, folly::none, wangle::TLSTicketKeySeeds::oldSeeds, folly::parseJson(), folly::readFile(), string, folly::dynamic::type(), and folly::WARNING.

Referenced by main(), and ticketFileUpdated().

114  {
115  try {
116  std::string jsonData;
117  if (!folly::readFile(fileName.c_str(), jsonData)) {
118  LOG(WARNING) << "Failed to read " << fileName
119  << "; Ticket seeds are unavailable.";
120  return folly::none;
121  }
122  folly::dynamic conf = folly::parseJson(jsonData);
123  if (conf.type() != dynamic::Type::OBJECT) {
124  LOG(WARNING) << "Error parsing " << fileName << " expected object";
125  return folly::none;
126  }
127  TLSTicketKeySeeds seedData;
128  if (conf.count("old")) {
129  insertSeeds(conf["old"], seedData.oldSeeds);
130  }
131  if (conf.count("current")) {
132  insertSeeds(conf["current"], seedData.currentSeeds);
133  }
134  if (conf.count("new")) {
135  insertSeeds(conf["new"], seedData.newSeeds);
136  }
137  return seedData;
138  } catch (const std::exception& ex) {
139  LOG(WARNING) << "Parsing " << fileName << " failed: " << ex.what();
140  return folly::none;
141  }
142 }
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
IfIsNonStringDynamicConvertible< K, std::size_t > count(K &&) const
Definition: dynamic-inl.h:843
const char * string
Definition: Conv.cpp:212
Type type() const
Definition: dynamic-inl.h:514
constexpr None none
Definition: Optional.h:87
void wangle::TLSCredProcessor::setCertPathsToWatch ( std::set< std::string certFiles)

Set cert related files to watch. This would include paths like cert, key, and CA. Cert callbacks will be fired if any of these change. Empty strings are ignored.

Definition at line 84 of file TLSCredProcessor.cpp.

References certFiles_, certFileUpdated(), folly::gen::move, and poller_.

Referenced by main(), setPollInterval(), and TEST_F().

84  {
85  for (const auto& path: certFiles_) {
86  poller_->removeFileToTrack(path);
87  }
88  certFiles_ = std::move(certFiles);
89  if (!certFiles_.empty()) {
90  auto certChangedCob = [this]() { certFileUpdated(); };
91  for (const auto& path: certFiles_) {
92  poller_->addFileToTrack(path, certChangedCob);
93  }
94  }
95 }
std::set< std::string > certFiles_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< FilePoller > poller_
void wangle::TLSCredProcessor::setPollInterval ( std::chrono::milliseconds  pollInterval)

Definition at line 56 of file TLSCredProcessor.cpp.

References certFiles_, poller_, setCertPathsToWatch(), setTicketPathToWatch(), and ticketFile_.

Referenced by TEST_F().

56  {
57  poller_->stop();
58  poller_ = std::make_unique<FilePoller>(pollInterval);
61 }
void setTicketPathToWatch(const std::string &ticketFile)
std::set< std::string > certFiles_
std::unique_ptr< FilePoller > poller_
void setCertPathsToWatch(std::set< std::string > certFiles)
void wangle::TLSCredProcessor::setTicketPathToWatch ( const std::string ticketFile)

Set the ticket path to watch. Any previous ticket path will stop being watched. This is not thread safe.

Definition at line 73 of file TLSCredProcessor.cpp.

References poller_, ticketFile_, and ticketFileUpdated().

Referenced by main(), setPollInterval(), and TEST_F().

73  {
74  if (!ticketFile_.empty()) {
75  poller_->removeFileToTrack(ticketFile_);
76  }
77  ticketFile_ = ticketFile;
78  if (!ticketFile_.empty()) {
79  auto ticketsChangedCob = [=]() { ticketFileUpdated(ticketFile); };
80  poller_->addFileToTrack(ticketFile_, ticketsChangedCob);
81  }
82 }
std::unique_ptr< FilePoller > poller_
void ticketFileUpdated(const std::string &ticketFile) noexcept
void wangle::TLSCredProcessor::stop ( )

Definition at line 50 of file TLSCredProcessor.cpp.

References poller_.

Referenced by ~TLSCredProcessor().

50  {
51  poller_->stop();
52 }
std::unique_ptr< FilePoller > poller_
void wangle::TLSCredProcessor::ticketFileUpdated ( const std::string ticketFile)
privatenoexcept

Definition at line 97 of file TLSCredProcessor.cpp.

References processTLSTickets(), and ticketCallbacks_.

Referenced by setTicketPathToWatch().

98  {
99  auto seeds = processTLSTickets(ticketFile);
100  if (seeds) {
101  for (auto& callback : ticketCallbacks_) {
102  callback(*seeds);
103  }
104  }
105 }
static folly::Optional< wangle::TLSTicketKeySeeds > processTLSTickets(const std::string &fileName)
std::vector< std::function< void(wangle::TLSTicketKeySeeds)> > ticketCallbacks_

Member Data Documentation

std::vector<std::function<void()> > wangle::TLSCredProcessor::certCallbacks_
private

Definition at line 89 of file TLSCredProcessor.h.

Referenced by addCertCallback(), and certFileUpdated().

std::set<std::string> wangle::TLSCredProcessor::certFiles_
private

Definition at line 87 of file TLSCredProcessor.h.

Referenced by setCertPathsToWatch(), and setPollInterval().

std::unique_ptr<FilePoller> wangle::TLSCredProcessor::poller_
private
std::vector<std::function<void(wangle::TLSTicketKeySeeds)> > wangle::TLSCredProcessor::ticketCallbacks_
private

Definition at line 88 of file TLSCredProcessor.h.

Referenced by addTicketCallback(), and ticketFileUpdated().

std::string wangle::TLSCredProcessor::ticketFile_
private

Definition at line 86 of file TLSCredProcessor.h.

Referenced by setPollInterval(), and setTicketPathToWatch().


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