File: k8s_net.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 (185 lines) | stat: -rw-r--r-- 5,236 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
/*
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_net.h
//
// connects and gets the data from k8s_net REST API interface
//
#ifndef MINIMAL_BUILD
#pragma once

#if defined(HAS_CAPTURE) && !defined(_WIN32)

#include "k8s_component.h"
#include "k8s_event_data.h"
#include "k8s_handler.h"
#include "k8s_event_data.h"
#include "uri.h"
#include "sinsp_curl.h"
#include <sstream>
#include <utility>

class k8s;

class k8s_net
{
public:
	typedef sinsp_ssl::ptr_t              ssl_ptr_t;
	typedef sinsp_bearer_token::ptr_t     bt_ptr_t;
	typedef k8s_component::ext_list_ptr_t ext_list_ptr_t;
	typedef user_event_filter_t::ptr_t    filter_ptr_t;
	typedef k8s_handler::ptr_t            handler_ptr_t;
	typedef k8s_handler::collector_t      collector_t;
	typedef k8s_handler::collector_ptr_t  collector_ptr_t;

	k8s_net(k8s& kube, k8s_state_t& state, const std::string& uri = "http://localhost:80",
		ssl_ptr_t ssl = nullptr,
		bt_ptr_t bt = nullptr,
		filter_ptr_t event_filter = nullptr,
		bool blocking_sockets = false,
		const std::string& node_selector = "");

	~k8s_net();

	static handler_ptr_t make_handler(k8s_state_t& state, const k8s_component::type component, bool connect = true,
									 handler_ptr_t dep = std::make_shared<k8s_dummy_handler>(),
									 collector_ptr_t collector = nullptr, const std::string& urlstr = "",
									 ssl_ptr_t ssl = nullptr, bt_ptr_t bt = nullptr, bool blocking = false,
									 filter_ptr_t event_filter = nullptr, const std::string& node_selector = "");
	void add_handler(const k8s_component::type_map::value_type& component);
	bool has_handler(const k8s_component::type_map::value_type& component);
	bool has_dependency(const k8s_component::type_map::value_type& component);

	bool is_state_built(const k8s_component::type_map::value_type& component);

	void watch();
	void stop_watching();
	bool is_healthy() const;

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

	typedef k8s_handler::handler_t                       handler_t;
	typedef std::map<k8s_component::type, handler_ptr_t> handler_map_t;

	const handler_map_t& handlers() const;
	static handler_ptr_t get_handler(const handler_map_t& handlers, k8s_component::type component);
	static handler_ptr_t get_handler(const handler_map_t&  handlers, const k8s_component::type_map::value_type& component);
	static handler_ptr_t get_dependency_handler(const handler_map_t&  handlers, const k8s_component::type_map::value_type& component);
	static handler_ptr_t get_dependency_handler(const handler_map_t&  handlers, const k8s_component::type& component);

private:
	void init();
	bool is_secure();
	void cleanup();

	k8s_state_t&    m_state;
	collector_ptr_t m_collector;
	uri             m_uri;
	ssl_ptr_t       m_ssl;
	bt_ptr_t        m_bt;
	bool            m_stopped;
	handler_map_t   m_handlers;
	bool            m_blocking_sockets = false;
	filter_ptr_t    m_event_filter;
	std::string     m_machine_id;
	std::string     m_node_selector;
};

inline bool k8s_net::is_secure()
{
	return m_uri.get_scheme() == "https";
}

inline bool k8s_net::is_healthy() const
{
	if(m_collector)
	{
		if (m_collector->get_steady_state())
		{
			return m_collector->subscription_count() == static_cast<int>(m_handlers.size());
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

inline bool k8s_net::has_handler(const k8s_component::type_map::value_type& component)
{
	auto it = m_handlers.find(component.first);
	return (it != m_handlers.end()) && it->second;
}

inline k8s_net::handler_ptr_t k8s_net::get_handler(const handler_map_t&  handlers, k8s_component::type component)
{
	auto it = handlers.find(component);
	if(it != handlers.end())
	{
		return it->second;
	}
	return nullptr;
}

inline k8s_net::handler_ptr_t k8s_net::get_handler(const handler_map_t&  handlers, const k8s_component::type_map::value_type& component)
{
	return get_handler(handlers, component.first);
}

inline bool k8s_net::is_state_built(const k8s_component::type_map::value_type& component)
{
	const auto& it = m_handlers.find(component.first);
	if(it != m_handlers.end())
	{
		return it->second && it->second->is_state_built();
	}
	return false;
}

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

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

inline const k8s_net::handler_map_t& k8s_net::handlers() const
{
	return m_handlers;
}

#else // !HAS_CAPTURE

#include "k8s_component.h"
#include "k8s_handler.h"

namespace k8s_net
{
	k8s_handler::ptr_t make_handler(k8s_state_t& state, const k8s_component::type component, bool /*connect*/);
}

#endif // HAS_CAPTURE

#endif // MINIMAL_BUILD