26 #include <boost/algorithm/string/predicate.hpp> 27 #include <boost/filesystem.hpp> 28 #include <boost/lexical_cast.hpp> 29 #include <boost/property_tree/info_parser.hpp> 36 namespace validator_config {
41 std::ifstream inputFile(filename);
43 NDN_THROW(
Error(
"Failed to read configuration file: " + filename));
45 load(inputFile, filename);
51 std::istringstream inputStream(input);
52 load(inputStream, filename);
60 boost::property_tree::read_info(input, tree);
62 catch (
const boost::property_tree::info_parser_error& e) {
64 " line " +
to_string(e.line()) +
": " + e.message()));
72 BOOST_ASSERT(!filename.empty());
78 m_shouldBypass =
false;
80 m_interestRules.clear();
84 m_isConfigured =
true;
86 for (
const auto& subSection : configSection) {
87 const std::string& sectionName = subSection.first;
90 if (boost::iequals(sectionName,
"rule")) {
93 m_dataRules.push_back(std::move(rule));
96 m_interestRules.push_back(std::move(rule));
99 else if (boost::iequals(sectionName,
"trust-anchor")) {
100 processConfigTrustAnchor(section, filename);
103 NDN_THROW(
Error(
"Error processing configuration file " + filename +
104 ": unrecognized section " + sectionName));
110 ValidationPolicyConfig::processConfigTrustAnchor(
const ConfigSection& configSection,
111 const std::string& filename)
113 using namespace boost::filesystem;
115 auto propertyIt = configSection.begin();
118 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type")) {
122 std::string type = propertyIt->second.data();
125 if (boost::iequals(type,
"file")) {
127 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"file-name")) {
131 std::string file = propertyIt->second.data();
134 time::nanoseconds refresh = getRefreshPeriod(propertyIt, configSection.end());
135 if (propertyIt != configSection.end())
141 else if (boost::iequals(type,
"base64")) {
143 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"base64-string"))
146 std::stringstream ss(propertyIt->second.data());
149 if (propertyIt != configSection.end())
152 auto idCert = io::load<Certificate>(ss);
153 if (idCert !=
nullptr) {
160 else if (boost::iequals(type,
"dir")) {
161 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"dir"))
164 std::string dirString(propertyIt->second.data());
167 time::nanoseconds refresh = getRefreshPeriod(propertyIt, configSection.end());
168 if (propertyIt != configSection.end())
171 path dirPath = absolute(dirString, path(filename).parent_path());
174 else if (boost::iequals(type,
"any")) {
175 m_shouldBypass =
true;
183 ValidationPolicyConfig::getRefreshPeriod(ConfigSection::const_iterator& it,
184 const ConfigSection::const_iterator& end)
186 auto refresh = time::nanoseconds::max();
191 if (!boost::iequals(it->first,
"refresh")) {
195 std::string inputString = it->second.data();
197 char unit = inputString[inputString.size() - 1];
198 std::string refreshString = inputString.substr(0, inputString.size() - 1);
200 int32_t refreshPeriod = -1;
202 refreshPeriod = boost::lexical_cast<int32_t>(refreshString);
204 catch (
const boost::bad_lexical_cast&) {
207 if (refreshPeriod < 0) {
211 if (refreshPeriod == 0) {
212 return getDefaultRefreshPeriod();
217 return time::hours(refreshPeriod);
219 return time::minutes(refreshPeriod);
221 return time::seconds(refreshPeriod);
228 ValidationPolicyConfig::getDefaultRefreshPeriod()
237 BOOST_ASSERT_MSG(!
hasInnerPolicy(),
"ValidationPolicyConfig must be a terminal inner policy");
239 if (m_shouldBypass) {
240 return continueValidation(
nullptr, state);
244 if (!state->getOutcome()) {
248 for (
const auto& rule : m_dataRules) {
251 return continueValidation(make_shared<CertificateRequest>(klName), state);
259 "No rule matched for data `" + data.
getName().
toUri() +
"`"});
266 BOOST_ASSERT_MSG(!
hasInnerPolicy(),
"ValidationPolicyConfig must be a terminal inner policy");
268 if (m_shouldBypass) {
269 return continueValidation(
nullptr, state);
273 if (!state->getOutcome()) {
277 for (
const auto& rule : m_interestRules) {
280 return continueValidation(make_shared<CertificateRequest>(klName), state);
288 "No rule matched for interest `" + interest.
getName().
toUri() +
"`"});
void loadAnchor(const std::string &groupId, Certificate &&cert)
load static trust anchor.
std::string to_string(const T &val)
static unique_ptr< Rule > create(const ConfigSection &configSection, const std::string &configFilename)
create a rule from configuration section
Represents an Interest packet.
std::function< void(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state)> ValidationContinuation
static Name getKeyLocatorName(const SignatureInfo &si, ValidationState &state)
const Name & getName() const noexcept
Get name.
Represents an absolute name.
boost::property_tree::ptree ConfigSection
bool hasInnerPolicy() const
Check if inner policy is set.
void checkPolicy(const Data &data, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Check data against the policy.
const Name & getName() const noexcept
void resetAnchors()
remove any previously loaded static or dynamic trust anchor
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Represents a Data packet.
void load(const std::string &filename)
Load policy from file filename.
void resetVerifiedCertificates()
Remove any cached verified certificates.