Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v2' into refactor-tracing-interc…
Browse files Browse the repository at this point in the history
…eptor
  • Loading branch information
XSAM committed Apr 14, 2022
2 parents 9202a15 + 72478fa commit 7e04286
Show file tree
Hide file tree
Showing 18 changed files with 550 additions and 212 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ jobs:

tests:
runs-on: ${{ matrix.platform }}
name: Unit tests on Go ${{ matrix.go }}
name: Unit tests on Go ${{ matrix.go }} / ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
go: [ '1.13.x', '1.14.x', '1.15.x' ]
go: [ '1.15.x', '1.16.x', '1.17.x' ]
platform: [ubuntu-latest, macos-latest]
steps:
- name: Checkout code into the Go module directory.
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.14

require (
github.com/golang/protobuf v1.5.2
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,13 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
Expand Down
9 changes: 4 additions & 5 deletions interceptors/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/oauth"
"google.golang.org/grpc/metadata"
grpcMetadata "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/util/metautils"
)

var authedMarker struct{}
Expand Down Expand Up @@ -66,9 +66,8 @@ func (s *assertingPingService) PingList(ping *testpb.PingListRequest, stream tes
}

func ctxWithToken(ctx context.Context, scheme string, token string) context.Context {
md := metadata.Pairs("authorization", fmt.Sprintf("%s %v", scheme, token))
nCtx := metautils.NiceMD(md).ToOutgoing(ctx)
return nCtx
md := grpcMetadata.Pairs("authorization", fmt.Sprintf("%s %v", scheme, token))
return metadata.MD(md).ToOutgoing(ctx)
}

func TestAuthTestSuite(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions interceptors/auth/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/util/metautils"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
)

