38 std::ifstream inputFile(filename);
40 NDN_THROW(
Error(
"Failed to read configuration file: " + filename));
42 load(inputFile, filename);
48 std::istringstream inputStream(input);
49 load(inputStream, filename);
57 boost::property_tree::read_info(input, tree);
59 catch (
const boost::property_tree::info_parser_error& e) {
61 " line " + to_string(e.line()) +
": " + e.message()));
69 BOOST_ASSERT(!filename.empty());
75 m_shouldBypass =
false;
77 m_interestRules.clear();
81 m_isConfigured =
true;
83 for (
const auto& subSection : configSection) {
84 const std::string& sectionName = subSection.first;
87 if (boost::iequals(sectionName,
"rule")) {
90 m_dataRules.push_back(std::move(rule));
93 m_interestRules.push_back(std::move(rule));
96 else if (boost::iequals(sectionName,
"trust-anchor")) {
97 processConfigTrustAnchor(section, filename);
100 NDN_THROW(
Error(
"Error processing configuration file " + filename +
101 ": unrecognized section " + sectionName));
107ValidationPolicyConfig::processConfigTrustAnchor(
const ConfigSection& configSection,
108 const std::string& filename)
110 auto propertyIt = configSection.begin();
113 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type")) {
117 auto baseDir = std::filesystem::absolute(filename).parent_path().lexically_normal();
118 std::string type = propertyIt->second.data();
121 if (boost::iequals(type,
"file")) {
123 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"file-name")) {
124 NDN_THROW(Error(
"Expecting <trust-anchor.file-name>"));
127 std::string file = propertyIt->second.data();
131 if (propertyIt != configSection.end())
132 NDN_THROW(Error(
"Expecting end of <trust-anchor>"));
136 else if (boost::iequals(type,
"base64")) {
138 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"base64-string"))
139 NDN_THROW(Error(
"Expecting <trust-anchor.base64-string>"));
141 std::stringstream ss(propertyIt->second.data());
144 if (propertyIt != configSection.end())
145 NDN_THROW(Error(
"Expecting end of <trust-anchor>"));
150 catch (
const io::Error& e) {
151 NDN_THROW_NESTED(Error(
"Cannot load certificate from base64-string: "s + e.what()));
154 else if (boost::iequals(type,
"dir")) {
155 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"dir"))
156 NDN_THROW(Error(
"Expecting <trust-anchor.dir>"));
158 std::string dirString(propertyIt->second.data());
162 if (propertyIt != configSection.end())
163 NDN_THROW(Error(
"Expecting end of <trust-anchor>"));
167 else if (boost::iequals(type,
"any")) {
168 m_shouldBypass =
true;
171 NDN_THROW(Error(
"Unrecognized <trust-anchor.type>: " + type));
176ValidationPolicyConfig::getRefreshPeriod(ConfigSection::const_iterator& it,
177 const ConfigSection::const_iterator& end)
179 auto refresh = time::nanoseconds::max();
184 if (!boost::iequals(it->first,
"refresh")) {
185 NDN_THROW(Error(
"Expecting <trust-anchor.refresh>"));
188 std::string inputString = it->second.data();
190 char unit = inputString[inputString.size() - 1];
191 std::string refreshString = inputString.substr(0, inputString.size() - 1);
193 int32_t refreshPeriod = -1;
195 refreshPeriod = boost::lexical_cast<int32_t>(refreshString);
197 catch (
const boost::bad_lexical_cast&) {
200 if (refreshPeriod < 0) {
201 NDN_THROW(Error(
"Bad refresh value: " + refreshString));
204 if (refreshPeriod == 0) {
205 return getDefaultRefreshPeriod();
216 NDN_THROW(Error(
"Bad refresh time unit: "s + unit));
221ValidationPolicyConfig::getDefaultRefreshPeriod()
230 BOOST_ASSERT_MSG(!
hasInnerPolicy(),
"ValidationPolicyConfig must be a terminal inner policy");
232 if (m_shouldBypass) {
233 return continueValidation(
nullptr, state);
237 if (!state->getOutcome()) {
243 for (
const auto& rule : m_dataRules) {
246 return continueValidation(make_shared<CertificateRequest>(klName), state);
254 "No rule matched for data `" + data.
getName().toUri() +
"`"});
261 BOOST_ASSERT_MSG(!
hasInnerPolicy(),
"ValidationPolicyConfig must be a terminal inner policy");
263 if (m_shouldBypass) {
264 return continueValidation(
nullptr, state);
268 if (!state->getOutcome()) {
273 if (!state->getOutcome()) {
279 for (
const auto& rule : m_interestRules) {
282 return continueValidation(make_shared<CertificateRequest>(klName), state);
290 "No rule matched for interest `" + interest.
getName().toUri() +
"`"});