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.parsedTarget.Endpoint, "unix:")
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 3 additions & 0 deletions internal/grpcutil/target.go
Expand Up @@ -43,6 +43,9 @@ func split2(s, sep string) (string, string, bool) {
// target}.
func ParseTarget(target string) (ret resolver.Target) {
var ok bool
if strings.HasPrefix(target, "unix:") {
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
return resolver.Target{Endpoint: target}
}
ret.Scheme, ret.Endpoint, ok = split2(target, "://")
if !ok {
return resolver.Target{Endpoint: target}
Expand Down
151 changes: 151 additions & 0 deletions test/unix_test.go
@@ -0,0 +1,151 @@
/*
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
*
* 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"
"net"
"os"
"testing"
"time"

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

// unixServer is used to test servers listening over a unix socket.
type unixServer struct {
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
// Guarantees we satisfy this interface; panics if unimplemented methods are called.
testpb.TestServiceServer

// Customizable implementations of server handlers.
emptyCall func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error)

// A client connected to this service the test may use. Created in Start().
client testpb.TestServiceClient
cc *grpc.ClientConn
s *grpc.Server

cleanups []func() // Lambdas executed in Stop(); populated by Start().
}

func (us *unixServer) EmptyCall(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return us.emptyCall(ctx, in)
}

func (us *unixServer) Start(network, address, target string) error {
lis, err := net.Listen(network, address)
if err != nil {
return err
}
us.cleanups = append(us.cleanups, func() { lis.Close() })

s := grpc.NewServer()
testpb.RegisterTestServiceServer(s, us)
go s.Serve(lis)
us.cleanups = append(us.cleanups, s.Stop)
us.s = s

cc, err := grpc.Dial(target, grpc.WithInsecure())
if err != nil {
return fmt.Errorf("grpc.Dial(%q) = %v", target, err)
}
us.cc = cc
if err := us.waitForReady(cc); err != nil {
return err
}
us.cleanups = append(us.cleanups, func() { cc.Close() })

us.client = testpb.NewTestServiceClient(cc)

return nil
}

func (us *unixServer) waitForReady(cc *grpc.ClientConn) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for {
s := cc.GetState()
if s == connectivity.Ready {
return nil
}
if !cc.WaitForStateChange(ctx, s) {
return ctx.Err()
}
}
}

func (us *unixServer) Stop() {
for i := len(us.cleanups) - 1; i >= 0; i-- {
us.cleanups[i]()
}
}

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 := &unixServer{
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.Unauthenticated, "no authority header")
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
}
if auths[0] != expectedAuthority {
return nil, status.Error(codes.Unauthenticated, fmt.Sprintf("invalid authority header %v, expected %v", auths[0], expectedAuthority))
}
} else {
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
return nil, status.Error(codes.Unauthenticated, "no authority header")
}
} else {
return nil, status.Error(codes.Unauthenticated, "failed to parse metadata")
}
return &testpb.Empty{}, nil
},
}
if err := us.Start("unix", address, target); err != nil {
t.Fatalf("Error starting endpoint server: %v\n", err)
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
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\n", err)
}
}

func (s) TestUnix1(t *testing.T) {
GarrettGutierrez1 marked this conversation as resolved.
Show resolved Hide resolved
runUnixTest(t, "sock.sock", "unix:sock.sock", "localhost")
}

func (s) TestUnix2(t *testing.T) {
runUnixTest(t, "/tmp/sock.sock", "unix:/tmp/sock.sock", "localhost")
}

func (s) TestUnix3(t *testing.T) {
runUnixTest(t, "/tmp/sock.sock", "unix:///tmp/sock.sock", "localhost")
}