ccsession.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. # Copyright (C) 2009 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and 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 INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. #
  16. # Client-side functionality for configuration and commands
  17. #
  18. # It keeps a cc-channel session with the configuration manager daemon,
  19. # and handles configuration updates and direct commands
  20. # modeled after ccsession.h/cc 'protocol' changes here need to be
  21. # made there as well
  22. """Classes and functions for handling configuration and commands
  23. This module provides the ModuleCCSession and UIModuleCCSession
  24. classes, as well as a set of utility functions to create and parse
  25. messages related to commands and configuration
  26. Modules should use the ModuleCCSession class to connect to the
  27. configuration manager, and receive updates and commands from
  28. other modules.
  29. Configuration user interfaces should use the UIModuleCCSession
  30. to connect to b10-cmdctl, and receive and send configuration and
  31. commands through that to the configuration manager.
  32. """
  33. from isc.cc import Session
  34. from isc.config.config_data import ConfigData, MultiConfigData, BIND10_CONFIG_DATA_VERSION
  35. import isc
  36. class ModuleCCSessionError(Exception): pass
  37. def parse_answer(msg):
  38. """Returns a tuple (rcode, value), where value depends on the
  39. command that was called. If rcode != 0, value is a string
  40. containing an error message"""
  41. if type(msg) != dict:
  42. raise ModuleCCSessionError("Answer message is not a dict: " + str(msg))
  43. if 'result' not in msg:
  44. raise ModuleCCSessionError("answer message does not contain 'result' element")
  45. elif type(msg['result']) != list:
  46. raise ModuleCCSessionError("wrong result type in answer message")
  47. elif len(msg['result']) < 1:
  48. raise ModuleCCSessionError("empty result list in answer message")
  49. elif type(msg['result'][0]) != int:
  50. raise ModuleCCSessionError("wrong rcode type in answer message")
  51. else:
  52. if len(msg['result']) > 1:
  53. if (msg['result'][0] != 0 and type(msg['result'][1]) != str):
  54. raise ModuleCCSessionError("rcode in answer message is non-zero, value is not a string")
  55. return msg['result'][0], msg['result'][1]
  56. else:
  57. return msg['result'][0], None
  58. def create_answer(rcode, arg = None):
  59. """Creates an answer packet for config&commands. rcode must be an
  60. integer. If rcode == 0, arg is an optional value that depends
  61. on what the command or option was. If rcode != 0, arg must be
  62. a string containing an error message"""
  63. if type(rcode) != int:
  64. raise ModuleCCSessionError("rcode in create_answer() must be an integer")
  65. if rcode != 0 and type(arg) != str:
  66. raise ModuleCCSessionError("arg in create_answer for rcode != 0 must be a string describing the error")
  67. if arg != None:
  68. return { 'result': [ rcode, arg ] }
  69. else:
  70. return { 'result': [ rcode ] }
  71. # 'fixed' commands
  72. """Fixed names for command and configuration messages"""
  73. COMMAND_CONFIG_UPDATE = "config_update"
  74. COMMAND_MODULE_SPECIFICATION_UPDATE = "module_specification_update"
  75. COMMAND_GET_COMMANDS_SPEC = "get_commands_spec"
  76. COMMAND_GET_CONFIG = "get_config"
  77. COMMAND_SET_CONFIG = "set_config"
  78. COMMAND_GET_MODULE_SPEC = "get_module_spec"
  79. COMMAND_MODULE_SPEC = "module_spec"
  80. COMMAND_SHUTDOWN = "shutdown"
  81. def parse_command(msg):
  82. """Parses what may be a command message. If it looks like one,
  83. the function returns (command, value) where command is a
  84. string. If it is not, this function returns None, None"""
  85. if type(msg) == dict and len(msg.items()) == 1:
  86. cmd, value = msg.popitem()
  87. if cmd == "command" and type(value) == list:
  88. if len(value) == 1 and type(value[0]) == str:
  89. return value[0], None
  90. elif len(value) > 1 and type(value[0]) == str:
  91. return value[0], value[1]
  92. return None, None
  93. def create_command(command_name, params = None):
  94. """Creates a module command message with the given command name (as
  95. specified in the module's specification, and an optional params
  96. object"""
  97. # TODO: validate_command with spec
  98. if type(command_name) != str:
  99. raise ModuleCCSessionError("command in create_command() not a string")
  100. cmd = [ command_name ]
  101. if params:
  102. cmd.append(params)
  103. msg = { 'command': cmd }
  104. return msg
  105. class ModuleCCSession(ConfigData):
  106. """This class maintains a connection to the command channel, as
  107. well as configuration options for modules. The module provides
  108. a specification file that contains the module name, configuration
  109. options, and commands. It also gives the ModuleCCSession two callback
  110. functions, one to call when there is a direct command to the
  111. module, and one to update the configuration run-time. These
  112. callbacks are called when 'check_command' is called on the
  113. ModuleCCSession"""
  114. def __init__(self, spec_file_name, config_handler, command_handler, cc_session = None):
  115. """Initialize a ModuleCCSession. This does *NOT* send the
  116. specification and request the configuration yet. Use start()
  117. for that once the ModuleCCSession has been initialized.
  118. specfile_name is the path to the specification file
  119. config_handler and command_handler are callback functions,
  120. see set_config_handler and set_command_handler for more
  121. information on their signatures."""
  122. module_spec = isc.config.module_spec_from_file(spec_file_name)
  123. ConfigData.__init__(self, module_spec)
  124. self._module_name = module_spec.get_module_name()
  125. self.set_config_handler(config_handler)
  126. self.set_command_handler(command_handler)
  127. if not cc_session:
  128. self._session = Session()
  129. else:
  130. self._session = cc_session
  131. self._session.group_subscribe(self._module_name, "*")
  132. self._remote_module_configs = {}
  133. def __del__(self):
  134. # If the CC Session obejct has been closed, it returns
  135. # immediately.
  136. if self._session._closed: return
  137. self._session.group_unsubscribe(self._module_name, "*")
  138. for module_name in self._remote_module_configs:
  139. self._session.group_unsubscribe(module_name)
  140. def start(self):
  141. """Send the specification for this module to the configuration
  142. manager, and request the current non-default configuration.
  143. The config_handler will be called with that configuration"""
  144. self.__send_spec()
  145. self.__request_config()
  146. def get_socket(self):
  147. """Returns the socket from the command channel session. This
  148. should *only* be used for select() loops to see if there
  149. is anything on the channel. If that loop is not completely
  150. time-critical, it is strongly recommended to only use
  151. check_command(), and not look at the socket at all."""
  152. return self._session._socket
  153. def close(self):
  154. """Close the session to the command channel"""
  155. self._session.close()
  156. def check_command(self, nonblock=True):
  157. """Check whether there is a command or configuration update on
  158. the channel. This function does a read on the cc session, and
  159. returns nothing.
  160. It calls check_command_without_recvmsg()
  161. to parse the received message.
  162. If nonblock is True, it just checks if there's a command
  163. and does nothing if there isn't. If nonblock is False, it
  164. waits until it arrives. It temporarily sets timeout to infinity,
  165. because commands may not come in arbitrary long time."""
  166. timeout_orig = self._session.get_timeout()
  167. self._session.set_timeout(0)
  168. try:
  169. msg, env = self._session.group_recvmsg(nonblock)
  170. finally:
  171. self._session.set_timeout(timeout_orig)
  172. self.check_command_without_recvmsg(msg, env)
  173. def check_command_without_recvmsg(self, msg, env):
  174. """Parse the given message to see if there is a command or a
  175. configuration update. Calls the corresponding handler
  176. functions if present. Responds on the channel if the
  177. handler returns a message."""
  178. # should we default to an answer? success-by-default? unhandled error?
  179. if msg is not None and not 'result' in msg:
  180. answer = None
  181. try:
  182. module_name = env['group']
  183. cmd, arg = isc.config.ccsession.parse_command(msg)
  184. if cmd == COMMAND_CONFIG_UPDATE:
  185. new_config = arg
  186. # If the target channel was not this module
  187. # it might be in the remote_module_configs
  188. if module_name != self._module_name:
  189. if module_name in self._remote_module_configs:
  190. # no checking for validity, that's up to the
  191. # module itself.
  192. newc = self._remote_module_configs[module_name].get_local_config()
  193. isc.cc.data.merge(newc, new_config)
  194. self._remote_module_configs[module_name].set_local_config(newc)
  195. # For other modules, we're not supposed to answer
  196. return
  197. # ok, so apparently this update is for us.
  198. errors = []
  199. if not self._config_handler:
  200. answer = create_answer(2, self._module_name + " has no config handler")
  201. elif not self.get_module_spec().validate_config(False, new_config, errors):
  202. answer = create_answer(1, ", ".join(errors))
  203. else:
  204. isc.cc.data.remove_identical(new_config, self.get_local_config())
  205. answer = self._config_handler(new_config)
  206. rcode, val = parse_answer(answer)
  207. if rcode == 0:
  208. newc = self.get_local_config()
  209. isc.cc.data.merge(newc, new_config)
  210. self.set_local_config(newc)
  211. else:
  212. # ignore commands for 'remote' modules
  213. if module_name == self._module_name:
  214. if self._command_handler:
  215. answer = self._command_handler(cmd, arg)
  216. else:
  217. answer = create_answer(2, self._module_name + " has no command handler")
  218. except Exception as exc:
  219. answer = create_answer(1, str(exc))
  220. if answer:
  221. self._session.group_reply(env, answer)
  222. def set_config_handler(self, config_handler):
  223. """Set the config handler for this module. The handler is a
  224. function that takes the full configuration and handles it.
  225. It should return an answer created with create_answer()"""
  226. self._config_handler = config_handler
  227. # should we run this right now since we've changed the handler?
  228. def set_command_handler(self, command_handler):
  229. """Set the command handler for this module. The handler is a
  230. function that takes a command as defined in the .spec file
  231. and return an answer created with create_answer()"""
  232. self._command_handler = command_handler
  233. def add_remote_config(self, spec_file_name):
  234. """Gives access to the configuration of a different module.
  235. These remote module options can at this moment only be
  236. accessed through get_remote_config_value(). This function
  237. also subscribes to the channel of the remote module name
  238. to receive the relevant updates. It is not possible to
  239. specify your own handler for this right now.
  240. start() must have been called on this CCSession
  241. prior to the call to this method.
  242. Returns the name of the module."""
  243. module_spec = isc.config.module_spec_from_file(spec_file_name)
  244. module_cfg = ConfigData(module_spec)
  245. module_name = module_spec.get_module_name()
  246. self._session.group_subscribe(module_name);
  247. # Get the current config for that module now
  248. seq = self._session.group_sendmsg(create_command(COMMAND_GET_CONFIG, { "module_name": module_name }), "ConfigManager")
  249. try:
  250. answer, env = self._session.group_recvmsg(False, seq)
  251. except isc.cc.SessionTimeout:
  252. raise ModuleCCSessionError("No answer from ConfigManager when "
  253. "asking about Remote module " +
  254. module_name)
  255. if answer:
  256. rcode, value = parse_answer(answer)
  257. if rcode == 0:
  258. if value != None and module_spec.validate_config(False, value):
  259. module_cfg.set_local_config(value);
  260. # all done, add it
  261. self._remote_module_configs[module_name] = module_cfg
  262. return module_name
  263. def remove_remote_config(self, module_name):
  264. """Removes the remote configuration access for this module"""
  265. if module_name in self._remote_module_configs:
  266. self._session.group_unsubscribe(module_name)
  267. del self._remote_module_configs[module_name]
  268. def get_remote_config_value(self, module_name, identifier):
  269. """Returns the current setting for the given identifier at the
  270. given module. If the module has not been added with
  271. add_remote_config, a ModuleCCSessionError is raised"""
  272. if module_name in self._remote_module_configs:
  273. return self._remote_module_configs[module_name].get_value(identifier)
  274. else:
  275. raise ModuleCCSessionError("Remote module " + module_name +
  276. " not found")
  277. def __send_spec(self):
  278. """Sends the data specification to the configuration manager"""
  279. msg = create_command(COMMAND_MODULE_SPEC, self.get_module_spec().get_full_spec())
  280. seq = self._session.group_sendmsg(msg, "ConfigManager")
  281. try:
  282. answer, env = self._session.group_recvmsg(False, seq)
  283. except isc.cc.SessionTimeout:
  284. # TODO: log an error?
  285. pass
  286. def __request_config(self):
  287. """Asks the configuration manager for the current configuration, and call the config handler if set.
  288. Raises a ModuleCCSessionError if there is no answer from the configuration manager"""
  289. seq = self._session.group_sendmsg(create_command(COMMAND_GET_CONFIG, { "module_name": self._module_name }), "ConfigManager")
  290. try:
  291. answer, env = self._session.group_recvmsg(False, seq)
  292. if answer:
  293. rcode, value = parse_answer(answer)
  294. if rcode == 0:
  295. errors = []
  296. if value != None:
  297. if self.get_module_spec().validate_config(False,
  298. value,
  299. errors):
  300. self.set_local_config(value);
  301. if self._config_handler:
  302. self._config_handler(value)
  303. else:
  304. raise ModuleCCSessionError(
  305. "Wrong data in configuration: " +
  306. " ".join(errors))
  307. else:
  308. # log error
  309. print("[" + self._module_name + "] Error requesting configuration: " + value)
  310. else:
  311. raise ModuleCCSessionError("No answer from configuration manager")
  312. except isc.cc.SessionTimeout:
  313. raise ModuleCCSessionError("CC Session timeout waiting for configuration manager")
  314. class UIModuleCCSession(MultiConfigData):
  315. """This class is used in a configuration user interface. It contains
  316. specific functions for getting, displaying, and sending
  317. configuration settings through the b10-cmdctl module."""
  318. def __init__(self, conn):
  319. """Initialize a UIModuleCCSession. The conn object that is
  320. passed must have send_GET and send_POST functions"""
  321. MultiConfigData.__init__(self)
  322. self._conn = conn
  323. self.request_specifications()
  324. self.request_current_config()
  325. def request_specifications(self):
  326. """Request the module specifications from b10-cmdctl"""
  327. # this step should be unnecessary but is the current way cmdctl returns stuff
  328. # so changes are needed there to make this clean (we need a command to simply get the
  329. # full specs for everything, including commands etc, not separate gets for that)
  330. specs = self._conn.send_GET('/module_spec')
  331. for module in specs.keys():
  332. self.set_specification(isc.config.ModuleSpec(specs[module]))
  333. def update_specs_and_config(self):
  334. self.request_specifications();
  335. self.request_current_config();
  336. def request_current_config(self):
  337. """Requests the current configuration from the configuration
  338. manager through b10-cmdctl, and stores those as CURRENT"""
  339. config = self._conn.send_GET('/config_data')
  340. if 'version' not in config or config['version'] != BIND10_CONFIG_DATA_VERSION:
  341. raise ModuleCCSessionError("Bad config version")
  342. self._set_current_config(config)
  343. def add_value(self, identifier, value_str = None):
  344. """Add a value to a configuration list. Raises a DataTypeError
  345. if the value does not conform to the list_item_spec field
  346. of the module config data specification. If value_str is
  347. not given, we add the default as specified by the .spec
  348. file."""
  349. module_spec = self.find_spec_part(identifier)
  350. if (type(module_spec) != dict or "list_item_spec" not in module_spec):
  351. raise isc.cc.data.DataNotFoundError(str(identifier) + " is not a list")
  352. cur_list, status = self.get_value(identifier)
  353. if not cur_list:
  354. cur_list = []
  355. # Hmm. Do we need to check for duplicates?
  356. value = None
  357. if value_str is not None:
  358. value = isc.cc.data.parse_value_str(value_str)
  359. else:
  360. if "item_default" in module_spec["list_item_spec"]:
  361. value = module_spec["list_item_spec"]["item_default"]
  362. if value is None:
  363. raise isc.cc.data.DataNotFoundError("No value given and no default for " + str(identifier))
  364. if value not in cur_list:
  365. cur_list.append(value)
  366. self.set_value(identifier, cur_list)
  367. def remove_value(self, identifier, value_str):
  368. """Remove a value from a configuration list. The value string
  369. must be a string representation of the full item. Raises
  370. a DataTypeError if the value at the identifier is not a list,
  371. or if the given value_str does not match the list_item_spec
  372. """
  373. module_spec = self.find_spec_part(identifier)
  374. if (type(module_spec) != dict or "list_item_spec" not in module_spec):
  375. raise isc.cc.data.DataNotFoundError(str(identifier) + " is not a list")
  376. if value_str is None:
  377. # we are directly removing an list index
  378. id, list_indices = isc.cc.data.split_identifier_list_indices(identifier)
  379. if list_indices is None:
  380. raise DataTypeError("identifier in remove_value() does not contain a list index, and no value to remove")
  381. else:
  382. self.set_value(identifier, None)
  383. else:
  384. value = isc.cc.data.parse_value_str(value_str)
  385. isc.config.config_data.check_type(module_spec, [value])
  386. cur_list, status = self.get_value(identifier)
  387. #if not cur_list:
  388. # cur_list = isc.cc.data.find_no_exc(self.config.data, identifier)
  389. if not cur_list:
  390. cur_list = []
  391. if value in cur_list:
  392. cur_list.remove(value)
  393. self.set_value(identifier, cur_list)
  394. def commit(self):
  395. """Commit all local changes, send them through b10-cmdctl to
  396. the configuration manager"""
  397. if self.get_local_changes():
  398. response = self._conn.send_POST('/ConfigManager/set_config',
  399. [ self.get_local_changes() ])
  400. answer = isc.cc.data.parse_value_str(response.read().decode())
  401. # answer is either an empty dict (on success), or one
  402. # containing errors
  403. if answer == {}:
  404. self.request_current_config()
  405. self.clear_local_changes()
  406. elif "error" in answer:
  407. print("Error: " + answer["error"])
  408. print("Configuration not committed")
  409. else:
  410. raise ModuleCCSessionError("Unknown format of answer in commit(): " + str(answer))