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

timer correction #61

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
78 changes: 78 additions & 0 deletions pkg/config/v1/config_data_mock_times.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package config

import (
"path"
"sniffer/pkg/utils"
"time"
)

type ConfigDataTimesMock struct {
}

func CreateTimesMockConfigData() *ConfigDataTimesMock {
return &ConfigDataTimesMock{}
}

func (c *ConfigDataTimesMock) IsFalcoEbpfEngine() bool {
return true
}

func (c *ConfigDataTimesMock) GetFalcoSyscallFilter() []string {
return []string{"open", "openat", "execve", "execveat"}
}

func (c *ConfigDataTimesMock) GetFalcoKernelObjPath() string {
return path.Join(utils.CurrentDir(), "..", "testdata", "mock_falco_ebpf_engine", "kernel_obj_mock.o")
}

func (c *ConfigDataTimesMock) GetEbpfEngineLoaderPath() string {
return path.Join(utils.CurrentDir(), "..", "testdata", "mock_falco_ebpf_engine", "userspace_app_mock")
}

func (c *ConfigDataTimesMock) GetUpdateDataPeriod() time.Duration {
return time.Duration(5) * time.Second
}

func (c *ConfigDataTimesMock) GetSniffingMaxTimes() time.Duration {
return time.Duration(10) * time.Second
}

func (c *ConfigDataTimesMock) IsRelevantCVEServiceEnabled() bool {
return true
}

func (c *ConfigDataTimesMock) GetNodeName() string {
return "minikube"
}

func (c *ConfigDataTimesMock) GetClusterName() string {
return "test"
}

func (c *ConfigDataTimesMock) SetNodeName() {
}

func (c *ConfigDataTimesMock) SetNamespace() {
}

func (c *ConfigDataTimesMock) SetContainerName() {
}

func (c *ConfigDataTimesMock) GetNamespace() string {
return "Namespace"
}

func (c *ConfigDataTimesMock) GetContainerName() string {
return "ContName"
}

func (c *ConfigDataTimesMock) SetBackgroundContextURL() {
}

func (c *ConfigDataTimesMock) GetBackgroundContextURL() string {
return "URLcontext"
}

