File: k8s_handler.h

package info (click to toggle)
falcosecurity-libs 0.1.1dev%2Bgit20220316.e5c53d64-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,732 kB
  • sloc: cpp: 55,770; ansic: 37,330; makefile: 74; sh: 13
file content (317 lines) | stat: -rw-r--r-- 9,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright (C) 2021 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/
//
// k8s_handler.h
//
#ifndef MINIMAL_BUILD
#pragma once

#include "json/json.h"
#include "sinsp_auth.h"
#include "socket_collector.h"
#include "k8s_state.h"
#include "k8s_api_error.h"
#include <unordered_set>

class sinsp;

class k8s_handler
{
public:
	typedef k8s_component::msg_reason msg_reason;
	typedef k8s_component::msg_data msg_data;

	typedef std::shared_ptr<k8s_handler>   ptr_t;
	typedef std::vector<std::string>       uri_list_t;
	typedef std::shared_ptr<Json::Value>   json_ptr_t;
	typedef std::shared_ptr<k8s_api_error> api_error_ptr;
#if defined(HAS_CAPTURE) && !defined(_WIN32)
	typedef sinsp_ssl::ptr_t                             ssl_ptr_t;
	typedef sinsp_bearer_token::ptr_t                    bt_ptr_t;
	typedef socket_data_handler<k8s_handler>             handler_t;
	typedef handler_t::ptr_t                             handler_ptr_t;
	typedef socket_collector<handler_t>                  collector_t;
	typedef std::shared_ptr<socket_collector<handler_t>> collector_ptr_t;
#endif // HAS_CAPTURE

	static const int default_timeout_ms = 1000L;

	k8s_handler(const std::string& id,
		bool is_captured,
#if defined(HAS_CAPTURE) && !defined(_WIN32)
		std::string url,
		const std::string& path,
		const std::string& state_filter,
		const std::string& event_filter,
		const std::string& null_filter,
		collector_ptr_t collector = nullptr,
		const std::string& http_version = "1.1",
		int timeout_ms = default_timeout_ms,
		ssl_ptr_t ssl = nullptr,
		bt_ptr_t bt = nullptr,
		bool watch = true,
		bool connect = true,
		ptr_t dependency_handler = nullptr,
		bool blocking_socket = false,
#endif // HAS_CAPTURE
		unsigned max_messages = ~0,
		k8s_state_t* state = nullptr,
		uint32_t data_max_b = K8S_DATA_MAX_B,
		uint32_t data_chunk_wait_us = K8S_DATA_CHUNK_WAIT_US);

	virtual ~k8s_handler();

	bool connection_error() const;
	bool is_alive() const;
	bool ready() const;
	void set_event_json(json_ptr_t json, const std::string&);
	const std::string& get_id() const;
#if defined(HAS_CAPTURE) && !defined(_WIN32)
	handler_ptr_t handler();
#endif // HAS_CAPTURE
	std::string get_url() const;

	void collect_data();
	void set_machine_id(const std::string& machine_id);
	const std::string& get_machine_id() const;

	bool is_state_built() const;
	std::string name() const;
	api_error_ptr error() const;
	virtual void handle_json(Json::Value&& root);

	unsigned get_max_messages() const;
	void set_max_messages(unsigned max_msgs);

protected:
	typedef std::unordered_set<std::string> ip_addr_list_t;

	virtual bool handle_component(const Json::Value& json, const msg_data* data = 0) = 0;
	msg_data get_msg_data(const std::string& evt, const std::string& type, const Json::Value& root);
#if defined(HAS_CAPTURE) && !defined(_WIN32)
	static bool is_ip_address(const std::string& addr);
#endif // HAS_CAPTURE

	k8s_pair_list extract_object(const Json::Value& object);

	template <typename T>
	void handle_selectors(T& component, const Json::Value& selector)
	{
		if(!selector.isNull())
		{
			component.set_selectors(extract_object(selector));
		}
		else
		{
			g_logger.log("K8s Replication Controller: Null selector object.", sinsp_logger::SEV_ERROR);
		}
	}

	void log_event(const msg_data& data);
	void handle_error(const msg_data& data, const Json::Value& root, bool log = true);
	void log_error(const msg_data& data, const Json::Value& root);
	void log_not_found(const msg_data& data) const;

	k8s_state_t* m_state = nullptr;
	bool         m_state_built = false;
	bool         m_data_received = false;
	static std::string ERROR_FILTER;

private:
	typedef void (k8s_handler::*callback_func_t)(json_ptr_t, const std::string&);

	typedef std::vector<json_ptr_t> event_list_t;

#if defined(HAS_CAPTURE) && !defined(_WIN32)
	static ip_addr_list_t hostname_to_ip(const std::string& hostname);
#endif // HAS_CAPTURE

	bool connect();
	void make_http();
	void send_data_request();
	void receive_response();
	void check_enabled();
	void check_state();
	void check_collector_status();
	void process_events();

