Skip to content

Commit

Permalink
Add configurable poll interval to TLSCredProcessor
Browse files Browse the repository at this point in the history
Summary:
Add a constructor which takes a poll Interval arg.
Add a function to set the poll Interval.

Differential Revision: D7094028

fbshipit-source-id: 10dcabb407a48e8ca5e13810e4b25ed80ace1050
  • Loading branch information
Yang Guo authored and facebook-github-bot committed Mar 1, 2018
1 parent 2835f4a commit 21e9e7d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
11 changes: 11 additions & 0 deletions wangle/ssl/TLSCredProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,22 @@ namespace wangle {
TLSCredProcessor::TLSCredProcessor()
: poller_(std::make_unique<FilePoller>(kCredentialPollInterval)) {}

TLSCredProcessor::TLSCredProcessor(std::chrono::milliseconds pollInterval)
: poller_(std::make_unique<FilePoller>(pollInterval)) {}

void TLSCredProcessor::stop() {
poller_->stop();
}

TLSCredProcessor::~TLSCredProcessor() { stop(); }

void TLSCredProcessor::setPollInterval(std::chrono::milliseconds pollInterval) {
poller_->stop();
poller_ = std::make_unique<FilePoller>(pollInterval);
setTicketPathToWatch(ticketFile_);
setCertPathsToWatch(certFiles_);
}

void TLSCredProcessor::addTicketCallback(
std::function<void(TLSTicketKeySeeds)> callback) {
ticketCallbacks_.push_back(std::move(callback));
Expand Down Expand Up @@ -130,4 +140,5 @@ void TLSCredProcessor::certFileUpdated() noexcept {
return folly::none;
}
}

}
3 changes: 3 additions & 0 deletions wangle/ssl/TLSCredProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace wangle {
class TLSCredProcessor {
public:
TLSCredProcessor();
explicit TLSCredProcessor(std::chrono::milliseconds pollInterval);

~TLSCredProcessor();

Expand All @@ -53,6 +54,8 @@ class TLSCredProcessor {

void stop();

void setPollInterval(std::chrono::milliseconds pollInterval);

/**
* This parses a TLS ticket file with the tickets and returns a
* TLSTicketKeySeeds structure if the file is valid.
Expand Down
42 changes: 36 additions & 6 deletions wangle/ssl/test/TLSCredProcessorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ TEST_F(ProcessTicketTest, handleAbsentFile) {
ASSERT_FALSE(seeds);
}

void updateModifiedTime(const std::string& fileName) {
void updateModifiedTime(const std::string& fileName, int elapsed) {
auto previous = fs::last_write_time(fileName);
auto newTime = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::from_time_t(previous) +
std::chrono::seconds(10));
std::chrono::seconds(elapsed));
fs::last_write_time(fileName, newTime);
}

Expand All @@ -99,13 +99,13 @@ TEST_F(ProcessTicketTest, TestUpdateTicketFile) {
certBaton.post();
});
CHECK(writeFile(validTicketData, ticketFile.c_str()));
updateModifiedTime(ticketFile);
updateModifiedTime(ticketFile,10);
EXPECT_TRUE(ticketBaton.try_wait_for(std::chrono::seconds(30)));
ASSERT_TRUE(ticketUpdated);
ASSERT_FALSE(certUpdated);
ticketUpdated = false;
CHECK(writeFile(validTicketData, certFile.c_str()));
updateModifiedTime(certFile);
updateModifiedTime(certFile,10);
EXPECT_TRUE(certBaton.try_wait_for(std::chrono::seconds(30)));
ASSERT_TRUE(certUpdated);
ASSERT_FALSE(ticketUpdated);
Expand All @@ -119,10 +119,40 @@ TEST_F(ProcessTicketTest, TestMultipleCerts) {
certBaton.post();
});
CHECK(writeFile(validTicketData, ticketFile.c_str()));
updateModifiedTime(ticketFile);
updateModifiedTime(ticketFile,10);
EXPECT_TRUE(certBaton.try_wait_for(std::chrono::seconds(30)));
certBaton.reset();
CHECK(writeFile(validTicketData, certFile.c_str()));
updateModifiedTime(certFile);
updateModifiedTime(certFile,10);
EXPECT_TRUE(certBaton.try_wait_for(std::chrono::seconds(30)));
}

TEST_F(ProcessTicketTest, TestSetPullInterval) {
Baton<> ticketBaton;
Baton<> certBaton;
TLSCredProcessor processor;
processor.setTicketPathToWatch(ticketFile);
processor.setCertPathsToWatch({certFile});
processor.setPollInterval(std::chrono::seconds(3));
bool ticketUpdated = false;
bool certUpdated = false;
processor.addTicketCallback([&](TLSTicketKeySeeds) {
ticketUpdated = true;
ticketBaton.post();
});
processor.addCertCallback([&]() {
certUpdated = true;
certBaton.post();
});
CHECK(writeFile(validTicketData, ticketFile.c_str()));
updateModifiedTime(ticketFile,3);
EXPECT_TRUE(ticketBaton.try_wait_for(std::chrono::seconds(5)));
ASSERT_TRUE(ticketUpdated);
ASSERT_FALSE(certUpdated);
ticketUpdated = false;
CHECK(writeFile(validTicketData, certFile.c_str()));
updateModifiedTime(certFile,3);
EXPECT_TRUE(certBaton.try_wait_for(std::chrono::seconds(5)));
ASSERT_TRUE(certUpdated);
ASSERT_FALSE(ticketUpdated);
}

0 comments on commit 21e9e7d

Please sign in to comment.