]> granicus.if.org Git - icinga2/blob - lib/remote/apilistener.hpp
Merge pull request #5967 from Icinga/feature/lazy-accessors
[icinga2] / lib / remote / apilistener.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 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 ApiListener final : 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();
61
62         static String GetApiDir();
63         static String GetCertsDir();
64         static String GetCaDir();
65         static String GetCertificateRequestsDir();
66
67         void UpdateSSLContext();
68
69         static ApiListener::Ptr GetInstance();
70
71         Endpoint::Ptr GetMaster() const;
72         bool IsMaster() const;
73
74         Endpoint::Ptr GetLocalEndpoint() 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();
81
82         void AddAnonymousClient(const JsonRpcConnection::Ptr& aclient);
83         void RemoveAnonymousClient(const JsonRpcConnection::Ptr& aclient);
84         std::set<JsonRpcConnection::Ptr> GetAnonymousClients() const;
85
86         void AddHttpClient(const HttpServerConnection::Ptr& aclient);
87         void RemoveHttpClient(const HttpServerConnection::Ptr& aclient);
88         std::set<HttpServerConnection::Ptr> GetHttpClients() 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();
103
104         static bool IsHACluster();
105         static String GetFromZoneName(const Zone::Ptr& fromZone);
106
107         static String GetDefaultCertPath();
108         static String GetDefaultKeyPath();
109         static String GetDefaultCaPath();
110
111 protected:
112         void OnConfigLoaded() override;
113         void OnAllConfigLoaded() override;
114         void Start(bool runtimeCreated) override;
115         void Stop(bool runtimeDeleted) override;
116
117         void ValidateTlsProtocolmin(const Lazy<String>& lvalue, const ValidationUtils& utils) override;
118
119 private:
120         std::shared_ptr<SSL_CTX> m_SSLContext;
121         std::set<TcpSocket::Ptr> m_Servers;
122
123         mutable boost::mutex m_AnonymousClientsLock;
124         mutable boost::mutex m_HttpClientsLock;
125         std::set<JsonRpcConnection::Ptr> m_AnonymousClients;
126         std::set<HttpServerConnection::Ptr> m_HttpClients;
127
128         Timer::Ptr m_Timer;
129         Timer::Ptr m_ReconnectTimer;
130         Timer::Ptr m_AuthorityTimer;
131         Timer::Ptr m_CleanupCertificateRequestsTimer;
132         Endpoint::Ptr m_LocalEndpoint;
133
134         static ApiListener::Ptr m_Instance;
135
136         void ApiTimerHandler();
137         void ApiReconnectTimerHandler();
138         void CleanupCertificateRequestsTimerHandler();
139
140         bool AddListener(const String& node, const String& service);
141         void AddConnection(const Endpoint::Ptr& endpoint);
142
143         void NewClientHandler(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
144         void NewClientHandlerInternal(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
145         void ListenerThreadProc(const Socket::Ptr& server);
146
147         WorkQueue m_RelayQueue;
148         WorkQueue m_SyncQueue{0, 4};
149
150         boost::mutex m_LogLock;
151         Stream::Ptr m_LogFile;
152         size_t m_LogMessageCount{0};
153
154         bool RelayMessageOne(const Zone::Ptr& zone, const MessageOrigin::Ptr& origin, const Dictionary::Ptr& message, const Endpoint::Ptr& currentMaster);
155         void SyncRelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
156         void PersistMessage(const Dictionary::Ptr& message, const ConfigObject::Ptr& secobj);
157
158         void OpenLogFile();
159         void RotateLogFile();
160         void CloseLogFile();
161         static void LogGlobHandler(std::vector<int>& files, const String& file);
162         void ReplayLog(const JsonRpcConnection::Ptr& client);
163
164         static void CopyCertificateFile(const String& oldCertPath, const String& newCertPath);
165
166         /* filesync */
167         static ConfigDirInformation LoadConfigDir(const String& dir);
168         static Dictionary::Ptr MergeConfigUpdate(const ConfigDirInformation& config);
169         static bool UpdateConfigDir(const ConfigDirInformation& oldConfig, const ConfigDirInformation& newConfig, const String& configDir, bool authoritative);
170
171         void SyncZoneDirs() const;
172         void SyncZoneDir(const Zone::Ptr& zone) const;
173
174         static void ConfigGlobHandler(ConfigDirInformation& config, const String& path, const String& file);
175         void SendConfigUpdate(const JsonRpcConnection::Ptr& aclient);
176
177         /* configsync */
178         void UpdateConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
179                 const JsonRpcConnection::Ptr& client = nullptr);
180         void DeleteConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
181                 const JsonRpcConnection::Ptr& client = nullptr);
182         void SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient);
183
184         void SyncClient(const JsonRpcConnection::Ptr& aclient, const Endpoint::Ptr& endpoint, bool needSync);
185 };
186
187 }
188
189 #endif /* APILISTENER_H */