2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2016-06-16 02:38:01 -04:00
|
|
|
|
|
|
|
|
#include "remote/variablequeryhandler.hpp"
|
|
|
|
|
#include "remote/httputility.hpp"
|
|
|
|
|
#include "remote/filterutility.hpp"
|
|
|
|
|
#include "base/configtype.hpp"
|
|
|
|
|
#include "base/scriptglobal.hpp"
|
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
|
#include "base/serializer.hpp"
|
2018-08-07 07:55:41 -04:00
|
|
|
#include "base/namespace.hpp"
|
2016-06-16 02:38:01 -04:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
|
|
REGISTER_URLHANDLER("/v1/variables", VariableQueryHandler);
|
|
|
|
|
|
2018-01-04 00:11:04 -05:00
|
|
|
class VariableTargetProvider final : public TargetProvider
|
2016-06-16 02:38:01 -04:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
DECLARE_PTR_TYPEDEFS(VariableTargetProvider);
|
|
|
|
|
|
2018-01-03 23:12:56 -05:00
|
|
|
void FindTargets(const String& type,
|
2017-12-19 09:50:05 -05:00
|
|
|
const std::function<void (const Value&)>& addTarget) const override
|
2016-06-16 02:38:01 -04:00
|
|
|
{
|
2025-10-01 03:37:13 -04:00
|
|
|
Namespace::Ptr globals = ScriptGlobal::GetGlobals();
|
|
|
|
|
ObjectLock olock(globals);
|
|
|
|
|
for (auto& [key, value] : globals) {
|
|
|
|
|
/* We want wo avoid leaking the TicketSalt over the API, so we remove it here,
|
|
|
|
|
* as early as possible, so it isn't possible to abuse the fact that all of the
|
|
|
|
|
* global variables we return here later get checked against a user-provided
|
|
|
|
|
* filter expression that can cause its content to be printed in an error message
|
|
|
|
|
* or potentially access them otherwise.
|
|
|
|
|
*/
|
|
|
|
|
if (key == "TicketSalt") {
|
|
|
|
|
continue;
|
2016-06-16 02:38:01 -04:00
|
|
|
}
|
2025-10-01 03:37:13 -04:00
|
|
|
|
|
|
|
|
addTarget(FilterUtility::GetTargetForVar(key, value.Val));
|
2016-06-16 02:38:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 23:12:56 -05:00
|
|
|
Value GetTargetByName(const String& type, const String& name) const override
|
2016-06-16 02:38:01 -04:00
|
|
|
{
|
2025-10-01 03:37:13 -04:00
|
|
|
if (name == "TicketSalt") {
|
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument{"Access to TicketSalt via /v1/variables is not permitted."});
|
|
|
|
|
}
|
2025-09-22 06:25:26 -04:00
|
|
|
return FilterUtility::GetTargetForVar(name, ScriptGlobal::Get(name));
|
2016-06-16 02:38:01 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 23:12:56 -05:00
|
|
|
bool IsValidType(const String& type) const override
|
2016-06-16 02:38:01 -04:00
|
|
|
{
|
|
|
|
|
return type == "Variable";
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 23:12:56 -05:00
|
|
|
String GetPluralName(const String& type) const override
|
2016-06-16 02:38:01 -04:00
|
|
|
{
|
|
|
|
|
return "variables";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
bool VariableQueryHandler::HandleRequest(
|
2025-06-11 04:28:16 -04:00
|
|
|
const WaitGroup::Ptr&,
|
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
|
|
|
)
|
2016-06-16 02:38: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)
|
2016-06-16 02:38:01 -04:00
|
|
|
return false;
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
if (request.method() != http::verb::get)
|
2016-06-16 02:38:01 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QueryDescription qd;
|
|
|
|
|
qd.Types.insert("Variable");
|
2016-08-18 13:00:14 -04:00
|
|
|
qd.Permission = "variables";
|
2016-06-16 02:38:01 -04:00
|
|
|
qd.Provider = new VariableTargetProvider();
|
|
|
|
|
|
|
|
|
|
params->Set("type", "Variable");
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
if (url->GetPath().size() >= 3)
|
|
|
|
|
params->Set("variable", url->GetPath()[2]);
|
2016-06-16 02:38:01 -04:00
|
|
|
|
|
|
|
|
std::vector<Value> objs;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
objs = FilterUtility::GetFilterTargets(qd, params, user, "variable");
|
|
|
|
|
} 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 variables found.",
|
2018-04-06 06:50:06 -04:00
|
|
|
DiagnosticInformation(ex));
|
2016-06-16 02:38:01 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
ArrayData results;
|
2016-06-16 02:38:01 -04:00
|
|
|
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr var : objs) {
|
2018-01-11 05:17:38 -05:00
|
|
|
results.emplace_back(new Dictionary({
|
|
|
|
|
{ "name", var->Get("name") },
|
|
|
|
|
{ "type", var->Get("type") },
|
|
|
|
|
{ "value", Serialize(var->Get("value"), 0) }
|
|
|
|
|
}));
|
2016-06-16 02:38:01 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
|
{ "results", new Array(std::move(results)) }
|
|
|
|
|
});
|
2016-06-16 02:38: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);
|
2016-06-16 02:38:01 -04:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|