2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-07-30 17:50:17 +02:00
|
|
|
|
|
|
|
#include "remote/actionshandler.hpp"
|
|
|
|
#include "remote/httputility.hpp"
|
|
|
|
#include "remote/filterutility.hpp"
|
|
|
|
#include "remote/apiaction.hpp"
|
2020-07-03 10:16:23 +02:00
|
|
|
#include "base/defer.hpp"
|
2015-07-30 17:50:17 +02:00
|
|
|
#include "base/exception.hpp"
|
2015-08-21 15:50:40 +02:00
|
|
|
#include "base/logger.hpp"
|
2015-07-30 17:50:17 +02:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2020-07-03 11:17:36 +02:00
|
|
|
thread_local ApiUser::Ptr ActionsHandler::AuthenticatedApiUser;
|
2020-07-03 10:16:23 +02:00
|
|
|
|
2015-07-30 17:50:17 +02:00
|
|
|
REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
bool ActionsHandler::HandleRequest(
|
2019-02-15 11:51:12 +01:00
|
|
|
AsioTlsStream& stream,
|
2019-02-15 10:33:01 +01:00
|
|
|
const ApiUser::Ptr& user,
|
|
|
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
|
|
|
const Url::Ptr& url,
|
|
|
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
2019-02-15 11:51:12 +01:00
|
|
|
const Dictionary::Ptr& params,
|
|
|
|
boost::asio::yield_context& yc,
|
2019-04-02 17:37:29 +02:00
|
|
|
HttpServerConnection& server
|
2019-02-15 10:33:01 +01:00
|
|
|
)
|
2015-07-30 17:50:17 +02:00
|
|
|
{
|
2019-02-15 10:33:01 +01:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
|
|
|
if (url->GetPath().size() != 3)
|
2015-09-28 16:08:14 +02:00
|
|
|
return false;
|
2015-09-22 17:58:12 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (request.method() != http::verb::post)
|
2015-09-28 16:08:14 +02:00
|
|
|
return false;
|
2015-07-30 17:50:17 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
String actionName = url->GetPath()[2];
|
2015-07-30 17:50:17 +02:00
|
|
|
|
|
|
|
ApiAction::Ptr action = ApiAction::GetByName(actionName);
|
|
|
|
|
2015-09-22 17:58:12 +02:00
|
|
|
if (!action) {
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonError(response, params, 404, "Action '" + actionName + "' does not exist.");
|
2015-09-22 17:58:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-07-30 17:50:17 +02:00
|
|
|
|
|
|
|
QueryDescription qd;
|
|
|
|
|
2015-08-24 08:01:33 +02:00
|
|
|
const std::vector<String>& types = action->GetTypes();
|
2015-08-26 10:58:59 +02:00
|
|
|
std::vector<Value> objs;
|
2015-07-30 17:50:17 +02:00
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
String permission = "actions/" + actionName;
|
|
|
|
|
2015-08-24 08:01:33 +02:00
|
|
|
if (!types.empty()) {
|
2015-08-26 10:58:59 +02:00
|
|
|
qd.Types = std::set<String>(types.begin(), types.end());
|
2015-09-28 08:57:25 +02:00
|
|
|
qd.Permission = permission;
|
2015-08-24 08:01:33 +02:00
|
|
|
|
2015-09-22 17:58:12 +02:00
|
|
|
try {
|
2015-09-28 08:57:25 +02:00
|
|
|
objs = FilterUtility::GetFilterTargets(qd, params, user);
|
2015-09-22 17:58:12 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonError(response, params, 404,
|
2017-12-19 15:50:05 +01:00
|
|
|
"No objects found.",
|
2018-04-06 10:30:27 +02:00
|
|
|
DiagnosticInformation(ex));
|
2015-09-22 17:58:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-02 11:50:07 +02:00
|
|
|
|
|
|
|
if (objs.empty()) {
|
|
|
|
HttpUtility::SendJsonError(response, params, 404,
|
|
|
|
"No objects found.");
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-28 08:57:25 +02:00
|
|
|
} else {
|
|
|
|
FilterUtility::CheckPermission(user, permission);
|
2018-01-04 09:14:55 +01:00
|
|
|
objs.emplace_back(nullptr);
|
2015-09-28 08:57:25 +02:00
|
|
|
}
|
2015-07-30 17:50:17 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData results;
|
2015-07-30 17:50:17 +02:00
|
|
|
|
2015-08-21 15:50:40 +02:00
|
|
|
Log(LogNotice, "ApiActionHandler")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Running action " << actionName;
|
2015-08-21 15:50:40 +02:00
|
|
|
|
2018-04-06 10:30:27 +02:00
|
|
|
bool verbose = false;
|
|
|
|
|
2020-07-03 11:17:36 +02:00
|
|
|
ActionsHandler::AuthenticatedApiUser = user;
|
|
|
|
Defer a ([]() {
|
|
|
|
ActionsHandler::AuthenticatedApiUser = nullptr;
|
2020-07-03 10:16:23 +02:00
|
|
|
});
|
|
|
|
|
2018-04-06 10:30:27 +02:00
|
|
|
if (params)
|
|
|
|
verbose = HttpUtility::GetLastParameter(params, "verbose");
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ConfigObject::Ptr& obj : objs) {
|
2015-07-30 17:50:17 +02:00
|
|
|
try {
|
2018-01-11 11:17:38 +01:00
|
|
|
results.emplace_back(action->Invoke(obj, params));
|
2015-07-30 17:50:17 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr fail = new Dictionary({
|
|
|
|
{ "code", 500 },
|
|
|
|
{ "status", "Action execution failed: '" + DiagnosticInformation(ex, false) + "'." }
|
|
|
|
});
|
|
|
|
|
2018-04-06 10:30:27 +02:00
|
|
|
/* Exception for actions. Normally we would handle this inside SendJsonError(). */
|
|
|
|
if (verbose)
|
|
|
|
fail->Set("diagnostic_information", DiagnosticInformation(ex));
|
2018-01-11 11:17:38 +01:00
|
|
|
|
|
|
|
results.emplace_back(std::move(fail));
|
2015-07-30 17:50:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 10:43:33 +02:00
|
|
|
int statusCode = 500;
|
2020-12-04 18:25:48 +01:00
|
|
|
std::set<int> okStatusCodes, nonOkStatusCodes;
|
2018-04-05 10:47:17 +02:00
|
|
|
|
|
|
|
for (const Dictionary::Ptr& res : results) {
|
2020-12-04 18:25:48 +01:00
|
|
|
if (!res->Contains("code")) {
|
|
|
|
continue;
|
2018-04-05 10:47:17 +02:00
|
|
|
}
|
2020-12-04 18:25:48 +01:00
|
|
|
|
|
|
|
auto code = res->Get("code");
|
|
|
|
|
|
|
|
if (code >= 200 && code <= 299) {
|
|
|
|
okStatusCodes.insert(code);
|
|
|
|
} else {
|
|
|
|
nonOkStatusCodes.insert(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t okSize = okStatusCodes.size();
|
|
|
|
size_t nonOkSize = nonOkStatusCodes.size();
|
|
|
|
|
|
|
|
if (okSize == 1u && nonOkSize == 0u) {
|
|
|
|
statusCode = *okStatusCodes.begin();
|
|
|
|
} else if (nonOkSize == 1u) {
|
|
|
|
statusCode = *nonOkStatusCodes.begin();
|
|
|
|
} else if (okSize >= 2u && nonOkSize == 0u) {
|
|
|
|
statusCode = 200;
|
2018-04-05 10:47:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
response.result(statusCode);
|
2018-04-06 10:03:09 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
{ "results", new Array(std::move(results)) }
|
|
|
|
});
|
2015-07-30 17:50:17 +02:00
|
|
|
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-07-30 17:50:17 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|