diff --git a/pkg/distributor/push.go b/pkg/distributor/push.go index 4566d72d375..2d4946412fd 100644 --- a/pkg/distributor/push.go +++ b/pkg/distributor/push.go @@ -122,7 +122,6 @@ func handler( if sourceIPs != nil { source := sourceIPs.Get(r) if source != "" { - ctx = util.AddSourceIPsToOutgoingContext(ctx, source) logger = utillog.WithSourceIPs(source, logger) } } diff --git a/pkg/util/extract_forwarded.go b/pkg/util/extract_forwarded.go deleted file mode 100644 index 9a4bab540a3..00000000000 --- a/pkg/util/extract_forwarded.go +++ /dev/null @@ -1,54 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/util/extract_forwarded.go -// Provenance-includes-license: Apache-2.0 -// Provenance-includes-copyright: The Cortex Authors. - -package util - -import ( - "context" - - "google.golang.org/grpc/metadata" -) - -// ipAddressesKey is key for the GRPC metadata where the IP addresses are stored -const ipAddressesKey = "extract-forwarded-x-forwarded-for" - -// GetSourceIPsFromOutgoingCtx extracts the source field from the GRPC context -func GetSourceIPsFromOutgoingCtx(ctx context.Context) string { - md, ok := metadata.FromOutgoingContext(ctx) - if !ok { - return "" - } - ipAddresses, ok := md[ipAddressesKey] - if !ok { - return "" - } - return ipAddresses[0] -} - -// GetSourceIPsFromIncomingCtx extracts the source field from the GRPC context -func GetSourceIPsFromIncomingCtx(ctx context.Context) string { - ipAddresses := metadata.ValueFromIncomingContext(ctx, ipAddressesKey) - if len(ipAddresses) == 0 { - return "" - } - return ipAddresses[0] -} - -// AddSourceIPsToOutgoingContext adds the given source to the GRPC context -func AddSourceIPsToOutgoingContext(ctx context.Context, source string) context.Context { - if source != "" { - ctx = metadata.AppendToOutgoingContext(ctx, ipAddressesKey, source) - } - return ctx -} - -// AddSourceIPsToIncomingContext adds the given source to the GRPC context -func AddSourceIPsToIncomingContext(ctx context.Context, source string) context.Context { - if source != "" { - md := metadata.Pairs(ipAddressesKey, source) - ctx = metadata.NewIncomingContext(ctx, md) - } - return ctx -} diff --git a/pkg/util/extract_forwarded_test.go b/pkg/util/extract_forwarded_test.go deleted file mode 100644 index 6e020b980f0..00000000000 --- a/pkg/util/extract_forwarded_test.go +++ /dev/null @@ -1,63 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/util/extract_forwarded_test.go -// Provenance-includes-license: Apache-2.0 -// Provenance-includes-copyright: The Cortex Authors. - -package util - -import ( - "context" - "testing" - - "github.com/stretchr/testify/assert" - "google.golang.org/grpc/metadata" -) - -func TestGetSourceFromOutgoingCtx(t *testing.T) { - tests := []struct { - name string - key string - value string - want string - }{ - { - name: "No value in key", - key: ipAddressesKey, - value: "", - want: "", - }, - { - name: "Value in key", - key: ipAddressesKey, - value: "172.16.1.1", - want: "172.16.1.1", - }, - { - name: "Stored under wrong key", - key: "wrongkey", - value: "172.16.1.1", - want: "", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // Test extracting from incoming context - ctx := context.Background() - if tt.value != "" { - md := metadata.Pairs(tt.key, tt.value) - ctx = metadata.NewIncomingContext(ctx, md) - } - got := GetSourceIPsFromIncomingCtx(ctx) - assert.Equal(t, tt.want, got) - - // Test extracting from outgoing context - ctx = context.Background() - if tt.value != "" { - md := metadata.Pairs(tt.key, tt.value) - ctx = metadata.NewOutgoingContext(ctx, md) - } - got = GetSourceIPsFromOutgoingCtx(ctx) - assert.Equal(t, tt.want, got) - }) - } -}