Refactored authentication code into a separate component.

This commit is contained in:
Gunnar Beutner 2012-04-20 16:06:06 +02:00
parent f7f8edb875
commit fb53dd345c
9 changed files with 118 additions and 36 deletions

15
icinga-app/icinga.conf Normal file
View 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" }
}
}

View File

@ -5,6 +5,8 @@ pkglib_LTLIBRARIES = \
libicinga.la
libicinga_la_SOURCES = \
authenticationcomponent.cpp \
authenticationcomponent.h \
endpoint.cpp \
endpoint.h \
endpointmanager.cpp \

View 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(&params))
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;
}

View 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 */

View File

@ -18,6 +18,7 @@
#include "icingaapplication.h"
#include "subscriptioncomponent.h"
#include "subscriptionmessage.h"
#include "authenticationcomponent.h"
#include "identitymessage.h"
#endif /* I2ICINGA_H */

View File

@ -11,6 +11,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="authenticationcomponent.cpp" />
<ClCompile Include="endpoint.cpp" />
<ClCompile Include="endpointmanager.cpp" />
<ClCompile Include="icingaapplication.cpp" />
@ -21,6 +22,7 @@
<ClCompile Include="virtualendpoint.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="authenticationcomponent.h" />
<ClInclude Include="endpoint.h" />
<ClInclude Include="endpointmanager.h" />
<ClInclude Include="i2-icinga.h" />

View File

@ -54,8 +54,11 @@ int IcingaApplication::Main(const vector<string>& args)
connectionCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedRpcConnectionHandler, shared_from_this());
SubscriptionComponent::Ptr subscriptionsComponent = make_shared<SubscriptionComponent>();
RegisterComponent(subscriptionsComponent);
AuthenticationComponent::Ptr authenticationComponent = make_shared<AuthenticationComponent>();
RegisterComponent(authenticationComponent);
SubscriptionComponent::Ptr subscriptionComponent = make_shared<SubscriptionComponent>();
RegisterComponent(subscriptionComponent);
ConfigObject::Ptr fileComponentConfig = make_shared<ConfigObject>("component", "configfile");
fileComponentConfig->SetPropertyString("configFilename", args[1]);

View File

@ -23,16 +23,6 @@ void JsonRpcEndpoint::SetClient(JsonRpcClient::Ptr client)
client->OnNewMessage += bind_weak(&JsonRpcEndpoint::NewMessageHandler, shared_from_this());
client->OnClosed += bind_weak(&JsonRpcEndpoint::ClientClosedHandler, 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

View File

@ -17,7 +17,6 @@ void SubscriptionComponent::Start(void)
m_SubscriptionEndpoint = make_shared<VirtualEndpoint>();
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::SetIdentity", bind_weak(&SubscriptionComponent::IdentityMessageHandler, shared_from_this()));
m_SubscriptionEndpoint->RegisterMethodSource("message::Subscribe");
m_SubscriptionEndpoint->RegisterMethodSource("message::Provide");
m_SubscriptionEndpoint->RegisterMethodSource("message::Welcome");
@ -107,26 +106,3 @@ int SubscriptionComponent::ProvideMessageHandler(const NewRequestEventArgs& nrea
nrea.Sender->RegisterMethodSource(method);
return 0;
}
int SubscriptionComponent::IdentityMessageHandler(const NewRequestEventArgs& nrea)
{
Message params;
if (!nrea.Request.GetParams(&params))
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;
}