Skip to content

Commit

Permalink
ROX-26344: adds runtime configuration to collector (#1866)
Browse files Browse the repository at this point in the history
Includes support for enabling/disabling external IPs
based on runtime config, deferring to existing feature
flag if none is provided
  • Loading branch information
Stringy authored Oct 1, 2024
1 parent 8b2d4e6 commit d7ceed3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
35 changes: 34 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 @@ -83,7 +86,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 {
if (runtime_config_.has_value()) {
const auto& cfg = runtime_config_.value();
const auto& network_cfg = cfg.network_connection_config();
return network_cfg.enable_external_ips();
}
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 @@ -102,6 +117,18 @@ class CollectorConfig {

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

void SetRuntimeConfig(sensor::CollectorConfig&& runtime_config) {
runtime_config_ = runtime_config;
}

void SetRuntimeConfig(sensor::CollectorConfig runtime_config) {
runtime_config_ = std::move(runtime_config);
}

const std::optional<sensor::CollectorConfig>& GetRuntimeConfig() const {
return runtime_config_;
}

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

protected:
Expand Down Expand Up @@ -156,6 +183,8 @@ class CollectorConfig {

std::optional<TlsConfig> tls_config_;

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

void HandleAfterglowEnvVars();
void HandleConnectionStatsEnvVars();
void HandleSinspEnvVars();
Expand All @@ -165,6 +194,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 2011 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

0 comments on commit d7ceed3

Please sign in to comment.