proxygen
TLSCredProcessor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 
18 #include <folly/dynamic.h>
19 #include <folly/json.h>
20 #include <folly/FileUtil.h>
21 #include <folly/Memory.h>
22 
23 using namespace folly;
24 
25 namespace {
26 
27 constexpr std::chrono::milliseconds kCredentialPollInterval =
28  std::chrono::duration_cast<std::chrono::milliseconds>(
29  std::chrono::seconds(10));
30 
31 void insertSeeds(const folly::dynamic& keyConfig,
32  std::vector<std::string>& seedList) {
33  if (!keyConfig.isArray()) {
34  return;
35  }
36  for (const auto& seed : keyConfig) {
37  seedList.push_back(seed.asString());
38  }
39 }
40 }
41 
42 namespace wangle {
43 
44 TLSCredProcessor::TLSCredProcessor()
45  : poller_(std::make_unique<FilePoller>(kCredentialPollInterval)) {}
46 
47 TLSCredProcessor::TLSCredProcessor(std::chrono::milliseconds pollInterval)
48  : poller_(std::make_unique<FilePoller>(pollInterval)) {}
49 
51  poller_->stop();
52 }
53 
55 
56 void TLSCredProcessor::setPollInterval(std::chrono::milliseconds pollInterval) {
57  poller_->stop();
58  poller_ = std::make_unique<FilePoller>(pollInterval);
61 }
62 
64  std::function<void(TLSTicketKeySeeds)> callback) {
65  ticketCallbacks_.push_back(std::move(callback));
66 }
67 
69  std::function<void()> callback) {
70  certCallbacks_.push_back(std::move(callback));
71 }
72 
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 }
83 
84 void TLSCredProcessor::setCertPathsToWatch(std::set<std::string> certFiles) {
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 }
96 
98  const std::string& ticketFile) noexcept {
99  auto seeds = processTLSTickets(ticketFile);
100  if (seeds) {
101  for (auto& callback : ticketCallbacks_) {
102  callback(*seeds);
103  }
104  }
105 }
106 
108  for (const auto& callback: certCallbacks_) {
109  callback();
110  }
111 }
112 
114  const std::string& fileName) {
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 }
143 
144 }
std::vector< std::string > newSeeds
void setTicketPathToWatch(const std::string &ticketFile)
std::vector< std::string > currentSeeds
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
static folly::Optional< wangle::TLSTicketKeySeeds > processTLSTickets(const std::string &fileName)
dynamic parseJson(StringPiece range)
Definition: json.cpp:900
static const int seed
std::set< std::string > certFiles_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
IfIsNonStringDynamicConvertible< K, std::size_t > count(K &&) const
Definition: dynamic-inl.h:843
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
bool isArray() const
Definition: dynamic-inl.h:498
std::unique_ptr< FilePoller > poller_
void setPollInterval(std::chrono::milliseconds pollInterval)
std::vector< std::function< void()> > certCallbacks_
void addCertCallback(std::function< void()> callback)
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Definition: Memory.h:259
std::vector< std::function< void(wangle::TLSTicketKeySeeds)> > ticketCallbacks_
void setCertPathsToWatch(std::set< std::string > certFiles)
const char * string
Definition: Conv.cpp:212
std::vector< std::string > oldSeeds
Type type() const
Definition: dynamic-inl.h:514
void ticketFileUpdated(const std::string &ticketFile) noexcept
constexpr None none
Definition: Optional.h:87
void addTicketCallback(std::function< void(wangle::TLSTicketKeySeeds)> callback)