Skip to content

Commit

Permalink
Merge branch 'master' into jwt-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachEddy authored Apr 17, 2018
2 parents 6bef62d + 3704645 commit 431aadf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

[[constraint]]
name = "google.golang.org/grpc"
version = "1.10.0"
version = "1.11.0"

[prune]
go-tests = true
Expand Down
20 changes: 11 additions & 9 deletions mw/auth/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package auth

import (
"context"
"errors"
"fmt"
"path"
"strings"

jwt "github.com/dgrijalva/jwt-go"
pdp "github.com/infobloxopen/themis/pdp-service"
"google.golang.org/grpc/transport"
"google.golang.org/grpc"
)

// functional options for the defaultBuilder
Expand Down Expand Up @@ -58,11 +57,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},
Expand All @@ -83,9 +81,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 {
Expand Down
56 changes: 56 additions & 0 deletions mw/auth/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

jwt "github.com/dgrijalva/jwt-go"
pdp "github.com/infobloxopen/themis/pdp-service"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

func TestWithJWT(t *testing.T) {
Expand Down Expand Up @@ -118,6 +120,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
Expand Down

0 comments on commit 431aadf

Please sign in to comment.