2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-08-12 08:15:01 -04:00
|
|
|
|
|
|
|
|
#include "remote/modifyobjecthandler.hpp"
|
2023-04-12 08:45:40 -04:00
|
|
|
#include "remote/configobjectslock.hpp"
|
2015-08-12 08:15:01 -04:00
|
|
|
#include "remote/httputility.hpp"
|
|
|
|
|
#include "remote/filterutility.hpp"
|
|
|
|
|
#include "remote/apiaction.hpp"
|
|
|
|
|
#include "base/exception.hpp"
|
2018-01-04 12:24:45 -05:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2015-08-12 08:15:01 -04:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2015-09-28 02:37:50 -04:00
|
|
|
REGISTER_URLHANDLER("/v1/objects", ModifyObjectHandler);
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
bool ModifyObjectHandler::HandleRequest(
|
2025-06-11 04:28:16 -04:00
|
|
|
const WaitGroup::Ptr& waitGroup,
|
2026-01-22 06:41:21 -05:00
|
|
|
const HttpApiRequest& request,
|
|
|
|
|
HttpApiResponse& response,
|
2025-07-23 03:44:26 -04:00
|
|
|
boost::asio::yield_context& yc
|
2019-02-15 04:33:01 -05:00
|
|
|
)
|
2015-08-12 08:15:01 -04:00
|
|
|
{
|
2019-02-15 04:33:01 -05:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
|
2025-07-23 03:37:05 -04:00
|
|
|
auto url = request.Url();
|
|
|
|
|
auto user = request.User();
|
|
|
|
|
auto params = request.Params();
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
if (url->GetPath().size() < 3 || url->GetPath().size() > 4)
|
2015-08-12 08:15:01 -04:00
|
|
|
return false;
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
if (request.method() != http::verb::post)
|
2015-08-12 08:15:01 -04:00
|
|
|
return false;
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
Type::Ptr type = FilterUtility::TypeFromPluralName(url->GetPath()[2]);
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2015-09-28 10:08:14 -04:00
|
|
|
if (!type) {
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonError(response, params, 400, "Invalid type specified.");
|
2015-09-28 10:08:14 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
2015-08-12 08:15:01 -04:00
|
|
|
|
|
|
|
|
QueryDescription qd;
|
2015-08-26 04:58:59 -04:00
|
|
|
qd.Types.insert(type->GetName());
|
2015-09-28 02:37:50 -04:00
|
|
|
qd.Permission = "objects/modify/" + type->GetName();
|
2015-08-12 08:15:01 -04:00
|
|
|
|
|
|
|
|
params->Set("type", type->GetName());
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
if (url->GetPath().size() >= 4) {
|
2015-08-12 08:15:01 -04:00
|
|
|
String attr = type->GetName();
|
|
|
|
|
boost::algorithm::to_lower(attr);
|
2019-02-15 04:33:01 -05:00
|
|
|
params->Set(attr, url->GetPath()[3]);
|
2015-08-12 08:15:01 -04:00
|
|
|
}
|
|
|
|
|
|
2016-02-04 16:40:01 -05:00
|
|
|
std::vector<Value> objs;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
objs = FilterUtility::GetFilterTargets(qd, params, user);
|
|
|
|
|
} catch (const std::exception& ex) {
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonError(response, params, 404,
|
2017-12-19 09:50:05 -05:00
|
|
|
"No objects found.",
|
2018-04-06 06:52:17 -04:00
|
|
|
DiagnosticInformation(ex));
|
2016-02-04 16:40:01 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2017-03-01 05:33:43 -05:00
|
|
|
Value attrsVal = params->Get("attrs");
|
|
|
|
|
|
2023-06-13 10:40:33 -04:00
|
|
|
if (attrsVal.GetReflectionType() != Dictionary::TypeInstance && attrsVal.GetType() != ValueEmpty) {
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonError(response, params, 400,
|
2019-12-03 05:29:10 -05:00
|
|
|
"Invalid type for 'attrs' attribute specified. Dictionary type is required."
|
|
|
|
|
"Or is this a POST query and you missed adding a 'X-HTTP-Method-Override: GET' header?");
|
2017-03-01 05:33:43 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dictionary::Ptr attrs = attrsVal;
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2023-06-13 10:40:33 -04:00
|
|
|
Value restoreAttrsVal = params->Get("restore_attrs");
|
|
|
|
|
|
|
|
|
|
if (restoreAttrsVal.GetReflectionType() != Array::TypeInstance && restoreAttrsVal.GetType() != ValueEmpty) {
|
|
|
|
|
HttpUtility::SendJsonError(response, params, 400,
|
|
|
|
|
"Invalid type for 'restore_attrs' attribute specified. Array type is required.");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array::Ptr restoreAttrs = restoreAttrsVal;
|
|
|
|
|
|
|
|
|
|
if (!(attrs || restoreAttrs)) {
|
|
|
|
|
HttpUtility::SendJsonError(response, params, 400,
|
|
|
|
|
"Missing both 'attrs' and 'restore_attrs'. "
|
|
|
|
|
"Or is this a POST query and you missed adding a 'X-HTTP-Method-Override: GET' header?");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 06:26:49 -04:00
|
|
|
bool verbose = false;
|
|
|
|
|
|
|
|
|
|
if (params)
|
|
|
|
|
verbose = HttpUtility::GetLastParameter(params, "verbose");
|
|
|
|
|
|
2023-04-12 08:45:40 -04:00
|
|
|
ConfigObjectsSharedLock lock (std::try_to_lock);
|
|
|
|
|
|
|
|
|
|
if (!lock) {
|
|
|
|
|
HttpUtility::SendJsonError(response, params, 503, "Icinga is reloading");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
ArrayData results;
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2025-06-11 04:28:16 -04:00
|
|
|
std::shared_lock wgLock{*waitGroup, std::try_to_lock};
|
|
|
|
|
if (!wgLock) {
|
|
|
|
|
HttpUtility::SendJsonError(response, params, 503, "Shutting down.");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 06:36:37 -04:00
|
|
|
for (ConfigObject::Ptr obj : objs) {
|
2015-08-12 09:27:35 -04:00
|
|
|
Dictionary::Ptr result1 = new Dictionary();
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2015-08-13 03:02:52 -04:00
|
|
|
result1->Set("type", type->GetName());
|
2015-08-12 09:27:35 -04:00
|
|
|
result1->Set("name", obj->GetName());
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2025-06-11 04:28:16 -04:00
|
|
|
if (!waitGroup->IsLockable()) {
|
|
|
|
|
if (wgLock) {
|
|
|
|
|
wgLock.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result1->Set("code", 503);
|
|
|
|
|
result1->Set("status", "Action skipped: Shutting down.");
|
|
|
|
|
|
|
|
|
|
results.emplace_back(std::move(result1));
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-12 09:27:35 -04:00
|
|
|
String key;
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2024-03-11 07:34:14 -04:00
|
|
|
// Lock the object name of the given type to prevent from being modified/deleted concurrently.
|
|
|
|
|
ObjectNameLock objectNameLock(type, obj->GetName());
|
|
|
|
|
|
2023-06-13 10:40:33 -04:00
|
|
|
try {
|
|
|
|
|
if (restoreAttrs) {
|
|
|
|
|
ObjectLock oLock (restoreAttrs);
|
|
|
|
|
|
|
|
|
|
for (auto& attr : restoreAttrs) {
|
|
|
|
|
key = attr;
|
|
|
|
|
obj->RestoreAttribute(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
|
result1->Set("code", 500);
|
|
|
|
|
result1->Set("status", "Attribute '" + key + "' could not be restored: " + DiagnosticInformation(ex, false));
|
|
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
|
result1->Set("diagnostic_information", DiagnosticInformation(ex));
|
|
|
|
|
|
|
|
|
|
results.push_back(std::move(result1));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-12 09:27:35 -04:00
|
|
|
try {
|
|
|
|
|
if (attrs) {
|
2015-08-12 08:15:01 -04:00
|
|
|
ObjectLock olock(attrs);
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Dictionary::Pair& kv : attrs) {
|
2015-08-12 08:15:01 -04:00
|
|
|
key = kv.first;
|
|
|
|
|
obj->ModifyAttribute(kv.first, kv.second);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-12 09:27:35 -04:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
|
result1->Set("code", 500);
|
2018-04-06 06:26:49 -04:00
|
|
|
result1->Set("status", "Attribute '" + key + "' could not be set: " + DiagnosticInformation(ex, false));
|
|
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
|
result1->Set("diagnostic_information", DiagnosticInformation(ex));
|
2023-06-13 10:40:33 -04:00
|
|
|
|
|
|
|
|
results.push_back(std::move(result1));
|
|
|
|
|
continue;
|
2015-08-12 08:15:01 -04:00
|
|
|
}
|
2015-08-12 09:27:35 -04:00
|
|
|
|
2023-06-13 10:40:33 -04:00
|
|
|
result1->Set("code", 200);
|
|
|
|
|
result1->Set("status", "Attributes updated.");
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
results.push_back(std::move(result1));
|
2015-08-12 08:15:01 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
|
{ "results", new Array(std::move(results)) }
|
|
|
|
|
});
|
2015-08-12 08:15:01 -04:00
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
response.result(http::status::ok);
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-08-12 08:15:01 -04:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|