Filter out ip adresses in service name detection

This commit is contained in:
Christian Schaible 2024-09-12 13:14:33 +02:00
parent be23f50c5a
commit fc895948bb
2 changed files with 19 additions and 1 deletions

View file

@ -12,6 +12,7 @@ import (
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
"net"
"net/url"
"regexp"
"slices"
@ -371,7 +372,8 @@ func NewAuditLogEntry(
func GetCalledServiceNameFromRequest(request *Request, fallbackName string) string {
var calledServiceName = fallbackName
host := request.Host
if !strings.Contains(host, "localhost") {
ip := net.ParseIP(host)
if ip == nil && !strings.Contains(host, "localhost") {
dotIdx := strings.Index(host, ".")
if dotIdx != -1 {
calledServiceName = host[0:dotIdx]

View file

@ -86,6 +86,22 @@ func Test_GetCalledServiceNameFromRequest(t *testing.T) {
serviceName := GetCalledServiceNameFromRequest(&request, "resource-manager")
assert.Equal(t, "resource-manager", serviceName)
})
t.Run(
"ip", func(t *testing.T) {
request := Request{Host: "127.0.0.1"}
serviceName := GetCalledServiceNameFromRequest(&request, "resource-manager")
assert.Equal(t, "resource-manager", serviceName)
},
)
t.Run(
"ip short", func(t *testing.T) {
request := Request{Host: "::1"}
serviceName := GetCalledServiceNameFromRequest(&request, "resource-manager")
assert.Equal(t, "resource-manager", serviceName)
},
)
}
func Test_NewPbInt64Value(t *testing.T) {