Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: use "localhost:port" as authority if target is ":port" #4017

Merged
merged 8 commits into from Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions clientconn.go
Expand Up @@ -259,6 +259,8 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
cc.authority = cc.dopts.authority
} else if strings.HasPrefix(cc.target, "unix:") {
cc.authority = "localhost"
} else if strings.HasPrefix(cc.parsedTarget.Endpoint, ":") {
cc.authority = "localhost" + cc.parsedTarget.Endpoint
} else {
// Use endpoint from "scheme://authority/endpoint" as the default
// authority for ClientConn.
Expand Down
73 changes: 49 additions & 24 deletions test/authority_test.go
Expand Up @@ -33,44 +33,48 @@ import (
testpb "google.golang.org/grpc/test/grpc_testing"
)

func authorityChecker(expectedAuthority string) func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Error(codes.InvalidArgument, "failed to parse metadata")
}
auths, ok := md[":authority"]
if !ok {
return nil, status.Error(codes.InvalidArgument, "no authority header")
}
if len(auths) < 1 {
dfawley marked this conversation as resolved.
Show resolved Hide resolved
return nil, status.Error(codes.InvalidArgument, "no authority header")
}
if auths[0] != expectedAuthority {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid authority header %v, expected %v", auths[0], expectedAuthority))
}
return &testpb.Empty{}, nil
}
}

func runUnixTest(t *testing.T, address, target, expectedAuthority string, dialer func(context.Context, string) (net.Conn, error)) {
if err := os.RemoveAll(address); err != nil {
t.Fatalf("Error removing socket file %v: %v\n", address, err)
}
us := &stubServer{
emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Error(codes.InvalidArgument, "failed to parse metadata")
}
auths, ok := md[":authority"]
if !ok {
return nil, status.Error(codes.InvalidArgument, "no authority header")
}
if len(auths) < 1 {
return nil, status.Error(codes.InvalidArgument, "no authority header")
}
if auths[0] != expectedAuthority {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid authority header %v, expected %v", auths[0], expectedAuthority))
}
return &testpb.Empty{}, nil
},
network: "unix",
address: address,
target: target,
ss := &stubServer{
emptyCall: authorityChecker(expectedAuthority),
network: "unix",
address: address,
target: target,
}
opts := []grpc.DialOption{}
if dialer != nil {
opts = append(opts, grpc.WithContextDialer(dialer))
}
if err := us.Start(nil, opts...); err != nil {
if err := ss.Start(nil, opts...); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
return
}
defer us.Stop()
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_, err := us.client.EmptyCall(ctx, &testpb.Empty{})
_, err := ss.client.EmptyCall(ctx, &testpb.Empty{})
if err != nil {
t.Errorf("us.client.EmptyCall(_, _) = _, %v; want _, nil", err)
}
Expand Down Expand Up @@ -147,3 +151,24 @@ func (s) TestUnixCustomDialer(t *testing.T) {
})
}
}

func (s) TestColonPortAuthority(t *testing.T) {
port := ":50051"
dfawley marked this conversation as resolved.
Show resolved Hide resolved
ss := &stubServer{
emptyCall: authorityChecker("localhost" + port),
network: "tcp",
address: "localhost" + port,
target: port,
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
return
Copy link
Member

Choose a reason for hiding this comment

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

return-after-fatal is unncessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed return

}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
dfawley marked this conversation as resolved.
Show resolved Hide resolved
defer cancel()
_, err := ss.client.EmptyCall(ctx, &testpb.Empty{})
if err != nil {
t.Errorf("us.client.EmptyCall(_, _) = _, %v; want _, nil", err)
}
}