From 0dae7e2b7d03be2c6f021c90501facba30983da1 Mon Sep 17 00:00:00 2001 From: Zachary Eddy Date: Mon, 16 Apr 2018 13:02:30 -0700 Subject: [PATCH] Update gRPC version from v1.10.0 to v1.11.0 --- Gopkg.lock | 6 ++--- Gopkg.toml | 2 +- mw/auth/options.go | 20 ++++++++------- mw/auth/options_test.go | 55 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 13 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index d57a7f6c..5f5c0923 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -217,12 +217,12 @@ "tap", "transport" ] - revision = "8e4536a86ab602859c20df5ebfd0bd4228d08655" - version = "v1.10.0" + revision = "d11072e7ca9811b1100b80ca0269ac831f06d024" + version = "v1.11.3" [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "ae9afecba22387a51b144a80aa90329104333fe037a29c14e495c4808168ef4f" + inputs-digest = "7c587a6db41fb6fcc707ef8405c9976a968314fc03ec4911447f915def61c0b6" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 0aebe165..dd6af05d 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -61,7 +61,7 @@ [[constraint]] name = "google.golang.org/grpc" - version = "1.10.0" + version = "1.11.0" [prune] go-tests = true diff --git a/mw/auth/options.go b/mw/auth/options.go index 96cd1292..df92d4ec 100644 --- a/mw/auth/options.go +++ b/mw/auth/options.go @@ -2,7 +2,6 @@ package auth import ( "context" - "errors" "fmt" "path" "strings" @@ -10,7 +9,7 @@ import ( jwt "github.com/dgrijalva/jwt-go" "github.com/grpc-ecosystem/go-grpc-middleware/auth" pdp "github.com/infobloxopen/themis/pdp-service" - "google.golang.org/grpc/transport" + "google.golang.org/grpc" ) // functional options for the defaultBuilder @@ -83,11 +82,10 @@ func WithCallback(attr attributer) option { // called by the client (e.g. ListPersons) func WithRequest() option { withRequestFunc := func(ctx context.Context) ([]*pdp.Attribute, error) { - stream, ok := transport.StreamFromContext(ctx) - if !ok { - return nil, errors.New("failed getting stream from context") + service, method, err := getRequestDetails(ctx) + if err != nil { + return nil, err } - service, method := getRequestDetails(*stream) service = stripPackageName(service) attributes := []*pdp.Attribute{ &pdp.Attribute{"operation", "string", method}, @@ -108,9 +106,13 @@ func stripPackageName(service string) string { return fields[len(fields)-1] } -func getRequestDetails(stream transport.Stream) (service, method string) { - fullMethodString := stream.Method() - return path.Dir(fullMethodString)[1:], path.Base(fullMethodString) +func getRequestDetails(ctx context.Context) (string, string, error) { + fullMethodString, ok := grpc.Method(ctx) + if !ok { + return "", "", ErrInternal + } + fmt.Println(fullMethodString) + return path.Dir(fullMethodString)[1:], path.Base(fullMethodString), nil } func combineAttributes(first, second []*pdp.Attribute) []*pdp.Attribute { diff --git a/mw/auth/options_test.go b/mw/auth/options_test.go index 5ef745a0..2a91954c 100644 --- a/mw/auth/options_test.go +++ b/mw/auth/options_test.go @@ -7,6 +7,7 @@ import ( jwt "github.com/dgrijalva/jwt-go" pdp "github.com/infobloxopen/themis/pdp-service" + "google.golang.org/grpc" "google.golang.org/grpc/metadata" ) @@ -124,6 +125,60 @@ func TestWithCallback(t *testing.T) { } } +type mockTransportStream struct{ method string } + +func (m mockTransportStream) Method() string { return m.method } +func (m mockTransportStream) SetHeader(metadata.MD) error { return nil } +func (m mockTransportStream) SendHeader(metadata.MD) error { return nil } +func (m mockTransportStream) SetTrailer(metadata.MD) error { return nil } + +func TestWithRequest(t *testing.T) { + var tests = []struct { + stream *mockTransportStream + expected []*pdp.Attribute + err error + }{ + { + stream: &mockTransportStream{method: "/PetStore/ListPets"}, + expected: []*pdp.Attribute{ + {Id: "operation", Type: "string", Value: "ListPets"}, + {Id: "application", Type: "string", Value: "petstore"}, + }, + err: nil, + }, + { + stream: &mockTransportStream{method: "/atlas.example.PetStore/ListPets"}, + expected: []*pdp.Attribute{ + {Id: "operation", Type: "string", Value: "ListPets"}, + {Id: "application", Type: "string", Value: "petstore"}, + }, + err: nil, + }, + { + stream: nil, + expected: []*pdp.Attribute{}, + err: ErrInternal, + }, + } + for _, test := range tests { + ctx := context.Background() + if test.stream != nil { + ctx = grpc.NewContextWithServerTransportStream( + context.Background(), + test.stream, + ) + } + builder := NewBuilder(WithRequest()) + req, err := builder.build(ctx) + if err != test.err { + t.Errorf("Unexpected error when building request: %v", err) + } + if !hasMatchingAttributes(req.Attributes, test.expected) { + t.Errorf("Invalid request attributes: %v - expected %v", req.GetAttributes(), test.expected) + } + } +} + func TestStripPackageName(t *testing.T) { var tests = []struct { fullname string