Skip to content

Commit 3f54253

Browse files
committed
Remove thread-local AuthenticatedApiUser & pass it as param instead
1 parent dc950a1 commit 3f54253

File tree

6 files changed

+86
-63
lines changed

6 files changed

+86
-63
lines changed

lib/icinga/apiactions.cpp

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ Dictionary::Ptr ApiActions::CreateResult(int code, const String& status,
5353
return result;
5454
}
5555

56-
Dictionary::Ptr ApiActions::ProcessCheckResult(const ConfigObject::Ptr& object,
57-
const Dictionary::Ptr& params)
56+
Dictionary::Ptr ApiActions::ProcessCheckResult(
57+
const ConfigObject::Ptr& object,
58+
const ApiUser::Ptr&,
59+
const Dictionary::Ptr& params
60+
)
5861
{
5962
using Result = Checkable::ProcessingResult;
6063

@@ -140,8 +143,11 @@ Dictionary::Ptr ApiActions::ProcessCheckResult(const ConfigObject::Ptr& object,
140143
return ApiActions::CreateResult(500, "Unexpected result (" + std::to_string(static_cast<int>(result)) + ") for object '" + checkable->GetName() + "'. Please submit a bug report at https://github.com/Icinga/icinga2");
141144
}
142145

143-
Dictionary::Ptr ApiActions::RescheduleCheck(const ConfigObject::Ptr& object,
144-
const Dictionary::Ptr& params)
146+
Dictionary::Ptr ApiActions::RescheduleCheck(
147+
const ConfigObject::Ptr& object,
148+
const ApiUser::Ptr&,
149+
const Dictionary::Ptr& params
150+
)
145151
{
146152
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
147153

@@ -165,8 +171,11 @@ Dictionary::Ptr ApiActions::RescheduleCheck(const ConfigObject::Ptr& object,
165171
return ApiActions::CreateResult(200, "Successfully rescheduled check for object '" + checkable->GetName() + "'.");
166172
}
167173

168-
Dictionary::Ptr ApiActions::SendCustomNotification(const ConfigObject::Ptr& object,
169-
const Dictionary::Ptr& params)
174+
Dictionary::Ptr ApiActions::SendCustomNotification(
175+
const ConfigObject::Ptr& object,
176+
const ApiUser::Ptr&,
177+
const Dictionary::Ptr& params
178+
)
170179
{
171180
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
172181

@@ -188,8 +197,11 @@ Dictionary::Ptr ApiActions::SendCustomNotification(const ConfigObject::Ptr& obje
188197
return ApiActions::CreateResult(200, "Successfully sent custom notification for object '" + checkable->GetName() + "'.");
189198
}
190199

191-
Dictionary::Ptr ApiActions::DelayNotification(const ConfigObject::Ptr& object,
192-
const Dictionary::Ptr& params)
200+
Dictionary::Ptr ApiActions::DelayNotification(
201+
const ConfigObject::Ptr& object,
202+
const ApiUser::Ptr&,
203+
const Dictionary::Ptr& params
204+
)
193205
{
194206
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
195207

@@ -206,8 +218,11 @@ Dictionary::Ptr ApiActions::DelayNotification(const ConfigObject::Ptr& object,
206218
return ApiActions::CreateResult(200, "Successfully delayed notifications for object '" + checkable->GetName() + "'.");
207219
}
208220

209-
Dictionary::Ptr ApiActions::AcknowledgeProblem(const ConfigObject::Ptr& object,
210-
const Dictionary::Ptr& params)
221+
Dictionary::Ptr ApiActions::AcknowledgeProblem(
222+
const ConfigObject::Ptr& object,
223+
const ApiUser::Ptr&,
224+
const Dictionary::Ptr& params
225+
)
211226
{
212227
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
213228

@@ -268,8 +283,11 @@ Dictionary::Ptr ApiActions::AcknowledgeProblem(const ConfigObject::Ptr& object,
268283
return ApiActions::CreateResult(200, "Successfully acknowledged problem for object '" + checkable->GetName() + "'.");
269284
}
270285

271-
Dictionary::Ptr ApiActions::RemoveAcknowledgement(const ConfigObject::Ptr& object,
272-
const Dictionary::Ptr& params)
286+
Dictionary::Ptr ApiActions::RemoveAcknowledgement(
287+
const ConfigObject::Ptr& object,
288+
const ApiUser::Ptr&,
289+
const Dictionary::Ptr& params
290+
)
273291
{
274292
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
275293

@@ -292,8 +310,11 @@ Dictionary::Ptr ApiActions::RemoveAcknowledgement(const ConfigObject::Ptr& objec
292310
return ApiActions::CreateResult(200, "Successfully removed acknowledgement for object '" + checkable->GetName() + "'.");
293311
}
294312

295-
Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object,
296-
const Dictionary::Ptr& params)
313+
Dictionary::Ptr ApiActions::AddComment(
314+
const ConfigObject::Ptr& object,
315+
const ApiUser::Ptr&,
316+
const Dictionary::Ptr& params
317+
)
297318
{
298319
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
299320

@@ -331,8 +352,11 @@ Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object,
331352
+ "'.", additional);
332353
}
333354

334-
Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object,
335-
const Dictionary::Ptr& params)
355+
Dictionary::Ptr ApiActions::RemoveComment(
356+
const ConfigObject::Ptr& object,
357+
const ApiUser::Ptr&,
358+
const Dictionary::Ptr& params
359+
)
336360
{
337361
ConfigObjectsSharedLock lock (std::try_to_lock);
338362

@@ -365,8 +389,11 @@ Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object,
365389
return ApiActions::CreateResult(200, "Successfully removed comment '" + commentName + "'.");
366390
}
367391

368-
Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
369-
const Dictionary::Ptr& params)
392+
Dictionary::Ptr ApiActions::ScheduleDowntime(
393+
const ConfigObject::Ptr& object,
394+
const ApiUser::Ptr&,
395+
const Dictionary::Ptr& params
396+
)
370397
{
371398
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
372399

@@ -535,8 +562,11 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
535562
downtimeName + "' for object '" + checkable->GetName() + "'.", additional);
536563
}
537564

538-
Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
539-
const Dictionary::Ptr& params)
565+
Dictionary::Ptr ApiActions::RemoveDowntime(
566+
const ConfigObject::Ptr& object,
567+
const ApiUser::Ptr&,
568+
const Dictionary::Ptr& params
569+
)
540570
{
541571
ConfigObjectsSharedLock lock (std::try_to_lock);
542572

@@ -588,24 +618,21 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
588618
}
589619
}
590620

