]> granicus.if.org Git - icinga2/blob - lib/remote/apilistener.hpp
Merge pull request #5571 from Icinga/feature/ca-proxy
[icinga2] / lib / remote / apilistener.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef APILISTENER_H
21 #define APILISTENER_H
22
23 #include "remote/apilistener.thpp"
24 #include "remote/jsonrpcconnection.hpp"
25 #include "remote/httpserverconnection.hpp"
26 #include "remote/endpoint.hpp"
27 #include "remote/messageorigin.hpp"
28 #include "base/configobject.hpp"
29 #include "base/timer.hpp"
30 #include "base/workqueue.hpp"
31 #include "base/tcpsocket.hpp"
32 #include "base/tlsstream.hpp"
33 #include <set>
34
35 namespace icinga
36 {
37
38 class JsonRpcConnection;
39
40 /**
41  * @ingroup remote
42  */
43 struct ConfigDirInformation
44 {
45         Dictionary::Ptr UpdateV1;
46         Dictionary::Ptr UpdateV2;
47 };
48
49 /**
50 * @ingroup remote
51 */
52 class I2_REMOTE_API ApiListener : public ObjectImpl<ApiListener>
53 {
54 public:
55         DECLARE_OBJECT(ApiListener);
56         DECLARE_OBJECTNAME(ApiListener);
57
58         static boost::signals2::signal<void(bool)> OnMasterChanged;
59
60         ApiListener(void);
61
62         static String GetApiDir(void);
63         static String GetCertsDir(void);
64         static String GetCaDir(void);
65         static String GetCertificateRequestsDir(void);
66
67         void UpdateSSLContext(void);
68
69         static ApiListener::Ptr GetInstance(void);
70
71         Endpoint::Ptr GetMaster(void) const;
72         bool IsMaster(void) const;
73
74         Endpoint::Ptr GetLocalEndpoint(void) const;
75
76         void SyncSendMessage(const Endpoint::Ptr& endpoint, const Dictionary::Ptr& message);
77         void RelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
78
79         static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
80         std::pair<Dictionary::Ptr, Dictionary::Ptr> GetStatus(void);
81
82         void AddAnonymousClient(const JsonRpcConnection::Ptr& aclient);
83         void RemoveAnonymousClient(const JsonRpcConnection::Ptr& aclient);
84         std::set<JsonRpcConnection::Ptr> GetAnonymousClients(void) const;
85
86         void AddHttpClient(const HttpServerConnection::Ptr& aclient);
87         void RemoveHttpClient(const HttpServerConnection::Ptr& aclient);
88         std::set<HttpServerConnection::Ptr> GetHttpClients(void) const;
89
90         static double CalculateZoneLag(const Endpoint::Ptr& endpoint);
91
92         /* filesync */
93         static Value ConfigUpdateHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
94         
95         /* configsync */
96         static void ConfigUpdateObjectHandler(const ConfigObject::Ptr& object, const Value& cookie);
97         static Value ConfigUpdateObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
98         static Value ConfigDeleteObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
99         
100         static Value HelloAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
101
102         static void UpdateObjectAuthority(void);
103
104         static bool IsHACluster(void);
105         static String GetFromZoneName(const Zone::Ptr& fromZone);
106
107 protected:
108         virtual void OnConfigLoaded(void) override;
109         virtual void OnAllConfigLoaded(void) override;
110         virtual void Start(bool runtimeCreated) override;
111         virtual void Stop(bool runtimeDeleted) override;
112
113         virtual void ValidateTlsProtocolmin(const String& value, const ValidationUtils& utils) override;
114
115 private:
116         boost::shared_ptr<SSL_CTX> m_SSLContext;
117         std::set<TcpSocket::Ptr> m_Servers;
118         std::set<JsonRpcConnection::Ptr> m_AnonymousClients;
119         std::set<HttpServerConnection::Ptr> m_HttpClients;
120         Timer::Ptr m_Timer;
121         Timer::Ptr m_ReconnectTimer;
122         Timer::Ptr m_AuthorityTimer;
123         Timer::Ptr m_CleanupCertificateRequestsTimer;
124         Endpoint::Ptr m_LocalEndpoint;
125
126         static ApiListener::Ptr m_Instance;
127
128         void ApiTimerHandler(void);
129         void ApiReconnectTimerHandler(void);
130         void CleanupCertificateRequestsTimerHandler(void);
131
132         bool AddListener(const String& node, const String& service);
133         void AddConnection(const Endpoint::Ptr& endpoint);
134
135         void NewClientHandler(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
136         void NewClientHandlerInternal(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
137         void ListenerThreadProc(const Socket::Ptr& server);
138
139         WorkQueue m_RelayQueue;
140         WorkQueue m_SyncQueue;
141
142         boost::mutex m_LogLock;
143         Stream::Ptr m_LogFile;
144         size_t m_LogMessageCount;
145
146         bool RelayMessageOne(const Zone::Ptr& zone, const MessageOrigin::Ptr& origin, const Dictionary::Ptr& message, const Endpoint::Ptr& currentMaster);
147         void SyncRelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
148         void PersistMessage(const Dictionary::Ptr& message, const ConfigObject::Ptr& secobj);
149
150         void OpenLogFile(void);
151         void RotateLogFile(void);
152         void CloseLogFile(void);
153         static void LogGlobHandler(std::vector<int>& files, const String& file);
154         void ReplayLog(const JsonRpcConnection::Ptr& client);
155
156         /* filesync */
157         static ConfigDirInformation LoadConfigDir(const String& dir);
158         static Dictionary::Ptr MergeConfigUpdate(const ConfigDirInformation& config);
159         static bool UpdateConfigDir(const ConfigDirInformation& oldConfig, const ConfigDirInformation& newConfig, const String& configDir, bool authoritative);
160
161         void SyncZoneDirs(void) const;
162         void SyncZoneDir(const Zone::Ptr& zone) const;
163
164         static void ConfigGlobHandler(ConfigDirInformation& config, const String& path, const String& file);
165         void SendConfigUpdate(const JsonRpcConnection::Ptr& aclient);
166
167         /* configsync */
168         void UpdateConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
169             const JsonRpcConnection::Ptr& client = JsonRpcConnection::Ptr());
170         void DeleteConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
171             const JsonRpcConnection::Ptr& client = JsonRpcConnection::Ptr());
172         void SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient);
173
174         void SyncClient(const JsonRpcConnection::Ptr& aclient, const Endpoint::Ptr& endpoint, bool needSync);
175 };
176
177 }
178
179 #endif /* APILISTENER_H */