098cbc9c67
all of the packages and tests work with both grpc and drpc. we'll probably need to do some jenkins pipelines to run the tests with drpc as well. most of the changes are really due to a bit of cleanup of the pkg/transport.Client api into an rpc.Dialer in the spirit of a net.Dialer. now that we don't need observers, we can pass around stateless configuration to everything rather than stateful things that issue observations. it also adds a DialAddressID for the case where we don't have a pb.Node, but we do have an address and want to assert some ID. this happened pretty frequently, and now there's no more weird contortions creating custom tls options, etc. a lot of the other changes are being consistent/using the abstractions in the rpc package to do rpc style things like finding peer information, or checking status codes. Change-Id: Ief62875e21d80a21b3c56a5a37f45887679f9412
90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package grpcauth_test
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
pb "google.golang.org/grpc/examples/helloworld/helloworld"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"storj.io/storj/internal/errs2"
|
|
"storj.io/storj/internal/testcontext"
|
|
"storj.io/storj/pkg/auth"
|
|
"storj.io/storj/pkg/auth/grpcauth"
|
|
)
|
|
|
|
func TestAPIKey(t *testing.T) {
|
|
ctx := testcontext.New(t)
|
|
defer ctx.Cleanup()
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
require.NoError(t, err)
|
|
// listener is closed in server.Stop() internally
|
|
|
|
server := grpc.NewServer(
|
|
grpc.UnaryInterceptor(grpcauth.InterceptAPIKey),
|
|
)
|
|
defer server.Stop()
|
|
|
|
pb.RegisterGreeterServer(server, &helloServer{})
|
|
|
|
ctx.Go(func() error {
|
|
err := errs2.IgnoreCanceled(server.Serve(listener))
|
|
t.Log(err)
|
|
return err
|
|
})
|
|
|
|
type testcase struct {
|
|
apikey string
|
|
expected codes.Code
|
|
}
|
|
|
|
for _, test := range []testcase{
|
|
{"", codes.Unauthenticated},
|
|
{"wrong key", codes.Unauthenticated},
|
|
{"good key", codes.OK},
|
|
} {
|
|
conn, err := grpc.DialContext(ctx, listener.Addr().String(),
|
|
grpc.WithPerRPCCredentials(grpcauth.NewDeprecatedAPIKeyCredentials(test.apikey)),
|
|
grpc.WithBlock(),
|
|
grpc.WithInsecure(),
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
client := pb.NewGreeterClient(conn)
|
|
response, err := client.SayHello(ctx, &pb.HelloRequest{Name: "Me"})
|
|
|
|
if test.expected == codes.OK {
|
|
require.NoError(t, err)
|
|
require.Equal(t, "Hello Me", response.Message)
|
|
} else {
|
|
require.Error(t, err)
|
|
require.Equal(t, test.expected, status.Code(err))
|
|
}
|
|
|
|
require.NoError(t, conn.Close())
|
|
}
|
|
}
|
|
|
|
type helloServer struct{}
|
|
|
|
// SayHello implements helloworld.GreeterServer
|
|
func (s *helloServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
|
key, ok := auth.GetAPIKey(ctx)
|
|
if !ok {
|
|
return nil, status.Errorf(codes.Unauthenticated, "Invalid API credentials")
|
|
}
|
|
if string(key) != "good key" {
|
|
return nil, status.Errorf(codes.Unauthenticated, "Invalid API credentials")
|
|
}
|
|
|
|
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
|
|
}
|