Skip to content

Commit

Permalink
Fixed component test
Browse files Browse the repository at this point in the history
Signed-off-by: Afek Berger <[email protected]>
  • Loading branch information
afek854 committed Nov 17, 2024
1 parent b14a3b2 commit def694f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/component-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
Test_11_EndpointTest,
Test_12_MergingProfilesTest,
Test_13_MergingNetworkNeighborhoodTest,
Test_14_RulePoliciesTest,
]
steps:
- name: Checkout code
Expand Down Expand Up @@ -97,9 +98,13 @@ jobs:
- name: Run test
run: |
cd tests && go test -v ./... -run ${{ matrix.test }} --timeout=20m --tags=component
- name: Print storage logs
- name: Print node agent & storage logs
if: always()
run: |
echo "Node agent logs"
kubectl logs $(kubectl get pods -n kubescape -o name | grep node-agent) -n kubescape -c node-agent
echo "-----------------------------------------"
echo "Storage logs"
kubectl logs $(kubectl get pods -n kubescape -o name | grep storage) -n kubescape
# - name: Upload plot images
Expand Down
4 changes: 4 additions & 0 deletions pkg/ruleengine/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"slices"
"strings"

"github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
"github.com/kubescape/node-agent/pkg/objectcache"

tracerexectype "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/exec/types"
Expand Down Expand Up @@ -168,9 +170,11 @@ func isAllowed(event *eventtypes.Event, objCache objectcache.ObjectCache, proces

if policy, ok := appProfile.PolicyByRuleId[ruleId]; ok {
if policy.AllowedContainer || slices.Contains(policy.AllowedProcesses, process) {
logger.L().Debug("process is allowed by policy", helpers.String("ruleID", ruleId), helpers.String("process", process))
return true, nil
}
}

logger.L().Debug("process is not allowed by policy", helpers.String("ruleID", ruleId), helpers.String("process", process))
return false, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ func (rule *R1010SymlinkCreatedOverSensitiveFile) DeleteRule() {
}

func (rule *R1010SymlinkCreatedOverSensitiveFile) ProcessEvent(eventType utils.EventType, event utils.K8sEvent, objCache objectcache.ObjectCache) ruleengine.RuleFailure {
logger.L().Debug("Processing event", helpers.String("ruleID", rule.ID()), helpers.String("eventType", string(eventType)))

if !rule.EvaluateRule(eventType, event, objCache.K8sObjectCache()) {
logger.L().Debug("Event does not match rule", helpers.String("ruleID", rule.ID()), helpers.String("eventType", string(eventType)))
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ func (rule *R1012HardlinkCreatedOverSensitiveFile) DeleteRule() {
}

func (rule *R1012HardlinkCreatedOverSensitiveFile) ProcessEvent(eventType utils.EventType, event utils.K8sEvent, objCache objectcache.ObjectCache) ruleengine.RuleFailure {

logger.L().Debug("Processing event", helpers.String("ruleID", rule.ID()), helpers.String("eventType", string(eventType)))
if !rule.EvaluateRule(eventType, event, objCache.K8sObjectCache()) {
logger.L().Debug("Event does not match rule", helpers.String("ruleID", rule.ID()), helpers.String("eventType", string(eventType)))
return nil
}

Expand Down
8 changes: 5 additions & 3 deletions tests/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,8 @@ func Test_14_RulePoliciesTest(t *testing.T) {

fmt.Println("After completed")

time.Sleep(30 * time.Second)
// wait for cache
time.Sleep(120 * time.Second)

// generate hardlink alert
_, _, err = endpointTraffic.ExecIntoPod([]string{"ln", "/etc/shadow", "/tmp/a"}, "")
Expand All @@ -1183,14 +1184,15 @@ func Test_14_RulePoliciesTest(t *testing.T) {
assert.NoError(t, err)

// Wait for the alert to be signaled
time.Sleep(30 * time.Second)
time.Sleep(60 * time.Second)

alerts, err := testutils.GetAlerts(endpointTraffic.Namespace)
if err != nil {
t.Errorf("Error getting alerts: %v", err)
}

assert.Equal(t, 1, len(alerts), "Expected 1 alert to be generated, but got %d alerts", len(alerts))
testutils.AssertContains(t, alerts, "Hardlink Created Over Sensitive File", "ln", "endpoint-traffic")
testutils.AssertNotContains(t, alerts, "Symlink Created Over Sensitive File", "ln", "endpoint-traffic")
}

func ptr(i int32) *int32 {
Expand Down

0 comments on commit def694f

Please sign in to comment.