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: set auth header to localhost for unix target #3730

Merged
merged 13 commits into from Jul 21, 2020
3 changes: 3 additions & 0 deletions clientconn.go
Expand Up @@ -245,6 +245,7 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *

// Determine the resolver to use.
cc.parsedTarget = grpcutil.ParseTarget(cc.target)
unixScheme := strings.HasPrefix(cc.target, "unix:")
channelz.Infof(logger, cc.channelzID, "parsed scheme: %q", cc.parsedTarget.Scheme)
resolverBuilder := cc.getResolver(cc.parsedTarget.Scheme)
if resolverBuilder == nil {
Expand All @@ -267,6 +268,8 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
cc.authority = creds.Info().ServerName
} else if cc.dopts.insecure && cc.dopts.authority != "" {
cc.authority = cc.dopts.authority
} else if unixScheme {
cc.authority = "localhost"
} else {
// Use endpoint from "scheme://authority/endpoint" as the default
// authority for ClientConn.
Expand Down
102 changes: 102 additions & 0 deletions test/authority_test.go
@@ -0,0 +1,102 @@
/*
*
* Copyright 2020 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package test

import (
"context"
"fmt"
"os"
"testing"
"time"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
testpb "google.golang.org/grpc/test/grpc_testing"
)

func runUnixTest(t *testing.T, address, target, expectedAuthority string) {
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) {
if md, ok := metadata.FromIncomingContext(ctx); ok {
if auths, ok := md[":authority"]; ok {
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
}
return nil, status.Error(codes.InvalidArgument, "no authority header")
}
return nil, status.Error(codes.InvalidArgument, "failed to parse metadata")
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
},
network: "unix",
address: address,
target: target,
}
if err := us.Start(nil); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
return
}
defer us.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_, err := us.client.EmptyCall(ctx, &testpb.Empty{})
if err != nil {
t.Errorf("us.client.EmptyCall(_, _) = _, %v; want _, nil", err)
}
}

func (s) TestUnix(t *testing.T) {
tests := []struct {
name string
address string
target string
authority string
}{
{
name: "Unix1",
address: "sock.sock",
target: "unix:sock.sock",
authority: "loclahost",
},
{
name: "Unix2",
address: "/tmp/sock.sock",
target: "unix:/tmp/sock.sock",
authority: "loclahost",
},
{
name: "Unix3",
address: "/tmp/sock.sock",
target: "unix:///tmp/sock.sock",
authority: "loclahost",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
runUnixTest(t, test.address, test.target, test.authority)
})
}
}
18 changes: 14 additions & 4 deletions test/end2end_test.go
Expand Up @@ -4991,6 +4991,10 @@ type stubServer struct {
cc *grpc.ClientConn
s *grpc.Server

network string
address string
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
target string

addr string // address of listener

cleanups []func() // Lambdas executed in Stop(); populated by Start().
Expand All @@ -5015,7 +5019,11 @@ func (ss *stubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption)
r := manual.NewBuilderWithScheme("whatever")
ss.r = r

lis, err := net.Listen("tcp", "localhost:0")
if ss.network == "" && ss.address == "" {
ss.network = "tcp"
ss.address = "localhost:0"
}
lis, err := net.Listen(ss.network, ss.address)
if err != nil {
return fmt.Errorf(`net.Listen("tcp", "localhost:0") = %v`, err)
}
Expand All @@ -5028,12 +5036,14 @@ func (ss *stubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption)
ss.cleanups = append(ss.cleanups, s.Stop)
ss.s = s

target := ss.r.Scheme() + ":///" + ss.addr
if ss.target == "" {
ss.target = ss.r.Scheme() + ":///" + ss.addr
}

opts := append([]grpc.DialOption{grpc.WithInsecure(), grpc.WithResolvers(r)}, dopts...)
cc, err := grpc.Dial(target, opts...)
cc, err := grpc.Dial(ss.target, opts...)
if err != nil {
return fmt.Errorf("grpc.Dial(%q) = %v", target, err)
return fmt.Errorf("grpc.Dial(%q) = %v", ss.target, err)
}
ss.cc = cc
ss.r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: ss.addr}}})
Expand Down