591-
Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object,
592-
const Dictionary::Ptr& params)
621+
Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr&, const ApiUser::Ptr&, const Dictionary::Ptr&)
593622
{
594623
Application::RequestShutdown();
595624

596625
return ApiActions::CreateResult(200, "Shutting down Icinga 2.");
597626
}
598627

599-
Dictionary::Ptr ApiActions::RestartProcess(const ConfigObject::Ptr& object,
600-
const Dictionary::Ptr& params)
628+
Dictionary::Ptr ApiActions::RestartProcess(const ConfigObject::Ptr&, const ApiUser::Ptr&, const Dictionary::Ptr&)
601629
{
602630
Application::RequestRestart();
603631

604632
return ApiActions::CreateResult(200, "Restarting Icinga 2.");
605633
}
606634

607-
Dictionary::Ptr ApiActions::GenerateTicket(const ConfigObject::Ptr&,
608-
const Dictionary::Ptr& params)
635+
Dictionary::Ptr ApiActions::GenerateTicket(const ConfigObject::Ptr&, const ApiUser::Ptr&, const Dictionary::Ptr& params)
609636
{
610637
if (!params->Contains("cn"))
611638
return ApiActions::CreateResult(400, "Option 'cn' is required");
@@ -653,7 +680,11 @@ Value ApiActions::GetSingleObjectByNameUsingPermissions(const String& type, cons
653680
return objs.at(0);
654681
};
655682

656-
Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, const Dictionary::Ptr& params)
683+
Dictionary::Ptr ApiActions::ExecuteCommand(
684+
const ConfigObject::Ptr& object,
685+
const ApiUser::Ptr& apiUser,
686+
const Dictionary::Ptr& params
687+
)
657688
{
658689
ApiListener::Ptr listener = ApiListener::GetInstance();
659690

@@ -717,11 +748,11 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
717748
nullptr, MacroProcessor::EscapeCallback(), nullptr, false
718749
);
719750

720-
if (!ActionsHandler::AuthenticatedApiUser)
751+
if (!apiUser)
721752
BOOST_THROW_EXCEPTION(std::invalid_argument("Can't find API user."));
722753

