]> granicus.if.org Git - icinga2/commitdiff
Refactored authentication code into a separate component.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 20 Apr 2012 14:06:06 +0000 (16:06 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 20 Apr 2012 14:06:06 +0000 (16:06 +0200)
icinga-app/icinga.conf [new file with mode: 0644]
icinga/Makefile.am
icinga/authenticationcomponent.cpp [new file with mode: 0644]
icinga/authenticationcomponent.h [new file with mode: 0644]
icinga/i2-icinga.h
icinga/icinga.vcxproj
icinga/icingaapplication.cpp
icinga/jsonrpcendpoint.cpp
icinga/subscriptioncomponent.cpp

diff --git a/icinga-app/icinga.conf b/icinga-app/icinga.conf
new file mode 100644 (file)
index 0000000..050bf4d
--- /dev/null
@@ -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
index 83165f632a06326fbb24a665ef631a7e076c9607..d965d5691200549c9a4ae2c7f22ace24bbf2f3a8 100644 (file)
@@ -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 (file)
index 0000000..833f2e6
--- /dev/null
@@ -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;
+}
diff --git a/icinga/authenticationcomponent.h b/icinga/authenticationcomponent.h
new file mode 100644 (file)
index 0000000..5d03e4a
--- /dev/null
@@ -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 */
index 9e8b2871728868fff4924428e866c9122c1edfd3..ed01699dc0b4f9fc9e8e2e67ee7ba2b83ad391ec 100644 (file)
@@ -18,6 +18,7 @@
 #include "icingaapplication.h"
 #include "subscriptioncomponent.h"
 #include "subscriptionmessage.h"
+#include "authenticationcomponent.h"
 #include "identitymessage.h"
 
 #endif /* I2ICINGA_H */
index 1a773372e955606cac332d5d01e2a44cdd849fac..e7f0406fb77dacff3c4f35725eafa4fa5d1c369b 100644 (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" />
index ebe1af691de40917bfe6a8c0ff1e26babf7fa399..bc7632a8eceb8c95ed28a763849601c648d70c39 100644 (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]);
index 057da887be0258df6e9851a8c74a0c46138611a9..ee19dad82979ce92d1edcfe9bc4f7532693a37fd 100644 (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
index f87ef71e9e9e5c245f21955c620020d872f2e27a..437fcce5f48bb7a2f2310bcb308c5796ab80a51b 100644 (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;
-}