mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-31 23:41:09 +02:00
use strings.Cut, add unit tests
This commit is contained in:
parent
9cf8207cd8
commit
21b79405cd
4 changed files with 183 additions and 35 deletions
|
@ -51,22 +51,25 @@ func populateLogEvent(
|
|||
evt *zerolog.Event,
|
||||
entry *envoy_data_accesslog_v3.HTTPAccessLogEntry,
|
||||
) *zerolog.Event {
|
||||
referer, _, _ := strings.Cut(entry.GetRequest().GetReferer(), "?")
|
||||
path, query, _ := strings.Cut(entry.GetRequest().GetPath(), "?")
|
||||
|
||||
switch field {
|
||||
case log.AccessLogFieldAuthority:
|
||||
return evt.Str(string(field), entry.GetRequest().GetAuthority())
|
||||
case log.AccessLogFieldDuration:
|
||||
dur := entry.CommonProperties.TimeToLastDownstreamTxByte.AsDuration()
|
||||
dur := entry.GetCommonProperties().GetTimeToLastDownstreamTxByte().AsDuration()
|
||||
return evt.Dur(string(field), dur)
|
||||
case log.AccessLogFieldForwardedFor:
|
||||
return evt.Str(string(field), entry.GetRequest().GetForwardedFor())
|
||||
case log.AccessLogFieldMethod:
|
||||
return evt.Str(string(field), entry.GetRequest().GetRequestMethod().String())
|
||||
case log.AccessLogFieldPath:
|
||||
return evt.Str(string(field), stripQueryString(entry.GetRequest().GetPath()))
|
||||
return evt.Str(string(field), path)
|
||||
case log.AccessLogFieldQuery:
|
||||
return evt.Str(string(field), getQueryString(entry.GetRequest().GetPath()))
|
||||
return evt.Str(string(field), query)
|
||||
case log.AccessLogFieldReferer:
|
||||
return evt.Str(string(field), stripQueryString(entry.GetRequest().GetReferer()))
|
||||
return evt.Str(string(field), referer)
|
||||
case log.AccessLogFieldRequestID:
|
||||
return evt.Str(string(field), entry.GetRequest().GetRequestId())
|
||||
case log.AccessLogFieldResponseCode:
|
||||
|
@ -74,7 +77,7 @@ func populateLogEvent(
|
|||
case log.AccessLogFieldResponseCodeDetails:
|
||||
return evt.Str(string(field), entry.GetResponse().GetResponseCodeDetails())
|
||||
case log.AccessLogFieldSize:
|
||||
return evt.Uint64(string(field), entry.Response.ResponseBodyBytes)
|
||||
return evt.Uint64(string(field), entry.GetResponse().GetResponseBodyBytes())
|
||||
case log.AccessLogFieldUpstreamCluster:
|
||||
return evt.Str(string(field), entry.GetCommonProperties().GetUpstreamCluster())
|
||||
case log.AccessLogFieldUserAgent:
|
||||
|
@ -83,17 +86,3 @@ func populateLogEvent(
|
|||
return evt
|
||||
}
|
||||
}
|
||||
|
||||
func getQueryString(str string) string {
|
||||
if idx := strings.Index(str, "?"); idx != -1 {
|
||||
return str[idx+1:]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func stripQueryString(str string) string {
|
||||
if idx := strings.Index(str, "?"); idx != -1 {
|
||||
str = str[:idx]
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
|
74
internal/controlplane/grpc_accesslog_test.go
Normal file
74
internal/controlplane/grpc_accesslog_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package controlplane
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
||||
envoy_data_accesslog_v3 "github.com/envoyproxy/go-control-plane/envoy/data/accesslog/v3"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/protobuf/types/known/durationpb"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
)
|
||||
|
||||
func Test_populateLogEvent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
entry := &envoy_data_accesslog_v3.HTTPAccessLogEntry{
|
||||
CommonProperties: &envoy_data_accesslog_v3.AccessLogCommon{
|
||||
TimeToLastDownstreamTxByte: durationpb.New(time.Second * 3),
|
||||
UpstreamCluster: "UPSTREAM-CLUSTER",
|
||||
},
|
||||
Request: &envoy_data_accesslog_v3.HTTPRequestProperties{
|
||||
Authority: "AUTHORITY",
|
||||
ForwardedFor: "FORWARDED-FOR",
|
||||
Path: "https://www.example.com/some/path?a=b",
|
||||
Referer: "https://www.example.com/referer?a=b",
|
||||
RequestId: "REQUEST-ID",
|
||||
RequestMethod: envoy_config_core_v3.RequestMethod_GET,
|
||||
UserAgent: "USER-AGENT",
|
||||
},
|
||||
Response: &envoy_data_accesslog_v3.HTTPResponseProperties{
|
||||
ResponseBodyBytes: 1234,
|
||||
ResponseCode: wrapperspb.UInt32(200),
|
||||
ResponseCodeDetails: "RESPONSE-CODE-DETAILS",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
field log.AccessLogField
|
||||
expect string
|
||||
}{
|
||||
{log.AccessLogFieldAuthority, `{"authority":"AUTHORITY"}`},
|
||||
{log.AccessLogFieldDuration, `{"duration":3000}`},
|
||||
{log.AccessLogFieldForwardedFor, `{"forwarded-for":"FORWARDED-FOR"}`},
|
||||
{log.AccessLogFieldMethod, `{"method":"GET"}`},
|
||||
{log.AccessLogFieldPath, `{"path":"https://www.example.com/some/path"}`},
|
||||
{log.AccessLogFieldQuery, `{"query":"a=b"}`},
|
||||
{log.AccessLogFieldReferer, `{"referer":"https://www.example.com/referer"}`},
|
||||
{log.AccessLogFieldRequestID, `{"request-id":"REQUEST-ID"}`},
|
||||
{log.AccessLogFieldResponseCode, `{"response-code":200}`},
|
||||
{log.AccessLogFieldResponseCodeDetails, `{"response-code-details":"RESPONSE-CODE-DETAILS"}`},
|
||||
{log.AccessLogFieldSize, `{"size":1234}`},
|
||||
{log.AccessLogFieldUpstreamCluster, `{"upstream-cluster":"UPSTREAM-CLUSTER"}`},
|
||||
{log.AccessLogFieldUserAgent, `{"user-agent":"USER-AGENT"}`},
|
||||
} {
|
||||
tc := tc
|
||||
t.Run(string(tc.field), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var buf bytes.Buffer
|
||||
log := zerolog.New(&buf)
|
||||
evt := log.Log()
|
||||
evt = populateLogEvent(tc.field, evt, entry)
|
||||
evt.Send()
|
||||
|
||||
assert.Equal(t, tc.expect, strings.TrimSpace(buf.String()))
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue