From 878cc83dc0a02d09f3a8bc5bbc4c667019b4d99c Mon Sep 17 00:00:00 2001 From: Afek Berger Date: Thu, 7 Nov 2024 11:17:53 +0200 Subject: [PATCH] Updated valid status codes (#390) * Updated valid status codes * Modifed component test --------- Signed-off-by: Afek Berger --- pkg/containerwatcher/v1/http.go | 7 +++++- tests/component_test.go | 30 +++++++++++------------ tests/resources/endpoint-traffic.yaml | 35 ++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/pkg/containerwatcher/v1/http.go b/pkg/containerwatcher/v1/http.go index 9b5ff2bf..2618f876 100644 --- a/pkg/containerwatcher/v1/http.go +++ b/pkg/containerwatcher/v1/http.go @@ -11,6 +11,11 @@ import ( "github.com/kubescape/go-logger/helpers" ) +const ( + StatusOK = 200 + StatusBadRequest = 300 +) + func (ch *IGContainerWatcher) httpEventCallback(event *tracerhttptype.Event) { if event.Type == types.DEBUG { return @@ -21,7 +26,7 @@ func (ch *IGContainerWatcher) httpEventCallback(event *tracerhttptype.Event) { return } - if event.Response == nil || event.Response.StatusCode == 404 { + if event.Response == nil || (event.Response.StatusCode < StatusOK || event.Response.StatusCode >= StatusBadRequest) { return } diff --git a/tests/component_test.go b/tests/component_test.go index f23e5796..46ee65f1 100644 --- a/tests/component_test.go +++ b/tests/component_test.go @@ -625,44 +625,44 @@ func Test_11_EndpointTest(t *testing.T) { assert.NoError(t, endpointTraffic.WaitForApplicationProfile(80, "ready")) // Merge methods - _, _, err = endpointTraffic.ExecIntoPod([]string{"wget", "http://127.0.0.1:8000"}, "") + _, _, err = endpointTraffic.ExecIntoPod([]string{"wget", "http://127.0.0.1:80"}, "") assert.NoError(t, err) - _, _, err = endpointTraffic.ExecIntoPod([]string{"wget", "http://127.0.0.1:8000", "--post-data", "test-data"}, "") + _, _, err = endpointTraffic.ExecIntoPod([]string{"wget", "http://127.0.0.1:80", "--post-data", "test-data"}, "") // Merge dynamic for i := 0; i < threshold; i++ { - endpointTraffic.ExecIntoPod([]string{"wget", fmt.Sprintf("http://127.0.0.1:8000/users/%d", i)}, "") + endpointTraffic.ExecIntoPod([]string{"wget", fmt.Sprintf("http://127.0.0.1:80/users/%d", i)}, "") } // Merge headers - _, _, err = endpointTraffic.ExecIntoPod([]string{"wget", "http://127.0.0.1:8000/users/99", "--header", "Connection:1234r"}, "") - _, _, err = endpointTraffic.ExecIntoPod([]string{"wget", "http://127.0.0.1:8000/users/12", "--header", "Connection:ziz"}, "") + _, _, err = endpointTraffic.ExecIntoPod([]string{"wget", "http://127.0.0.1:80/users/99", "--header", "Connection:1234r"}, "") + _, _, err = endpointTraffic.ExecIntoPod([]string{"wget", "http://127.0.0.1:80/users/12", "--header", "Connection:ziz"}, "") - err = endpointTraffic.WaitForApplicationProfileCompletion(80) - if err != nil { - t.Errorf("Error waiting for application profile to be completed: %v", err) - } + err = endpointTraffic.WaitForApplicationProfileCompletion(10) applicationProfile, err := endpointTraffic.GetApplicationProfile() + if err != nil { + t.Errorf("Error getting application profile: %v", err) + } - headers := map[string][]string{"Connection": {"close"}, "Host": {"127.0.0.1:8000"}} + headers := map[string][]string{"Connection": {"close"}, "Host": {"127.0.0.1:80"}} rawJSON, err := json.Marshal(headers) assert.NoError(t, err) endpoint2 := v1beta1.HTTPEndpoint{ - Endpoint: ":8000/", + Endpoint: ":80/", Methods: []string{"GET", "POST"}, Internal: false, Direction: "inbound", Headers: rawJSON, } - headers = map[string][]string{"Host": {"127.0.0.1:8000"}, "Connection": {"1234r", "close", "ziz"}} + headers = map[string][]string{"Host": {"127.0.0.1:80"}, "Connection": {"1234r", "close", "ziz"}} rawJSON, err = json.Marshal(headers) assert.NoError(t, err) endpoint1 := v1beta1.HTTPEndpoint{ - Endpoint: ":8000/users/" + dynamicpathdetector.DynamicIdentifier, + Endpoint: ":80/users/" + dynamicpathdetector.DynamicIdentifier, Methods: []string{"GET"}, Internal: false, Direction: "inbound", @@ -703,9 +703,7 @@ func Test_11_EndpointTest(t *testing.T) { sort.Strings(savedEndpoint.Methods) assert.Equal(t, e, savedEndpoint) } else { - // Until upgrading helm chart with new storage version - fmt.Printf("Endpoint %v not found in the saved endpoints", savedEndpoint) - //t.Error + t.Errorf("Endpoint %v not found in the saved endpoints", savedEndpoint) } } diff --git a/tests/resources/endpoint-traffic.yaml b/tests/resources/endpoint-traffic.yaml index 7c6d1b22..6897faf4 100644 --- a/tests/resources/endpoint-traffic.yaml +++ b/tests/resources/endpoint-traffic.yaml @@ -18,6 +18,39 @@ spec: - name: endpoint-traffic image: armoafekb/afek-b-tests:ptrace_test imagePullPolicy: Always - command: [ "/bin/sh", "-c", "mkdir -p users && for i in $(seq 0 104); do touch users/$i; done && python3 -m http.server" ] + # Create a volume mount for the script + volumeMounts: + - name: server-script + mountPath: /app + command: ["/bin/sh"] + args: ["-c", "echo '$(SERVER_SCRIPT)' > /app/server.py && python3 /app/server.py"] ports: - containerPort: 80 + env: + - name: SERVER_SCRIPT + value: | + from http.server import HTTPServer, BaseHTTPRequestHandler + class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write(b"GET request received successfully") + def do_POST(self): + content_length = int(self.headers["Content-Length"]) + post_data = self.rfile.read(content_length) + print(f"Received POST data: {post_data.decode()}") + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write(b"POST request received successfully") + def run_server(port=80): + server_address = ("", port) + httpd = HTTPServer(server_address, SimpleHTTPRequestHandler) + print(f"Server running on port {port}") + httpd.serve_forever() + if __name__ == "__main__": + run_server() + volumes: + - name: server-script + emptyDir: {} \ No newline at end of file