Skip to content

Commit 7d4c4b1

Browse files
committed
Update Falco libs to 0.21.0
The latest version of Falco has a number of changes that are incompatible with collector, biggest ones are: - Removal of the container manager code in favor of a plugin. - Major refactoring of sinsp. In order to make collector compatible again, we had to drop the ContainerEngine that we implemented in favor of a method in the event extractor that will get the container id from the cgroups when it is called. The ContainerMetadata is also essentially dead in the water, since we can't get container metadata without the container plugin. Filtering of events that used to happen in the inspector itself has been moved into collector, since we can't filter events by container id without the container engine.
1 parent f555c23 commit 7d4c4b1

13 files changed

+216
-192
lines changed

collector/lib/ContainerEngine.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

collector/lib/ContainerMetadata.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,15 @@ ContainerMetadata::ContainerMetadata(sinsp* inspector) : event_extractor_(std::m
1111
}
1212

1313
std::string ContainerMetadata::GetNamespace(sinsp_evt* event) {
14-
const char* ns = event_extractor_->get_k8s_namespace(event);
15-
return ns != nullptr ? ns : "";
14+
return "";
1615
}
1716

1817
std::string ContainerMetadata::GetNamespace(const std::string& container_id) {
1918
return GetContainerLabel(container_id, "io.kubernetes.pod.namespace");
2019
}
2120

2221
std::string ContainerMetadata::GetContainerLabel(const std::string& container_id, const std::string& label) {
23-
auto containers = inspector_->m_container_manager.get_containers();
24-
const auto& container = containers->find(container_id);
25-
if (container == containers->end()) {
26-
return "";
27-
}
28-
29-
const auto& labels = container->second->m_labels;
30-
const auto& label_it = labels.find(label);
31-
if (label_it == labels.end()) {
32-
return "";
33-
}
34-
35-
return label_it->second;
22+
return "";
3623
}
3724

38-
} // namespace collector
25+
} // namespace collector

