Skip to content

Commit

Permalink
Updated valid status codes (#390)
Browse files Browse the repository at this point in the history
* Updated valid status codes

* Modifed component test

---------

Signed-off-by: Afek Berger <afekb@armosec.io>
  • Loading branch information
afek854 authored Nov 7, 2024
1 parent 8c43514 commit 878cc83
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
7 changes: 6 additions & 1 deletion pkg/containerwatcher/v1/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
30 changes: 14 additions & 16 deletions tests/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}

}
Expand Down
35 changes: 34 additions & 1 deletion tests/resources/endpoint-traffic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}

0 comments on commit 878cc83

Please sign in to comment.