From ef3ed6b826a60fddc0aac132bc43dc46b5525d83 Mon Sep 17 00:00:00 2001 From: Mattia Codato Date: Fri, 3 Jul 2020 11:00:40 +0200 Subject: [PATCH] Make authenticatedApiUser a static property of ActionsHandler --- lib/icinga/apiactions.cpp | 9 ++++----- lib/remote/actionshandler.cpp | 6 +++--- lib/remote/actionshandler.hpp | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index 0ff487942..76cb51788 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -659,12 +659,11 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, return ApiActions::CreateResult(404, "Can't find a valid notification for '" + resolved_notification + "'."); /* Get author */ - if (!authenticatedApiUser) - return ApiActions::CreateResult(401, "Can't find API user"); + if (!ActionsHandler::authenticatedApiUser) + BOOST_THROW_EXCEPTION(std::invalid_argument("Can't find API user.")); - String author = authenticatedApiUser->GetName(); - - cmd->Execute(notification, user, cr, NotificationType::NotificationCustom, author, "", execMacros, false); + cmd->Execute(notification, user, cr, NotificationType::NotificationCustom, + ActionsHandler::authenticatedApiUser->GetName(), "", execMacros, false); } } diff --git a/lib/remote/actionshandler.cpp b/lib/remote/actionshandler.cpp index 8668b8ea6..175e38a3a 100644 --- a/lib/remote/actionshandler.cpp +++ b/lib/remote/actionshandler.cpp @@ -11,7 +11,7 @@ using namespace icinga; -thread_local ApiUser::Ptr authenticatedApiUser; +thread_local ApiUser::Ptr ActionsHandler::authenticatedApiUser; REGISTER_URLHANDLER("/v1/actions", ActionsHandler); @@ -74,9 +74,9 @@ bool ActionsHandler::HandleRequest( bool verbose = false; - authenticatedApiUser = user; + ActionsHandler::authenticatedApiUser = user; Defer a ([&]() { - authenticatedApiUser = nullptr; + ActionsHandler::authenticatedApiUser = nullptr; }); if (params) diff --git a/lib/remote/actionshandler.hpp b/lib/remote/actionshandler.hpp index a7b70ff0d..e99b40cd9 100644 --- a/lib/remote/actionshandler.hpp +++ b/lib/remote/actionshandler.hpp @@ -5,8 +5,6 @@ #include "remote/httphandler.hpp" -extern thread_local icinga::ApiUser::Ptr authenticatedApiUser; - namespace icinga { @@ -15,6 +13,8 @@ class ActionsHandler final : public HttpHandler public: DECLARE_PTR_TYPEDEFS(ActionsHandler); + static thread_local ApiUser::Ptr authenticatedApiUser; + bool HandleRequest( AsioTlsStream& stream, const ApiUser::Ptr& user,