27 #include <boost/algorithm/string/predicate.hpp> 32 namespace validator_config {
36 const shared_ptr<ValidationState>& state)
53 , m_relation(relation)
59 const shared_ptr<ValidationState>& state)
65 std::ostringstream os;
66 os <<
"KeyLocator check failed: name relation " << m_name <<
" " << m_relation
67 <<
" for packet " << pktName <<
" is invalid" 68 <<
" (KeyLocator=" << klName <<
", identity=" << identity <<
")";
82 bool result = m_regex.
match(klName);
84 std::ostringstream os;
85 os <<
"KeyLocator check failed: regex " << m_regex <<
" for packet " << pktName <<
" is invalid" 86 <<
" (KeyLocator=" << klName <<
")";
93 const std::string& klNameExpr,
const std::string klNameExpand,
95 : m_hyperPRegex(pktNameExpr, pktNameExpand)
96 , m_hyperKRegex(klNameExpr, klNameExpand)
97 , m_hyperRelation(hyperRelation)
103 const shared_ptr<ValidationState>& state)
105 if (!m_hyperPRegex.
match(pktName) || !m_hyperKRegex.
match(klName)) {
106 std::ostringstream os;
107 os <<
"Packet " << pktName <<
" (" <<
"KeyLocator=" << klName <<
") does not match " 108 <<
"the hyper relation rule pkt=" << m_hyperPRegex <<
", key=" << m_hyperKRegex;
115 std::ostringstream os;
116 os <<
"KeyLocator check failed: hyper relation " << m_hyperRelation
117 <<
" pkt=" << m_hyperPRegex <<
", key=" << m_hyperKRegex
118 <<
" of packet " << pktName <<
" (KeyLocator=" << klName <<
") is invalid";
127 auto propertyIt = configSection.begin();
130 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type")) {
131 BOOST_THROW_EXCEPTION(
Error(
"Expecting <checker.type>"));
134 std::string type = propertyIt->second.data();
135 if (boost::iequals(type,
"customized")) {
136 return createCustomizedChecker(configSection, configFilename);
138 else if (boost::iequals(type,
"hierarchical")) {
139 return createHierarchicalChecker(configSection, configFilename);
142 BOOST_THROW_EXCEPTION(
Error(
"Unrecognized <checker.type>: " + type));
147 Checker::createCustomizedChecker(
const ConfigSection& configSection,
148 const std::string& configFilename)
150 auto propertyIt = configSection.begin();
155 if (propertyIt != configSection.end() && boost::iequals(propertyIt->first,
"sig-type")) {
161 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"key-locator")) {
162 BOOST_THROW_EXCEPTION(Error(
"Expecting <checker.key-locator>"));
165 auto checker = createKeyLocatorChecker(propertyIt->second, configFilename);
168 if (propertyIt != configSection.end()) {
169 BOOST_THROW_EXCEPTION(Error(
"Expecting end of <checker>"));
175 Checker::createHierarchicalChecker(
const ConfigSection& configSection,
176 const std::string& configFilename)
178 auto propertyIt = configSection.begin();
183 if (propertyIt != configSection.end() && boost::iequals(propertyIt->first,
"sig-type")) {
188 if (propertyIt != configSection.end()) {
189 BOOST_THROW_EXCEPTION(Error(
"Expecting end of <checker>"));
191 return make_unique<HyperRelationChecker>(
"^(<>*)$",
"\\1",
192 "^(<>*)<KEY><>$",
"\\1",
197 Checker::createKeyLocatorChecker(
const ConfigSection& configSection,
198 const std::string& configFilename)
200 auto propertyIt = configSection.begin();
203 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"type"))
204 BOOST_THROW_EXCEPTION(Error(
"Expecting <checker.key-locator.type>"));
206 std::string type = propertyIt->second.data();
207 if (boost::iequals(type,
"name"))
208 return createKeyLocatorNameChecker(configSection, configFilename);
210 BOOST_THROW_EXCEPTION(Error(
"Unrecognized <checker.key-locator.type>: " + type));
214 Checker::createKeyLocatorNameChecker(
const ConfigSection& configSection,
215 const std::string& configFilename)
217 auto propertyIt = configSection.begin();
220 if (propertyIt == configSection.end())
221 BOOST_THROW_EXCEPTION(Error(
"Unexpected end of <checker.key-locator>"));
223 if (boost::iequals(propertyIt->first,
"name")) {
226 name =
Name(propertyIt->second.data());
228 catch (
const Name::Error&) {
229 BOOST_THROW_EXCEPTION(Error(
"Invalid <checker.key-locator.name>: " + propertyIt->second.data()));
233 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first,
"relation")) {
234 BOOST_THROW_EXCEPTION(Error(
"Expecting <checker.key-locator.relation>"));
237 std::string relationString = propertyIt->second.data();
242 if (propertyIt != configSection.end()) {
243 BOOST_THROW_EXCEPTION(Error(
"Expecting end of <checker.key-locator>"));
245 return make_unique<NameRelationChecker>(
name, relation);
247 else if (boost::iequals(propertyIt->first,
"regex")) {
248 std::string regexString = propertyIt->second.data();
251 if (propertyIt != configSection.end()) {
252 BOOST_THROW_EXCEPTION(Error(
"Expecting end of <checker.key-locator>"));
256 return make_unique<RegexChecker>(
Regex(regexString));
258 catch (
const Regex::Error&) {
259 BOOST_THROW_EXCEPTION(Error(
"Invalid <checker.key-locator.regex>: " + regexString));
262 else if (boost::iequals(propertyIt->first,
"hyper-relation")) {
264 ConfigSection::const_iterator hPropertyIt = hSection.begin();
267 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"k-regex")) {
268 BOOST_THROW_EXCEPTION(Error(
"Expecting <checker.key-locator.hyper-relation.k-regex>"));
271 std::string kRegex = hPropertyIt->second.data();
275 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"k-expand")) {
276 BOOST_THROW_EXCEPTION(Error(
"Expecting <checker.key-locator.hyper-relation.k-expand>"));
279 std::string kExpand = hPropertyIt->second.data();
283 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"h-relation")) {
284 BOOST_THROW_EXCEPTION(Error(
"Expecting <checker.key-locator.hyper-relation.h-relation>"));
287 std::string hRelation = hPropertyIt->second.data();
291 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"p-regex")) {
292 BOOST_THROW_EXCEPTION(Error(
"Expecting <checker.key-locator.hyper-relation.p-regex>"));
295 std::string pRegex = hPropertyIt->second.data();
299 if (hPropertyIt == hSection.end() || !boost::iequals(hPropertyIt->first,
"p-expand")) {
300 BOOST_THROW_EXCEPTION(Error(
"Expecting <checker.key-locator.hyper-relation.p-expand>"));
303 std::string pExpand = hPropertyIt->second.data();
306 if (hPropertyIt != hSection.end()) {
307 BOOST_THROW_EXCEPTION(Error(
"Expecting end of <checker.key-locator.hyper-relation>"));
312 return make_unique<HyperRelationChecker>(pRegex, pExpand, kRegex, kExpand, relation);
314 catch (
const Regex::Error&) {
315 BOOST_THROW_EXCEPTION(Error(
"Invalid regex for <key-locator.hyper-relation>"));
319 BOOST_THROW_EXCEPTION(Error(
"Unrecognized <checker.key-locator>: " + propertyIt->first));
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Copyright (c) 2011-2015 Regents of the University of California.
NameRelationChecker(const Name &name, const NameRelation &relation)
bool checkNames(const Name &pktName, const Name &klName, const shared_ptr< ValidationState > &state) override
const size_t MIN_SIZE
minimal number of components for Signed Interest
RegexChecker(const Regex ®ex)
bool checkNameRelation(NameRelation relation, const Name &name1, const Name &name2)
check whether name1 and name2 satisfies relation
bool checkNames(const Name &pktName, const Name &klName, const shared_ptr< ValidationState > &state) override
NameRelation getNameRelationFromString(const std::string &relationString)
convert relationString to NameRelation
Represents an absolute name.
size_t size() const
Get number of components.
boost::property_tree::ptree ConfigSection
virtual Name expand(const std::string &expand="")
bool checkNames(const Name &pktName, const Name &klName, const shared_ptr< ValidationState > &state) override
bool match(const Name &name)
HyperRelationChecker(const std::string &pktNameExpr, const std::string pktNameExpand, const std::string &klNameExpr, const std::string klNameExpand, const NameRelation &hyperRelation)
bool check(uint32_t pktType, const Name &pktName, const Name &klName, const shared_ptr< ValidationState > &state)
Check if packet name ane KeyLocator satisfy the checker's conditions.
virtual bool checkNames(const Name &pktName, const Name &klName, const shared_ptr< ValidationState > &state)=0
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
static unique_ptr< Checker > create(const ConfigSection &configSection, const std::string &configFilename)
create a checker from configuration section