	const std::string& translate_name(const std::string& event_name);
	bool dependency_ready() const;

	std::string     m_id;
	std::string     m_machine_id;
#if defined(HAS_CAPTURE) && !defined(_WIN32)
	collector_ptr_t m_collector;
	handler_ptr_t   m_handler;
	std::string     m_path;
	std::string     m_state_filter;
	std::string     m_event_filter;
	std::string     m_null_filter;
	std::string*    m_filter;
	long            m_timeout_ms;
	std::string     m_url;
	bool            m_req_sent = false;
	bool            m_resp_recvd = false;
	json_query      m_jq;
	std::string     m_http_version;
	ssl_ptr_t       m_ssl;
	bt_ptr_t        m_bt;

	// some handlers only fetch state and die by design (eg. api or extensions handlers
	// have no need to continuously watch for updates)
	// this flag indicates whether handler should continue to watch after receiving
	// the initial state
	bool m_watch;
	bool m_watching = false; // indication of being in watch mode

	// flag indicating whether to connect to K8s API server (no connection needed when
	// replaying capture)
	bool m_connect;

	// k8s_handler on which this handler depends; the dependency handler must not be null and
	// it must have its state fully built before this handler can begin building its own state
	ptr_t m_dependency_handler;

	bool m_blocking_socket = false;

#endif // HAS_CAPTURE

	// limits the number of messages handled in single cycle
	unsigned m_max_messages = ~0;
	bool m_state_processing_started = false;

	event_list_t m_events;

	// error indicating something went wrong with the K8s component handled by this handler
	// this error is later examined by k8s::check_components() and if it is
	// HTTP status > 400, one of the following actions is taken:
	//  - if component is critical for consistent k8s state (eg. namespace, node, pod),
	//    exception is thrown and, consequently, the whole k8s framework will be destroyed
	//  - if component is not critical (eg. extensions like daemonset or deployment),
	//    error is logged and handler is destroyed, but the k8s framework continues to
	//    exist without it, only receiving data for existing components
	api_error_ptr m_error;

	// this capture flag does not indicate whether we are in global capture mode,
	// it is only an indication of whether this handler data should be captured
	// at all (eg. there is no need to capture api or extensions detection data)
	//
	// global capture flag is checked in the k8s state call
	bool m_is_captured = false;

	bool m_connect_logged = false;
};

inline unsigned k8s_handler::get_max_messages() const
{
	return m_max_messages;
}

inline void k8s_handler::set_max_messages(unsigned max_msgs)
{
	m_max_messages = max_msgs;
}

#if defined(HAS_CAPTURE) && !defined(_WIN32)
inline k8s_handler::handler_ptr_t k8s_handler::handler()
{
	return m_handler;
}
#endif // HAS_CAPTURE

inline std::string k8s_handler::get_url() const
{
#if defined(HAS_CAPTURE) && !defined(_WIN32)
	return m_url;
#else
	return "";
#endif
}

inline const std::string& k8s_handler::get_id() const
{
	return m_id;
}

inline void k8s_handler::set_machine_id(const std::string& machine_id)
{
	m_machine_id = machine_id;
}

inline const std::string& k8s_handler::get_machine_id() const
{
	return m_machine_id;
}

inline bool k8s_handler::ready() const
{
	return m_data_received;
}

inline bool k8s_handler::is_state_built() const
{
	return m_state_built;
}

inline void k8s_handler::log_event(const msg_data& data)
{
	g_logger.log("K8s " + data.get_reason_desc() + ' ' +
				 data.m_kind + ' ' +
				 data.m_name + " [" + data.m_uid + "]",
				 sinsp_logger::SEV_DEBUG);
}

inline void k8s_handler::log_not_found(const msg_data& data) const
{
	g_logger.log("K8s " + name() + " not found [" + data.m_uid + "]: " + data.m_name,
				 sinsp_logger::SEV_ERROR);
}

inline k8s_handler::api_error_ptr k8s_handler::error() const
{
	return m_error;
}

// This dummy class serves only as a dependency stand-in for handlers
// which have no dependencies (eg. nodes handler, which is first populated
// into the state and has no dependency; or special-purpose handlers,
// such as delegator, api handler etc), but the logic requires a non-null
// pointer to handler to determine whether dependency is ready; to avoid
// special-casing eg. nodes handler all over the place, we use this dummy
// liar, always returning true for its state being built, as the dependency
class k8s_dummy_handler : public k8s_handler
{
public:
	k8s_dummy_handler(): k8s_handler("k8s_dummy_handler", false,
#if defined(HAS_CAPTURE) && !defined(_WIN32)
									 "", "", "", "",  "", nullptr,
									 "", 0, nullptr, nullptr,
									 false, false, nullptr, false,
#endif // HAS_CAPTURE
									 ~0, nullptr)
	{
		m_state_built = true;
	}

private:
	virtual bool handle_component(const Json::Value& json, const msg_data* data = 0)
	{
		return false;
	};
};

#endif // MINIMAL_BUILD