723754
/* Get endpoint */
724-
Endpoint::Ptr endpointPtr = GetSingleObjectByNameUsingPermissions(Endpoint::GetTypeName(), resolved_endpoint, ActionsHandler::AuthenticatedApiUser);
755+
Endpoint::Ptr endpointPtr = GetSingleObjectByNameUsingPermissions(Endpoint::GetTypeName(), resolved_endpoint, apiUser);
725756

726757
if (!endpointPtr)
727758
return ApiActions::CreateResult(404, "Can't find a valid endpoint for '" + resolved_endpoint + "'.");
@@ -779,7 +810,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
779810
Dictionary::Ptr execParams = new Dictionary();
780811

781812
if (command_type == "CheckCommand") {
782-
CheckCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(CheckCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
813+
CheckCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(CheckCommand::GetTypeName(), resolved_command, apiUser);
783814

784815
if (!cmd)
785816
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
@@ -791,7 +822,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
791822
cmd->Execute(checkable, cr, listener->GetWaitGroup(), execMacros, false);
792823
}
793824
} else if (command_type == "EventCommand") {
794-
EventCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(EventCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
825+
EventCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(EventCommand::GetTypeName(), resolved_command, apiUser);
795826

796827
if (!cmd)
797828
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
@@ -803,7 +834,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
803834
cmd->Execute(checkable, execMacros, false);
804835
}
805836
} else if (command_type == "NotificationCommand") {
806-
NotificationCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(NotificationCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
837+
NotificationCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(NotificationCommand::GetTypeName(), resolved_command, apiUser);
807838

808839
if (!cmd)
809840
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
@@ -820,7 +851,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
820851
MacroProcessor::EscapeCallback(), nullptr, false
821852
);
822853

823-
User::Ptr user = GetSingleObjectByNameUsingPermissions(User::GetTypeName(), resolved_user, ActionsHandler::AuthenticatedApiUser);
854+
User::Ptr user = GetSingleObjectByNameUsingPermissions(User::GetTypeName(), resolved_user, apiUser);
824855

825856
if (!user)
826857
return ApiActions::CreateResult(404, "Can't find a valid user for '" + resolved_user + "'.");
@@ -839,7 +870,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
839870
MacroProcessor::EscapeCallback(), nullptr, false
840871
);
841872

842-
Notification::Ptr notification = GetSingleObjectByNameUsingPermissions(Notification::GetTypeName(), resolved_notification, ActionsHandler::AuthenticatedApiUser);
873+
Notification::Ptr notification = GetSingleObjectByNameUsingPermissions(Notification::GetTypeName(), resolved_notification, apiUser);
843874

844875
if (!notification)
845876
return ApiActions::CreateResult(404, "Can't find a valid notification for '" + resolved_notification + "'.");
@@ -852,7 +883,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
852883
});
853884

854885
cmd->Execute(notification, user, cr, NotificationType::NotificationCustom,
855-
ActionsHandler::AuthenticatedApiUser->GetName(), "", execMacros, false);
886+
apiUser->GetName(), "", execMacros, false);
856887
}
857888
}
858889

