icinga2/lib/remote/actionshandler.cpp

146 lines
3.5 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#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"
#include "base/exception.hpp"
#include "base/logger.hpp"
#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
REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
bool ActionsHandler::HandleRequest(
2019-02-15 11:51:12 +01:00
AsioTlsStream& stream,
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,
HttpServerConnection& server
)
{
namespace http = boost::beast::http;
if (url->GetPath().size() != 3)
2015-09-28 16:08:14 +02:00
return false;
if (request.method() != http::verb::post)
2015-09-28 16:08:14 +02:00
return false;
String actionName = url->GetPath()[2];
ApiAction::Ptr action = ApiAction::GetByName(actionName);
if (!action) {
HttpUtility::SendJsonError(response, params, 404, "Action '" + actionName + "' does not exist.");
return true;
}
QueryDescription qd;
const std::vector<String>& types = action->GetTypes();
std::vector<Value> objs;
2015-09-28 08:57:25 +02:00
String permission = "actions/" + actionName;
if (!types.empty()) {
qd.Types = std::set<String>(types.begin(), types.end());
2015-09-28 08:57:25 +02:00
qd.Permission = permission;
try {
2015-09-28 08:57:25 +02:00
objs = FilterUtility::GetFilterTargets(qd, params, user);
} catch (const std::exception& ex) {
HttpUtility::SendJsonError(response, params, 404,
"No objects found.",
2018-04-06 10:30:27 +02:00
DiagnosticInformation(ex));
return true;
}
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);
objs.emplace_back(nullptr);
2015-09-28 08:57:25 +02:00
}
ArrayData results;
Log(LogNotice, "ApiActionHandler")
<< "Running action " << actionName;
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");
for (const ConfigObject::Ptr& obj : objs) {
try {
results.emplace_back(action->Invoke(obj, params));
} catch (const std::exception& ex) {
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));
results.emplace_back(std::move(fail));
}
}
2018-04-06 10:43:33 +02:00
int statusCode = 500;
std::set<int> okStatusCodes, nonOkStatusCodes;
for (const Dictionary::Ptr& res : results) {
if (!res->Contains("code")) {
continue;
}
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;
}
response.result(statusCode);
2018-04-06 10:03:09 +02:00
Dictionary::Ptr result = new Dictionary({
{ "results", new Array(std::move(results)) }
});
HttpUtility::SendJsonBody(response, params, result);
return true;
}