collector/lib/NetworkConnection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ std::ostream& operator<<(std::ostream& os, const ContainerEndpoint& container_en
380380
class Connection {
381381
public:
382382
Connection() : flags_(0) {}
383-
Connection(std::string container, const Endpoint& local, const Endpoint& remote, L4Proto l4proto, bool is_server)
384-
: container_(std::move(container)), local_(local), remote_(remote), flags_((static_cast<uint8_t>(l4proto) << 1) | ((is_server) ? 1 : 0)) {}
383+
Connection(std::string_view container, const Endpoint& local, const Endpoint& remote, L4Proto l4proto, bool is_server)
384+
: container_(container), local_(local), remote_(remote), flags_((static_cast<uint8_t>(l4proto) << 1) | ((is_server) ? 1 : 0)) {}
385385

386386
const std::string& container() const { return container_; }
387387
const Endpoint& local() const { return local_; }

collector/lib/NetworkSignalHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ std::optional<Connection> NetworkSignalHandler::GetConnection(sinsp_evt* evt) {
133133
const Endpoint* local = is_server ? &server : &client;
134134
const Endpoint* remote = is_server ? &client : &server;
135135

136-
const std::string* container_id = event_extractor_->get_container_id(evt);
136+
auto container_id = event_extractor_->get_container_id(evt);
137137
if (!container_id) {
138138
return std::nullopt;
139139
}

collector/lib/Process.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <libsinsp/sinsp.h>
66

77
#include "CollectorStats.h"
8+
#include "system-inspector/EventExtractor.h"
89
#include "system-inspector/Service.h"
910

1011
namespace collector {
@@ -32,7 +33,10 @@ std::string Process::container_id() const {
3233
WaitForProcessInfo();
3334

3435
if (system_inspector_threadinfo_) {
35-
return system_inspector_threadinfo_->m_container_id;
36+
auto container_id = system_inspector::EventExtractor::get_container_id(system_inspector_threadinfo_.get());
37+
if (container_id) {
38+
return std::string{*container_id};
39+
}
3640
}
3741

3842
return NOT_AVAILABLE;

collector/lib/ProcessSignalFormatter.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ using LineageInfo = ProcessSignalFormatter::LineageInfo;
2222
using Timestamp = google::protobuf::Timestamp;
2323
using TimeUtil = google::protobuf::util::TimeUtil;
2424

25+
using EventExtractor = system_inspector::EventExtractor;
26+
2527
namespace {
2628

2729
enum ProcessSignalType {
@@ -59,7 +61,7 @@ std::string extract_proc_args(sinsp_threadinfo* tinfo) {
5961
ProcessSignalFormatter::ProcessSignalFormatter(
6062
sinsp* inspector,
6163
const CollectorConfig& config) : event_names_(EventNames::GetInstance()),
62-
event_extractor_(std::make_unique<system_inspector::EventExtractor>()),
64+
event_extractor_(std::make_unique<EventExtractor>()),
6365
container_metadata_(inspector),
6466
config_(config) {
6567
event_extractor_->Init(inspector);
@@ -163,10 +165,10 @@ ProcessSignal* ProcessSignalFormatter::CreateProcessSignal(sinsp_evt* event) {
163165
}
164166

165167
// set user and group id credentials
166-
if (const uint32_t* uid = event_extractor_->get_uid(event)) {
168+
if (auto uid = EventExtractor::get_uid(event)) {
167169
signal->set_uid(*uid);
168170
}
169-
if (const uint32_t* gid = event_extractor_->get_gid(event)) {
171+
if (auto gid = EventExtractor::get_gid(event)) {
170172
signal->set_gid(*gid);
171173
}
172174

@@ -176,7 +178,7 @@ ProcessSignal* ProcessSignalFormatter::CreateProcessSignal(sinsp_evt* event) {
176178
signal->set_allocated_time(timestamp);
177179

178180
// set container_id
179-
if (const std::string* container_id = event_extractor_->get_container_id(event)) {
181+
if (auto container_id = EventExtractor::get_container_id(event)) {
180182
signal->set_container_id(*container_id);
181183
}
182184

@@ -232,16 +234,25 @@ ProcessSignal* ProcessSignalFormatter::CreateProcessSignal(sinsp_threadinfo* tin
232234
signal->set_pid(tinfo->m_pid);
233235

234236
// set user and group id credentials
235-
signal->set_uid(tinfo->m_user.uid());
236-
signal->set_gid(tinfo->m_group.gid());
237+
auto uid = EventExtractor::get_uid(tinfo);
238+
if (uid) {
239+
signal->set_uid(*uid);
240+
}
241+
auto gid = EventExtractor::get_gid(tinfo);
242+
if (gid) {
243+
signal->set_gid(*gid);
244+
}
237245

238246
// set time
239247
auto timestamp = Allocate<Timestamp>();
240248
*timestamp = TimeUtil::NanosecondsToTimestamp(tinfo->m_clone_ts);
241249
signal->set_allocated_time(timestamp);
242250

243251
// set container_id
244-
signal->set_container_id(tinfo->m_container_id);
252+
auto container_id = EventExtractor::get_container_id(tinfo);
253+
if (container_id) {
254+
signal->set_container_id(*container_id);
255+
}
245256

246257
// set process lineage
247258
std::vector<LineageInfo> lineage;
@@ -265,7 +276,7 @@ std::string ProcessSignalFormatter::ProcessDetails(sinsp_evt* event) {
265276
std::stringstream ss;
266277
const std::string* path = event_extractor_->get_exepath(event);
267278
const std::string* name = event_extractor_->get_comm(event);
268-
const std::string* container_id = event_extractor_->get_container_id(event);
279+
auto container_id = EventExtractor::get_container_id(event);
269280
const char* args = event_extractor_->get_proc_args(event);
270281
const int64_t* pid = event_extractor_->get_pid(event);
271282

@@ -347,7 +358,7 @@ void ProcessSignalFormatter::GetProcessLineage(sinsp_threadinfo* tinfo,
347358
// all platforms.
348359
//
349360
if (pt->m_vpid == 0) {
350-
if (pt->m_container_id.empty()) {
361+
if (!EventExtractor::get_container_id(pt)) {
351362
return false;
352363
}
353364
} else if (pt->m_pid == pt->m_vpid) {
@@ -361,7 +372,10 @@ void ProcessSignalFormatter::GetProcessLineage(sinsp_threadinfo* tinfo,
361372
// Collapse parent child processes that have the same path
362373
if (lineage.empty() || (lineage.back().parent_exec_file_path() != pt->m_exepath)) {
363374
LineageInfo info;
364-
info.set_parent_uid(pt->m_user.uid());
375+
auto uid = EventExtractor::get_uid(pt);
376+
if (uid) {
377+
info.set_parent_uid(*uid);
378+
}
365379
info.set_parent_exec_file_path(pt->m_exepath);
366380
lineage.push_back(info);
367381
}

collector/lib/Utility.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ const char* SignalName(int signum) {
5757
}
5858
}
5959

60-
std::ostream& operator<<(std::ostream& os, const sinsp_threadinfo* t) {
61-
if (t) {
62-
os << "Container: \"" << t->m_container_id << "\", Name: " << t->m_comm << ", PID: " << t->m_pid << ", Args: " << t->m_exe;
63-
} else {
64-
os << "NULL\n";
65-
}
66-
return os;
67-
}
68-
6960
const char* UUIDStr() {
7061
uuid_t uuid;
7162
constexpr int kUuidStringLength = 36; // uuid_unparse manpage says so.

collector/lib/Utility.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ std::string Str(Args&&... args) {
6363
return string_stream.str();
6464
}
6565

66-
std::ostream& operator<<(std::ostream& os, const sinsp_threadinfo* t);
67-
6866
// UUIDStr returns UUID in string format.
6967
const char* UUIDStr();
7068

collector/lib/system-inspector/EventExtractor.h

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "libsinsp/sinsp.h"
88

99
#include "Logging.h"
10+
#include "Utility.h"
11+
#include "threadinfo.h"
1012

1113
namespace collector::system_inspector {
1214

@@ -129,16 +131,11 @@ class EventExtractor {
129131
//
130132
// ADD ANY NEW FIELDS BELOW THIS LINE
131133

132-
// Container related fields
133-
TINFO_FIELD(container_id);
134-
135134
// Process related fields
136135
TINFO_FIELD(comm);
137136
TINFO_FIELD(exe);
138137
TINFO_FIELD(exepath);
139138
TINFO_FIELD(pid);
140-
TINFO_FIELD_RAW_GETTER(uid, m_user.uid, uint32_t);
141-
TINFO_FIELD_RAW_GETTER(gid, m_group.gid, uint32_t);
142139
FIELD_CSTR(proc_args, "proc.args");
143140

144141
// General event information
@@ -148,15 +145,57 @@ class EventExtractor {
148145
FIELD_RAW_SAFE(client_port, "fd.cport", uint16_t);
149146
FIELD_RAW_SAFE(server_port, "fd.sport", uint16_t);
150147

151-
// k8s metadata
152-
FIELD_CSTR(k8s_namespace, "k8s.ns.name");
153-
154148
#undef TINFO_FIELD
155149
#undef FIELD_RAW
156150
#undef FIELD_CSTR
157151
#undef EVT_ARG
158152
#undef EVT_ARG_RAW
159153
#undef DECLARE_FILTER_CHECK
154+
155+
public:
156+
static std::optional<std::string_view> get_container_id(const sinsp_threadinfo* tinfo) {
157+
for (const auto& [_, cgroup] : tinfo->cgroups()) {
158+
auto container_id = ExtractContainerIDFromCgroup(cgroup);
159+
if (container_id) {
160+
return container_id;
161+
}
162+
}
163+
164+
return {};
165+
}
166+
167+
static std::optional<std::string_view> get_container_id(const sinsp_evt* evt) {
168+
const auto* tinfo = evt->get_tinfo();
169+
if (tinfo == nullptr) {
170+
return {};
171+
}
172+
173+
return get_container_id(tinfo);
174+
}
175+
176+
static std::optional<uint32_t> get_uid(sinsp_threadinfo* tinfo) {
177+
return tinfo->m_uid;
178+
}
179+
180+
static std::optional<uint32_t> get_uid(sinsp_evt* evt) {
181+
auto* tinfo = evt->get_tinfo();
182+
if (tinfo == nullptr) {
183+
return {};
184+
}
185+
return get_uid(tinfo);
186+
}
187+
188+
static std::optional<uint32_t> get_gid(sinsp_threadinfo* tinfo) {
189+
return tinfo->m_gid;
190+
}
191+
192+
static std::optional<uint32_t> get_gid(sinsp_evt* evt) {
193+
auto* tinfo = evt->get_tinfo();
194+
if (tinfo == nullptr) {
195+
return {};
196+
}
197+
return get_gid(tinfo);
198+
}
160199
};
161200

162201
} // namespace collector::system_inspector

collector/lib/system-inspector/Service.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <linux/ioctl.h>
88

9-
#include "libsinsp/container_engine/sinsp_container_type.h"
109
#include "libsinsp/parsers.h"
1110
#include "libsinsp/sinsp.h"
1211

@@ -15,7 +14,6 @@
1514
#include "CollectionMethod.h"
1615
#include "CollectorException.h"
1716
#include "CollectorStats.h"
18-
#include "ContainerEngine.h"
1917
#include "ContainerMetadata.h"
2018
#include "EventExtractor.h"
2119
#include "EventNames.h"
@@ -50,7 +48,7 @@ Service::Service(const CollectorConfig& config)
5048
inspector_->disable_log_timestamps();
5149
inspector_->set_log_callback(logging::InspectorLogCallback);
5250

53-
inspector_->set_import_users(config.ImportUsers(), false);
51+
inspector_->set_import_users(config.ImportUsers());
5452
inspector_->set_thread_timeout_s(30);
5553
inspector_->set_auto_threads_purging_interval_s(60);
5654
inspector_->m_thread_manager->set_max_thread_table_size(config.GetSinspThreadCacheSize());
@@ -62,6 +60,7 @@ Service::Service(const CollectorConfig& config)
6260
inspector_->get_parser()->set_track_connection_status(true);
6361
}
6462

63+
/*
6564
if (config.EnableRuntimeConfig()) {
6665
uint64_t mask = 1 << CT_CRI |
6766
1 << CT_CRIO |
@@ -87,6 +86,7 @@ Service::Service(const CollectorConfig& config)
8786
}
8887
8988
inspector_->set_filter("container.id != 'host'");
89+
*/
9090

9191
// The self-check handlers should only operate during start up,
9292
// so they are added to the handler list first, so they have access
@@ -160,6 +160,12 @@ sinsp_evt* Service::GetNext() {
160160
return nullptr;
161161
}
162162

163+
// If there is no container id, this is an event from the host.
164+
// We ignore these for now.
165+
if (!EventExtractor::get_container_id(event)) {
166+
return nullptr;
167+
}
168+
163169
userspace_stats_.event_parse_micros[event->get_type()] += (NowMicros() - parse_start);
164170
++userspace_stats_.nUserspaceEvents[event->get_type()];
165171

@@ -296,7 +302,8 @@ bool Service::SendExistingProcesses(SignalHandler* handler) {
296302
}
297303

298304
return threads->loop([&](sinsp_threadinfo& tinfo) {
299-
if (!tinfo.m_container_id.empty() && tinfo.is_main_thread()) {
305+
auto container_id = EventExtractor::get_container_id(&tinfo);
306+
if (container_id && tinfo.is_main_thread()) {
300307
auto result = handler->HandleExistingProcess(&tinfo);
301308
if (result == SignalHandler::ERROR || result == SignalHandler::NEEDS_REFRESH) {
302309
CLOG(WARNING) << "Failed to write existing process signal: " << &tinfo;

0 commit comments

Comments
 (0)