lib/icinga/apiactions.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ namespace icinga
1717
class ApiActions
1818
{
1919
public:
20-
static Dictionary::Ptr ProcessCheckResult(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
21-
static Dictionary::Ptr RescheduleCheck(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
22-
static Dictionary::Ptr SendCustomNotification(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
23-
static Dictionary::Ptr DelayNotification(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
24-
static Dictionary::Ptr AcknowledgeProblem(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
25-
static Dictionary::Ptr RemoveAcknowledgement(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
26-
static Dictionary::Ptr AddComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
27-
static Dictionary::Ptr RemoveComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
28-
static Dictionary::Ptr ScheduleDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
29-
static Dictionary::Ptr RemoveDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
30-
static Dictionary::Ptr ShutdownProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
31-
static Dictionary::Ptr RestartProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
32-
static Dictionary::Ptr GenerateTicket(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
33-
static Dictionary::Ptr ExecuteCommand(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
20+
static Dictionary::Ptr ProcessCheckResult(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
21+
static Dictionary::Ptr RescheduleCheck(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
22+
static Dictionary::Ptr SendCustomNotification(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
23+
static Dictionary::Ptr DelayNotification(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
24+
static Dictionary::Ptr AcknowledgeProblem(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
25+
static Dictionary::Ptr RemoveAcknowledgement(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
26+
static Dictionary::Ptr AddComment(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
27+
static Dictionary::Ptr RemoveComment(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
28+
static Dictionary::Ptr ScheduleDowntime(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
29+
static Dictionary::Ptr RemoveDowntime(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
30+
static Dictionary::Ptr ShutdownProcess(const ConfigObject::Ptr&, const ApiUser::Ptr&, const Dictionary::Ptr&);
31+
static Dictionary::Ptr RestartProcess(const ConfigObject::Ptr&, const ApiUser::Ptr&, const Dictionary::Ptr&);
32+
static Dictionary::Ptr GenerateTicket(const ConfigObject::Ptr& object, const ApiUser::Ptr&, const Dictionary::Ptr& params);
33+
static Dictionary::Ptr ExecuteCommand(const ConfigObject::Ptr& object, const ApiUser::Ptr& apiUser, const Dictionary::Ptr& params);
3434

3535
private:
3636
static Dictionary::Ptr CreateResult(int code, const String& status, const Dictionary::Ptr& additional = nullptr);

lib/remote/actionshandler.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
using namespace icinga;
1313

14-
thread_local ApiUser::Ptr ActionsHandler::AuthenticatedApiUser;
15-
1614
REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
1715

1816
bool ActionsHandler::HandleRequest(
@@ -79,11 +77,6 @@ bool ActionsHandler::HandleRequest(
7977

8078
bool verbose = false;
8179

82-
ActionsHandler::AuthenticatedApiUser = user;
83-
Defer a ([]() {
84-
ActionsHandler::AuthenticatedApiUser = nullptr;
85-
});
86-
8780
if (params)
8881
verbose = HttpUtility::GetLastParameter(params, "verbose");
8982

@@ -110,7 +103,7 @@ bool ActionsHandler::HandleRequest(
110103
}
111104

112105
try {
113-
results.emplace_back(action->Invoke(obj, params));
106+
results.emplace_back(action->Invoke(obj, user, params));
114107
} catch (const std::exception& ex) {
115108
Dictionary::Ptr fail = new Dictionary({
116109
{ "code", 500 },

lib/remote/actionshandler.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class ActionsHandler final : public HttpHandler
1313
public:
1414
DECLARE_PTR_TYPEDEFS(ActionsHandler);
1515

16-
static thread_local ApiUser::Ptr AuthenticatedApiUser;
17-
1816
bool HandleRequest(
1917
const WaitGroup::Ptr& waitGroup,
2018
const HttpRequest& request,

lib/remote/apiaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ ApiAction::ApiAction(std::vector<String> types, Callback action)
99
: m_Types(std::move(types)), m_Callback(std::move(action))
1010
{ }
1111

12-
Value ApiAction::Invoke(const ConfigObject::Ptr& target, const Dictionary::Ptr& params)
12+
Value ApiAction::Invoke(const ConfigObject::Ptr& target, const ApiUser::Ptr& user, const Dictionary::Ptr& params)
1313
{
14-
return m_Callback(target, params);
14+
return m_Callback(target, user, params);
1515
}
1616

1717
const std::vector<String>& ApiAction::GetTypes() const

lib/remote/apiaction.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "base/value.hpp"
99
#include "base/dictionary.hpp"
1010
#include "base/configobject.hpp"
11+
#include "remote/apiuser.hpp"
1112
#include <vector>
1213
#include <boost/algorithm/string/replace.hpp>
1314

@@ -24,11 +25,11 @@ class ApiAction final : public Object
2425
public:
2526
DECLARE_PTR_TYPEDEFS(ApiAction);
2627

27-
typedef std::function<Value(const ConfigObject::Ptr& target, const Dictionary::Ptr& params)> Callback;
28+
typedef std::function<Value(const ConfigObject::Ptr& target, const ApiUser::Ptr&, const Dictionary::Ptr& params)> Callback;
2829

2930
ApiAction(std::vector<String> registerTypes, Callback function);
3031

31-
Value Invoke(const ConfigObject::Ptr& target, const Dictionary::Ptr& params);
32+
Value Invoke(const ConfigObject::Ptr& target, const ApiUser::Ptr& user, const Dictionary::Ptr& params);
3233

3334
const std::vector<String>& GetTypes() const;
3435

0 commit comments

Comments
 (0)