loader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef ACL_LOADER_H
  15. #define ACL_LOADER_H
  16. #include "acl.h"
  17. #include <cc/data.h>
  18. #include <boost/function.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <map>
  21. namespace isc {
  22. namespace acl {
  23. /**
  24. * \brief Exception for bad ACL specifications.
  25. *
  26. * This will be thrown by the Loader if the ACL description is malformed
  27. * in some way.
  28. *
  29. * It also can hold optional JSON element where was the error detected, so
  30. * it can be examined.
  31. *
  32. * Checks may subclass this exception for similar errors if they see it fit.
  33. */
  34. class LoaderError : public BadValue {
  35. private:
  36. const data::ConstElementPtr element_;
  37. public:
  38. /**
  39. * \brief Constructor.
  40. *
  41. * Should be used with isc_throw if the fourth argument isn't used.
  42. *
  43. * \param file The file where the throw happened.
  44. * \param line Similar as file, just for the line number.
  45. * \param what Human readable description of what happened.
  46. * \param element This might be passed to hold the JSON element where
  47. * the error was detected.
  48. */
  49. LoaderError(const char* file, size_t line, const char* what,
  50. data::ConstElementPtr element = data::ConstElementPtr()) :
  51. BadValue(file, line, what),
  52. element_(element)
  53. {}
  54. ~ LoaderError() throw() {}
  55. /**
  56. * \brief Get the element.
  57. *
  58. * This returns the element where the error was detected. Note that it
  59. * might be NULL in some situations.
  60. */
  61. const data::ConstElementPtr& element() const {
  62. return (element_);
  63. }
  64. };
  65. /**
  66. * \brief Loader of the default actions of ACLs.
  67. *
  68. * Declared outside the Loader class, as this one does not need to be
  69. * templated. This will throw LoaderError if the parameter isn't string
  70. * or if it doesn't contain one of the accepted values.
  71. *
  72. * \param action The JSON representation of the action. It must be a string
  73. * and contain one of "ACCEPT", "REJECT" or "DENY".
  74. * \note We could define different names or add aliases if needed.
  75. */
  76. Action defaultActionLoader(data::ConstElementPtr action);
  77. /**
  78. * \brief Loader of ACLs.
  79. *
  80. * The goal of this class is to convert JSON description of an ACL to object
  81. * of the Acl class (including the checks inside it).
  82. *
  83. * The class can be used to load the checks only. This is supposed to be used
  84. * by compound checks to create the subexpressions.
  85. *
  86. * To allow any kind of checks to exist in the application, creators are
  87. * registered for the names of the checks.
  88. */
  89. template<typename Context, typename Action = isc::acl::Action> class Loader {
  90. public:
  91. /**
  92. * \brief Constructor.
  93. *
  94. * \param actionLoader is the loader which will be used to convert actions
  95. * from their JSON representation. The default value is suitable for
  96. * the isc::acl::Action enum. If you did not specify the second
  97. * template argument, you don't need to specify this loader.
  98. */
  99. Loader(boost::function1<Action, data::ConstElementPtr> actionLoader =
  100. &defaultActionLoader)
  101. { }
  102. /**
  103. * \brief Creator of the checks.
  104. *
  105. * This can be registered within the Loader and will be used to create the
  106. * checks.
  107. */
  108. class CheckCreator {
  109. public:
  110. /**
  111. * \brief List of names supported by this loader.
  112. *
  113. * List of all names for which this loader is able to create the
  114. * checks. There can be multiple names, to support both aliases
  115. * to the same checks and creators capable of creating multiple
  116. * types of checks.
  117. */
  118. virtual std::vector<std::string> names() const = 0;
  119. /**
  120. * \brief Creates the check.
  121. *
  122. * This function does the actuall creation. It is passed all the
  123. * relevant data and is supposed to return shared pointer to the
  124. * check.
  125. *
  126. * It is expected to throw the LoaderError exception when the
  127. * definition is invalid.
  128. *
  129. * \param name The type name of the check. If the creator creates
  130. * only one type of check, it can safely ignore this parameter.
  131. * \param definition The part of JSON describing the parameters of
  132. * check. As there's no way for the loader to know how the
  133. * parameters might look like, they are not checked in any way.
  134. * Therefore it's up to the creator (or the check being created)
  135. * to validate the data and throw if it is bad.
  136. */
  137. virtual boost::shared_ptr<Check<Context> > create(
  138. const std::string& name, data::ConstElementPtr definition) = 0;
  139. /**
  140. * \brief Is list or-abbreviation allowed?
  141. *
  142. * If this returns true and the parameter is list, the loader will
  143. * call the create method with each element of the list and aggregate
  144. * all the results in OR compound check. If it is false, the parameter
  145. * is passed verbatim no matter if it is or isn't a list.
  146. *
  147. * The rationale behind this is that it is common to specify list of
  148. * something that matches (eg. list of IP addresses).
  149. */
  150. virtual bool allowListAbbreviation() const {
  151. return (true);
  152. }
  153. };
  154. /**
  155. * \brief Register another check creator.
  156. *
  157. * Adds a creator to the list of known ones. The creator's list of names
  158. * must be disjoint with the names already known to the creator or the
  159. * LoaderError exception is thrown. In such case, the creator is not
  160. * registered under any of the names. In case of other exceptions, like
  161. * bad_alloc, only weak exception safety is guaranteed.
  162. *
  163. * \param creator Shared pointer to the creator.
  164. * \note We don't support deregistration yet, but it is expected it will
  165. * be needed in future, when we have some kind of plugins. These
  166. * plugins might want to unload, in which case they would need to
  167. * deregister their creators. It is expected they would pass the same
  168. * pointer to such method as they pass here.
  169. */
  170. void registerCreator(boost::shared_ptr<CheckCreator> creator) {
  171. // First check we can insert all the names
  172. typedef std::vector<std::string> Strings;
  173. const Strings names(creator->names());
  174. for (Strings::const_iterator i(names.begin()); i != names.end();
  175. ++i) {
  176. if (creators_.find(*i) != creators_.end()) {
  177. isc_throw(LoaderError, "The loader already contains creator "
  178. "named " << *i);
  179. }
  180. }
  181. // Now insert them
  182. for (Strings::const_iterator i(names.begin()); i != names.end();
  183. ++i) {
  184. creators_[*i] = creator;
  185. }
  186. }
  187. /**
  188. * \brief Load a check.
  189. *
  190. * This parses a check dict (block) and calls a creator (or creators, if
  191. * more than one check is found inside) for it. It ignores the "action"
  192. * key, as it is a reserved keyword used to specify actions inside the
  193. * ACL.
  194. *
  195. * This may throw LoaderError if it is not a dict or if some of the type
  196. * names is not known (there's no creator registered for it). The
  197. * exceptions from creators aren't caught.
  198. *
  199. * \param description The JSON description of the check.
  200. */
  201. boost::shared_ptr<Check<Context> > loadCheck(const data::ConstElementPtr&
  202. description)
  203. {
  204. // Get the description as a map
  205. typedef std::map<std::string, data::ConstElementPtr> Map;
  206. Map map;
  207. try {
  208. map = description->mapValue();
  209. }
  210. catch (const data::TypeError&) {
  211. throw LoaderError(__FILE__, __LINE__,
  212. "Check description is not a map",
  213. description);
  214. }
  215. // Remove the action keyword
  216. map.erase("action");
  217. // Now, do we have any definition? Or is it and abbreviation?
  218. switch (map.size()) {
  219. case 0:
  220. throw LoaderError(__FILE__, __LINE__,
  221. "Check description is empty",
  222. description);
  223. case 1: {
  224. // Get the first and only item
  225. const Map::const_iterator checkDesc(map.begin());
  226. const std::string& name(checkDesc->first);
  227. const typename Creators::const_iterator
  228. creatorIt(creators_.find(name));
  229. if (creatorIt == creators_.end()) {
  230. throw LoaderError(__FILE__, __LINE__,
  231. ("No creator for ACL check " +
  232. name).c_str(),
  233. description);
  234. }
  235. if (creatorIt->second->allowListAbbreviation() &&
  236. checkDesc->second->getType() == data::Element::list) {
  237. throw LoaderError(__FILE__, __LINE__,
  238. "Not implemented (OR-abbreviated form)",
  239. checkDesc->second);
  240. }
  241. // Create the check and return it
  242. return (creatorIt->second->create(name, checkDesc->second));
  243. }
  244. default:
  245. throw LoaderError(__FILE__, __LINE__,
  246. "Not implemented (AND-abbreviated form)",
  247. description);
  248. }
  249. }
  250. /**
  251. * \brief Load an ACL.
  252. *
  253. * This parses an ACL list, creates the checks and actions of each element
  254. * and returns it. It may throw LoaderError if it isn't a list or the
  255. * "action" key is missing in some element. Also, no exceptions from
  256. * loadCheck (therefore from whatever creator is used) and from the
  257. * actionLoader passed to constructor are not caught.
  258. *
  259. * \param description The JSON list of ACL.
  260. */
  261. boost::shared_ptr<Acl<Context, Action> > load(const data::ConstElementPtr&
  262. description);
  263. private:
  264. typedef std::map<std::string, boost::shared_ptr<CheckCreator> > Creators;
  265. Creators creators_;
  266. };
  267. }
  268. }
  269. #endif