From: Gunnar Beutner Date: Fri, 20 Apr 2012 14:06:06 +0000 (+0200) Subject: Refactored authentication code into a separate component. X-Git-Tag: v0.0.1~609 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fb53dd345c8437a2c6a2c8bd997d9510b1e686c3;p=icinga2 Refactored authentication code into a separate component. --- diff --git a/icinga-app/icinga.conf b/icinga-app/icinga.conf new file mode 100644 index 000000000..050bf4df6 --- /dev/null +++ b/icinga-app/icinga.conf @@ -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" } + } +} \ No newline at end of file diff --git a/icinga/Makefile.am b/icinga/Makefile.am index 83165f632..d965d5691 100644 --- a/icinga/Makefile.am +++ b/icinga/Makefile.am @@ -5,6 +5,8 @@ pkglib_LTLIBRARIES = \ libicinga.la libicinga_la_SOURCES = \ + authenticationcomponent.cpp \ + authenticationcomponent.h \ endpoint.cpp \ endpoint.h \ endpointmanager.cpp \ diff --git a/icinga/authenticationcomponent.cpp b/icinga/authenticationcomponent.cpp new file mode 100644 index 000000000..833f2e6cd --- /dev/null +++ b/icinga/authenticationcomponent.cpp @@ -0,0 +1,68 @@ +#include "i2-icinga.h" + +using namespace icinga; + +IcingaApplication::Ptr AuthenticationComponent::GetIcingaApplication(void) const +{ + return static_pointer_cast(GetApplication()); +} + +string AuthenticationComponent::GetName(void) const +{ + return "authenticationcomponent"; +} + +void AuthenticationComponent::Start(void) +{ + m_AuthenticationEndpoint = make_shared(); + 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; +} diff --git a/icinga/authenticationcomponent.h b/icinga/authenticationcomponent.h new file mode 100644 index 000000000..5d03e4a9e --- /dev/null +++ b/icinga/authenticationcomponent.h @@ -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 */ diff --git a/icinga/i2-icinga.h b/icinga/i2-icinga.h index 9e8b28717..ed01699dc 100644 --- a/icinga/i2-icinga.h +++ b/icinga/i2-icinga.h @@ -18,6 +18,7 @@ #include "icingaapplication.h" #include "subscriptioncomponent.h" #include "subscriptionmessage.h" +#include "authenticationcomponent.h" #include "identitymessage.h" #endif /* I2ICINGA_H */ diff --git a/icinga/icinga.vcxproj b/icinga/icinga.vcxproj index 1a773372e..e7f0406fb 100644 --- a/icinga/icinga.vcxproj +++ b/icinga/icinga.vcxproj @@ -11,6 +11,7 @@ + @@ -21,6 +22,7 @@ + diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index ebe1af691..bc7632a8e 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -54,8 +54,11 @@ int IcingaApplication::Main(const vector& args) connectionCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedRpcConnectionHandler, shared_from_this()); - SubscriptionComponent::Ptr subscriptionsComponent = make_shared(); - RegisterComponent(subscriptionsComponent); + AuthenticationComponent::Ptr authenticationComponent = make_shared(); + RegisterComponent(authenticationComponent); + + SubscriptionComponent::Ptr subscriptionComponent = make_shared(); + RegisterComponent(subscriptionComponent); ConfigObject::Ptr fileComponentConfig = make_shared("component", "configfile"); fileComponentConfig->SetPropertyString("configFilename", args[1]); diff --git a/icinga/jsonrpcendpoint.cpp b/icinga/jsonrpcendpoint.cpp index 057da887b..ee19dad82 100644 --- a/icinga/jsonrpcendpoint.cpp +++ b/icinga/jsonrpcendpoint.cpp @@ -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 diff --git a/icinga/subscriptioncomponent.cpp b/icinga/subscriptioncomponent.cpp index f87ef71e9..437fcce5f 100644 --- a/icinga/subscriptioncomponent.cpp +++ b/icinga/subscriptioncomponent.cpp @@ -17,7 +17,6 @@ void SubscriptionComponent::Start(void) m_SubscriptionEndpoint = make_shared(); 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(¶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; -}