Skip to content
This repository has been archived by the owner on Mar 29, 2020. It is now read-only.

Commit

Permalink
attempt to forward metadata according to the latest grpc-proxy librar…
Browse files Browse the repository at this point in the history
…y api
  • Loading branch information
lalomartins committed Feb 10, 2018
1 parent 4f62bab commit 3155065
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions extras/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,44 @@ import (
"github.com/mwitkow/grpc-proxy/proxy"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
)

func GetDirector(config Config) func(context.Context, string) (*grpc.ClientConn, error) {
func GetDirector(config Config) func(context.Context, string) (context.Context, *grpc.ClientConn, error) {

credentialsCache := make(map[string] credentials.TransportCredentials)

return func(ctx context.Context, fullMethodName string) (*grpc.ClientConn, error) {
return func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
for _, backend := range config.Backends {
if strings.HasPrefix(fullMethodName, backend.Filter) {
if (config.Verbose) {
fmt.Printf("Found: %s > %s \n", fullMethodName, backend.Backend)
}
if backend.CertFile == "" {
return grpc.DialContext(ctx, backend.Backend, grpc.WithCodec(proxy.Codec()),
md, _ := metadata.FromIncomingContext(ctx)
outCtx, _ := context.WithCancel(ctx)
outCtx = metadata.NewOutgoingContext(outCtx, md.Copy())
con, err := grpc.DialContext(outCtx, backend.Backend, grpc.WithCodec(proxy.Codec()),
grpc.WithInsecure())
return outCtx, con, err
}
creds := GetCredentials(credentialsCache, backend)
if creds != nil {
return grpc.DialContext(ctx, backend.Backend, grpc.WithCodec(proxy.Codec()),
md, _ := metadata.FromIncomingContext(ctx)
outCtx, _ := context.WithCancel(ctx)
outCtx = metadata.NewOutgoingContext(outCtx, md.Copy())
con, err := grpc.DialContext(outCtx, backend.Backend, grpc.WithCodec(proxy.Codec()),
grpc.WithTransportCredentials(creds))
return outCtx, con, err
}
grpclog.Fatalf("Failed to create TLS credentials")
return nil, grpc.Errorf(codes.FailedPrecondition, "Backend TLS is not configured properly in grpc-proxy")
return nil, nil, grpc.Errorf(codes.FailedPrecondition, "Backend TLS is not configured properly in grpc-proxy")
}
}
if (config.Verbose) {
fmt.Println("Not found: ", fullMethodName)
}
return nil, grpc.Errorf(codes.Unimplemented, "Unknown method")
return nil, nil, grpc.Errorf(codes.Unimplemented, "Unknown method")
}
}

Expand All @@ -52,4 +61,4 @@ func GetCredentials(cache map[string] credentials.TransportCredentials, backend
}
cache[backend.Backend] = creds
return creds
}
}

1 comment on commit 3155065

@lalomartins
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: I don't know Go

Please sign in to comment.