| Kea 3.0.2-git

hooked_command_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cc/command_interpreter.h>
10#include <config/hooked_command_mgr.h>
11#include <config/config_log.h>
12#include <hooks/callout_handle.h>
13#include <hooks/hooks_manager.h>
14#include <hooks/server_hooks.h>
15#include <boost/pointer_cast.hpp>
16#include <vector>
17
18 using namespace isc::data;
19 using namespace isc::hooks;
20
21 namespace isc {
22 namespace config {
23
24 HookedCommandMgr::HookedCommandMgr()
25 : BaseCommandMgr() {
26}
27
28 bool
29 HookedCommandMgr::delegateCommandToHookLibrary(const std::string& cmd_name,
30 const ConstElementPtr& params,
31 const ConstElementPtr& original_cmd,
32 ElementPtr& answer) {
33
34 ConstElementPtr hook_response;
35 if (HooksManager::commandHandlersPresent(cmd_name)) {
36
37 CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
38
39 // Set status to normal.
40 callout_handle->setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
41
42 // Delete previously set arguments.
43 callout_handle->deleteAllArguments();
44
45 ConstElementPtr command = original_cmd ? original_cmd :
46 createCommand(cmd_name, params);
47
48 // And pass it to the hook library.
49 callout_handle->setArgument("command", command);
50 callout_handle->setArgument("response", hook_response);
51
52 HooksManager::callCommandHandlers(cmd_name, *callout_handle);
53
54 // The callouts should set the response.
55 callout_handle->getArgument("response", hook_response);
56
57 answer = boost::const_pointer_cast<Element>(hook_response);
58
59 return (true);
60 }
61
62 return (false);
63}
64
65 ConstElementPtr
66 HookedCommandMgr::handleCommand(const std::string& cmd_name,
67 const ConstElementPtr& params,
68 const ConstElementPtr& original_cmd) {
69
70 // The 'list-commands' is a special case. Hook libraries do not implement
71 // this command. We determine what commands are supported by the hook
72 // libraries by checking what hook points are present that have callouts
73 // registered.
74 if ((cmd_name != "list-commands")) {
75 ElementPtr hook_response;
76 // Check if there are any hooks libraries to process this command.
77 if (delegateCommandToHookLibrary(cmd_name, params, original_cmd,
78 hook_response)) {
79 // Hooks libraries processed this command so simply return a
80 // result.
81 return (hook_response);
82 }
83
84 }
85
86 // If we're here it means that the callouts weren't called. We need
87 // to handle the command using local Command Manager.
88 ConstElementPtr response = BaseCommandMgr::handleCommand(cmd_name,
89 params,
90 original_cmd);
91
92 // If we're processing 'list-commands' command we may need to include
93 // commands supported by hooks libraries in the response.
94 if (cmd_name == "list-commands") {
95 // Hooks names can be used to decode what commands are supported.
96 const std::vector<std::string>& hooks =
97 ServerHooks::getServerHooksPtr()->getHookNames();
98
99 // Only update the response if there are any hooks present.
100 if (!hooks.empty()) {
101 ElementPtr hooks_commands = Element::createList();
102 for (auto const& h : hooks) {
103 // Try to convert hook name to command name. If non-empty
104 // string is returned it means that the hook point may have
105 // command handlers associated with it. Otherwise, it means that
106 // existing hook points are not for command handlers but for
107 // regular callouts.
108 std::string command_name = ServerHooks::hookToCommandName(h);
109 if (!command_name.empty()) {
110 // Final check: are command handlers registered for this
111 // hook point? If there are no command handlers associated,
112 // it means that the hook library was already unloaded.
113 if (HooksManager::commandHandlersPresent(command_name)) {
114 hooks_commands->add(Element::create(command_name));
115 }
116 }
117 }
118
119 // If there is at least one hook point with command handlers
120 // registered
121 // for it, combine the lists of commands.
122 if (!hooks_commands->empty()) {
123 response = combineCommandsLists(response, createAnswer(CONTROL_RESULT_SUCCESS, hooks_commands));
124 }
125 }
126 }
127
128 return (response);
129}
130
131
132} // end of namespace isc::config
133} // end of namespace isc
callout_handle.h
CalloutHandle::NEXT_STEP_CONTINUE
@ NEXT_STEP_CONTINUE
continue normally
Definition callout_handle.h:87
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
isc::config::BaseCommandMgr::BaseCommandMgr
BaseCommandMgr()
Constructor.
Definition base_command_mgr.cc:47
isc::config::BaseCommandMgr::handleCommand
virtual isc::data::ConstElementPtr handleCommand(const std::string &cmd_name, const isc::data::ConstElementPtr &params, const isc::data::ConstElementPtr &original_cmd)
Handles the command having a given name and arguments.
Definition base_command_mgr.cc:164
isc::config::HookedCommandMgr::HookedCommandMgr
HookedCommandMgr()
Constructor.
Definition hooked_command_mgr.cc:24
isc::config::HookedCommandMgr::handleCommand
virtual isc::data::ConstElementPtr handleCommand(const std::string &cmd_name, const isc::data::ConstElementPtr &params, const isc::data::ConstElementPtr &original_cmd)
Handles the command having a given name and arguments.
Definition hooked_command_mgr.cc:66
isc::config::HookedCommandMgr::delegateCommandToHookLibrary
bool delegateCommandToHookLibrary(const std::string &cmd_name, const isc::data::ConstElementPtr &params, const isc::data::ConstElementPtr &original_cmd, isc::data::ElementPtr &answer)
Handles the command within the hooks libraries.
Definition hooked_command_mgr.cc:29
isc::hooks::HooksManager::callCommandHandlers
static void callCommandHandlers(const std::string &command_name, CalloutHandle &handle)
Calls the callouts/command handlers for a given command name.
Definition hooks_manager.cc:87
isc::hooks::HooksManager::commandHandlersPresent
static bool commandHandlersPresent(const std::string &command_name)
Checks if control command handlers are present for the specified command.
Definition hooks_manager.cc:64
isc::hooks::HooksManager::createCalloutHandle
static boost::shared_ptr< CalloutHandle > createCalloutHandle()
Return callout handle.
Definition hooks_manager.cc:203
isc::hooks::ServerHooks::getServerHooksPtr
static ServerHooksPtr getServerHooksPtr()
Returns pointer to ServerHooks object.
Definition server_hooks.cc:177
isc::hooks::ServerHooks::hookToCommandName
static std::string hookToCommandName(const std::string &hook_name)
Returns command name for a specified hook name.
Definition server_hooks.cc:207
command_interpreter.h
This file contains several functions and constants that are used for handling commands and responses ...
config_log.h
hooked_command_mgr.h
hooks_manager.h
isc::config
Definition command_interpreter.cc:23
isc::config::createCommand
ConstElementPtr createCommand(const std::string &command)
Creates a standard command message with no argument (of the form { "command": "my_command" }...
Definition command_interpreter.cc:168
isc::config::combineCommandsLists
ConstElementPtr combineCommandsLists(const ConstElementPtr &response1, const ConstElementPtr &response2)
Combines lists of commands carried in two responses.
Definition command_interpreter.cc:267
isc::config::createAnswer
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
Definition command_interpreter.cc:54
isc::config::CONTROL_RESULT_SUCCESS
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
Definition command_interpreter.h:42
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::hooks
Definition callout_handle.cc:21
isc::hooks::CalloutHandlePtr
boost::shared_ptr< CalloutHandle > CalloutHandlePtr
A shared pointer to a CalloutHandle object.
Definition callout_handle.h:470
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
server_hooks.h