func (c *ConfigDataTimesMock) GetAccountID() string {
return "AccountID"
}
2 changes: 1 addition & 1 deletion pkg/conthandler/container_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (ch *ContainerHandler) startRelevancyProcess(contEvent v1.ContainerEventDat
now := time.Now()
configStopTime := config.GetConfigurationConfigContext().GetSniffingMaxTimes()
stopSniffingTime := now.Add(configStopTime)
for start := time.Now(); start.Before(stopSniffingTime); {
for ;time.Now().Before(stopSniffingTime); {
go ch.getSBOM(contEvent)
ctx, span := otel.Tracer("").Start(context.GetBackgroundContext(), "container monitoring", trace.WithAttributes(attribute.String("containerID", contEvent.GetContainerID()), attribute.String("container workload", contEvent.GetK8SWorkloadID())))
err = ch.startTimer(watchedContainer, contEvent.GetContainerID())
Expand Down
67 changes: 63 additions & 4 deletions pkg/conthandler/container_main_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"path"
"sniffer/pkg/config"
configV1 "sniffer/pkg/config/v1"
conthadlerV1 "sniffer/pkg/conthandler/v1"
conthandlerV1 "sniffer/pkg/conthandler/v1"
accumulator "sniffer/pkg/event_data_storage"
"sniffer/pkg/sbom"
"sniffer/pkg/storageclient"
"sniffer/pkg/utils"
"testing"
Expand Down Expand Up @@ -53,12 +54,12 @@ func TestContMainHandler(t *testing.T) {
RedisInstanceID.SetKind("deployment")
RedisInstanceID.SetName("redis")
RedisInstanceID.SetContainerName("redis")
contHandler.containersEventChan <- *conthadlerV1.CreateNewContainerEvent(RedisImageID, RedisContainerIDContHandler, RedisPodName, RedisWLID, &RedisInstanceID, conthadlerV1.ContainerRunning)
contHandler.containersEventChan <- *conthandlerV1.CreateNewContainerEvent(RedisImageID, RedisContainerIDContHandler, RedisPodName, RedisWLID, &RedisInstanceID, conthandlerV1.ContainerRunning)
}()

event := <-contHandler.containersEventChan
if event.GetContainerEventType() != conthadlerV1.ContainerRunning {
t.Fatalf("event container type is wrong, get: %s expected: %s", event.GetContainerEventType(), conthadlerV1.ContainerRunning)
if event.GetContainerEventType() != conthandlerV1.ContainerRunning {
t.Fatalf("event container type is wrong, get: %s expected: %s", event.GetContainerEventType(), conthandlerV1.ContainerRunning)
}
if event.GetContainerID() != RedisContainerIDContHandler {
t.Fatalf("container ID is wrong, get: %s expected: %s", event.GetContainerID(), RedisContainerIDContHandler)
Expand All @@ -69,3 +70,61 @@ func TestContMainHandler(t *testing.T) {
t.Fatalf("handleNewContainerEvent failed with error %v", err)
}
}



func TestContMainHandlerStopMonitorAfterXMinutes(t *testing.T) {
configPath := path.Join(utils.CurrentDir(), "..", "..", "configuration", "ConfigurationFile.json")
t.Setenv(config.ConfigEnvVar, configPath)

cfg := config.GetConfigurationConfigContext()
configData, err := cfg.GetConfigurationReader()
if err != nil {
t.Fatalf("GetConfigurationReader failed with err %v", err)
}
err = cfg.ParseConfiguration(configV1.CreateTimesMockConfigData(), configData)
if err != nil {
t.Fatalf("ParseConfiguration failed with err %v", err)
}

cacheAccumulatorErrorChan := make(chan error)
acc := accumulator.GetAccumulator()
err = acc.StartAccumulator(cacheAccumulatorErrorChan)
if err != nil {
t.Fatalf("StartAccumulator failed with err %v", err)
}

contHandler, err := CreateContainerHandler(nil, storageclient.CreateSBOMStorageHttpClientMock())
if err != nil {
t.Fatalf("CreateContainerHandler failed with err %v", err)
}
RedisInstanceID := instanceidhandler.InstanceID{}
RedisInstanceID.SetAPIVersion("apps/v1")
RedisInstanceID.SetNamespace("any")
RedisInstanceID.SetKind("deployment")
RedisInstanceID.SetName("redis")
RedisInstanceID.SetContainerName("redis")
contEvent := conthandlerV1.CreateNewContainerEvent(RedisImageID, RedisContainerIDContHandler, RedisWLID, RedisPodName, &RedisInstanceID, conthandlerV1.ContainerRunning)

newWatchedContainer := watchedContainerData{
containerAggregator: CreateAggregator(getShortContainerID(contEvent.GetContainerID())),
snifferTicker: createTicker(),
event: *contEvent,
sbomClient: sbom.CreateSBOMStorageClient(contHandler.storageClient, contEvent.GetK8SWorkloadID(), contEvent.GetImageID(), contEvent.GetInstanceID()),
syncChannel: map[string]chan error{
StepGetSBOM: make(chan error, 10),
StepEventAggregator: make(chan error, 10),
},
}
contHandler.watchedContainers.Store(contEvent.GetContainerID(), newWatchedContainer)
now := time.Now()
contHandler.startRelevancyProcess(*contEvent)
stopTime := time.Now()
elapsedTime := stopTime.Sub(now)
if elapsedTime.Minutes() < config.GetConfigurationConfigContext().GetSniffingMaxTimes().Minutes() {
t.Fatalf("elapsedTime is too little, should be %f < %f", elapsedTime.Minutes(), config.GetConfigurationConfigContext().GetSniffingMaxTimes().Minutes())
}
if elapsedTime.Minutes() > (config.GetConfigurationConfigContext().GetSniffingMaxTimes().Minutes() + float64(time.Minute)) {
t.Fatalf("elapsedTime is too High, should be %f > %f", elapsedTime.Minutes(), config.GetConfigurationConfigContext().GetSniffingMaxTimes().Minutes())
}
}
Loading