Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROX-26344: adds runtime configuration to collector #1866

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion collector/lib/CollectorConfig.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#ifndef _COLLECTOR_CONFIG_H_
#define _COLLECTOR_CONFIG_H_

#include <optional>
#include <ostream>
#include <vector>

#include <json/json.h>

#include <grpcpp/channel.h>

#include <internalapi/sensor/collector.pb.h>

#include "CollectionMethod.h"
#include "HostConfig.h"
#include "NetworkConnection.h"
Expand Down Expand Up @@ -75,7 +78,19 @@ class CollectorConfig {
bool IsProcessesListeningOnPortsEnabled() const { return enable_processes_listening_on_ports_; }
bool ImportUsers() const { return import_users_; }
bool CollectConnectionStatus() const { return collect_connection_status_; }
bool EnableExternalIPs() const { return enable_external_ips_; }

// EnableExternalIPs will check for the existence
// of a runtime configuration, and defer to that value
// otherwise, we rely on the feature flag (env var)
bool EnableExternalIPs() const {
JoukoVirtanen marked this conversation as resolved.
Show resolved Hide resolved
if (runtime_config_.has_value()) {
auto cfg = runtime_config_.value();
auto network_cfg = cfg.network_connection_config();
return network_cfg.enable_external_ips();
Molter73 marked this conversation as resolved.
Show resolved Hide resolved
}
return enable_external_ips_;
}

bool EnableConnectionStats() const { return enable_connection_stats_; }
bool EnableDetailedMetrics() const { return enable_detailed_metrics_; }
bool EnableRuntimeConfig() const { return enable_runtime_config_; }
Expand All @@ -93,6 +108,14 @@ class CollectorConfig {

static std::pair<option::ArgStatus, std::string> CheckConfiguration(const char* config, Json::Value* root);

void SetRuntimeConfig(sensor::CollectorConfig runtime_config) {
runtime_config_ = std::move(runtime_config);
}
Molter73 marked this conversation as resolved.
Show resolved Hide resolved

std::optional<sensor::CollectorConfig> GetRuntimeConfig() const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not copy the object every time it's accessed.

Suggested change
std::optional<sensor::CollectorConfig> GetRuntimeConfig() const {
const std::optional<sensor::CollectorConfig>& GetRuntimeConfig() const {

return runtime_config_;
}

std::shared_ptr<grpc::Channel> grpc_channel;

protected:
Expand Down Expand Up @@ -146,6 +169,8 @@ class CollectorConfig {

std::optional<TlsConfig> tls_config_;

std::optional<sensor::CollectorConfig> runtime_config_;

void HandleAfterglowEnvVars();
void HandleConnectionStatsEnvVars();
void HandleSinspEnvVars();
Expand All @@ -155,6 +180,10 @@ class CollectorConfig {
void SetSinspTotalBufferSize(unsigned int total_buffer_size);
void SetSinspCpuPerBuffer(unsigned int buffer_size);
void SetHostConfig(HostConfig* config);

void SetEnableExternalIPs(bool value) {
enable_external_ips_ = value;
}
};

std::ostream& operator<<(std::ostream& os, const CollectorConfig& c);
Expand Down
2 changes: 1 addition & 1 deletion collector/proto/third_party/stackrox
Submodule stackrox updated 1963 files
58 changes: 58 additions & 0 deletions collector/test/CollectorConfigTest.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <optional>

#include "CollectorArgs.h"
#include "CollectorConfig.h"
#include "gmock/gmock.h"
Expand Down Expand Up @@ -26,6 +28,10 @@ class MockCollectorConfig : public CollectorConfig {
void MockSetSinspCpuPerBuffer(unsigned int value) {
SetSinspCpuPerBuffer(value);
}

void MockSetEnableExternalIPs(bool value) {
SetEnableExternalIPs(value);
}
};

// Test that unmodified value is returned, when some dependency values are
Expand Down Expand Up @@ -87,4 +93,56 @@ TEST(CollectorConfigTest, TestSinspCpuPerBufferAdjusted) {
EXPECT_EQ(16384, config.GetSinspBufferSize());
}

TEST(CollectorConfigTest, TestSetRuntimeConfig) {
MockCollectorConfig config;

EXPECT_EQ(std::nullopt, config.GetRuntimeConfig());

sensor::CollectorConfig runtime_config;

config.SetRuntimeConfig(runtime_config);

EXPECT_NE(std::nullopt, config.GetRuntimeConfig());
}

TEST(CollectorConfigTest, TestEnableExternalIpsFeatureFlag) {
MockCollectorConfig config;

// without the presence of the runtime configuration
// the enable_external_ips_ flag should be used

config.MockSetEnableExternalIPs(false);

EXPECT_FALSE(config.EnableExternalIPs());

config.MockSetEnableExternalIPs(true);

EXPECT_TRUE(config.EnableExternalIPs());
}

TEST(CollectorConfigTest, TestEnableExternalIpsRuntimeConfig) {
MockCollectorConfig config;

// With the presence of runtime config, the feature
// flag should be ignored

config.MockSetEnableExternalIPs(true);

sensor::CollectorConfig runtime_config;
sensor::NetworkConnectionConfig* network_config = runtime_config.mutable_network_connection_config();

network_config->set_enable_external_ips(false);

config.SetRuntimeConfig(runtime_config);

EXPECT_FALSE(config.EnableExternalIPs());

config.MockSetEnableExternalIPs(false);

network_config->set_enable_external_ips(true);
config.SetRuntimeConfig(runtime_config);

EXPECT_TRUE(config.EnableExternalIPs());
}

} // namespace collector
Loading