2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
#include "remote/configpackageshandler.hpp"
|
|
|
|
#include "remote/configpackageutility.hpp"
|
2015-07-21 16:10:13 +02:00
|
|
|
#include "remote/httputility.hpp"
|
2015-09-28 08:57:25 +02:00
|
|
|
#include "remote/filterutility.hpp"
|
2015-07-21 16:10:13 +02:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
REGISTER_URLHANDLER("/v1/config/packages", ConfigPackagesHandler);
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
bool ConfigPackagesHandler::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-21 16:10:13 +02:00
|
|
|
{
|
2019-02-15 10:33:01 +01:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
|
|
|
if (url->GetPath().size() > 4)
|
2015-09-28 16:08:14 +02:00
|
|
|
return false;
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (request.method() == http::verb::get)
|
|
|
|
HandleGet(user, request, url, response, params);
|
|
|
|
else if (request.method() == http::verb::post)
|
|
|
|
HandlePost(user, request, url, response, params);
|
|
|
|
else if (request.method() == http::verb::delete_)
|
|
|
|
HandleDelete(user, request, url, response, params);
|
2015-07-21 16:10:13 +02:00
|
|
|
else
|
2015-09-28 16:08:14 +02:00
|
|
|
return false;
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
return true;
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
void ConfigPackagesHandler::HandleGet(
|
|
|
|
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,
|
|
|
|
const Dictionary::Ptr& params
|
|
|
|
)
|
2015-07-21 16:10:13 +02:00
|
|
|
{
|
2019-02-15 10:33:01 +01:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
FilterUtility::CheckPermission(user, "config/query");
|
|
|
|
|
2018-03-07 13:27:31 +01:00
|
|
|
std::vector<String> packages;
|
|
|
|
|
|
|
|
try {
|
|
|
|
packages = ConfigPackageUtility::GetPackages();
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
HttpUtility::SendJsonError(response, params, 500, "Could not retrieve packages.",
|
2018-04-06 09:53:54 +02:00
|
|
|
DiagnosticInformation(ex));
|
2018-03-07 13:27:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData results;
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2017-09-20 16:40:02 +02:00
|
|
|
{
|
2019-05-08 16:06:46 +02:00
|
|
|
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticPackageMutex());
|
|
|
|
|
2017-09-20 16:40:02 +02:00
|
|
|
for (const String& package : packages) {
|
2019-05-08 16:43:27 +02:00
|
|
|
String activeStage;
|
|
|
|
|
|
|
|
try {
|
|
|
|
activeStage = ConfigPackageUtility::GetActiveStage(package);
|
|
|
|
} catch (const std::exception&) { } /* Should never happen. */
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
results.emplace_back(new Dictionary({
|
|
|
|
{ "name", package },
|
|
|
|
{ "stages", Array::FromVector(ConfigPackageUtility::GetStages(package)) },
|
2019-05-08 16:43:27 +02:00
|
|
|
{ "active-stage", activeStage }
|
2018-01-11 11:17:38 +01:00
|
|
|
}));
|
2017-09-20 16:40:02 +02:00
|
|
|
}
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
{ "results", new Array(std::move(results)) }
|
|
|
|
});
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
response.result(http::status::ok);
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
void ConfigPackagesHandler::HandlePost(
|
|
|
|
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,
|
|
|
|
const Dictionary::Ptr& params
|
|
|
|
)
|
2015-07-21 16:10:13 +02:00
|
|
|
{
|
2019-02-15 10:33:01 +01:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
FilterUtility::CheckPermission(user, "config/modify");
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (url->GetPath().size() >= 4)
|
|
|
|
params->Set("package", url->GetPath()[3]);
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
String packageName = HttpUtility::GetLastParameter(params, "package");
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
if (!ConfigPackageUtility::ValidateName(packageName)) {
|
2018-04-05 17:21:14 +02:00
|
|
|
HttpUtility::SendJsonError(response, params, 400, "Invalid package name '" + packageName + "'.");
|
2015-07-21 16:10:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-05-08 16:06:46 +02:00
|
|
|
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticPackageMutex());
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
ConfigPackageUtility::CreatePackage(packageName);
|
2015-07-21 16:10:13 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2018-04-05 17:21:14 +02:00
|
|
|
HttpUtility::SendJsonError(response, params, 500, "Could not create package '" + packageName + "'.",
|
2018-04-06 09:53:54 +02:00
|
|
|
DiagnosticInformation(ex));
|
2017-12-18 11:04:40 +01:00
|
|
|
return;
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result1 = new Dictionary({
|
|
|
|
{ "code", 200 },
|
2018-04-06 09:53:54 +02:00
|
|
|
{ "package", packageName },
|
2018-01-11 11:17:38 +01:00
|
|
|
{ "status", "Created package." }
|
|
|
|
});
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
{ "results", new Array({ result1 }) }
|
|
|
|
});
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
response.result(http::status::ok);
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
void ConfigPackagesHandler::HandleDelete(
|
|
|
|
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,
|
|
|
|
const Dictionary::Ptr& params
|
|
|
|
)
|
2015-07-21 16:10:13 +02:00
|
|
|
{
|
2019-02-15 10:33:01 +01:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
FilterUtility::CheckPermission(user, "config/modify");
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (url->GetPath().size() >= 4)
|
|
|
|
params->Set("package", url->GetPath()[3]);
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
String packageName = HttpUtility::GetLastParameter(params, "package");
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
if (!ConfigPackageUtility::ValidateName(packageName)) {
|
2018-04-05 17:21:14 +02:00
|
|
|
HttpUtility::SendJsonError(response, params, 400, "Invalid package name '" + packageName + "'.");
|
2015-07-21 16:10:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-08-28 17:58:29 +02:00
|
|
|
ConfigPackageUtility::DeletePackage(packageName);
|
2015-07-21 16:10:13 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2018-04-06 09:53:54 +02:00
|
|
|
HttpUtility::SendJsonError(response, params, 500, "Failed to delete package '" + packageName + "'.",
|
|
|
|
DiagnosticInformation(ex));
|
2018-04-12 19:24:08 +02:00
|
|
|
return;
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 09:53:54 +02:00
|
|
|
Dictionary::Ptr result1 = new Dictionary({
|
|
|
|
{ "code", 200 },
|
|
|
|
{ "package", packageName },
|
2018-04-12 19:24:08 +02:00
|
|
|
{ "status", "Deleted package." }
|
2018-04-06 09:53:54 +02:00
|
|
|
});
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
2018-04-06 09:53:54 +02:00
|
|
|
{ "results", new Array({ result1 }) }
|
2018-01-11 11:17:38 +01:00
|
|
|
});
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
response.result(http::status::ok);
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|