2015-11-02 16:35:21 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2015-11-02 16:35:21 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "remote/consolehandler.hpp"
|
|
|
|
#include "remote/httputility.hpp"
|
|
|
|
#include "remote/filterutility.hpp"
|
|
|
|
#include "config/configcompiler.hpp"
|
|
|
|
#include "base/configtype.hpp"
|
|
|
|
#include "base/configwriter.hpp"
|
|
|
|
#include "base/scriptglobal.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/serializer.hpp"
|
|
|
|
#include "base/timer.hpp"
|
2018-08-07 13:55:41 +02:00
|
|
|
#include "base/namespace.hpp"
|
2015-11-02 16:35:21 +01:00
|
|
|
#include "base/initialize.hpp"
|
2017-11-21 14:07:44 +01:00
|
|
|
#include <boost/thread/once.hpp>
|
2015-11-02 16:35:21 +01:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_URLHANDLER("/v1/console", ConsoleHandler);
|
|
|
|
|
|
|
|
static boost::mutex l_QueryMutex;
|
|
|
|
static std::map<String, ApiScriptFrame> l_ApiScriptFrames;
|
|
|
|
static Timer::Ptr l_FrameCleanupTimer;
|
|
|
|
static boost::mutex l_ApiScriptMutex;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static void ScriptFrameCleanupHandler()
|
2015-11-02 16:35:21 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_ApiScriptMutex);
|
|
|
|
|
|
|
|
std::vector<String> cleanup_keys;
|
|
|
|
|
|
|
|
typedef std::pair<String, ApiScriptFrame> KVPair;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const KVPair& kv : l_ApiScriptFrames) {
|
2015-11-02 16:35:21 +01:00
|
|
|
if (kv.second.Seen < Utility::GetTime() - 1800)
|
|
|
|
cleanup_keys.push_back(kv.first);
|
|
|
|
}
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& key : cleanup_keys)
|
2015-11-02 16:35:21 +01:00
|
|
|
l_ApiScriptFrames.erase(key);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static void EnsureFrameCleanupTimer()
|
2017-11-21 14:07:44 +01:00
|
|
|
{
|
|
|
|
static boost::once_flag once = BOOST_ONCE_INIT;
|
|
|
|
|
|
|
|
boost::call_once(once, []() {
|
|
|
|
l_FrameCleanupTimer = new Timer();
|
|
|
|
l_FrameCleanupTimer->OnTimerExpired.connect(std::bind(ScriptFrameCleanupHandler));
|
|
|
|
l_FrameCleanupTimer->SetInterval(30);
|
|
|
|
l_FrameCleanupTimer->Start();
|
|
|
|
});
|
|
|
|
}
|
2015-11-02 16:35:21 +01:00
|
|
|
|
2016-05-10 15:16:35 +02:00
|
|
|
bool ConsoleHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params)
|
2015-11-02 16:35:21 +01:00
|
|
|
{
|
2018-04-04 12:23:45 +02:00
|
|
|
if (request.RequestUrl->GetPath().size() != 3)
|
2015-11-02 16:35:21 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (request.RequestMethod != "POST")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QueryDescription qd;
|
|
|
|
|
|
|
|
String methodName = request.RequestUrl->GetPath()[2];
|
2016-05-10 15:16:35 +02:00
|
|
|
|
2015-11-08 17:53:22 +01:00
|
|
|
FilterUtility::CheckPermission(user, "console");
|
2015-11-02 16:35:21 +01:00
|
|
|
|
|
|
|
String session = HttpUtility::GetLastParameter(params, "session");
|
|
|
|
|
|
|
|
if (session.IsEmpty())
|
|
|
|
session = Utility::NewUniqueID();
|
|
|
|
|
|
|
|
String command = HttpUtility::GetLastParameter(params, "command");
|
|
|
|
|
|
|
|
bool sandboxed = HttpUtility::GetLastParameter(params, "sandboxed");
|
|
|
|
|
2015-11-07 09:11:16 +01:00
|
|
|
if (methodName == "execute-script")
|
2017-12-20 15:31:05 +01:00
|
|
|
return ExecuteScriptHelper(request, response, params, command, session, sandboxed);
|
2015-11-07 09:11:16 +01:00
|
|
|
else if (methodName == "auto-complete-script")
|
2017-12-20 15:31:05 +01:00
|
|
|
return AutocompleteScriptHelper(request, response, params, command, session, sandboxed);
|
2015-11-02 16:35:21 +01:00
|
|
|
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonError(response, params, 400, "Invalid method specified: " + methodName);
|
2015-11-02 16:35:21 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsoleHandler::ExecuteScriptHelper(HttpRequest& request, HttpResponse& response,
|
2017-12-20 15:31:05 +01:00
|
|
|
const Dictionary::Ptr& params, const String& command, const String& session, bool sandboxed)
|
2015-11-02 16:35:21 +01:00
|
|
|
{
|
2016-12-08 00:59:24 +01:00
|
|
|
Log(LogNotice, "Console")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Executing expression: " << command;
|
2015-11-02 16:35:21 +01:00
|
|
|
|
2017-11-21 14:07:44 +01:00
|
|
|
EnsureFrameCleanupTimer();
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
ApiScriptFrame& lsf = l_ApiScriptFrames[session];
|
|
|
|
lsf.Seen = Utility::GetTime();
|
|
|
|
|
|
|
|
if (!lsf.Locals)
|
|
|
|
lsf.Locals = new Dictionary();
|
|
|
|
|
|
|
|
String fileName = "<" + Convert::ToString(lsf.NextLine) + ">";
|
|
|
|
lsf.NextLine++;
|
|
|
|
|
|
|
|
lsf.Lines[fileName] = command;
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr resultInfo;
|
2017-12-15 05:34:46 +01:00
|
|
|
std::unique_ptr<Expression> expr;
|
2015-11-02 16:35:21 +01:00
|
|
|
Value exprResult;
|
|
|
|
|
|
|
|
try {
|
|
|
|
expr = ConfigCompiler::CompileText(fileName, command);
|
|
|
|
|
2018-01-03 10:19:24 +01:00
|
|
|
ScriptFrame frame(true);
|
2015-11-02 16:35:21 +01:00
|
|
|
frame.Locals = lsf.Locals;
|
|
|
|
frame.Self = lsf.Locals;
|
|
|
|
frame.Sandboxed = sandboxed;
|
|
|
|
|
|
|
|
exprResult = expr->Evaluate(frame);
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
resultInfo = new Dictionary({
|
|
|
|
{ "code", 200 },
|
|
|
|
{ "status", "Executed successfully." },
|
|
|
|
{ "result", Serialize(exprResult, 0) }
|
|
|
|
});
|
2015-11-02 16:35:21 +01:00
|
|
|
} catch (const ScriptError& ex) {
|
|
|
|
DebugInfo di = ex.GetDebugInfo();
|
|
|
|
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
|
|
|
|
msgbuf << di.Path << ": " << lsf.Lines[di.Path] << "\n"
|
2017-12-19 15:50:05 +01:00
|
|
|
<< String(di.Path.GetLength() + 2, ' ')
|
|
|
|
<< String(di.FirstColumn, ' ') << String(di.LastColumn - di.FirstColumn + 1, '^') << "\n"
|
|
|
|
<< ex.what() << "\n";
|
2015-11-02 16:35:21 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
resultInfo = new Dictionary({
|
|
|
|
{ "code", 500 },
|
|
|
|
{ "status", String(msgbuf.str()) },
|
|
|
|
{ "incomplete_expression", ex.IsIncompleteExpression() },
|
|
|
|
{ "debug_info", new Dictionary({
|
|
|
|
{ "path", di.Path },
|
|
|
|
{ "first_line", di.FirstLine },
|
|
|
|
{ "first_column", di.FirstColumn },
|
|
|
|
{ "last_line", di.LastLine },
|
|
|
|
{ "last_column", di.LastColumn }
|
|
|
|
}) }
|
|
|
|
});
|
2015-11-02 16:35:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
{ "results", new Array({ resultInfo }) }
|
|
|
|
});
|
2015-11-02 16:35:21 +01:00
|
|
|
|
|
|
|
response.SetStatus(200, "OK");
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-11-02 16:35:21 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsoleHandler::AutocompleteScriptHelper(HttpRequest& request, HttpResponse& response,
|
2017-12-20 15:31:05 +01:00
|
|
|
const Dictionary::Ptr& params, const String& command, const String& session, bool sandboxed)
|
2015-11-02 16:35:21 +01:00
|
|
|
{
|
|
|
|
Log(LogInformation, "Console")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Auto-completing expression: " << command;
|
2015-11-02 16:35:21 +01:00
|
|
|
|
2017-11-21 14:07:44 +01:00
|
|
|
EnsureFrameCleanupTimer();
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
ApiScriptFrame& lsf = l_ApiScriptFrames[session];
|
|
|
|
lsf.Seen = Utility::GetTime();
|
|
|
|
|
|
|
|
if (!lsf.Locals)
|
|
|
|
lsf.Locals = new Dictionary();
|
|
|
|
|
|
|
|
|
2018-01-03 10:19:24 +01:00
|
|
|
ScriptFrame frame(true);
|
2015-11-02 16:35:21 +01:00
|
|
|
frame.Locals = lsf.Locals;
|
|
|
|
frame.Self = lsf.Locals;
|
|
|
|
frame.Sandboxed = sandboxed;
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result1 = new Dictionary({
|
|
|
|
{ "code", 200 },
|
|
|
|
{ "status", "Auto-completed successfully." },
|
|
|
|
{ "suggestions", Array::FromVector(GetAutocompletionSuggestions(command, frame)) }
|
|
|
|
});
|
2015-11-02 16:35:21 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
{ "results", new Array({ result1 }) }
|
|
|
|
});
|
2015-11-02 16:35:21 +01:00
|
|
|
|
|
|
|
response.SetStatus(200, "OK");
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-11-02 16:35:21 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddSuggestion(std::vector<String>& matches, const String& word, const String& suggestion)
|
|
|
|
{
|
|
|
|
if (suggestion.Find(word) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
matches.push_back(suggestion);
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:36:47 +02:00
|
|
|
static void AddSuggestions(std::vector<String>& matches, const String& word, const String& pword, bool withFields, const Value& value)
|
2016-08-12 11:25:36 +02:00
|
|
|
{
|
|
|
|
String prefix;
|
|
|
|
|
|
|
|
if (!pword.IsEmpty())
|
|
|
|
prefix = pword + ".";
|
|
|
|
|
|
|
|
if (value.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr dict = value;
|
|
|
|
|
|
|
|
ObjectLock olock(dict);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : dict) {
|
2016-08-12 11:25:36 +02:00
|
|
|
AddSuggestion(matches, word, prefix + kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (value.IsObjectType<Namespace>()) {
|
|
|
|
Namespace::Ptr ns = value;
|
|
|
|
|
|
|
|
ObjectLock olock(ns);
|
|
|
|
for (const Namespace::Pair& kv : ns) {
|
|
|
|
AddSuggestion(matches, word, prefix + kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:36:47 +02:00
|
|
|
if (withFields) {
|
|
|
|
Type::Ptr type = value.GetReflectionType();
|
2016-08-12 11:25:36 +02:00
|
|
|
|
2016-08-12 15:36:47 +02:00
|
|
|
for (int i = 0; i < type->GetFieldCount(); i++) {
|
|
|
|
Field field = type->GetFieldInfo(i);
|
2016-08-12 11:25:36 +02:00
|
|
|
|
2016-08-12 15:36:47 +02:00
|
|
|
AddSuggestion(matches, word, prefix + field.Name);
|
|
|
|
}
|
2016-08-12 11:25:36 +02:00
|
|
|
|
2016-08-12 13:13:01 +02:00
|
|
|
while (type) {
|
|
|
|
Object::Ptr prototype = type->GetPrototype();
|
|
|
|
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(prototype);
|
2016-08-12 11:25:36 +02:00
|
|
|
|
2016-08-12 13:13:01 +02:00
|
|
|
if (dict) {
|
|
|
|
ObjectLock olock(dict);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : dict) {
|
2016-08-12 13:13:01 +02:00
|
|
|
AddSuggestion(matches, word, prefix + kv.first);
|
|
|
|
}
|
2016-08-12 11:25:36 +02:00
|
|
|
}
|
|
|
|
|
2016-08-12 13:13:01 +02:00
|
|
|
type = type->GetBaseType();
|
|
|
|
}
|
2016-08-12 11:25:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
std::vector<String> ConsoleHandler::GetAutocompletionSuggestions(const String& word, ScriptFrame& frame)
|
2017-12-13 12:54:14 +01:00
|
|
|
{
|
2015-11-02 16:35:21 +01:00
|
|
|
std::vector<String> matches;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& keyword : ConfigWriter::GetKeywords()) {
|
2015-11-02 16:35:21 +01:00
|
|
|
AddSuggestion(matches, word, keyword);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(frame.Locals);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : frame.Locals) {
|
2015-11-02 16:35:21 +01:00
|
|
|
AddSuggestion(matches, word, kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(ScriptGlobal::GetGlobals());
|
2018-08-07 13:55:41 +02:00
|
|
|
for (const Namespace::Pair& kv : ScriptGlobal::GetGlobals()) {
|
2015-11-02 16:35:21 +01:00
|
|
|
AddSuggestion(matches, word, kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:37:23 +02:00
|
|
|
Namespace::Ptr systemNS = ScriptGlobal::Get("System");
|
|
|
|
|
|
|
|
AddSuggestions(matches, word, "", false, systemNS);
|
|
|
|
AddSuggestions(matches, word, "", true, systemNS->Get("Configuration"));
|
2018-08-07 13:55:41 +02:00
|
|
|
AddSuggestions(matches, word, "", false, ScriptGlobal::Get("Types"));
|
|
|
|
AddSuggestions(matches, word, "", false, ScriptGlobal::Get("Icinga"));
|
2016-08-12 11:25:36 +02:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
String::SizeType cperiod = word.RFind(".");
|
|
|
|
|
2016-02-08 17:10:12 +01:00
|
|
|
if (cperiod != String::NPos) {
|
2015-11-02 16:35:21 +01:00
|
|
|
String pword = word.SubStr(0, cperiod);
|
|
|
|
|
|
|
|
Value value;
|
|
|
|
|
|
|
|
try {
|
2017-12-15 05:34:46 +01:00
|
|
|
std::unique_ptr<Expression> expr = ConfigCompiler::CompileText("temp", pword);
|
2015-11-02 16:35:21 +01:00
|
|
|
|
|
|
|
if (expr)
|
|
|
|
value = expr->Evaluate(frame);
|
|
|
|
|
2016-08-12 13:13:01 +02:00
|
|
|
AddSuggestions(matches, word, pword, true, value);
|
2015-11-02 16:35:21 +01:00
|
|
|
} catch (...) { /* Ignore the exception */ }
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|