24 #include "../../util/io.hpp" 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 {
39 : m_shouldBypass(false)
40 , m_isConfigured(false)
47 std::ifstream inputFile(filename);
49 BOOST_THROW_EXCEPTION(
Error(
"Failed to read configuration file: " + filename));
51 load(inputFile, filename);
57 std::istringstream inputStream(input);
58 load(inputStream, filename);
66 boost::property_tree::read_info(input, tree);
68 catch (
const boost::property_tree::info_parser_error& e) {
69 BOOST_THROW_EXCEPTION(
Error(
"Failed to parse configuration file " + filename +
70 " line " +
to_string(e.line()) +
": " + e.message()));
79 m_shouldBypass =
false;
81 m_interestRules.clear();
82 m_validator->resetAnchors();
83 m_validator->resetVerifiedCertificates();
85 m_isConfigured =
true;
87 BOOST_ASSERT(!filename.empty());
89 if (configSection.begin() == configSection.end()) {
90 BOOST_THROW_EXCEPTION(
Error(
"Error processing configuration file " + filename +
": no data"));
93 for (
const auto& subSection : configSection) {
94 const std::string& sectionName = subSection.first;
97 if (boost::iequals(sectionName,
"rule")) {
100 m_dataRules.push_back(std::move(rule));
103 m_interestRules.push_back(std::move(rule));
106 else if (boost::iequals(sectionName,
"trust-anchor")) {
107 processConfigTrustAnchor(section, filename);
110 BOOST_THROW_EXCEPTION(
Error(
"Error processing configuration file " + filename +
111 ": unrecognized section " + sectionName));
117 ValidationPolicyConfig::processConfigTrustAnchor(
const ConfigSection& configSection,
118 const std::string& filename)
120 using namespace boost::filesystem;
122 auto propertyIt = configSection.begin();
125 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type")) {
126 BOOST_THROW_EXCEPTION(
Error(
"Expecting <trust-anchor.type>"));
129 std::string type = propertyIt->second.data();
132 if (boost::iequals(type,
"file")) {
134 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"file-name")) {
135 BOOST_THROW_EXCEPTION(Error(
"Expecting <trust-anchor.file-name>"));
138 std::string file = propertyIt->second.data();
141 time::nanoseconds refresh = getRefreshPeriod(propertyIt, configSection.end());
142 if (propertyIt != configSection.end())
143 BOOST_THROW_EXCEPTION(Error(
"Expecting end of <trust-anchor>"));
145 m_validator->loadAnchor(filename, absolute(file, path(filename).parent_path()).
string(),
148 else if (boost::iequals(type,
"base64")) {
150 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"base64-string"))
151 BOOST_THROW_EXCEPTION(Error(
"Expecting <trust-anchor.base64-string>"));
153 std::stringstream ss(propertyIt->second.data());
156 if (propertyIt != configSection.end())
157 BOOST_THROW_EXCEPTION(Error(
"Expecting end of <trust-anchor>"));
159 auto idCert = io::load<Certificate>(ss);
160 if (idCert !=
nullptr) {
161 m_validator->loadAnchor(
"", std::move(*idCert));
164 BOOST_THROW_EXCEPTION(Error(
"Cannot decode certificate from base64-string"));
167 else if (boost::iequals(type,
"dir")) {
168 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"dir"))
169 BOOST_THROW_EXCEPTION(Error(
"Expecting <trust-anchor.dir>"));
171 std::string dirString(propertyIt->second.data());
174 time::nanoseconds refresh = getRefreshPeriod(propertyIt, configSection.end());
175 if (propertyIt != configSection.end())
176 BOOST_THROW_EXCEPTION(Error(
"Expecting end of <trust-anchor>"));
178 path dirPath = absolute(dirString, path(filename).parent_path());
179 m_validator->loadAnchor(dirString, dirPath.string(), refresh,
true);
181 else if (boost::iequals(type,
"any")) {
182 m_shouldBypass =
true;
185 BOOST_THROW_EXCEPTION(Error(
"Unrecognized <trust-anchor.type>: " + type));
190 ValidationPolicyConfig::getRefreshPeriod(ConfigSection::const_iterator& it,
191 const ConfigSection::const_iterator& end)
193 auto refresh = time::nanoseconds::max();
198 if (!boost::iequals(it->first,
"refresh")) {
199 BOOST_THROW_EXCEPTION(Error(
"Expecting <trust-anchor.refresh>"));
202 std::string inputString = it->second.data();
204 char unit = inputString[inputString.size() - 1];
205 std::string refreshString = inputString.substr(0, inputString.size() - 1);
207 int32_t refreshPeriod = -1;
209 refreshPeriod = boost::lexical_cast<int32_t>(refreshString);
211 catch (
const boost::bad_lexical_cast&) {
214 if (refreshPeriod < 0) {
215 BOOST_THROW_EXCEPTION(Error(
"Bad refresh value: " + refreshString));
218 if (refreshPeriod == 0) {
219 return getDefaultRefreshPeriod();
224 return time::hours(refreshPeriod);
226 return time::minutes(refreshPeriod);
228 return time::seconds(refreshPeriod);
230 BOOST_THROW_EXCEPTION(Error(std::string(
"Bad refresh time unit: ") + unit));
235 ValidationPolicyConfig::getDefaultRefreshPeriod()
244 BOOST_ASSERT_MSG(!
hasInnerPolicy(),
"ValidationPolicyConfig must be a terminal inner policy");
246 if (m_shouldBypass) {
247 return continueValidation(
nullptr, state);
251 if (!state->getOutcome()) {
255 for (
const auto& rule : m_dataRules) {
258 return continueValidation(make_shared<CertificateRequest>(
Interest(klName)), state);
266 "No rule matched for data `" + data.
getName().
toUri() +
"`"});
273 BOOST_ASSERT_MSG(!
hasInnerPolicy(),
"ValidationPolicyConfig must be a terminal inner policy");
275 if (m_shouldBypass) {
276 return continueValidation(
nullptr, state);
280 if (!state->getOutcome()) {
284 for (
const auto& rule : m_interestRules) {
287 return continueValidation(make_shared<CertificateRequest>(
Interest(klName)), state);
295 "No rule matched for interest `" + interest.
getName().
toUri() +
"`"});
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Get URI representation of the name.
const Name & getName() const
Get name.
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)
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.
std::string to_string(const V &v)
Represents a Data packet.
void load(const std::string &filename)
const Name & getName() const