Skip to content
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
4 changes: 2 additions & 2 deletions containers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,9 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R
}

if conn.dstWorkload.Namespace == "external" && (r.Protocol == l7.ProtocolHTTP || r.Protocol == l7.ProtocolHTTP2) {
request, error := l7.ParseHttpRequest(string(r.Payload))
host, error := l7.ParseHostFromHttpRequest(string(r.Payload))
if error == nil {
conn.dstWorkload.Name = request.Host
conn.dstWorkload.Name = host
}
}
stats := c.l7Stats.get(r.Protocol, conn.Dest, conn.ActualDest, r, conn.srcWorkload, conn.dstWorkload, conn.actualDestWorkload)
Expand Down
6 changes: 3 additions & 3 deletions containers/l7.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func (s L7Stats) get(protocol l7.Protocol, destination, actualDestination netadd
constLabels["path"] = path
constLabels["method"] = method
if dstWorkload.Namespace == "external" {
request, error := l7.ParseHttpRequest(string(r.Payload))
if error == nil {
constLabels["destination_workload_name"] = request.Host
host, err := l7.ParseHostFromHttpRequest(string(r.Payload))
if err == nil {
constLabels["destination_workload_name"] = host
}
}
hOpts := L7Latency[protocol]
Expand Down
32 changes: 24 additions & 8 deletions ebpftracer/l7/http.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package l7

import (
"bufio"
"bytes"
"encoding/base64"
"errors"
"log"
"net/http"
"regexp"
"strings"
)
Expand Down Expand Up @@ -51,14 +50,31 @@ func ParseHttpAndRest(payload []byte) (string, string, string, string) {
return string(method), string(uri), string(headers), string(d)
}

func ParseHttpRequest(request string) (*http.Request, error) {
request = strings.ReplaceAll(request, "\\n", "\r\n")
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(request)))
if err != nil {
return nil, err
func ParseHostFromHttpRequest(request string) (string, error) {
// Split the request string into lines
lines := strings.Split(request, "\n")

// Search for the Host header within the lines
var host string
for _, line := range lines {
if strings.HasPrefix(line, "Host:") {
parts := strings.SplitN(line, ": ", 2)
if len(parts) == 2 {
host = parts[1]
break
}
}
}

// Check if the Host header is found
if host == "" {
err := errors.New("host header not found")
return "", err
}
return req, nil

return host, nil
}

func SanitizeString(input string) string {
// Regular expression patterns to match various sensitive data formats
sensitivePatterns := []*regexp.Regexp{
Expand Down
6 changes: 0 additions & 6 deletions ebpftracer/l7/l7_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,3 @@ func TestParseMongo(t *testing.T) {
binary.LittleEndian.PutUint32(payload[mongoHeaderLength+mongoSectionKindLength:], dataSize+1)
assert.Equal(t, `<truncated>`, ParseMongo(payload))
}

func TestParseRequest(t *testing.T) {
m, _ := ParseHttpRequest(`HEAD /1 HTTP/1.1\nHost: 127.0.0.1\nUser-Agent: curl/8.0.1\nAccept: */*\n\nxzxxxxxxzx`)
assert.Equal(t, "HEAD", m.Method)
assert.Equal(t, "127.0.0.1", m.Host)
}