var (
Expand All @@ -23,7 +23,7 @@ var (
// case-insensitive format (see rfc2617, sec 1.2). If no such authorization is found, or the token
// is of wrong scheme, an error with gRPC status `Unauthenticated` is returned.
func AuthFromMD(ctx context.Context, expectedScheme string) (string, error) {
val := metautils.ExtractIncoming(ctx).Get(headerAuthorize)
val := metadata.ExtractIncoming(ctx).Get(headerAuthorize)
if val == "" {
return "", status.Errorf(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
}
Expand Down
22 changes: 11 additions & 11 deletions interceptors/auth/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,60 @@ import (

"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
grpcMetadata "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/util/metautils"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
)

func TestAuthFromMD(t *testing.T) {
for _, run := range []struct {
md metadata.MD
md grpcMetadata.MD
value string
errCode codes.Code
msg string
}{
{
md: metadata.Pairs("authorization", "bearer some_token"),
md: grpcMetadata.Pairs("authorization", "bearer some_token"),
value: "some_token",
msg: "must extract simple bearer tokens without case checking",
},
{
md: metadata.Pairs("authorization", "Bearer some_token"),
md: grpcMetadata.Pairs("authorization", "Bearer some_token"),
value: "some_token",
msg: "must extract simple bearer tokens with case checking",
},
{
md: metadata.Pairs("authorization", "Bearer some multi string bearer"),
md: grpcMetadata.Pairs("authorization", "Bearer some multi string bearer"),
value: "some multi string bearer",
msg: "must handle string based bearers",
},
{
md: metadata.Pairs("authorization", "Basic login:passwd"),
md: grpcMetadata.Pairs("authorization", "Basic login:passwd"),
value: "",
errCode: codes.Unauthenticated,
msg: "must check authentication type",
},
{
md: metadata.Pairs("authorization", "Basic login:passwd", "authorization", "bearer some_token"),
md: grpcMetadata.Pairs("authorization", "Basic login:passwd", "authorization", "bearer some_token"),
value: "",
errCode: codes.Unauthenticated,
msg: "must not allow multiple authentication methods",
},
{
md: metadata.Pairs("authorization", ""),
md: grpcMetadata.Pairs("authorization", ""),
value: "",
errCode: codes.Unauthenticated,
msg: "authorization string must not be empty",
},
{
md: metadata.Pairs("authorization", "Bearer"),
md: grpcMetadata.Pairs("authorization", "Bearer"),
value: "",
errCode: codes.Unauthenticated,
msg: "bearer token must not be empty",
},
} {
ctx := metautils.NiceMD(run.md).ToIncoming(context.TODO())
ctx := metadata.MD(run.md).ToIncoming(context.TODO())
out, err := AuthFromMD(ctx, "bearer")
if run.errCode != codes.OK {
assert.Equal(t, run.errCode, status.Code(err), run.msg)
Expand Down
10 changes: 5 additions & 5 deletions interceptors/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"golang.org/x/net/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
grpcMetadata "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/util/metautils"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
)

const (
Expand Down Expand Up @@ -170,11 +170,11 @@ func (s *serverStreamingRetryingStream) CloseSend() error {
return s.getStream().CloseSend()
}

func (s *serverStreamingRetryingStream) Header() (metadata.MD, error) {
func (s *serverStreamingRetryingStream) Header() (grpcMetadata.MD, error) {
return s.getStream().Header()
}

func (s *serverStreamingRetryingStream) Trailer() metadata.MD {
func (s *serverStreamingRetryingStream) Trailer() grpcMetadata.MD {
return s.getStream().Trailer()
}

Expand Down Expand Up @@ -296,7 +296,7 @@ func perCallContext(parentCtx context.Context, callOpts *options, attempt uint)
ctx, cancel = context.WithTimeout(ctx, callOpts.perCallTimeout)
}
if attempt > 0 && callOpts.includeHeader {
mdClone := metautils.ExtractOutgoing(ctx).Clone().Set(AttemptMetadataKey, fmt.Sprintf("%d", attempt))
mdClone := metadata.ExtractOutgoing(ctx).Clone().Set(AttemptMetadataKey, fmt.Sprintf("%d", attempt))
ctx = mdClone.ToOutgoing(ctx)
}
return ctx, cancel
Expand Down
18 changes: 9 additions & 9 deletions interceptors/skip/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
grpcMetadata "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
Expand Down Expand Up @@ -42,7 +42,7 @@ type skipPingService struct {
}

func checkMetadata(ctx context.Context, grpcType interceptors.GRPCType, service string, method string) error {
m, _ := metadata.FromIncomingContext(ctx)
m, _ := grpcMetadata.FromIncomingContext(ctx)
if typeFromMetadata := m.Get(keyGRPCType)[0]; typeFromMetadata != string(grpcType) {
return status.Errorf(codes.Internal, fmt.Sprintf("expected grpc type %s, got: %s", grpcType, typeFromMetadata))
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func (s *skipPingService) PingList(_ *testpb.PingListRequest, stream testpb.Test
}

func filter(ctx context.Context, gRPCType interceptors.GRPCType, service string, method string) bool {
m, _ := metadata.FromIncomingContext(ctx)
m, _ := grpcMetadata.FromIncomingContext(ctx)
// Set parameters into metadata
m.Set(keyGRPCType, string(gRPCType))
m.Set(keyService, service)
Expand Down Expand Up @@ -144,14 +144,14 @@ func (s *SkipSuite) TestPing() {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var m metadata.MD
var m grpcMetadata.MD
if tc.skip {
m = metadata.New(map[string]string{
m = grpcMetadata.New(map[string]string{
"skip": "true",
})
}

resp, err := s.Client.Ping(metadata.NewOutgoingContext(s.SimpleCtx(), m), testpb.GoodPing)
resp, err := s.Client.Ping(grpcMetadata.NewOutgoingContext(s.SimpleCtx(), m), testpb.GoodPing)
require.NoError(t, err)

var value string
Expand Down Expand Up @@ -182,14 +182,14 @@ func (s *SkipSuite) TestPingList() {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var m metadata.MD
var m grpcMetadata.MD
if tc.skip {
m = metadata.New(map[string]string{
m = grpcMetadata.New(map[string]string{
"skip": "true",
})
}

stream, err := s.Client.PingList(metadata.NewOutgoingContext(s.SimpleCtx(), m), testpb.GoodPingList)
stream, err := s.Client.PingList(grpcMetadata.NewOutgoingContext(s.SimpleCtx(), m), testpb.GoodPingList)
require.NoError(t, err)

for {
Expand Down
10 changes: 5 additions & 5 deletions util/metautils/doc.go → metadata/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// Licensed under the Apache License 2.0.

/*
Package `metautils` provides convenience functions for dealing with gRPC metadata.MD objects inside
Package `metadata` provides convenience functions for dealing with gRPC metadata.MD objects inside
Context handlers.
While the upstream grpc-go package contains decent functionality (see https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md)
they are hard to use.
The majority of functions center around the NiceMD, which is a convenience wrapper around metadata.MD. For example
The majority of functions center around the MD, which is a convenience wrapper around metadata.MD. For example
the following code allows you to easily extract incoming metadata (server handler) and put it into a new client context
metadata.
nmd := metautils.ExtractIncoming(serverCtx).Clone(":authorization", ":custom")
clientCtx := nmd.Set("x-client-header", "2").Set("x-another", "3").ToOutgoing(ctx)
md := metadata.ExtractIncoming(serverCtx).Clone(":authorization", ":custom")
clientCtx := md.Set("x-client-header", "2").Set("x-another", "3").ToOutgoing(ctx)
*/

package metautils
package metadata
Loading

0 comments on commit 7e04286

Please sign in to comment.