Skip to content

Commit

Permalink
Merge pull request #377 from kubescape/appprofile
Browse files Browse the repository at this point in the history
remove relevancy manager
  • Loading branch information
matthyx authored Dec 16, 2024
2 parents e4550b3 + 2389984 commit 48aaca8
Show file tree
Hide file tree
Showing 32 changed files with 148 additions and 165,064 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/grafana/pyroscope-go v1.2.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/inspektor-gadget/inspektor-gadget v0.35.0
github.com/kinbiko/jsonassert v1.2.0
github.com/kubescape/backend v0.0.25
github.com/kubescape/go-logger v0.0.23
github.com/kubescape/k8s-interface v0.0.177
Expand Down Expand Up @@ -201,6 +200,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/kastenhq/goversion v0.0.0-20230811215019-93b2f8823953 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/kinbiko/jsonassert v1.2.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/knqyf263/go-rpmdb v0.1.1 // indirect
Expand Down
25 changes: 2 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/kubescape/node-agent/pkg/containerwatcher/v1"
"github.com/kubescape/node-agent/pkg/dnsmanager"
"github.com/kubescape/node-agent/pkg/exporters"
"github.com/kubescape/node-agent/pkg/filehandler/v1"
"github.com/kubescape/node-agent/pkg/healthmanager"
"github.com/kubescape/node-agent/pkg/malwaremanager"
malwaremanagerv1 "github.com/kubescape/node-agent/pkg/malwaremanager/v1"
Expand All @@ -43,13 +42,10 @@ import (
objectcachev1 "github.com/kubescape/node-agent/pkg/objectcache/v1"
"github.com/kubescape/node-agent/pkg/processmanager"
processmanagerv1 "github.com/kubescape/node-agent/pkg/processmanager/v1"
"github.com/kubescape/node-agent/pkg/relevancymanager"
relevancymanagerv1 "github.com/kubescape/node-agent/pkg/relevancymanager/v1"
rulebinding "github.com/kubescape/node-agent/pkg/rulebindingmanager"
rulebindingcachev1 "github.com/kubescape/node-agent/pkg/rulebindingmanager/cache"
"github.com/kubescape/node-agent/pkg/rulemanager"
rulemanagerv1 "github.com/kubescape/node-agent/pkg/rulemanager/v1"
"github.com/kubescape/node-agent/pkg/sbomhandler/syfthandler"
"github.com/kubescape/node-agent/pkg/sbommanager"
sbommanagerv1 "github.com/kubescape/node-agent/pkg/sbommanager/v1"
"github.com/kubescape/node-agent/pkg/seccompmanager"
Expand Down Expand Up @@ -194,23 +190,6 @@ func main() {
applicationProfileManager = applicationprofilemanager.CreateApplicationProfileManagerMock()
}

// Create the relevancy manager
var relevancyManager relevancymanager.RelevancyManagerClient
if cfg.EnableRelevancy {
fileHandler, err := filehandler.CreateInMemoryFileHandler()
if err != nil {
logger.L().Ctx(ctx).Fatal("failed to create the filehandler for relevancy manager", helpers.Error(err))
}

sbomHandler := syfthandler.CreateSyftSBOMHandler(storageClient)
relevancyManager, err = relevancymanagerv1.CreateRelevancyManager(ctx, cfg, clusterData.ClusterName, fileHandler, k8sClient, sbomHandler, preRunningContainersIDs)
if err != nil {
logger.L().Ctx(ctx).Fatal("error creating the relevancy manager", helpers.Error(err))
}
} else {
relevancyManager = relevancymanager.CreateRelevancyManagerMock()
}

// Create the network and DNS managers
var networkManagerClient networkmanager.NetworkManagerClient
var dnsManagerClient dnsmanager.DNSManagerClient
Expand Down Expand Up @@ -282,7 +261,7 @@ func main() {
var profileManager nodeprofilemanager.NodeProfileManagerClient
if cfg.EnableNodeProfile {
// FIXME validate the HTTPExporterConfig before we use it ?
profileManager = nodeprofilemanagerv1.NewNodeProfileManager(cfg, *clusterData, cfg.NodeName, k8sObjectCache, relevancyManager, ruleManager)
profileManager = nodeprofilemanagerv1.NewNodeProfileManager(cfg, *clusterData, cfg.NodeName, k8sObjectCache, ruleManager)
} else {
profileManager = nodeprofilemanager.NewNodeProfileManagerMock()
}
Expand Down Expand Up @@ -320,7 +299,7 @@ func main() {
}

// Create the container handler
mainHandler, err := containerwatcher.CreateIGContainerWatcher(cfg, applicationProfileManager, k8sClient, igK8sClient, relevancyManager, networkManagerClient, dnsManagerClient, prometheusExporter, ruleManager, malwareManager, sbomManager, preRunningContainersIDs, &ruleBindingNotify, containerRuntime, nil, nil, processManager)
mainHandler, err := containerwatcher.CreateIGContainerWatcher(cfg, applicationProfileManager, k8sClient, igK8sClient, networkManagerClient, dnsManagerClient, prometheusExporter, ruleManager, malwareManager, sbomManager, preRunningContainersIDs, &ruleBindingNotify, containerRuntime, nil, nil, processManager)
if err != nil {
logger.L().Ctx(ctx).Fatal("error creating the container watcher", helpers.Error(err))
}
Expand Down
49 changes: 28 additions & 21 deletions pkg/applicationprofilemanager/v1/applicationprofile_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ func (am *ApplicationProfileManager) ensureInstanceID(container *containercollec
return fmt.Errorf("failed to get workload: %w", err)
}
pod := wl.(*workloadinterface.Workload)

// fill container type, index and names
if watchedContainer.ContainerType == utils.Unknown {
if err := watchedContainer.SetContainerInfo(pod, container.K8s.ContainerName); err != nil {
return fmt.Errorf("failed to set container info: %w", err)
}
}
// get pod template hash
watchedContainer.TemplateHash, _ = pod.GetLabel("pod-template-hash")

// find parentWlid
kind, name, err := am.k8sClient.CalculateWorkloadParentRecursive(pod)
if err != nil {
Expand All @@ -117,7 +121,7 @@ func (am *ApplicationProfileManager) ensureInstanceID(container *containercollec
return fmt.Errorf("failed to validate WLID: %w", err)
}
watchedContainer.ParentResourceVersion = w.GetResourceVersion()
// find instanceID
// find instanceID - this has to be the last one
instanceIDs, err := instanceidhandler.GenerateInstanceID(pod)
if err != nil {
return fmt.Errorf("failed to generate instanceID: %w", err)
Expand All @@ -128,10 +132,6 @@ func (am *ApplicationProfileManager) ensureInstanceID(container *containercollec
watchedContainer.InstanceID = instanceIDs[i]
}
}
// fill container type, index and names
if watchedContainer.ContainerType == utils.Unknown {
watchedContainer.SetContainerInfo(pod, container.K8s.ContainerName)
}
return nil
}

Expand Down Expand Up @@ -370,16 +370,17 @@ func (am *ApplicationProfileManager) saveProfile(ctx context.Context, watchedCon
ObjectMeta: metav1.ObjectMeta{
Name: slug,
Annotations: map[string]string{
helpersv1.InstanceIDMetadataKey: watchedContainer.InstanceID.GetStringNoContainer(),
helpersv1.WlidMetadataKey: watchedContainer.Wlid,
helpersv1.CompletionMetadataKey: string(watchedContainer.GetCompletionStatus()),
helpersv1.StatusMetadataKey: string(watchedContainer.GetStatus()),
},
Labels: utils.GetLabels(watchedContainer, true),
},
}
addContainers := func(containers []v1beta1.ApplicationProfileContainer, containerNames []string) []v1beta1.ApplicationProfileContainer {
for _, name := range containerNames {
seccompProfile, err := am.seccompManager.GetSeccompProfile(name, watchedContainer.SeccompProfilePath)
addContainers := func(containers []v1beta1.ApplicationProfileContainer, containerInfos []utils.ContainerInfo) []v1beta1.ApplicationProfileContainer {
for _, info := range containerInfos {
seccompProfile, err := am.seccompManager.GetSeccompProfile(info.Name, watchedContainer.SeccompProfilePath)
if err != nil {
logger.L().Ctx(ctx).Error("ApplicationProfileManager - failed to get seccomp profile", helpers.Error(err),
helpers.String("slug", slug),
Expand All @@ -388,22 +389,24 @@ func (am *ApplicationProfileManager) saveProfile(ctx context.Context, watchedCon
helpers.String("k8s workload", watchedContainer.K8sContainerID))
}
containers = append(containers, v1beta1.ApplicationProfileContainer{
Name: name,
Name: info.Name,
Endpoints: make([]v1beta1.HTTPEndpoint, 0),
Execs: make([]v1beta1.ExecCalls, 0),
Opens: make([]v1beta1.OpenCalls, 0),
Capabilities: make([]string, 0),
Syscalls: make([]string, 0),
PolicyByRuleId: make(map[string]v1beta1.RulePolicy),
SeccompProfile: seccompProfile,
ImageTag: info.ImageTag,
ImageID: info.ImageID,
})
}
return containers
}
newObject.Spec.Architectures = []string{runtime.GOARCH}
newObject.Spec.Containers = addContainers(newObject.Spec.Containers, watchedContainer.ContainerNames[utils.Container])
newObject.Spec.InitContainers = addContainers(newObject.Spec.InitContainers, watchedContainer.ContainerNames[utils.InitContainer])
newObject.Spec.EphemeralContainers = addContainers(newObject.Spec.EphemeralContainers, watchedContainer.ContainerNames[utils.EphemeralContainer])
newObject.Spec.Containers = addContainers(newObject.Spec.Containers, watchedContainer.ContainerInfos[utils.Container])
newObject.Spec.InitContainers = addContainers(newObject.Spec.InitContainers, watchedContainer.ContainerInfos[utils.InitContainer])
newObject.Spec.EphemeralContainers = addContainers(newObject.Spec.EphemeralContainers, watchedContainer.ContainerInfos[utils.EphemeralContainer])
// enrich container
newContainer := utils.GetApplicationProfileContainer(newObject, watchedContainer.ContainerType, watchedContainer.ContainerIndex)
utils.EnrichApplicationProfileContainer(newContainer, capabilities, observedSyscalls, execs, opens, endpoints, rulePolicies, watchedContainer.ImageID, watchedContainer.ImageTag)
Expand Down Expand Up @@ -433,12 +436,12 @@ func (am *ApplicationProfileManager) saveProfile(ctx context.Context, watchedCon
helpers.String("k8s workload", watchedContainer.K8sContainerID))
} else {
var replaceOperations []utils.PatchOperation
containerNames := watchedContainer.ContainerNames[watchedContainer.ContainerType]
containerNames := watchedContainer.ContainerInfos[watchedContainer.ContainerType]
// check existing container
existingContainer := utils.GetApplicationProfileContainer(existingObject, watchedContainer.ContainerType, watchedContainer.ContainerIndex)
if existingContainer == nil {
name := containerNames[watchedContainer.ContainerIndex]
seccompProfile, err := am.seccompManager.GetSeccompProfile(name, watchedContainer.SeccompProfilePath)
info := containerNames[watchedContainer.ContainerIndex]
seccompProfile, err := am.seccompManager.GetSeccompProfile(info.Name, watchedContainer.SeccompProfilePath)
if err != nil {
logger.L().Ctx(ctx).Error("ApplicationProfileManager - failed to get seccomp profile", helpers.Error(err),
helpers.String("slug", slug),
Expand All @@ -448,14 +451,16 @@ func (am *ApplicationProfileManager) saveProfile(ctx context.Context, watchedCon
}
logger.L().Debug("ApplicationProfileManager - got seccomp profile", helpers.Interface("profile", seccompProfile))
existingContainer = &v1beta1.ApplicationProfileContainer{
Name: containerNames[watchedContainer.ContainerIndex],
Name: info.Name,
Endpoints: make([]v1beta1.HTTPEndpoint, 0),
Execs: make([]v1beta1.ExecCalls, 0),
Opens: make([]v1beta1.OpenCalls, 0),
Capabilities: make([]string, 0),
Syscalls: make([]string, 0),
PolicyByRuleId: make(map[string]v1beta1.RulePolicy),
SeccompProfile: seccompProfile,
ImageTag: info.ImageTag,
ImageID: info.ImageID,
}
}
// update it
Expand All @@ -480,8 +485,8 @@ func (am *ApplicationProfileManager) saveProfile(ctx context.Context, watchedCon
}
// 3b. ensure the slice has all the containers
for i := len(existingContainers); i < len(containerNames); i++ {
name := containerNames[i]
seccompProfile, err := am.seccompManager.GetSeccompProfile(name, watchedContainer.SeccompProfilePath)
info := containerNames[i]
seccompProfile, err := am.seccompManager.GetSeccompProfile(info.Name, watchedContainer.SeccompProfilePath)
if err != nil {
logger.L().Ctx(ctx).Error("ApplicationProfileManager - failed to get seccomp profile", helpers.Error(err),
helpers.String("slug", slug),
Expand All @@ -493,14 +498,16 @@ func (am *ApplicationProfileManager) saveProfile(ctx context.Context, watchedCon
Op: "add",
Path: fmt.Sprintf("/spec/%s/%d", watchedContainer.ContainerType, i),
Value: v1beta1.ApplicationProfileContainer{
Name: name,
Name: info.Name,
Endpoints: make([]v1beta1.HTTPEndpoint, 0),
Execs: make([]v1beta1.ExecCalls, 0),
Opens: make([]v1beta1.OpenCalls, 0),
Capabilities: make([]string, 0),
Syscalls: make([]string, 0),
PolicyByRuleId: make(map[string]v1beta1.RulePolicy),
SeccompProfile: seccompProfile,
ImageTag: info.ImageTag,
ImageID: info.ImageID,
},
})
}
Expand Down
1 change: 0 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Config struct {
EnableRuntimeDetection bool `mapstructure:"runtimeDetectionEnabled"`
EnableHttpDetection bool `mapstructure:"httpDetectionEnabled"`
EnableNetworkTracing bool `mapstructure:"networkServiceEnabled"`
EnableRelevancy bool `mapstructure:"relevantCVEServiceEnabled"`
EnableNodeProfile bool `mapstructure:"nodeProfileServiceEnabled"`
NodeProfileInterval time.Duration `mapstructure:"nodeProfileInterval"`
EnableSeccomp bool `mapstructure:"seccompServiceEnabled"`
Expand Down
1 change: 0 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func TestLoadConfig(t *testing.T) {
EnableFullPathTracing: true,
EnableApplicationProfile: true,
EnableMalwareDetection: true,
EnableRelevancy: true,
EnableNetworkTracing: true,
EnableNodeProfile: true,
EnableHttpDetection: true,
Expand Down
9 changes: 2 additions & 7 deletions pkg/containerwatcher/v1/container_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/kubescape/node-agent/pkg/config"
"github.com/kubescape/node-agent/pkg/containerwatcher"
"github.com/kubescape/node-agent/pkg/dnsmanager"
events "github.com/kubescape/node-agent/pkg/ebpf/events"
"github.com/kubescape/node-agent/pkg/ebpf/events"
tracerhardlink "github.com/kubescape/node-agent/pkg/ebpf/gadgets/hardlink/tracer"
tracerhardlinktype "github.com/kubescape/node-agent/pkg/ebpf/gadgets/hardlink/types"
tracerhttp "github.com/kubescape/node-agent/pkg/ebpf/gadgets/http/tracer"
Expand All @@ -45,7 +45,6 @@ import (
"github.com/kubescape/node-agent/pkg/metricsmanager"
"github.com/kubescape/node-agent/pkg/networkmanager"
"github.com/kubescape/node-agent/pkg/processmanager"
"github.com/kubescape/node-agent/pkg/relevancymanager"
rulebinding "github.com/kubescape/node-agent/pkg/rulebindingmanager"
"github.com/kubescape/node-agent/pkg/rulemanager"
"github.com/kubescape/node-agent/pkg/sbommanager"
Expand Down Expand Up @@ -91,7 +90,6 @@ type IGContainerWatcher struct {
applicationProfileManager applicationprofilemanager.ApplicationProfileManagerClient
igK8sClient *containercollection.K8sClient
k8sClient *k8sinterface.KubernetesApi
relevancyManager relevancymanager.RelevancyManagerClient
networkManager networkmanager.NetworkManagerClient
dnsManager dnsmanager.DNSManagerClient
ruleManager rulemanager.RuleManagerClient
Expand Down Expand Up @@ -162,7 +160,7 @@ type IGContainerWatcher struct {

var _ containerwatcher.ContainerWatcher = (*IGContainerWatcher)(nil)

func CreateIGContainerWatcher(cfg config.Config, applicationProfileManager applicationprofilemanager.ApplicationProfileManagerClient, k8sClient *k8sinterface.KubernetesApi, igK8sClient *containercollection.K8sClient, relevancyManager relevancymanager.RelevancyManagerClient, networkManagerClient networkmanager.NetworkManagerClient, dnsManagerClient dnsmanager.DNSManagerClient, metrics metricsmanager.MetricsManager, ruleManager rulemanager.RuleManagerClient, malwareManager malwaremanager.MalwareManagerClient, sbomManager sbommanager.SbomManagerClient, preRunningContainers mapset.Set[string], ruleBindingPodNotify *chan rulebinding.RuleBindingNotify, runtime *containerutilsTypes.RuntimeConfig, thirdPartyEventReceivers *maps.SafeMap[utils.EventType, mapset.Set[containerwatcher.EventReceiver]], thirdPartyEnricher containerwatcher.ThirdPartyEnricher, processManager processmanager.ProcessManagerClient) (*IGContainerWatcher, error) { // Use container collection to get notified for new containers
func CreateIGContainerWatcher(cfg config.Config, applicationProfileManager applicationprofilemanager.ApplicationProfileManagerClient, k8sClient *k8sinterface.KubernetesApi, igK8sClient *containercollection.K8sClient, networkManagerClient networkmanager.NetworkManagerClient, dnsManagerClient dnsmanager.DNSManagerClient, metrics metricsmanager.MetricsManager, ruleManager rulemanager.RuleManagerClient, malwareManager malwaremanager.MalwareManagerClient, sbomManager sbommanager.SbomManagerClient, preRunningContainers mapset.Set[string], ruleBindingPodNotify *chan rulebinding.RuleBindingNotify, runtime *containerutilsTypes.RuntimeConfig, thirdPartyEventReceivers *maps.SafeMap[utils.EventType, mapset.Set[containerwatcher.EventReceiver]], thirdPartyEnricher containerwatcher.ThirdPartyEnricher, processManager processmanager.ProcessManagerClient) (*IGContainerWatcher, error) { // Use container collection to get notified for new containers
containerCollection := &containercollection.ContainerCollection{}
// Create a tracer collection instance
tracerCollection, err := tracercollection.NewTracerCollection(containerCollection)
Expand Down Expand Up @@ -221,7 +219,6 @@ func CreateIGContainerWatcher(cfg config.Config, applicationProfileManager appli
metrics.ReportEvent(utils.ExecveEventType)
processManager.ReportEvent(utils.ExecveEventType, &event)
applicationProfileManager.ReportFileExec(k8sContainerID, path, event.Args)
relevancyManager.ReportFileExec(event.Runtime.ContainerID, k8sContainerID, path)
rulePolicyReporter.ReportEvent(utils.ExecveEventType, &event, k8sContainerID, event.Comm)

// Report exec events to event receivers
Expand Down Expand Up @@ -251,7 +248,6 @@ func CreateIGContainerWatcher(cfg config.Config, applicationProfileManager appli

metrics.ReportEvent(utils.OpenEventType)
applicationProfileManager.ReportFileOpen(k8sContainerID, path, event.Flags)
relevancyManager.ReportFileOpen(event.Runtime.ContainerID, k8sContainerID, path)
ruleManager.ReportEvent(utils.OpenEventType, &event)
malwareManager.ReportEvent(utils.OpenEventType, &event)

Expand Down Expand Up @@ -426,7 +422,6 @@ func CreateIGContainerWatcher(cfg config.Config, applicationProfileManager appli
applicationProfileManager: applicationProfileManager,
igK8sClient: igK8sClient,
k8sClient: k8sClient,
relevancyManager: relevancyManager,
networkManager: networkManagerClient,
dnsManager: dnsManagerClient,
ruleManager: ruleManager,
Expand Down
Loading

0 comments on commit 48aaca8

Please sign in to comment.