mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-24 06:05:01 +02:00
Refactored authentication code into a separate component.
This commit is contained in:
parent
f7f8edb875
commit
fb53dd345c
15
icinga-app/icinga.conf
Normal file
15
icinga-app/icinga.conf
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"component": {
|
||||||
|
"configrpc": { "replicate": "0", "configSource": "1" }
|
||||||
|
},
|
||||||
|
|
||||||
|
"rpclistener": {
|
||||||
|
"kekslistener": { "replicate": "0", "port": "7777" }
|
||||||
|
},
|
||||||
|
"rpcconnection": {
|
||||||
|
"keksclient": { "replicate": "0", "hostname": "localhost", "port": "7777" }
|
||||||
|
},
|
||||||
|
"host": {
|
||||||
|
"localhost": { "ipaddr": "127.0.0.1" }
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,8 @@ pkglib_LTLIBRARIES = \
|
|||||||
libicinga.la
|
libicinga.la
|
||||||
|
|
||||||
libicinga_la_SOURCES = \
|
libicinga_la_SOURCES = \
|
||||||
|
authenticationcomponent.cpp \
|
||||||
|
authenticationcomponent.h \
|
||||||
endpoint.cpp \
|
endpoint.cpp \
|
||||||
endpoint.h \
|
endpoint.h \
|
||||||
endpointmanager.cpp \
|
endpointmanager.cpp \
|
||||||
|
68
icinga/authenticationcomponent.cpp
Normal file
68
icinga/authenticationcomponent.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include "i2-icinga.h"
|
||||||
|
|
||||||
|
using namespace icinga;
|
||||||
|
|
||||||
|
IcingaApplication::Ptr AuthenticationComponent::GetIcingaApplication(void) const
|
||||||
|
{
|
||||||
|
return static_pointer_cast<IcingaApplication>(GetApplication());
|
||||||
|
}
|
||||||
|
|
||||||
|
string AuthenticationComponent::GetName(void) const
|
||||||
|
{
|
||||||
|
return "authenticationcomponent";
|
||||||
|
}
|
||||||
|
|
||||||
|
void AuthenticationComponent::Start(void)
|
||||||
|
{
|
||||||
|
m_AuthenticationEndpoint = make_shared<VirtualEndpoint>();
|
||||||
|
m_AuthenticationEndpoint->RegisterMethodHandler("message::SetIdentity", bind_weak(&AuthenticationComponent::IdentityMessageHandler, shared_from_this()));
|
||||||
|
|
||||||
|
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
|
||||||
|
mgr->OnNewEndpoint += bind_weak(&AuthenticationComponent::NewEndpointHandler, shared_from_this());
|
||||||
|
mgr->ForeachEndpoint(bind(&AuthenticationComponent::NewEndpointHandler, this, _1));
|
||||||
|
mgr->RegisterEndpoint(m_AuthenticationEndpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AuthenticationComponent::Stop(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int AuthenticationComponent::NewEndpointHandler(const NewEndpointEventArgs& neea)
|
||||||
|
{
|
||||||
|
if (neea.Endpoint->IsLocal())
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
JsonRpcRequest request;
|
||||||
|
request.SetVersion("2.0");
|
||||||
|
request.SetMethod("message::SetIdentity");
|
||||||
|
|
||||||
|
IdentityMessage params;
|
||||||
|
params.SetIdentity("keks");
|
||||||
|
request.SetParams(params);
|
||||||
|
|
||||||
|
neea.Endpoint->ProcessRequest(m_AuthenticationEndpoint, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
int AuthenticationComponent::IdentityMessageHandler(const NewRequestEventArgs& nrea)
|
||||||
|
{
|
||||||
|
Message params;
|
||||||
|
if (!nrea.Request.GetParams(¶ms))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
IdentityMessage identityMessage = params;
|
||||||
|
|
||||||
|
string identity;
|
||||||
|
if (!identityMessage.GetIdentity(&identity))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
nrea.Sender->SetIdentity(identity);
|
||||||
|
|
||||||
|
/* there's no authentication for now, just tell them it's ok to send messages */
|
||||||
|
JsonRpcRequest request;
|
||||||
|
request.SetVersion("2.0");
|
||||||
|
request.SetMethod("message::Welcome");
|
||||||
|
nrea.Sender->ProcessRequest(m_AuthenticationEndpoint, request);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
25
icinga/authenticationcomponent.h
Normal file
25
icinga/authenticationcomponent.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef AUTHENTICATIONCOMPONENT_H
|
||||||
|
#define AUTHENTICATIONCOMPONENT_H
|
||||||
|
|
||||||
|
namespace icinga
|
||||||
|
{
|
||||||
|
|
||||||
|
class AuthenticationComponent : public Component
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
VirtualEndpoint::Ptr m_AuthenticationEndpoint;
|
||||||
|
|
||||||
|
IcingaApplication::Ptr GetIcingaApplication(void) const;
|
||||||
|
|
||||||
|
int NewEndpointHandler(const NewEndpointEventArgs& neea);
|
||||||
|
int IdentityMessageHandler(const NewRequestEventArgs& nrea);
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual string GetName(void) const;
|
||||||
|
virtual void Start(void);
|
||||||
|
virtual void Stop(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* AUTHENTICATIONCOMPONENT_H */
|
@ -18,6 +18,7 @@
|
|||||||
#include "icingaapplication.h"
|
#include "icingaapplication.h"
|
||||||
#include "subscriptioncomponent.h"
|
#include "subscriptioncomponent.h"
|
||||||
#include "subscriptionmessage.h"
|
#include "subscriptionmessage.h"
|
||||||
|
#include "authenticationcomponent.h"
|
||||||
#include "identitymessage.h"
|
#include "identitymessage.h"
|
||||||
|
|
||||||
#endif /* I2ICINGA_H */
|
#endif /* I2ICINGA_H */
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
</ProjectConfiguration>
|
</ProjectConfiguration>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="authenticationcomponent.cpp" />
|
||||||
<ClCompile Include="endpoint.cpp" />
|
<ClCompile Include="endpoint.cpp" />
|
||||||
<ClCompile Include="endpointmanager.cpp" />
|
<ClCompile Include="endpointmanager.cpp" />
|
||||||
<ClCompile Include="icingaapplication.cpp" />
|
<ClCompile Include="icingaapplication.cpp" />
|
||||||
@ -21,6 +22,7 @@
|
|||||||
<ClCompile Include="virtualendpoint.cpp" />
|
<ClCompile Include="virtualendpoint.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="authenticationcomponent.h" />
|
||||||
<ClInclude Include="endpoint.h" />
|
<ClInclude Include="endpoint.h" />
|
||||||
<ClInclude Include="endpointmanager.h" />
|
<ClInclude Include="endpointmanager.h" />
|
||||||
<ClInclude Include="i2-icinga.h" />
|
<ClInclude Include="i2-icinga.h" />
|
||||||
|
@ -54,8 +54,11 @@ int IcingaApplication::Main(const vector<string>& args)
|
|||||||
|
|
||||||
connectionCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedRpcConnectionHandler, shared_from_this());
|
connectionCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedRpcConnectionHandler, shared_from_this());
|
||||||
|
|
||||||
SubscriptionComponent::Ptr subscriptionsComponent = make_shared<SubscriptionComponent>();
|
AuthenticationComponent::Ptr authenticationComponent = make_shared<AuthenticationComponent>();
|
||||||
RegisterComponent(subscriptionsComponent);
|
RegisterComponent(authenticationComponent);
|
||||||
|
|
||||||
|
SubscriptionComponent::Ptr subscriptionComponent = make_shared<SubscriptionComponent>();
|
||||||
|
RegisterComponent(subscriptionComponent);
|
||||||
|
|
||||||
ConfigObject::Ptr fileComponentConfig = make_shared<ConfigObject>("component", "configfile");
|
ConfigObject::Ptr fileComponentConfig = make_shared<ConfigObject>("component", "configfile");
|
||||||
fileComponentConfig->SetPropertyString("configFilename", args[1]);
|
fileComponentConfig->SetPropertyString("configFilename", args[1]);
|
||||||
|
@ -23,16 +23,6 @@ void JsonRpcEndpoint::SetClient(JsonRpcClient::Ptr client)
|
|||||||
client->OnNewMessage += bind_weak(&JsonRpcEndpoint::NewMessageHandler, shared_from_this());
|
client->OnNewMessage += bind_weak(&JsonRpcEndpoint::NewMessageHandler, shared_from_this());
|
||||||
client->OnClosed += bind_weak(&JsonRpcEndpoint::ClientClosedHandler, shared_from_this());
|
client->OnClosed += bind_weak(&JsonRpcEndpoint::ClientClosedHandler, shared_from_this());
|
||||||
client->OnError += bind_weak(&JsonRpcEndpoint::ClientErrorHandler, shared_from_this());
|
client->OnError += bind_weak(&JsonRpcEndpoint::ClientErrorHandler, shared_from_this());
|
||||||
|
|
||||||
JsonRpcRequest request;
|
|
||||||
request.SetVersion("2.0");
|
|
||||||
request.SetMethod("message::SetIdentity");
|
|
||||||
|
|
||||||
IdentityMessage params;
|
|
||||||
params.SetIdentity("keks");
|
|
||||||
request.SetParams(params);
|
|
||||||
|
|
||||||
client->SendMessage(request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JsonRpcEndpoint::IsLocal(void) const
|
bool JsonRpcEndpoint::IsLocal(void) const
|
||||||
|
@ -17,7 +17,6 @@ void SubscriptionComponent::Start(void)
|
|||||||
m_SubscriptionEndpoint = make_shared<VirtualEndpoint>();
|
m_SubscriptionEndpoint = make_shared<VirtualEndpoint>();
|
||||||
m_SubscriptionEndpoint->RegisterMethodHandler("message::Subscribe", bind_weak(&SubscriptionComponent::SubscribeMessageHandler, shared_from_this()));
|
m_SubscriptionEndpoint->RegisterMethodHandler("message::Subscribe", bind_weak(&SubscriptionComponent::SubscribeMessageHandler, shared_from_this()));
|
||||||
m_SubscriptionEndpoint->RegisterMethodHandler("message::Provide", bind_weak(&SubscriptionComponent::ProvideMessageHandler, shared_from_this()));
|
m_SubscriptionEndpoint->RegisterMethodHandler("message::Provide", bind_weak(&SubscriptionComponent::ProvideMessageHandler, shared_from_this()));
|
||||||
m_SubscriptionEndpoint->RegisterMethodHandler("message::SetIdentity", bind_weak(&SubscriptionComponent::IdentityMessageHandler, shared_from_this()));
|
|
||||||
m_SubscriptionEndpoint->RegisterMethodSource("message::Subscribe");
|
m_SubscriptionEndpoint->RegisterMethodSource("message::Subscribe");
|
||||||
m_SubscriptionEndpoint->RegisterMethodSource("message::Provide");
|
m_SubscriptionEndpoint->RegisterMethodSource("message::Provide");
|
||||||
m_SubscriptionEndpoint->RegisterMethodSource("message::Welcome");
|
m_SubscriptionEndpoint->RegisterMethodSource("message::Welcome");
|
||||||
@ -107,26 +106,3 @@ int SubscriptionComponent::ProvideMessageHandler(const NewRequestEventArgs& nrea
|
|||||||
nrea.Sender->RegisterMethodSource(method);
|
nrea.Sender->RegisterMethodSource(method);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SubscriptionComponent::IdentityMessageHandler(const NewRequestEventArgs& nrea)
|
|
||||||
{
|
|
||||||
Message params;
|
|
||||||
if (!nrea.Request.GetParams(¶ms))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
IdentityMessage identityMessage = params;
|
|
||||||
|
|
||||||
string identity;
|
|
||||||
if (!identityMessage.GetIdentity(&identity))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
nrea.Sender->SetIdentity(identity);
|
|
||||||
|
|
||||||
/* there's no authentication for now, just tell them it's ok to send messages */
|
|
||||||
JsonRpcRequest request;
|
|
||||||
request.SetVersion("2.0");
|
|
||||||
request.SetMethod("message::Welcome");
|
|
||||||
nrea.Sender->ProcessRequest(m_SubscriptionEndpoint, request);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user