Kea 3.1.8
cmd_response_creator.cc
Go to the documentation of this file.
1// Copyright (C) 2021-2026 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
10#include <config/command_mgr.h>
11#include <config/config_log.h>
14#include <http/response_json.h>
15#include <boost/pointer_cast.hpp>
16#include <iostream>
17
18using namespace isc::config;
19using namespace isc::data;
20using namespace isc::http;
21using namespace std;
22
23namespace isc {
24namespace config {
25
27
29
34
38 const HttpStatusCode& status_code) const {
39 HttpResponsePtr response = createStockHttpResponseInternal(request, status_code);
40 response->finalize();
41 return (response);
42}
43
45CmdResponseCreator::
46createStockHttpResponseInternal(const HttpRequestPtr& request,
47 const HttpStatusCode& status_code) const {
48 // The request hasn't been finalized so the request object
49 // doesn't contain any information about the HTTP version number
50 // used. But, the context should have this data (assuming the
51 // HTTP version is parsed OK).
52 HttpVersion http_version(request->context()->http_version_major_,
53 request->context()->http_version_minor_);
54 // We only accept HTTP version 1.0 or 1.1. If other version number is found
55 // we fall back to HTTP/1.0.
56 if ((http_version < HttpVersion(1, 0)) || (HttpVersion(1, 1) < http_version)) {
57 http_version.major_ = 1;
58 http_version.minor_ = 0;
59 }
60 // This will generate the response holding JSON content.
61 HttpResponsePtr response(new HttpResponseJson(http_version, status_code));
62 return (response);
63}
64
66CmdResponseCreator::createDynamicHttpResponse(HttpRequestPtr request) {
67 HttpResponseJsonPtr http_response;
68
69 // Check the basic HTTP authentication.
71 http_response = http_auth_config_->checkAuth(*this, request);
72 if (http_response) {
73 return (http_response);
74 }
75 }
76
77 // The request is always non-null, because this is verified by the
78 // createHttpResponse method. Let's try to convert it to the
79 // PostHttpRequestJson type as this is the type generated by the
80 // createNewHttpRequest. If the conversion result is null it means that
81 // the caller did not use createNewHttpRequest method to create this
82 // instance. This is considered an error in the server logic.
83 PostHttpRequestJsonPtr request_json = boost::dynamic_pointer_cast<
84 PostHttpRequestJson>(request);
85 if (!request_json) {
86 // Notify the client that we have a problem with our server.
87 return (createStockHttpResponse(request, HttpStatusCode::INTERNAL_SERVER_ERROR));
88 }
89
90 // We have already checked that the request is finalized so the call
91 // to getBodyAsJson must not trigger an exception.
92 ConstElementPtr command = request_json->getBodyAsJson();
93
94 // Filter the command.
95 http_response = filterCommand(request, command, command_accept_list_);
96 if (http_response) {
97 return (http_response);
98 }
99
100 // Process command doesn't generate exceptions but can possibly return
101 // null response, if the handler is not implemented properly. This is
102 // again an internal server issue.
104
105 if (!response) {
106 // Notify the client that we have a problem with our server.
107 return (createStockHttpResponse(request, HttpStatusCode::INTERNAL_SERVER_ERROR));
108 }
109
110 // Normal Responses coming from the Kea control socket must always be wrapped in
111 // a list as backword compatibility with the deprecated (removed) Control-Agent
112 // which contain responses from multiple daemons.
113 // If we're emulating that for backward compatibility, then we need to wrap
114 // the answer in a list if it isn't in one already.
115 if (emulateAgentResponse() && (response->getType() != Element::list)) {
116 ElementPtr response_list = Element::createList();
117 response_list->add(boost::const_pointer_cast<Element>(response));
118 response = response_list;
119 }
120
121 // The response is OK, so let's create new HTTP response with the status OK.
122 http_response = boost::dynamic_pointer_cast<
123 HttpResponseJson>(createStockHttpResponseInternal(request, HttpStatusCode::OK));
124 http_response->setBodyAsJson(response);
125 http_response->finalize();
126
127 return (http_response);
128}
129
132 const ConstElementPtr& body,
133 const unordered_set<string>& accept) {
134 HttpResponseJsonPtr response;
135 if (!body || accept.empty()) {
136 return (response);
137 }
138 if (body->getType() != Element::map) {
139 return (response);
140 }
141 ConstElementPtr elem = body->get(CONTROL_COMMAND);
142 if (!elem || (elem->getType() != Element::string)) {
143 return (response);
144 }
145 string command = elem->stringValue();
146 if (command.empty() || accept.count(command)) {
147 return (response);
148 }
149
150 // Reject the command.
153 .arg(command)
154 .arg(request->getRemote());
155 // From CmdResponseCreator::createStockHttpResponseInternal.
156 HttpVersion http_version(request->context()->http_version_major_,
157 request->context()->http_version_minor_);
158 if ((http_version < HttpVersion(1, 0)) ||
159 (HttpVersion(1, 1) < http_version)) {
160 http_version.major_ = 1;
161 http_version.minor_ = 0;
162 }
164 response.reset(new HttpResponseJson(http_version, status_code));
165 ElementPtr response_body = Element::createMap();
166 uint16_t result = HttpResponse::statusCodeToNumber(status_code);
167 response_body->set(CONTROL_RESULT,
168 Element::create(static_cast<long long>(result)));
169 const string& text = HttpResponse::statusCodeToString(status_code);
170 response_body->set(CONTROL_TEXT, Element::create(text));
171 response->setBodyAsJson(response_body);
172 response->finalize();
173 return (response);
174}
175
176} // end of namespace isc::config
177} // end of namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
@ map
Definition data.h:160
@ list
Definition data.h:159
@ string
Definition data.h:157
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:349
virtual isc::data::ConstElementPtr processCommand(const isc::data::ConstElementPtr &cmd)
Triggers command processing.
http::HttpResponseJsonPtr filterCommand(const http::HttpRequestPtr &request, const data::ConstElementPtr &body, const std::unordered_set< std::string > &accept)
Filter commands.
static http::HttpAuthConfigPtr http_auth_config_
The server current authentication configuration.
virtual http::HttpResponsePtr createStockHttpResponse(const http::HttpRequestPtr &request, const http::HttpStatusCode &status_code) const
Creates stock HTTP response.
virtual http::HttpRequestPtr createNewHttpRequest() const
Create a new request.
bool emulateAgentResponse()
Indicates whether or not agent response emulation is enabled.
static std::unordered_set< std::string > command_accept_list_
The server command accept list.
static CommandMgr & instance()
CommandMgr is a singleton class.
Represents HTTP response with JSON content.
static uint16_t statusCodeToNumber(const HttpStatusCode &status_code)
Convenience method converting status code to numeric value.
Definition response.cc:184
static std::string statusCodeToString(const HttpStatusCode &status_code)
Converts status code to string.
Definition response.cc:173
Represents HTTP POST request with JSON body.
This file contains several functions and constants that are used for handling commands and responses ...
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
const char * CONTROL_TEXT
String used for storing textual description ("text")
const char * CONTROL_COMMAND
String used for commands ("command")
const isc::log::MessageID COMMAND_HTTP_LISTENER_COMMAND_REJECTED
const char * CONTROL_RESULT
String used for result, i.e. integer status ("result")
const int DBG_COMMAND
Definition config_log.h:24
isc::log::Logger command_logger("commands")
Command processing Logger.
Definition config_log.h:21
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
HttpStatusCode
HTTP status codes (cf RFC 2068)
Definition response.h:30
boost::shared_ptr< PostHttpRequestJson > PostHttpRequestJsonPtr
Pointer to PostHttpRequestJson.
boost::shared_ptr< HttpAuthConfig > HttpAuthConfigPtr
Type of shared pointers to HTTP authentication configuration.
Definition auth_config.h:97
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition response.h:81
boost::shared_ptr< HttpRequest > HttpRequestPtr
Pointer to the HttpRequest object.
Definition request.h:30
Defines the logger used by the top-level component of kea-lfc.
HTTP protocol version.
Definition http_types.h:14
unsigned minor_
Minor HTTP version.
Definition http_types.h:16
unsigned major_
Major HTTP version.
Definition http_types.h:15